Skip to content

Commit

Permalink
Add tests for validating webhhok
Browse files Browse the repository at this point in the history
Signed-off-by: Md Soharab Ansari <[email protected]>
  • Loading branch information
soharab-ic committed Feb 26, 2024
1 parent 1b45834 commit 0d43b6a
Showing 1 changed file with 132 additions and 1 deletion.
133 changes: 132 additions & 1 deletion pkg/webhook/pod/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,30 @@ func (f fakeWebhookClient) GetNamespaceLabels(ctx context.Context, client client
return map[string]string{controllers.ApplicationNamespaceSelectorLabelKey: "green"}, nil
}
func (f fakeWebhookClient) GetAllServiceExports(ctx context.Context, client client.Client, slice string) (*v1beta1.ServiceExportList, error) {
return &v1beta1.ServiceExportList{}, nil
return &v1beta1.ServiceExportList{
Items: []v1beta1.ServiceExport{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-1",
Namespace: "test-ns-1",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"server.com"},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-2",
Namespace: "test-ns-2",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"traffic.com"},
},
},
},
}, nil
}

func (f fakeWebhookClient) GetSliceOverlayNetworkType(ctx context.Context, client client.Client, sliceName string) (string, error) {
Expand Down Expand Up @@ -131,3 +154,111 @@ var _ = Describe("Deploy Webhook", func() {
})
})
})

var _ = Describe("Validating Webhook", func() {
fakeWhClient := new(fakeWebhookClient)
webhookServer := pod.WebhookServer{
SliceInfoClient: fakeWhClient,
}
Describe("ValidateServiceExport", func() {
Context("ServiceExport Object with no alias conflict", func() {
serviceExportList := &v1beta1.ServiceExportList{
Items: []v1beta1.ServiceExport{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-3",
Namespace: "test-ns-1",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"hello.com"},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-4",
Namespace: "test-ns-2",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"connect.com"},
},
},
},
}
It("should be created", func() {
for _, serviceExport := range serviceExportList.Items {
is, _, _ := webhookServer.ValidateServiceExport(&serviceExport, context.Background())
Expect(is).To(BeTrue())
}
})
})

Context("Alias already exist", func() {
serviceExportList := &v1beta1.ServiceExportList{
Items: []v1beta1.ServiceExport{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-3",
Namespace: "test-ns-1",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"Server.com"},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-4",
Namespace: "test-ns-2",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"traffic.com"},
},
},
},
}
It("should be rejected", func() {
for _, serviceExport := range serviceExportList.Items {
is, _, _ := webhookServer.ValidateServiceExport(&serviceExport, context.Background())
Expect(is).To(BeFalse())
}
})
})

Context("Update ServiceExport aliases", func() {
serviceExport := &v1beta1.ServiceExport{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-2",
Namespace: "test-ns-2",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"Traffic.com", "connect.com"},
},
}
It("should be updated", func() {
is, _, _ := webhookServer.ValidateServiceExport(serviceExport, context.Background())
Expect(is).To(BeTrue())
})
})

Context("Update ServiceExport with conflicting alias", func() {
serviceExport := &v1beta1.ServiceExport{
ObjectMeta: metav1.ObjectMeta{
Name: "svcex-1",
Namespace: "test-ns-1",
},
Spec: v1beta1.ServiceExportSpec{
Slice: "test-slice",
Aliases: []string{"traffic.com"},
},
}
It("should be rejected", func() {
is, _, _ := webhookServer.ValidateServiceExport(serviceExport, context.Background())
Expect(is).To(BeFalse())
})
})
})
})

0 comments on commit 0d43b6a

Please sign in to comment.