Skip to content

Commit

Permalink
Merge pull request #331 from rramkumar1/backend-config-validator
Browse files Browse the repository at this point in the history
Add fixtures and helpers in e2e framework for BackendConfig
  • Loading branch information
bowei authored Jun 14, 2018
2 parents 84b0c0c + 40ff255 commit ac80228
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
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
}

0 comments on commit ac80228

Please sign in to comment.