From 5aca4630e42e169c24e565501cf641689f992b42 Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Wed, 1 May 2024 14:45:45 +0530 Subject: [PATCH] kubectl/describe/svc: refactor TestDescribeService Signed-off-by: Daman Arora Kubernetes-commit: df417aa9b8813d0e5e1208e9039f140edeff39db --- pkg/describe/describe_test.go | 177 +++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 76 deletions(-) diff --git a/pkg/describe/describe_test.go b/pkg/describe/describe_test.go index 61d39379..e55f3918 100644 --- a/pkg/describe/describe_test.go +++ b/pkg/describe/describe_test.go @@ -25,6 +25,9 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/lithammer/dedent" + "github.com/stretchr/testify/assert" + appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" @@ -652,9 +655,9 @@ func getResourceList(cpu, memory string) corev1.ResourceList { func TestDescribeService(t *testing.T) { singleStack := corev1.IPFamilyPolicySingleStack testCases := []struct { - name string - service *corev1.Service - expect []string + name string + service *corev1.Service + expected string }{ { name: "test1", @@ -676,24 +679,31 @@ func TestDescribeService(t *testing.T) { ClusterIP: "1.2.3.4", IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, LoadBalancerIP: "5.6.7.8", - SessionAffinity: "None", - ExternalTrafficPolicy: "Local", + SessionAffinity: corev1.ServiceAffinityNone, + ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal, HealthCheckNodePort: 32222, }, }, - expect: []string{ - "Name", "bar", - "Namespace", "foo", - "Selector", "blah=heh", - "Type", "LoadBalancer", - "IP", "1.2.3.4", - "Port", "port-tcp", "8080/TCP", - "TargetPort", "9527/TCP", - "NodePort", "port-tcp", "31111/TCP", - "Session Affinity", "None", - "External Traffic Policy", "Local", - "HealthCheck NodePort", "32222", - }, + expected: dedent.Dedent(` + Name: bar + Namespace: foo + Labels: + Annotations: + Selector: blah=heh + Type: LoadBalancer + IP Families: IPv4 + IP: 1.2.3.4 + IPs: + IP: 5.6.7.8 + Port: port-tcp 8080/TCP + TargetPort: 9527/TCP + NodePort: port-tcp 31111/TCP + Endpoints: + Session Affinity: None + External Traffic Policy: Local + HealthCheck NodePort: 32222 + Events: + `)[1:], }, { name: "test2", @@ -715,24 +725,31 @@ func TestDescribeService(t *testing.T) { ClusterIP: "1.2.3.4", IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, LoadBalancerIP: "5.6.7.8", - SessionAffinity: "None", - ExternalTrafficPolicy: "Local", + SessionAffinity: corev1.ServiceAffinityNone, + ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal, HealthCheckNodePort: 32222, }, }, - expect: []string{ - "Name", "bar", - "Namespace", "foo", - "Selector", "blah=heh", - "Type", "LoadBalancer", - "IP", "1.2.3.4", - "Port", "port-tcp", "8080/TCP", - "TargetPort", "targetPort/TCP", - "NodePort", "port-tcp", "31111/TCP", - "Session Affinity", "None", - "External Traffic Policy", "Local", - "HealthCheck NodePort", "32222", - }, + expected: dedent.Dedent(` + Name: bar + Namespace: foo + Labels: + Annotations: + Selector: blah=heh + Type: LoadBalancer + IP Families: IPv4 + IP: 1.2.3.4 + IPs: + IP: 5.6.7.8 + Port: port-tcp 8080/TCP + TargetPort: targetPort/TCP + NodePort: port-tcp 31111/TCP + Endpoints: + Session Affinity: None + External Traffic Policy: Local + HealthCheck NodePort: 32222 + Events: + `)[1:], }, { name: "test-ServiceIPFamily", @@ -754,25 +771,31 @@ func TestDescribeService(t *testing.T) { ClusterIP: "1.2.3.4", IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, LoadBalancerIP: "5.6.7.8", - SessionAffinity: "None", - ExternalTrafficPolicy: "Local", + SessionAffinity: corev1.ServiceAffinityNone, + ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal, HealthCheckNodePort: 32222, }, }, - expect: []string{ - "Name", "bar", - "Namespace", "foo", - "Selector", "blah=heh", - "Type", "LoadBalancer", - "IP", "1.2.3.4", - "IP Families", "IPv4", - "Port", "port-tcp", "8080/TCP", - "TargetPort", "targetPort/TCP", - "NodePort", "port-tcp", "31111/TCP", - "Session Affinity", "None", - "External Traffic Policy", "Local", - "HealthCheck NodePort", "32222", - }, + expected: dedent.Dedent(` + Name: bar + Namespace: foo + Labels: + Annotations: + Selector: blah=heh + Type: LoadBalancer + IP Families: IPv4 + IP: 1.2.3.4 + IPs: + IP: 5.6.7.8 + Port: port-tcp 8080/TCP + TargetPort: targetPort/TCP + NodePort: port-tcp 31111/TCP + Endpoints: + Session Affinity: None + External Traffic Policy: Local + HealthCheck NodePort: 32222 + Events: + `)[1:], }, { name: "test-ServiceIPFamilyPolicy+ClusterIPs", @@ -796,43 +819,45 @@ func TestDescribeService(t *testing.T) { IPFamilyPolicy: &singleStack, ClusterIPs: []string{"1.2.3.4"}, LoadBalancerIP: "5.6.7.8", - SessionAffinity: "None", - ExternalTrafficPolicy: "Local", + SessionAffinity: corev1.ServiceAffinityNone, + ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal, HealthCheckNodePort: 32222, }, }, - expect: []string{ - "Name", "bar", - "Namespace", "foo", - "Selector", "blah=heh", - "Type", "LoadBalancer", - "IP", "1.2.3.4", - "IP Families", "IPv4", - "IP Family Policy", "SingleStack", - "IPs", "1.2.3.4", - "Port", "port-tcp", "8080/TCP", - "TargetPort", "targetPort/TCP", - "NodePort", "port-tcp", "31111/TCP", - "Session Affinity", "None", - "External Traffic Policy", "Local", - "HealthCheck NodePort", "32222", - }, + expected: dedent.Dedent(` + Name: bar + Namespace: foo + Labels: + Annotations: + Selector: blah=heh + Type: LoadBalancer + IP Family Policy: SingleStack + IP Families: IPv4 + IP: 1.2.3.4 + IPs: 1.2.3.4 + IP: 5.6.7.8 + Port: port-tcp 8080/TCP + TargetPort: targetPort/TCP + NodePort: port-tcp 31111/TCP + Endpoints: + Session Affinity: None + External Traffic Policy: Local + HealthCheck NodePort: 32222 + Events: + `)[1:], }, } - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - fake := fake.NewSimpleClientset(testCase.service) - c := &describeClient{T: t, Namespace: "foo", Interface: fake} + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + fakeClient := fake.NewSimpleClientset(tc.service) + c := &describeClient{T: t, Namespace: "foo", Interface: fakeClient} d := ServiceDescriber{c} out, err := d.Describe("foo", "bar", DescriberSettings{ShowEvents: true}) if err != nil { t.Errorf("unexpected error: %v", err) } - for _, expected := range testCase.expect { - if !strings.Contains(out, expected) { - t.Errorf("expected to find %q in output: %q", expected, out) - } - } + + assert.Equal(t, tc.expected, out) }) } }