Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixtures and helpers in e2e framework for BackendConfig #331

Merged
merged 1 commit into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/e2e-test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestBasic(t *testing.T) {
Framework.RunWithSandbox(tc.desc, t, func(t *testing.T, s *e2e.Sandbox) {
t.Parallel()

_, _, err := e2e.CreateEchoService(s, "service-1")
_, _, err := e2e.CreateEchoService(s, "service-1", nil)
if err != nil {
t.Fatalf("error creating echo service: %v", err)
}
Expand Down
24 changes: 21 additions & 3 deletions pkg/e2e/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const (
)

// CreateEchoService creates the pod and service serving echoheaders.
func CreateEchoService(s *Sandbox, name string) (*v1.Pod, *v1.Service, error) {
func CreateEchoService(s *Sandbox, name string, annotations map[string]string) (*v1.Pod, *v1.Service, error) {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"app": name},
Name: name,
Labels: map[string]string{"app": name},
Annotations: annotations,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
Expand Down Expand Up @@ -70,3 +71,20 @@ func CreateEchoService(s *Sandbox, name string) (*v1.Pod, *v1.Service, error) {

return pod, service, nil
}

// CreateSecret creates a secret from the given data.
func CreateSecret(s *Sandbox, name string, data map[string][]byte) (*v1.Secret, error) {
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Data: data,
}
var err error
if secret, err = s.f.Clientset.Core().Secrets(s.Namespace).Create(secret); err != nil {
return nil, err
}
glog.V(2).Infof("Secret %q:%q created", s.Namespace, name)

return secret, nil
}
61 changes: 61 additions & 0 deletions pkg/fuzz/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
backendconfig "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
)

// NewService is a helper function for creating a simple Service spec.
Expand Down Expand Up @@ -176,3 +177,63 @@ func (i *IngressBuilder) AddTLS(hosts []string, secretName string) *IngressBuild
})
return i
}

// BackendConfigBuilder is syntactic sugar for creating BackendConfig specs for testing
// purposes.
//
// backendConfig := NewBackendConfigBuilder("ns1", "name1").Build()
type BackendConfigBuilder struct {
backendConfig *backendconfig.BackendConfig
}

// NewBackendConfigBuilder instantiates a new BackendConfig.
func NewBackendConfigBuilder(ns, name string) *BackendConfigBuilder {
var backendConfig = &backendconfig.BackendConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
}
return &BackendConfigBuilder{backendConfig: backendConfig}
}

// NewBackendConfigBuilderFromExisting creates a BackendConfigBuilder from an existing
// BackendConfig object. The BackendConfigBuilder object will be copied.
func NewBackendConfigBuilderFromExisting(b *backendconfig.BackendConfig) *BackendConfigBuilder {
return &BackendConfigBuilder{backendConfig: b.DeepCopy()}
}

// Build returns a constructed BackendConfig. The BackendConfig is a copy, so the Builder
// can be reused to construct multiple BackendConfig definitions.
func (b *BackendConfigBuilder) Build() *backendconfig.BackendConfig {
return b.backendConfig.DeepCopy()
}

// EnableCDN enables or disables CDN on the BackendConfig.
func (b *BackendConfigBuilder) EnableCDN(enabled bool) *BackendConfigBuilder {
if b.backendConfig.Spec.Cdn == nil {
b.backendConfig.Spec.Cdn = &backendconfig.CDNConfig{}
}
b.backendConfig.Spec.Cdn.Enabled = enabled
return b
}

// SetCachePolicy specifies the cache policy on the BackendConfig.
func (b *BackendConfigBuilder) SetCachePolicy(cachePolicy *backendconfig.CacheKeyPolicy) *BackendConfigBuilder {
if b.backendConfig.Spec.Cdn == nil {
b.backendConfig.Spec.Cdn = &backendconfig.CDNConfig{}
}
b.backendConfig.Spec.Cdn.CachePolicy = cachePolicy
return b
}

// SetIAPConfig enables or disables IAP on the BackendConfig and also sets
// the secret which contains the OAuth credentials.
func (b *BackendConfigBuilder) SetIAPConfig(enabled bool, secret string) *BackendConfigBuilder {
if b.backendConfig.Spec.Iap == nil {
b.backendConfig.Spec.Iap = &backendconfig.IAPConfig{}
}
b.backendConfig.Spec.Iap.Enabled = enabled
b.backendConfig.Spec.Iap.OAuthClientCredentials = &backendconfig.OAuthClientCredentials{SecretName: secret}
return b
}