mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	core/v1: add validation and defaulting unit tests for when internalTrafficPolicy is nil and Service type is ExternalName
Signed-off-by: Andrew Sy Kim <kim.andrewsy@gmail.com>
This commit is contained in:
		@@ -1840,19 +1840,19 @@ func TestSetDefaultServiceInternalTrafficPolicy(t *testing.T) {
 | 
				
			|||||||
	local := v1.ServiceInternalTrafficPolicyLocal
 | 
						local := v1.ServiceInternalTrafficPolicyLocal
 | 
				
			||||||
	testCases := []struct {
 | 
						testCases := []struct {
 | 
				
			||||||
		name                          string
 | 
							name                          string
 | 
				
			||||||
		expectedInternalTrafficPolicy v1.ServiceInternalTrafficPolicyType
 | 
							expectedInternalTrafficPolicy *v1.ServiceInternalTrafficPolicyType
 | 
				
			||||||
		svc                           v1.Service
 | 
							svc                           v1.Service
 | 
				
			||||||
		featureGateOn                 bool
 | 
							featureGateOn                 bool
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name:                          "must set default internalTrafficPolicy",
 | 
								name:                          "must set default internalTrafficPolicy",
 | 
				
			||||||
			expectedInternalTrafficPolicy: v1.ServiceInternalTrafficPolicyCluster,
 | 
								expectedInternalTrafficPolicy: &cluster,
 | 
				
			||||||
			svc:                           v1.Service{},
 | 
								svc:                           v1.Service{},
 | 
				
			||||||
			featureGateOn:                 true,
 | 
								featureGateOn:                 true,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name:                          "must not set default internalTrafficPolicy when it's cluster",
 | 
								name:                          "must not set default internalTrafficPolicy when it's cluster",
 | 
				
			||||||
			expectedInternalTrafficPolicy: v1.ServiceInternalTrafficPolicyCluster,
 | 
								expectedInternalTrafficPolicy: &cluster,
 | 
				
			||||||
			svc: v1.Service{
 | 
								svc: v1.Service{
 | 
				
			||||||
				Spec: v1.ServiceSpec{
 | 
									Spec: v1.ServiceSpec{
 | 
				
			||||||
					InternalTrafficPolicy: &cluster,
 | 
										InternalTrafficPolicy: &cluster,
 | 
				
			||||||
@@ -1860,9 +1860,19 @@ func TestSetDefaultServiceInternalTrafficPolicy(t *testing.T) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
			featureGateOn: true,
 | 
								featureGateOn: true,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                          "must not set default internalTrafficPolicy when type is ExternalName",
 | 
				
			||||||
 | 
								expectedInternalTrafficPolicy: nil,
 | 
				
			||||||
 | 
								svc: v1.Service{
 | 
				
			||||||
 | 
									Spec: v1.ServiceSpec{
 | 
				
			||||||
 | 
										Type: v1.ServiceTypeExternalName,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								featureGateOn: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name:                          "must not set default internalTrafficPolicy when it's local",
 | 
								name:                          "must not set default internalTrafficPolicy when it's local",
 | 
				
			||||||
			expectedInternalTrafficPolicy: v1.ServiceInternalTrafficPolicyLocal,
 | 
								expectedInternalTrafficPolicy: &local,
 | 
				
			||||||
			svc: v1.Service{
 | 
								svc: v1.Service{
 | 
				
			||||||
				Spec: v1.ServiceSpec{
 | 
									Spec: v1.ServiceSpec{
 | 
				
			||||||
					InternalTrafficPolicy: &local,
 | 
										InternalTrafficPolicy: &local,
 | 
				
			||||||
@@ -1872,7 +1882,7 @@ func TestSetDefaultServiceInternalTrafficPolicy(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name:                          "must not set default internalTrafficPolicy when gate is disabled",
 | 
								name:                          "must not set default internalTrafficPolicy when gate is disabled",
 | 
				
			||||||
			expectedInternalTrafficPolicy: "",
 | 
								expectedInternalTrafficPolicy: nil,
 | 
				
			||||||
			svc:                           v1.Service{},
 | 
								svc:                           v1.Service{},
 | 
				
			||||||
			featureGateOn:                 false,
 | 
								featureGateOn:                 false,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
@@ -1883,14 +1893,8 @@ func TestSetDefaultServiceInternalTrafficPolicy(t *testing.T) {
 | 
				
			|||||||
			obj := roundTrip(t, runtime.Object(&test.svc))
 | 
								obj := roundTrip(t, runtime.Object(&test.svc))
 | 
				
			||||||
			svc := obj.(*v1.Service)
 | 
								svc := obj.(*v1.Service)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if test.expectedInternalTrafficPolicy == "" {
 | 
								if !reflect.DeepEqual(svc.Spec.InternalTrafficPolicy, test.expectedInternalTrafficPolicy) {
 | 
				
			||||||
				if svc.Spec.InternalTrafficPolicy != nil {
 | 
									t.Errorf("expected .spec.internalTrafficPolicy: %v got %v", test.expectedInternalTrafficPolicy, svc.Spec.InternalTrafficPolicy)
 | 
				
			||||||
					t.Fatalf("expected .spec.internalTrafficPolicy: null, got %v", *svc.Spec.InternalTrafficPolicy)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				if *svc.Spec.InternalTrafficPolicy != test.expectedInternalTrafficPolicy {
 | 
					 | 
				
			||||||
					t.Fatalf("expected .spec.internalTrafficPolicy: %v got %v", test.expectedInternalTrafficPolicy, *svc.Spec.InternalTrafficPolicy)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12281,6 +12281,16 @@ func TestValidateServiceCreate(t *testing.T) {
 | 
				
			|||||||
			featureGates: []featuregate.Feature{features.ServiceInternalTrafficPolicy},
 | 
								featureGates: []featuregate.Feature{features.ServiceInternalTrafficPolicy},
 | 
				
			||||||
			numErrs:      1,
 | 
								numErrs:      1,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "internalTrafficPolicy field nil when type is ExternalName",
 | 
				
			||||||
 | 
								tweakSvc: func(s *core.Service) {
 | 
				
			||||||
 | 
									s.Spec.InternalTrafficPolicy = nil
 | 
				
			||||||
 | 
									s.Spec.Type = core.ServiceTypeExternalName
 | 
				
			||||||
 | 
									s.Spec.ExternalName = "foo.bar.com"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								featureGates: []featuregate.Feature{features.ServiceInternalTrafficPolicy},
 | 
				
			||||||
 | 
								numErrs:      0,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name: "invalid internalTraffic field",
 | 
								name: "invalid internalTraffic field",
 | 
				
			||||||
			tweakSvc: func(s *core.Service) {
 | 
								tweakSvc: func(s *core.Service) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user