From 5d5df38b7e557738161bc6eb4bfa7388634eb66a Mon Sep 17 00:00:00 2001 From: Rohit Ramkumar Date: Thu, 14 Jun 2018 19:00:54 -0700 Subject: [PATCH] testing improvements --- pkg/backendconfig/utils_test.go | 10 +- pkg/backends/features/cdn_test.go | 152 ++++-------------- pkg/backends/features/iap_test.go | 128 ++++++--------- pkg/composite/composite_test.go | 15 ++ pkg/controller/translator/translator_test.go | 154 +++++++++++++++---- pkg/controller/translator/utils_test.go | 10 +- pkg/test/utils.go | 12 ++ 7 files changed, 242 insertions(+), 239 deletions(-) diff --git a/pkg/backendconfig/utils_test.go b/pkg/backendconfig/utils_test.go index 248ca31b85..4433d986e9 100644 --- a/pkg/backendconfig/utils_test.go +++ b/pkg/backendconfig/utils_test.go @@ -66,9 +66,11 @@ func TestBackendConfigName(t *testing.T) { } for _, tc := range testCases { - result := BackendConfigName(tc.backendConfigs, tc.svcPort) - if result != tc.expected { - t.Errorf("%s: expected %s but got %s", tc.desc, tc.expected, result) - } + t.Run(tc.desc, func(t *testing.T) { + result := BackendConfigName(tc.backendConfigs, tc.svcPort) + if result != tc.expected { + t.Errorf("%s: expected %s but got %s", tc.desc, tc.expected, result) + } + }) } } diff --git a/pkg/backends/features/cdn_test.go b/pkg/backends/features/cdn_test.go index 851e6fb899..485fef9362 100644 --- a/pkg/backends/features/cdn_test.go +++ b/pkg/backends/features/cdn_test.go @@ -17,7 +17,6 @@ limitations under the License. package features import ( - "reflect" "testing" backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" @@ -27,14 +26,14 @@ import ( func TestEnsureCDN(t *testing.T) { testCases := []struct { - desc string - sp utils.ServicePort - be *composite.BackendService - expected bool + desc string + sp utils.ServicePort + be *composite.BackendService + updateExpected bool }{ { - "settings are identical, no update needed", - utils.ServicePort{ + desc: "settings are identical, no update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Cdn: &backendconfigv1beta1.CDNConfig{ @@ -46,7 +45,7 @@ func TestEnsureCDN(t *testing.T) { }, }, }, - &composite.BackendService{ + be: &composite.BackendService{ EnableCDN: true, CdnPolicy: &composite.BackendServiceCdnPolicy{ CacheKeyPolicy: &composite.CacheKeyPolicy{ @@ -54,153 +53,60 @@ func TestEnsureCDN(t *testing.T) { }, }, }, - false, + updateExpected: false, }, { - "cache settings are different, update needed", - utils.ServicePort{ - BackendConfig: &backendconfigv1beta1.BackendConfig{ - Spec: backendconfigv1beta1.BackendConfigSpec{ - Cdn: &backendconfigv1beta1.CDNConfig{ - Enabled: true, - CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{ - QueryStringWhitelist: []string{"foo"}, - }, - }, - }, - }, - }, - &composite.BackendService{ - EnableCDN: true, - }, - true, - }, - { - "enabled setting is different, update needed", - utils.ServicePort{ - BackendConfig: &backendconfigv1beta1.BackendConfig{ - Spec: backendconfigv1beta1.BackendConfigSpec{ - Cdn: &backendconfigv1beta1.CDNConfig{ - Enabled: true, - }, - }, - }, - }, - &composite.BackendService{ - EnableCDN: false, - }, - true, - }, - } - - for _, testCase := range testCases { - result := EnsureCDN(testCase.sp, testCase.be) - if result != testCase.expected { - t.Errorf("%v: expected %v but got %v", testCase.desc, testCase.expected, result) - } - } -} - -func TestApplyCDNSettings(t *testing.T) { - testCases := []struct { - desc string - sp utils.ServicePort - be *composite.BackendService - expected composite.BackendService - }{ - { - "apply settings on empty BackendService", - utils.ServicePort{ - BackendConfig: &backendconfigv1beta1.BackendConfig{ - Spec: backendconfigv1beta1.BackendConfigSpec{ - Cdn: &backendconfigv1beta1.CDNConfig{ - Enabled: true, - CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{ - IncludeHost: true, - }, - }, - }, - }, - }, - &composite.BackendService{}, - composite.BackendService{ - EnableCDN: true, - CdnPolicy: &composite.BackendServiceCdnPolicy{ - CacheKeyPolicy: &composite.CacheKeyPolicy{ - IncludeHost: true, - }, - }, - }, - }, - { - "overwrite some fields on existing settings", - utils.ServicePort{ + desc: "cache settings are different, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Cdn: &backendconfigv1beta1.CDNConfig{ Enabled: true, CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{ IncludeHost: true, + IncludeQueryString: false, IncludeProtocol: false, - IncludeQueryString: true, }, }, }, }, }, - &composite.BackendService{ - EnableCDN: false, - CdnPolicy: &composite.BackendServiceCdnPolicy{ - CacheKeyPolicy: &composite.CacheKeyPolicy{ - IncludeHost: false, - IncludeProtocol: true, - IncludeQueryString: true, - }, - }, - }, - composite.BackendService{ + be: &composite.BackendService{ EnableCDN: true, CdnPolicy: &composite.BackendServiceCdnPolicy{ CacheKeyPolicy: &composite.CacheKeyPolicy{ - IncludeHost: true, - IncludeProtocol: false, + IncludeHost: false, IncludeQueryString: true, + IncludeProtocol: true, }, }, }, + updateExpected: true, }, { - "no feature settings in spec", - utils.ServicePort{ + desc: "enabled setting is different, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ - Cdn: nil, - }, - }, - }, - &composite.BackendService{ - EnableCDN: true, - CdnPolicy: &composite.BackendServiceCdnPolicy{ - CacheKeyPolicy: &composite.CacheKeyPolicy{ - IncludeHost: true, + Cdn: &backendconfigv1beta1.CDNConfig{ + Enabled: true, + }, }, }, }, - composite.BackendService{ + be: &composite.BackendService{ EnableCDN: false, - CdnPolicy: &composite.BackendServiceCdnPolicy{ - CacheKeyPolicy: &composite.CacheKeyPolicy{ - IncludeHost: false, - }, - }, }, + updateExpected: true, }, } - for _, testCase := range testCases { - applyCDNSettings(testCase.sp, testCase.be) - if !reflect.DeepEqual(testCase.expected, *testCase.be) { - t.Errorf("%v: expected %+v but got %+v", testCase.desc, testCase.expected, *testCase.be) - } + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + result := EnsureCDN(tc.sp, tc.be) + if result != tc.updateExpected { + t.Errorf("%v: expected %v but got %v", tc.desc, tc.updateExpected, result) + } + }) } } diff --git a/pkg/backends/features/iap_test.go b/pkg/backends/features/iap_test.go index aa0a61ccec..b0e2229f4b 100644 --- a/pkg/backends/features/iap_test.go +++ b/pkg/backends/features/iap_test.go @@ -19,7 +19,6 @@ package features import ( "crypto/sha256" "fmt" - "reflect" "testing" backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" @@ -27,16 +26,16 @@ import ( "k8s.io/ingress-gce/pkg/utils" ) -func TestApplyIAPSettings(t *testing.T) { +func TestEnsureIAP(t *testing.T) { testCases := []struct { - desc string - sp utils.ServicePort - be *composite.BackendService - expected composite.BackendService + desc string + sp utils.ServicePort + be *composite.BackendService + updateExpected bool }{ { - "apply settings on empty BackendService", - utils.ServicePort{ + desc: "settings are identical, no update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Iap: &backendconfigv1beta1.IAPConfig{ @@ -49,18 +48,18 @@ func TestApplyIAPSettings(t *testing.T) { }, }, }, - &composite.BackendService{}, - composite.BackendService{ + be: &composite.BackendService{ Iap: &composite.BackendServiceIAP{ - Enabled: true, - Oauth2ClientId: "foo", - Oauth2ClientSecret: "bar", + Enabled: true, + Oauth2ClientId: "foo", + Oauth2ClientSecretSha256: fmt.Sprintf("%x", sha256.Sum256([]byte("bar"))), }, }, + updateExpected: false, }, { - "overwrite some fields on existing settings", - utils.ServicePort{ + desc: "no existing settings, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Iap: &backendconfigv1beta1.IAPConfig{ @@ -73,93 +72,66 @@ func TestApplyIAPSettings(t *testing.T) { }, }, }, - &composite.BackendService{ - Iap: &composite.BackendServiceIAP{ - Enabled: false, - Oauth2ClientId: "foo", - Oauth2ClientSecret: "bar", - }, - }, - composite.BackendService{ - Iap: &composite.BackendServiceIAP{ - Enabled: true, - Oauth2ClientId: "foo", - Oauth2ClientSecret: "baz", - }, + be: &composite.BackendService{ + Iap: nil, }, + updateExpected: true, }, { - "no feature settings in spec", - utils.ServicePort{ + desc: "client id is different, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ - Iap: nil, + Iap: &backendconfigv1beta1.IAPConfig{ + Enabled: true, + OAuthClientCredentials: &backendconfigv1beta1.OAuthClientCredentials{ + ClientID: "foo", + ClientSecret: "baz", + }, + }, }, }, }, - &composite.BackendService{ + be: &composite.BackendService{ Iap: &composite.BackendServiceIAP{ - Enabled: false, - Oauth2ClientId: "foo", - Oauth2ClientSecret: "bar", - }, - }, - composite.BackendService{ - Iap: &composite.BackendServiceIAP{ - Enabled: false, - Oauth2ClientId: "", - Oauth2ClientSecret: "", + Enabled: true, + Oauth2ClientId: "bar", + Oauth2ClientSecretSha256: fmt.Sprintf("%x", sha256.Sum256([]byte("baz"))), }, }, + updateExpected: true, }, - } - - for _, testCase := range testCases { - applyIAPSettings(testCase.sp, testCase.be) - if !reflect.DeepEqual(testCase.expected, *testCase.be) { - t.Errorf("%v: expected %+v but got %+v", testCase.desc, testCase.expected, *testCase.be) - } - } -} - -func TestEnsureIAP(t *testing.T) { - testCases := []struct { - desc string - sp utils.ServicePort - be *composite.BackendService - expected bool - }{ { - "settings are identical, no update needed", - utils.ServicePort{ + desc: "client secret is different, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Iap: &backendconfigv1beta1.IAPConfig{ Enabled: true, OAuthClientCredentials: &backendconfigv1beta1.OAuthClientCredentials{ ClientID: "foo", - ClientSecret: "bar", + ClientSecret: "baz", }, }, }, }, }, - &composite.BackendService{ + be: &composite.BackendService{ Iap: &composite.BackendServiceIAP{ Enabled: true, Oauth2ClientId: "foo", Oauth2ClientSecretSha256: fmt.Sprintf("%x", sha256.Sum256([]byte("bar"))), }, }, - false, + updateExpected: true, }, { - "credential settings are different, update needed", - utils.ServicePort{ + desc: "enabled setting is different, update needed", + sp: utils.ServicePort{ BackendConfig: &backendconfigv1beta1.BackendConfig{ Spec: backendconfigv1beta1.BackendConfigSpec{ Iap: &backendconfigv1beta1.IAPConfig{ - Enabled: true, + Enabled: false, OAuthClientCredentials: &backendconfigv1beta1.OAuthClientCredentials{ ClientID: "foo", ClientSecret: "baz", @@ -168,21 +140,23 @@ func TestEnsureIAP(t *testing.T) { }, }, }, - &composite.BackendService{ + be: &composite.BackendService{ Iap: &composite.BackendServiceIAP{ - Enabled: true, - Oauth2ClientId: "foo", - Oauth2ClientSecret: "bar", + Enabled: true, + Oauth2ClientId: "foo", + Oauth2ClientSecretSha256: fmt.Sprintf("%x", sha256.Sum256([]byte("baz"))), }, }, - true, + updateExpected: true, }, } - for _, testCase := range testCases { - result := EnsureIAP(testCase.sp, testCase.be) - if result != testCase.expected { - t.Errorf("%v: expected %v but got %v", testCase.desc, testCase.expected, result) - } + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + result := EnsureIAP(tc.sp, tc.be) + if result != tc.updateExpected { + t.Errorf("%v: expected %v but got %v", tc.desc, tc.updateExpected, result) + } + }) } } diff --git a/pkg/composite/composite_test.go b/pkg/composite/composite_test.go index 93fb471766..41485a0077 100644 --- a/pkg/composite/composite_test.go +++ b/pkg/composite/composite_test.go @@ -149,6 +149,21 @@ func TestToAlpha(t *testing.T) { } } +func TestToBeta(t *testing.T) { + composite := BackendService{ + Version: meta.VersionGA, + SecurityPolicy: "foo", + } + beta, err := composite.toBeta() + if err != nil { + t.Fatalf("composite.toBeta() =_, %v; want _, nil", err) + } + // Verify that a known beta field value is preserved. + if beta.SecurityPolicy != "foo" { + t.Errorf("beta.SecurityPolicy = %q, want foo", beta.SecurityPolicy) + } +} + func TestToGA(t *testing.T) { composite := BackendService{ Version: meta.VersionGA, diff --git a/pkg/controller/translator/translator_test.go b/pkg/controller/translator/translator_test.go index 92272fc44b..b997575637 100644 --- a/pkg/controller/translator/translator_test.go +++ b/pkg/controller/translator/translator_test.go @@ -31,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/kubernetes/fake" + backendconfig "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" backendconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned/fake" "k8s.io/ingress-gce/pkg/test" @@ -44,12 +45,12 @@ var ( defaultBackend = utils.ServicePortID{Service: types.NamespacedName{Name: "default-http-backend", Namespace: "kube-system"}, Port: intstr.FromString("http")} ) -func fakeTranslator(negEnabled bool) *Translator { +func fakeTranslator(negEnabled, backendConfigEnabled bool) *Translator { client := fake.NewSimpleClientset() backendConfigClient := backendconfigclient.NewSimpleClientset() namer := utils.NewNamer("uid1", "") - ctx := context.NewControllerContext(client, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, negEnabled, false) + ctx := context.NewControllerContext(client, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, negEnabled, backendConfigEnabled) gce := &Translator{ namer: namer, ctx: ctx, @@ -58,7 +59,7 @@ func fakeTranslator(negEnabled bool) *Translator { } func TestTranslateIngress(t *testing.T) { - translator := fakeTranslator(false) + translator := fakeTranslator(false, false) svcLister := translator.ctx.ServiceInformer.GetIndexer() // default backend @@ -82,12 +83,14 @@ func TestTranslateIngress(t *testing.T) { }) svcLister.Add(svc) - cases := map[string]struct { + cases := []struct { + desc string ing *extensions.Ingress wantErrCount int wantGCEURLMap *utils.GCEURLMap }{ - "Default Backend Only": { + { + desc: "default backend only", ing: test.NewIngress(types.NamespacedName{Name: "my-ingress", Namespace: "default"}, extensions.IngressSpec{ Backend: test.Backend("first-service", intstr.FromInt(80)), @@ -95,47 +98,56 @@ func TestTranslateIngress(t *testing.T) { wantErrCount: 0, wantGCEURLMap: &utils.GCEURLMap{DefaultBackend: &utils.ServicePort{ID: utils.ServicePortID{Service: types.NamespacedName{Name: "first-service", Namespace: "default"}, Port: intstr.FromInt(80)}}}, }, - "No Backend": { + { + desc: "no backend", ing: test.NewIngress(types.NamespacedName{Name: "my-ingress", Namespace: "default"}, extensions.IngressSpec{}), wantErrCount: 0, wantGCEURLMap: &utils.GCEURLMap{DefaultBackend: &utils.ServicePort{ID: utils.ServicePortID{Service: types.NamespacedName{Name: "default-http-backend", Namespace: "kube-system"}, Port: intstr.FromString("http")}}}, }, - "No Host": { + { + desc: "no host", ing: ingressFromFile("ingress-no-host.yaml"), wantErrCount: 0, wantGCEURLMap: gceURLMapFromFile("ingress-no-host.json"), }, - "Single Host": { + { + desc: "single host", ing: ingressFromFile("ingress-single-host.yaml"), wantErrCount: 0, wantGCEURLMap: gceURLMapFromFile("ingress-single-host.json"), }, - "Two Hosts": { + { + desc: "two hosts", ing: ingressFromFile("ingress-two-hosts.yaml"), wantErrCount: 0, wantGCEURLMap: gceURLMapFromFile("ingress-two-hosts.json"), }, - "Multiple Paths": { + { + desc: "multiple paths", ing: ingressFromFile("ingress-multi-paths.yaml"), wantErrCount: 0, wantGCEURLMap: gceURLMapFromFile("ingress-multi-paths.json"), }, - "Multiple Empty Paths": { + { + desc: "multiple empty paths", ing: ingressFromFile("ingress-multi-empty.yaml"), wantErrCount: 0, wantGCEURLMap: gceURLMapFromFile("ingress-multi-empty.json"), }, - "Missing Rule Service": { + { + desc: "missing rule service", ing: ingressFromFile("ingress-missing-rule-svc.yaml"), wantErrCount: 1, wantGCEURLMap: gceURLMapFromFile("ingress-missing-rule-svc.json"), }, - "Missing Multiple Rule Service": { + { + desc: "missing multiple rule service", ing: ingressFromFile("ingress-missing-multi-svc.yaml"), wantErrCount: 2, wantGCEURLMap: gceURLMapFromFile("ingress-missing-multi-svc.json"), }, - "Missing Default Service": { + { + desc: "missing default service", ing: test.NewIngress(types.NamespacedName{Name: "my-ingress", Namespace: "default"}, extensions.IngressSpec{ Backend: test.Backend("random-service", intstr.FromInt(80)), @@ -145,30 +157,32 @@ func TestTranslateIngress(t *testing.T) { }, } - for name, tc := range cases { - t.Run(name, func(t *testing.T) { + for _, tc := range cases { + t.Run(tc.desc, func(t *testing.T) { gotGCEURLMap, gotErrs := translator.TranslateIngress(tc.ing, defaultBackend) if len(gotErrs) != tc.wantErrCount { - t.Errorf("TranslateIngress() = _, %+v, want %v errs", gotErrs, tc.wantErrCount) + t.Errorf("%s: TranslateIngress() = _, %+v, want %v errs", tc.desc, gotErrs, tc.wantErrCount) } // Check that the GCEURLMaps point to the same ServicePortIDs. if !utils.EqualMapping(gotGCEURLMap, tc.wantGCEURLMap) { - t.Errorf("TranslateIngress() = %+v\nwant\n%+v", gotGCEURLMap.String(), tc.wantGCEURLMap.String()) + t.Errorf("%s: TranslateIngress() = %+v\nwant\n%+v", tc.desc, gotGCEURLMap.String(), tc.wantGCEURLMap.String()) } }) } } func TestGetServicePort(t *testing.T) { - cases := map[string]struct { + cases := []struct { + desc string spec apiv1.ServiceSpec annotations map[string]string id utils.ServicePortID wantErr bool wantPort bool }{ - "ClusterIP Service": { + { + desc: "clusterIP service", spec: apiv1.ServiceSpec{ Type: apiv1.ServiceTypeClusterIP, Ports: []apiv1.ServicePort{{Name: "http", Port: 80}}, @@ -177,16 +191,18 @@ func TestGetServicePort(t *testing.T) { wantErr: true, wantPort: false, }, - "Missing Port": { + { + desc: "missing port", spec: apiv1.ServiceSpec{ - Type: apiv1.ServiceTypeClusterIP, + Type: apiv1.ServiceTypeNodePort, Ports: []apiv1.ServicePort{{Name: "http", Port: 80}}, }, id: utils.ServicePortID{Port: intstr.FromString("badport")}, wantErr: true, wantPort: false, }, - "AppProtocols malformed": { + { + desc: "app protocols malformed", spec: apiv1.ServiceSpec{ Type: apiv1.ServiceTypeNodePort, Ports: []apiv1.ServicePort{{Name: "http", Port: 80}}, @@ -199,12 +215,12 @@ func TestGetServicePort(t *testing.T) { wantPort: true, }, } - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - translator := fakeTranslator(false) + for _, tc := range cases { + t.Run(tc.desc, func(t *testing.T) { + translator := fakeTranslator(false, true) svcLister := translator.ctx.ServiceInformer.GetIndexer() - svcName := types.NamespacedName{Name: name, Namespace: "default"} + svcName := types.NamespacedName{Name: "foo", Namespace: "default"} svc := test.NewService(svcName, tc.spec) svc.Annotations = tc.annotations svcLister.Add(svc) @@ -221,8 +237,84 @@ func TestGetServicePort(t *testing.T) { } } +func TestGetServicePortWithBackendConfigEnabled(t *testing.T) { + backendConfig := test.NewBackendConfig(types.NamespacedName{Name: "config-http", Namespace: "default"}, backendconfig.BackendConfigSpec{ + Cdn: &backendconfig.CDNConfig{ + Enabled: true, + }, + }) + + testCases := []struct { + desc string + annotations map[string]string + id utils.ServicePortID + wantErr bool + wantPort bool + }{ + { + desc: "error getting backend config", + annotations: map[string]string{ + annotations.BackendConfigKey: `{"ports":{"https":"config-https"}}`, + }, + id: utils.ServicePortID{Port: intstr.FromString("https")}, + wantErr: true, + wantPort: true, + }, + { + desc: "no backend config annotation", + annotations: nil, + id: utils.ServicePortID{Port: intstr.FromString("http")}, + wantErr: false, + wantPort: true, + }, + { + desc: "no backend config name for port", + annotations: map[string]string{ + annotations.BackendConfigKey: `{"ports":{"https":"config-https"}}`, + }, + id: utils.ServicePortID{Port: intstr.FromString("http")}, + wantErr: false, + wantPort: true, + }, + { + desc: "successfully got backend config for port", + annotations: map[string]string{ + annotations.BackendConfigKey: `{"ports":{"http":"config-http"}}`, + }, + id: utils.ServicePortID{Port: intstr.FromString("http")}, + wantErr: false, + wantPort: true, + }, + } + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + translator := fakeTranslator(false, true) + svcLister := translator.ctx.ServiceInformer.GetIndexer() + backendConfigLister := translator.ctx.BackendConfigInformer.GetIndexer() + svcName := types.NamespacedName{Name: "foo", Namespace: "default"} + svc := test.NewService(svcName, apiv1.ServiceSpec{ + Type: apiv1.ServiceTypeNodePort, + Ports: []apiv1.ServicePort{{Name: "http", Port: 80}, {Name: "https", Port: 443}}, + }) + tc.id.Service = svcName + svc.Annotations = tc.annotations + + svcLister.Add(svc) + backendConfigLister.Add(backendConfig) + + port, gotErr := translator.getServicePort(tc.id) + if (gotErr != nil) != tc.wantErr { + t.Errorf("%s: translator.getServicePort(%+v) = _, %v, want err? %v", tc.desc, tc.id, gotErr, tc.wantErr) + } + if (port != nil) != tc.wantPort { + t.Errorf("%s: translator.getServicePort(%+v) = %v, want port? %v", tc.desc, tc.id, port, tc.wantPort) + } + }) + } +} + func TestGetProbe(t *testing.T) { - translator := fakeTranslator(false) + translator := fakeTranslator(false, false) nodePortToHealthCheck := map[utils.ServicePort]string{ {NodePort: 3001, Protocol: annotations.ProtocolHTTP}: "/healthz", {NodePort: 3002, Protocol: annotations.ProtocolHTTPS}: "/foo", @@ -245,7 +337,7 @@ func TestGetProbe(t *testing.T) { } func TestGetProbeNamedPort(t *testing.T) { - translator := fakeTranslator(false) + translator := fakeTranslator(false, false) nodePortToHealthCheck := map[utils.ServicePort]string{ {NodePort: 3001, Protocol: annotations.ProtocolHTTP}: "/healthz", } @@ -268,7 +360,7 @@ func TestGetProbeNamedPort(t *testing.T) { } func TestGetProbeCrossNamespace(t *testing.T) { - translator := fakeTranslator(false) + translator := fakeTranslator(false, false) firstPod := &apiv1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -392,7 +484,7 @@ func getProbePath(p *apiv1.Probe) string { } func TestGatherEndpointPorts(t *testing.T) { - translator := fakeTranslator(true) + translator := fakeTranslator(true, false) ep1 := "ep1" ep2 := "ep2" diff --git a/pkg/controller/translator/utils_test.go b/pkg/controller/translator/utils_test.go index 58408069ad..3effcb365a 100644 --- a/pkg/controller/translator/utils_test.go +++ b/pkg/controller/translator/utils_test.go @@ -84,9 +84,11 @@ func TestServicePort(t *testing.T) { } for _, tc := range testCases { - result := ServicePort(testSvc, tc.port) - if !reflect.DeepEqual(result, tc.expected) { - t.Errorf("%s: expected %+v but got %+v", tc.desc, tc.expected, result) - } + t.Run(tc.desc, func(t *testing.T) { + result := ServicePort(testSvc, tc.port) + if !reflect.DeepEqual(result, tc.expected) { + t.Errorf("%s: expected %+v but got %+v", tc.desc, tc.expected, result) + } + }) } } diff --git a/pkg/test/utils.go b/pkg/test/utils.go index 0e856bacad..7460ff256b 100644 --- a/pkg/test/utils.go +++ b/pkg/test/utils.go @@ -7,6 +7,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/kubernetes/scheme" + backendconfig "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" ) // NewIngress returns an Ingress with the given spec. @@ -39,6 +40,17 @@ func NewService(name types.NamespacedName, spec api_v1.ServiceSpec) *api_v1.Serv } } +// NewBackendConfig returns a BackendConfig with the given spec. +func NewBackendConfig(name types.NamespacedName, spec backendconfig.BackendConfigSpec) *backendconfig.BackendConfig { + return &backendconfig.BackendConfig{ + ObjectMeta: meta_v1.ObjectMeta{ + Name: name.Name, + Namespace: name.Namespace, + }, + Spec: spec, + } +} + // Backend returns an IngressBackend with the given service name/port. func Backend(name string, port intstr.IntOrString) *extensions.IngressBackend { return &extensions.IngressBackend{