Skip to content

Commit

Permalink
add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverMKing committed Nov 4, 2023
1 parent 1f0e14a commit fc6f14c
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
104 changes: 104 additions & 0 deletions pkg/controller/nginxingress/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package nginxingress

import (
"context"
"testing"

approutingv1alpha1 "github.com/Azure/aks-app-routing-operator/api/v1alpha1"
"github.com/stretchr/testify/require"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestGetDefaultIngressClassControllerClass(t *testing.T) {
cl := fake.NewClientBuilder().Build()

// when default IngressClass doesn't exist in cluster it defaults to webapprouting.kubernetes.azure.com/nginx
cc, err := getDefaultIngressClassControllerClass(cl)
require.NoError(t, err)
require.Equal(t, "webapprouting.kubernetes.azure.com/nginx", cc)

// when IngressClass exists in cluster we take whatever is in the Spec.Controller field
controller := "controllerField"
ic := &netv1.IngressClass{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultIcName,
},
Spec: netv1.IngressClassSpec{
Controller: controller,
},
}
require.NoError(t, cl.Create(context.Background(), ic))
cc, err = getDefaultIngressClassControllerClass(cl)
require.NoError(t, err)
require.Equal(t, controller, cc)
}

func TestIsDefaultNic(t *testing.T) {
cases := []struct {
Name string
Nic *approutingv1alpha1.NginxIngressController
Expected bool
}{
{
Name: "nil nic",
Nic: nil,
Expected: false,
},
{
Name: "default name, default IngressClassName",
Nic: &approutingv1alpha1.NginxIngressController{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultNicName,
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
IngressClassName: DefaultIcName,
},
},
Expected: true,
},
{
Name: "default name, non default IngressClassName",
Nic: &approutingv1alpha1.NginxIngressController{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultNicName,
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
IngressClassName: "non-default",
},
},
Expected: false,
},
{
Name: "non default name, default IngressClassName",
Nic: &approutingv1alpha1.NginxIngressController{
ObjectMeta: metav1.ObjectMeta{
Name: "non-default",
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
IngressClassName: DefaultIcName,
},
},
Expected: false,
},
{
Name: "non default name, non default IngressClassName",
Nic: &approutingv1alpha1.NginxIngressController{
ObjectMeta: metav1.ObjectMeta{
Name: "non-default",
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
IngressClassName: "non-default",
},
},
Expected: false,
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
require.Equal(t, c.Expected, IsDefaultNic(c.Nic))
})
}
}
44 changes: 44 additions & 0 deletions pkg/webhook/nginxingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

approutingv1alpha1 "github.com/Azure/aks-app-routing-operator/api/v1alpha1"
"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
"github.com/Azure/aks-app-routing-operator/pkg/controller/nginxingress"
"github.com/Azure/aks-app-routing-operator/pkg/controller/testutils"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -91,6 +92,13 @@ func TestNginxIngressResourceValidator(t *testing.T) {
}
require.NoError(t, cl.Create(context.Background(), existingNic))

defaultIc := &netv1.IngressClass{
ObjectMeta: metav1.ObjectMeta{
Name: nginxingress.DefaultIcName,
},
}
require.NoError(t, cl.Create(context.Background(), defaultIc))

cases := []struct {
name string
req admission.Request
Expand Down Expand Up @@ -238,6 +246,42 @@ func TestNginxIngressResourceValidator(t *testing.T) {
authenticator: validUser,
expected: admission.Allowed(""),
},
{
name: "valid nginx ingress controller, valid user, default nic",
req: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Create,
Object: runtime.RawExtension{
Raw: toRaw(func() *approutingv1alpha1.NginxIngressController {
copy := validNginxIngressController.DeepCopy()
copy.Spec.IngressClassName = nginxingress.DefaultIcName
copy.Name = nginxingress.DefaultNicName
return copy
}()),
},
},
},
authenticator: validUser,
expected: admission.Allowed(""),
},
{
name: "valid nginx ingress controller, invalid user, default nic",
req: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Create,
Object: runtime.RawExtension{
Raw: toRaw(func() *approutingv1alpha1.NginxIngressController {
copy := validNginxIngressController.DeepCopy()
copy.Spec.IngressClassName = nginxingress.DefaultIcName
copy.Name = nginxingress.DefaultNicName
return copy
}()),
},
},
},
authenticator: invalidUser,
expected: admission.Denied("invalid user"),
},
}

metrics.InitControllerMetrics(nginxResourceValidationName)
Expand Down

0 comments on commit fc6f14c

Please sign in to comment.