From 40ff255d0a9cc0f59fe362072639b15c1f2d5070 Mon Sep 17 00:00:00 2001 From: Rohit Ramkumar Date: Mon, 11 Jun 2018 15:13:17 -0700 Subject: [PATCH] Add fixtures and helpers in e2e framework for BackendConfig --- cmd/e2e-test/basic_test.go | 2 +- pkg/e2e/fixtures.go | 24 +++++++++++++-- pkg/fuzz/helpers.go | 61 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/cmd/e2e-test/basic_test.go b/cmd/e2e-test/basic_test.go index db8fe20250..602836389d 100644 --- a/cmd/e2e-test/basic_test.go +++ b/cmd/e2e-test/basic_test.go @@ -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) } diff --git a/pkg/e2e/fixtures.go b/pkg/e2e/fixtures.go index 59e9e2ceff..b8dde53a95 100644 --- a/pkg/e2e/fixtures.go +++ b/pkg/e2e/fixtures.go @@ -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{ @@ -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 +} diff --git a/pkg/fuzz/helpers.go b/pkg/fuzz/helpers.go index 1b2ab4676f..ce4ebd9be5 100644 --- a/pkg/fuzz/helpers.go +++ b/pkg/fuzz/helpers.go @@ -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. @@ -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 +}