Skip to content

Commit

Permalink
test: add invalid type tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito committed Jan 21, 2025
1 parent 376a72b commit 03a0586
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
32 changes: 32 additions & 0 deletions api/policies/v1/admissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestAdmissionPolicyDefault(t *testing.T) {
assert.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestAdmissionPolicyDefaultWithInvalidType(t *testing.T) {
defaulter := admissionPolicyDefaulter{logger: logr.Discard()}
obj := &corev1.Pod{}

err := defaulter.Default(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicy object, got *v1.Pod")
}

func TestAdmissionPolicyValidateCreate(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyFactory().Build()
Expand Down Expand Up @@ -136,6 +144,15 @@ func TestAdmissionPolicyValidateCreateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateCreateWithInvalidType(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateCreate(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateUpdate(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
oldPolicy := NewAdmissionPolicyFactory().Build()
Expand Down Expand Up @@ -180,6 +197,21 @@ func TestAdmissionPolicyValidateUpdateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateUpdateWithInvalidType(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}
oldPolicy := NewAdmissionPolicyFactory().Build()
newPolicy := NewAdmissionPolicyFactory().Build()

warnings, err := validator.ValidateUpdate(context.Background(), obj, newPolicy)
require.ErrorContains(t, err, "expected an AdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)

warnings, err = validator.ValidateUpdate(context.Background(), oldPolicy, obj)
require.ErrorContains(t, err, "expected an AdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestAdmissionPolicyValidateDelete(t *testing.T) {
validator := admissionPolicyValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyFactory().Build()
Expand Down
32 changes: 32 additions & 0 deletions api/policies/v1/admissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestAdmissionPolicyGroupDefault(t *testing.T) {
assert.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestAdmissionPolicyGroupDefaultWithInvalidType(t *testing.T) {
defaulter := admissionPolicyGroupDefaulter{logger: logr.Discard()}
obj := &corev1.Pod{}

err := defaulter.Default(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicyGroup object, got *v1.Pod")
}

func TestAdmissionPolicyGroupValidateCreate(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyGroupFactory().Build()
Expand Down Expand Up @@ -137,6 +145,15 @@ func TestAdmissionPolicyGroupValidateCreateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateCreateWithInvalidType(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateCreate(context.Background(), obj)
require.ErrorContains(t, err, "expected an AdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateUpdate(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
oldPolicy := NewAdmissionPolicyGroupFactory().Build()
Expand Down Expand Up @@ -181,6 +198,21 @@ func TestAdmissionPolicyGroupValidateUpdateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateUpdateWithInvalidType(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}
oldPolicy := NewAdmissionPolicyGroupFactory().Build()
newPolicy := NewAdmissionPolicyGroupFactory().Build()

warnings, err := validator.ValidateUpdate(context.Background(), obj, newPolicy)
require.ErrorContains(t, err, "expected an AdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)

warnings, err = validator.ValidateUpdate(context.Background(), oldPolicy, obj)
require.ErrorContains(t, err, "expected an AdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateDelete(t *testing.T) {
validator := admissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewAdmissionPolicyGroupFactory().Build()
Expand Down
32 changes: 32 additions & 0 deletions api/policies/v1/clusteradmissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestClusterAdmissionPolicyDefault(t *testing.T) {
assert.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestClusterAdmissionPolicyDefaultWithInvalidType(t *testing.T) {
defaulter := clusterAdmissionPolicyDefaulter{logger: logr.Discard()}
obj := &corev1.Pod{}

err := defaulter.Default(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicy object, got *v1.Pod")
}

func TestClusterAdmissionPolicyValidateCreate(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyFactory().Build()
Expand Down Expand Up @@ -137,6 +145,15 @@ func TestClusterAdmissionPolicyValidateCreateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateCreateWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateCreate(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateUpdate(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
oldPolicy := NewClusterAdmissionPolicyFactory().Build()
Expand Down Expand Up @@ -181,6 +198,21 @@ func TestClusterAdmissionPolicyValidateUpdateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateUpdateWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
obj := &corev1.Pod{}
oldPolicy := NewClusterAdmissionPolicyFactory().Build()
newPolicy := NewClusterAdmissionPolicyFactory().Build()

warnings, err := validator.ValidateUpdate(context.Background(), obj, newPolicy)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)

warnings, err = validator.ValidateUpdate(context.Background(), oldPolicy, obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicy object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateDelete(t *testing.T) {
validator := clusterAdmissionPolicyValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyFactory().Build()
Expand Down
34 changes: 33 additions & 1 deletion api/policies/v1/clusteradmissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestClusterAdmissionPolicyGroupDefault(t *testing.T) {
assert.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestClusterAdmissionPolicyGroupDefaultWithInvalidType(t *testing.T) {
defaulter := clusterAdmissionPolicyGroupDefaulter{logger: logr.Discard()}
obj := &corev1.Pod{}

err := defaulter.Default(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicyGroup object, got *v1.Pod")
}

func TestClusterAdmissionPolicyGroupValidateCreate(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyGroupFactory().Build()
Expand Down Expand Up @@ -137,6 +145,15 @@ func TestClusterAdmissionPolicyGroupValidateCreateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyGroupValidateCreateWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

warnings, err := validator.ValidateCreate(context.Background(), obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyGroupValidateUpdate(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
oldPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
Expand Down Expand Up @@ -181,6 +198,21 @@ func TestClusterAdmissionPolicyGroupValidateUpdateWithErrors(t *testing.T) {
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyGroupValidateUpdateWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}
oldPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
newPolicy := NewClusterAdmissionPolicyGroupFactory().Build()

warnings, err := validator.ValidateUpdate(context.Background(), obj, newPolicy)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)

warnings, err = validator.ValidateUpdate(context.Background(), oldPolicy, obj)
require.ErrorContains(t, err, "expected a ClusterAdmissionPolicyGroup object, got *v1.Pod")
assert.Empty(t, warnings)
}

func TestClusterAdmissionPolicyGroupValidateDelete(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
policy := NewClusterAdmissionPolicyGroupFactory().Build()
Expand All @@ -190,7 +222,7 @@ func TestClusterAdmissionPolicyGroupValidateDelete(t *testing.T) {
assert.Empty(t, warnings)
}

func TestClusteerAdmissionPolicyGroupValidateDeleteWithInvalidType(t *testing.T) {
func TestClusterAdmissionPolicyGroupValidateDeleteWithInvalidType(t *testing.T) {
validator := clusterAdmissionPolicyGroupValidator{logger: logr.Discard()}
obj := &corev1.Pod{}

Expand Down

0 comments on commit 03a0586

Please sign in to comment.