From 6ab3a219716d9ac3e9f75b2aa4d39a9235fb87f4 Mon Sep 17 00:00:00 2001 From: chentao1596 Date: Mon, 27 Mar 2017 10:06:07 +0800 Subject: [PATCH 1/2] add unit test cases for core/pkg/ingress/controller/annotations --- .../ingress/controller/annotations_test.go | 82 ++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/core/pkg/ingress/controller/annotations_test.go b/core/pkg/ingress/controller/annotations_test.go index 1b3b9b08d..0b0aa5d91 100644 --- a/core/pkg/ingress/controller/annotations_test.go +++ b/core/pkg/ingress/controller/annotations_test.go @@ -35,17 +35,19 @@ const ( annotationAffinityType = "ingress.kubernetes.io/affinity" annotationAffinityCookieName = "ingress.kubernetes.io/session-cookie-name" annotationAffinityCookieHash = "ingress.kubernetes.io/session-cookie-hash" + annotationAuthTlsSecret = "ingress.kubernetes.io/auth-tls-secret" ) type mockCfg struct { + MockSecrets map[string]*api.Secret } func (m mockCfg) GetDefaultBackend() defaults.Backend { return defaults.Backend{} } -func (m mockCfg) GetSecret(string) (*api.Secret, error) { - return nil, nil +func (m mockCfg) GetSecret(name string) (*api.Secret, error) { + return m.MockSecrets[name], nil } func (m mockCfg) GetAuthCertificate(string) (*resolver.AuthSSLCert, error) { @@ -218,3 +220,79 @@ func TestAffinitySession(t *testing.T) { } } } + +func TestContainsCertificateAuth(t *testing.T) { + ec := newAnnotationExtractor(mockCfg{}) + + foos := []struct { + name string + annotations map[string]string + result bool + }{ + {"nil_annotations", nil, false}, + {"empty_annotations", map[string]string{}, false}, + {"not_exist_annotations", map[string]string{annotationAffinityType: "cookie"}, false}, + {"exist_annotations", map[string]string{annotationAuthTlsSecret: "default/foo_secret"}, true}, + } + + for _, foo := range foos { + t.Run(foo.name, func(t *testing.T) { + ing := buildIngress() + ing.SetAnnotations(foo.annotations) + r := ec.ContainsCertificateAuth(ing) + if r != foo.result { + t.Errorf("Returned %t but expected %t for %s", r, foo.result, foo.name) + } + }) + } +} + +func TestCertificateAuthSecret(t *testing.T) { + resolver := mockCfg{} + resolver.MockSecrets = map[string]*api.Secret{ + "default/foo_secret": { + ObjectMeta: api.ObjectMeta{ + Name: "foo_secret_name", + }, + }, + } + ec := newAnnotationExtractor(resolver) + + foos := []struct { + name string + annotations map[string]string + eerr bool + ename string + }{ + {"nil_annotations", nil, true, ""}, + {"empty_annotations", map[string]string{}, true, ""}, + {"not_exist_annotations", map[string]string{annotationAffinityType: "cookie"}, true, ""}, + {"exist_annotations", map[string]string{annotationAuthTlsSecret: "default/foo_secret"}, false, "foo_secret_name"}, + } + + for _, foo := range foos { + t.Run(foo.name, func(t *testing.T) { + ing := buildIngress() + ing.SetAnnotations(foo.annotations) + r, err := ec.CertificateAuthSecret(ing) + + if foo.eerr { + if err == nil { + t.Fatalf("Exepected error for %s", foo.name) + } + } else { + if err != nil { + t.Fatalf("Unexpected error %v for %s", err, foo.name) + } + + rname := "" + if r != nil { + rname = r.GetName() + } + if rname != foo.ename { + t.Errorf("Returned %s but expected %s for %s", rname, foo.ename, foo.name) + } + } + }) + } +} From 767591fa1847d66bedd6bc8529c447c87caf6c49 Mon Sep 17 00:00:00 2001 From: chentao1596 Date: Mon, 27 Mar 2017 10:11:40 +0800 Subject: [PATCH 2/2] remove unused constants --- core/pkg/ingress/controller/annotations.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/pkg/ingress/controller/annotations.go b/core/pkg/ingress/controller/annotations.go index 5a8beeadf..b0ab4218b 100644 --- a/core/pkg/ingress/controller/annotations.go +++ b/core/pkg/ingress/controller/annotations.go @@ -109,7 +109,6 @@ const ( healthCheck = "HealthCheck" sslPassthrough = "SSLPassthrough" sessionAffinity = "SessionAffinity" - certificateAuth = "CertificateAuth" ) func (e *annotationExtractor) SecureUpstream(ing *extensions.Ingress) bool {