func TestBeforeCreate(t *testing.T) {
	withIP := func(family *api.IPFamily, ip string) *api.Service {
		svc := makeValidService()
		svc.Spec.IPFamily = family
		svc.Spec.ClusterIP = ip
		return &svc
	}

	ipv4 := api.IPv4Protocol
	ipv6 := api.IPv6Protocol
	testCases := []struct {
		name               string
		cidr               string
		configureDualStack bool
		enableDualStack    bool
		in                 *api.Service
		expect             *api.Service
		expectErr          bool
	}{
		{
			name:   "does not set ipfamily when dual stack gate is disabled",
			cidr:   "10.0.0.0/16",
			in:     withIP(nil, ""),
			expect: withIP(nil, ""),
		},

		{
			name:   "clears ipfamily when dual stack gate is disabled",
			cidr:   "10.0.0.0/16",
			in:     withIP(&ipv4, ""),
			expect: withIP(nil, ""),
		},

		{
			name:            "allows ipfamily to configured ipv4 value",
			cidr:            "10.0.0.0/16",
			enableDualStack: true,
			in:              withIP(nil, ""),
			expect:          withIP(&ipv4, ""),
		},
		{
			name:               "allows ipfamily to configured ipv4 value when dual stack is in use",
			cidr:               "10.0.0.0/16",
			enableDualStack:    true,
			configureDualStack: true,
			in:                 withIP(nil, ""),
			expect:             withIP(&ipv4, ""),
		},
		{
			name:            "allows ipfamily to configured ipv6 value",
			cidr:            "fd00::/64",
			enableDualStack: true,
			in:              withIP(nil, ""),
			expect:          withIP(&ipv6, ""),
		},
		{
			name:               "allows ipfamily to configured ipv6 value when dual stack is in use",
			cidr:               "fd00::/64",
			enableDualStack:    true,
			configureDualStack: true,
			in:                 withIP(nil, ""),
			expect:             withIP(&ipv6, ""),
		},

		{
			name:            "rejects ipv6 ipfamily when single-stack ipv4",
			enableDualStack: true,
			cidr:            "10.0.0.0/16",
			in:              withIP(&ipv6, ""),
			expectErr:       true,
		},
		{
			name:            "rejects ipv4 ipfamily when single-stack ipv6",
			enableDualStack: true,
			cidr:            "fd00::/64",
			in:              withIP(&ipv4, ""),
			expectErr:       true,
		},
		{
			name:            "rejects implicit ipv4 ipfamily when single-stack ipv6",
			enableDualStack: true,
			cidr:            "fd00::/64",
			in:              withIP(nil, "10.0.1.0"),
			expectErr:       true,
		},
		{
			name:            "rejects implicit ipv6 ipfamily when single-stack ipv4",
			enableDualStack: true,
			cidr:            "10.0.0.0/16",
			in:              withIP(nil, "fd00::1"),
			expectErr:       true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)()
			testStrategy, _ := newStrategy(tc.cidr, tc.configureDualStack)
			ctx := genericapirequest.NewDefaultContext()
			err := rest.BeforeCreate(testStrategy, ctx, runtime.Object(tc.in))
			if tc.expectErr != (err != nil) {
				t.Fatalf("unexpected error: %v", err)
			}
			if err != nil {
				return
			}
			if tc.expect != nil && tc.in != nil {
				tc.expect.ObjectMeta = tc.in.ObjectMeta
			}
			if !reflect.DeepEqual(tc.expect, tc.in) {
				t.Fatalf("unexpected change: %s", diff.ObjectReflectDiff(tc.expect, tc.in))
			}
		})
	}
}
// TODO: This should be done on types that are not part of our API
This should be done on types that are not part of our API
// TODO : This should be done on types that are not part of our API
func TestBeforeCreate(t *testing.T) {
	withIP := func(family *api.IPFamily, ip string) *api.Service {
		svc := makeValidService()
		svc.Spec.IPFamily = family
		svc.Spec.ClusterIP = ip
		return &svc
	}
	ipv4 := api.IPv4Protocol
	ipv6 := api.IPv6Protocol
	testCases := []struct {
		name               string
		cidr               string
		configureDualStack bool
		enableDualStack    bool
		in                 *api.Service
		expect             *api.Service
		expectErr          bool
	}{
		{
			name:   "does not set ipfamily when dual stack gate is disabled",
			cidr:   "10.0.0.0/16",
			in:     withIP(nil, ""),
			expect: withIP(nil, ""),
		},
		{
			name:   "clears ipfamily when dual stack gate is disabled",
			cidr:   "10.0.0.0/16",
			in:     withIP(&ipv4, ""),
			expect: withIP(nil, ""),
		},
		{
			name:            "allows ipfamily to configured ipv4 value",
			cidr:            "10.0.0.0/16",
			enableDualStack: true,
			in:              withIP(nil, ""),
			expect:          withIP(&ipv4, ""),
		},
		{
			name:               "allows ipfamily to configured ipv4 value when dual stack is in use",
			cidr:               "10.0.0.0/16",
			enableDualStack:    true,
			configureDualStack: true,
			in:                 withIP(nil, ""),
			expect:             withIP(&ipv4, ""),
		},
		{
			name:            "allows ipfamily to configured ipv6 value",
			cidr:            "fd00::/64",
			enableDualStack: true,
			in:              withIP(nil, ""),
			expect:          withIP(&ipv6, ""),
		},
		{
			name:               "allows ipfamily to configured ipv6 value when dual stack is in use",
			cidr:               "fd00::/64",
			enableDualStack:    true,
			configureDualStack: true,
			in:                 withIP(nil, ""),
			expect:             withIP(&ipv6, ""),
		},
		{
			name:            "rejects ipv6 ipfamily when single-stack ipv4",
			enableDualStack: true,
			cidr:            "10.0.0.0/16",
			in:              withIP(&ipv6, ""),
			expectErr:       true,
		},
		{
			name:            "rejects ipv4 ipfamily when single-stack ipv6",
			enableDualStack: true,
			cidr:            "fd00::/64",
			in:              withIP(&ipv4, ""),
			expectErr:       true,
		},
		{
			name:            "rejects implicit ipv4 ipfamily when single-stack ipv6",
			enableDualStack: true,
			cidr:            "fd00::/64",
			in:              withIP(nil, "10.0.1.0"),
			expectErr:       true,
		},
		{
			name:            "rejects implicit ipv6 ipfamily when single-stack ipv4",
			enableDualStack: true,
			cidr:            "10.0.0.0/16",
			in:              withIP(nil, "fd00::1"),
			expectErr:       true,
		},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)()
			testStrategy, _ := newStrategy(tc.cidr, tc.configureDualStack)
			ctx := genericapirequest.NewDefaultContext()
			err := rest.BeforeCreate(testStrategy, ctx, runtime.Object(tc.in))
			if tc.expectErr != (err != nil) {
				t.Fatalf("unexpected error: %v", err)
			}
			if err != nil {
				return
			}
			if tc.expect != nil && tc.in != nil {
				tc.expect.ObjectMeta = tc.in.ObjectMeta
			}
			if !reflect.DeepEqual(tc.expect, tc.in) {
				t.Fatalf("unexpected change: %s", diff.ObjectReflectDiff(tc.expect, tc.in))
			}
		})
	}
}
func TestBeforeCreate ( t * testing . T ) { withIP := func ( family * api . IPFamily , ip string ) * api . Service { svc := makeValidService ( ) svc . Spec . IPFamily = family svc . Spec . ClusterIP = ip return & svc } ipv4 := api . IPv4Protocol ipv6 := api . IPv6Protocol testCases := [ ] struct { name string cidr string configureDualStack bool enableDualStack bool in * api . Service expect * api . Service expectErr bool } { { name : " " , cidr : " " , in : withIP ( nil , " " ) , expect : withIP ( nil , " " ) , } , { name : " " , cidr : " " , in : withIP ( & ipv4 , " " ) , expect : withIP ( nil , " " ) , } , { name : " " , cidr : " " , enableDualStack : true , in : withIP ( nil , " " ) , expect : withIP ( & ipv4 , " " ) , } , { name : " " , cidr : " " , enableDualStack : true , configureDualStack : true , in : withIP ( nil , " " ) , expect : withIP ( & ipv4 , " " ) , } , { name : " " , cidr : " " , enableDualStack : true , in : withIP ( nil , " " ) , expect : withIP ( & ipv6 , " " ) , } , { name : " " , cidr : " " , enableDualStack : true , configureDualStack : true , in : withIP ( nil , " " ) , expect : withIP ( & ipv6 , " " ) , } , { name : " " , enableDualStack : true , cidr : " " , in : withIP ( & ipv6 , " " ) , expectErr : true , } , { name : " " , enableDualStack : true , cidr : " " , in : withIP ( & ipv4 , " " ) , expectErr : true , } , { name : " " , enableDualStack : true , cidr : " " , in : withIP ( nil , " " ) , expectErr : true , } , { name : " " , enableDualStack : true , cidr : " " , in : withIP ( nil , " " ) , expectErr : true , } , } for _ , tc := range testCases { t . Run ( tc . name , func ( t * testing . T ) { defer featuregatetesting . SetFeatureGateDuringTest ( t , utilfeature . DefaultFeatureGate , features . IPv6DualStack , tc . enableDualStack ) ( ) testStrategy , _ := newStrategy ( tc . cidr , tc . configureDualStack ) ctx := genericapirequest . NewDefaultContext ( ) err := rest . BeforeCreate ( testStrategy , ctx , runtime . Object ( tc . in ) ) if tc . expectErr != ( err != nil ) { t . Fatalf ( " " , err ) } if err != nil { return } if tc . expect != nil && tc . in != nil { tc . expect . ObjectMeta = tc . in . ObjectMeta } if ! reflect . DeepEqual ( tc . expect , tc . in ) { t . Fatalf ( " " , diff . ObjectReflectDiff ( tc . expect , tc . in ) ) } } ) } }
This should be done on types that are not part of our API
This should be done on types that are not part of our API