kubectl/describe/svc: refactor TestDescribeService
Signed-off-by: Daman Arora <aroradaman@gmail.com> Kubernetes-commit: df417aa9b8813d0e5e1208e9039f140edeff39db
This commit is contained in:
		
							parent
							
								
									771a13a844
								
							
						
					
					
						commit
						5aca4630e4
					
				|  | @ -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:                   <none> | ||||
| 				Annotations:              <none> | ||||
| 				Selector:                 blah=heh | ||||
| 				Type:                     LoadBalancer | ||||
| 				IP Families:              IPv4 | ||||
| 				IP:                       1.2.3.4 | ||||
| 				IPs:                      <none> | ||||
| 				IP:                       5.6.7.8 | ||||
| 				Port:                     port-tcp  8080/TCP | ||||
| 				TargetPort:               9527/TCP | ||||
| 				NodePort:                 port-tcp  31111/TCP | ||||
| 				Endpoints:                <none> | ||||
| 				Session Affinity:         None | ||||
| 				External Traffic Policy:  Local | ||||
| 				HealthCheck NodePort:     32222 | ||||
| 				Events:                   <none> | ||||
| 			`)[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:                   <none> | ||||
| 				Annotations:              <none> | ||||
| 				Selector:                 blah=heh | ||||
| 				Type:                     LoadBalancer | ||||
| 				IP Families:              IPv4 | ||||
| 				IP:                       1.2.3.4 | ||||
| 				IPs:                      <none> | ||||
| 				IP:                       5.6.7.8 | ||||
| 				Port:                     port-tcp  8080/TCP | ||||
| 				TargetPort:               targetPort/TCP | ||||
| 				NodePort:                 port-tcp  31111/TCP | ||||
| 				Endpoints:                <none> | ||||
| 				Session Affinity:         None | ||||
| 				External Traffic Policy:  Local | ||||
| 				HealthCheck NodePort:     32222 | ||||
| 				Events:                   <none> | ||||
| 			`)[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:                   <none> | ||||
| 				Annotations:              <none> | ||||
| 				Selector:                 blah=heh | ||||
| 				Type:                     LoadBalancer | ||||
| 				IP Families:              IPv4 | ||||
| 				IP:                       1.2.3.4 | ||||
| 				IPs:                      <none> | ||||
| 				IP:                       5.6.7.8 | ||||
| 				Port:                     port-tcp  8080/TCP | ||||
| 				TargetPort:               targetPort/TCP | ||||
| 				NodePort:                 port-tcp  31111/TCP | ||||
| 				Endpoints:                <none> | ||||
| 				Session Affinity:         None | ||||
| 				External Traffic Policy:  Local | ||||
| 				HealthCheck NodePort:     32222 | ||||
| 				Events:                   <none> | ||||
| 			`)[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:                   <none> | ||||
| 				Annotations:              <none> | ||||
| 				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:                <none> | ||||
| 				Session Affinity:         None | ||||
| 				External Traffic Policy:  Local | ||||
| 				HealthCheck NodePort:     32222 | ||||
| 				Events:                   <none> | ||||
| 			`)[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) | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue