|  | 
|  | 1 | +package subnetport | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"fmt" | 
|  | 6 | +	"net/http" | 
|  | 7 | +	"testing" | 
|  | 8 | + | 
|  | 9 | +	"github.com/agiledragon/gomonkey/v2" | 
|  | 10 | +	"github.com/stretchr/testify/assert" | 
|  | 11 | +	admissionv1 "k8s.io/api/admission/v1" | 
|  | 12 | +	v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 13 | +	"k8s.io/apimachinery/pkg/runtime" | 
|  | 14 | +	"k8s.io/apimachinery/pkg/util/json" | 
|  | 15 | +	clientgoscheme "k8s.io/client-go/kubernetes/scheme" | 
|  | 16 | +	"sigs.k8s.io/controller-runtime/pkg/client" | 
|  | 17 | +	"sigs.k8s.io/controller-runtime/pkg/client/fake" | 
|  | 18 | +	"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | 
|  | 19 | + | 
|  | 20 | +	"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1" | 
|  | 21 | +	"github.com/vmware-tanzu/nsx-operator/pkg/util" | 
|  | 22 | +) | 
|  | 23 | + | 
|  | 24 | +func TestAddressBindingValidator_Handle(t *testing.T) { | 
|  | 25 | +	req1, _ := json.Marshal(&v1alpha1.AddressBinding{ | 
|  | 26 | +		ObjectMeta: v1.ObjectMeta{ | 
|  | 27 | +			Namespace: "ns1", | 
|  | 28 | +			Name:      "ab1", | 
|  | 29 | +		}, | 
|  | 30 | +		Spec: v1alpha1.AddressBindingSpec{ | 
|  | 31 | +			VMName:        "vm1", | 
|  | 32 | +			InterfaceName: "inf1", | 
|  | 33 | +		}, | 
|  | 34 | +	}) | 
|  | 35 | +	req1New, _ := json.Marshal(&v1alpha1.AddressBinding{ | 
|  | 36 | +		ObjectMeta: v1.ObjectMeta{ | 
|  | 37 | +			Namespace: "ns1", | 
|  | 38 | +			Name:      "ab1", | 
|  | 39 | +		}, | 
|  | 40 | +		Spec: v1alpha1.AddressBindingSpec{ | 
|  | 41 | +			VMName:        "vm1", | 
|  | 42 | +			InterfaceName: "inf1new", | 
|  | 43 | +		}, | 
|  | 44 | +	}) | 
|  | 45 | +	req2, _ := json.Marshal(&v1alpha1.AddressBinding{ | 
|  | 46 | +		ObjectMeta: v1.ObjectMeta{ | 
|  | 47 | +			Namespace: "ns1", | 
|  | 48 | +			Name:      "ab2", | 
|  | 49 | +		}, | 
|  | 50 | +		Spec: v1alpha1.AddressBindingSpec{ | 
|  | 51 | +			VMName:        "vm1", | 
|  | 52 | +			InterfaceName: "inf2", | 
|  | 53 | +		}, | 
|  | 54 | +	}) | 
|  | 55 | +	type args struct { | 
|  | 56 | +		req admission.Request | 
|  | 57 | +	} | 
|  | 58 | +	tests := []struct { | 
|  | 59 | +		name        string | 
|  | 60 | +		args        args | 
|  | 61 | +		prepareFunc func(*testing.T, client.Client, context.Context) *gomonkey.Patches | 
|  | 62 | +		want        admission.Response | 
|  | 63 | +	}{ | 
|  | 64 | +		{ | 
|  | 65 | +			name: "delete", | 
|  | 66 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Delete}}}, | 
|  | 67 | +			want: admission.Allowed(""), | 
|  | 68 | +		}, | 
|  | 69 | +		{ | 
|  | 70 | +			name: "create decode error", | 
|  | 71 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Create}}}, | 
|  | 72 | +			want: admission.Errored(http.StatusBadRequest, fmt.Errorf("there is no content to decode")), | 
|  | 73 | +		}, | 
|  | 74 | +		{ | 
|  | 75 | +			name: "create", | 
|  | 76 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Create, Object: runtime.RawExtension{Raw: req1}}}}, | 
|  | 77 | +			want: admission.Allowed(""), | 
|  | 78 | +		}, | 
|  | 79 | +		{ | 
|  | 80 | +			name: "create list error", | 
|  | 81 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Create, Object: runtime.RawExtension{Raw: req1}}}}, | 
|  | 82 | +			prepareFunc: func(t *testing.T, client client.Client, ctx context.Context) *gomonkey.Patches { | 
|  | 83 | +				return gomonkey.ApplyMethodSeq(client, "List", []gomonkey.OutputCell{{ | 
|  | 84 | +					Values: gomonkey.Params{fmt.Errorf("mock error")}, | 
|  | 85 | +					Times:  1, | 
|  | 86 | +				}}) | 
|  | 87 | +			}, | 
|  | 88 | +			want: admission.Errored(http.StatusInternalServerError, fmt.Errorf("mock error")), | 
|  | 89 | +		}, | 
|  | 90 | +		{ | 
|  | 91 | +			name: "create dup", | 
|  | 92 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Create, Object: runtime.RawExtension{Raw: req2}}}}, | 
|  | 93 | +			want: admission.Denied("interface already has AddressBinding"), | 
|  | 94 | +		}, | 
|  | 95 | +		{ | 
|  | 96 | +			name: "update decode error", | 
|  | 97 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Update, Object: runtime.RawExtension{Raw: req1}}}}, | 
|  | 98 | +			want: admission.Errored(http.StatusBadRequest, fmt.Errorf("there is no content to decode")), | 
|  | 99 | +		}, | 
|  | 100 | +		{ | 
|  | 101 | +			name: "update changed", | 
|  | 102 | +			args: args{req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Operation: admissionv1.Update, Object: runtime.RawExtension{Raw: req1New}, OldObject: runtime.RawExtension{Raw: req1}}}}, | 
|  | 103 | +			want: admission.Denied("update AddressBinding is not allowed"), | 
|  | 104 | +		}, | 
|  | 105 | +	} | 
|  | 106 | +	for _, tt := range tests { | 
|  | 107 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 108 | +			scheme := clientgoscheme.Scheme | 
|  | 109 | +			v1alpha1.AddToScheme(scheme) | 
|  | 110 | +			client := fake.NewClientBuilder().WithScheme(scheme).WithStatusSubresource(&v1alpha1.AddressBinding{}).WithIndex(&v1alpha1.AddressBinding{}, util.AddressBindingNamespaceVMIndexKey, addressBindingNamespaceVMIndexFunc).Build() | 
|  | 111 | +			decoder := admission.NewDecoder(scheme) | 
|  | 112 | +			ctx := context.TODO() | 
|  | 113 | +			client.Create(ctx, &v1alpha1.AddressBinding{ | 
|  | 114 | +				ObjectMeta: v1.ObjectMeta{ | 
|  | 115 | +					Namespace: "ns1", | 
|  | 116 | +					Name:      "ab2a", | 
|  | 117 | +				}, | 
|  | 118 | +				Spec: v1alpha1.AddressBindingSpec{ | 
|  | 119 | +					VMName:        "vm1", | 
|  | 120 | +					InterfaceName: "inf2", | 
|  | 121 | +				}, | 
|  | 122 | +			}) | 
|  | 123 | +			if tt.prepareFunc != nil { | 
|  | 124 | +				patches := tt.prepareFunc(t, client, ctx) | 
|  | 125 | +				defer patches.Reset() | 
|  | 126 | +			} | 
|  | 127 | +			v := &AddressBindingValidator{ | 
|  | 128 | +				Client:  client, | 
|  | 129 | +				decoder: decoder, | 
|  | 130 | +			} | 
|  | 131 | +			assert.Equalf(t, tt.want, v.Handle(ctx, tt.args.req), "Handle()") | 
|  | 132 | +		}) | 
|  | 133 | +	} | 
|  | 134 | +} | 
0 commit comments