Skip to content

Commit

Permalink
chore(tests): move types factories to policies/v1 package.
Browse files Browse the repository at this point in the history
Move the factories used in tests to build our types to the policies/v1
package. Therefore, all the tests under /api and /internal/controller
directories can use them.

Signed-off-by: José Guilherme Vanz <jguilhermevanz@suse.com>
  • Loading branch information
jvanz committed Jan 10, 2025
1 parent 73de594 commit 6306fe5
Show file tree
Hide file tree
Showing 14 changed files with 1,054 additions and 766 deletions.
12 changes: 6 additions & 6 deletions api/policies/v1/admissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ import (
)

func TestAdmissionPolicyDefault(t *testing.T) {
policy := admissionPolicyFactory()
policy := AdmissionPolicy{}
policy.Default()

require.Equal(t, constants.DefaultPolicyServer, policy.GetPolicyServer())
require.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestAdmissionPolicyValidateCreate(t *testing.T) {
policy := admissionPolicyFactory()
policy := NewAdmissionPolicyFactory().Build()
warnings, err := policy.ValidateCreate()
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestAdmissionPolicyValidateUpdate(t *testing.T) {
oldPolicy := admissionPolicyFactory()
newPolicy := admissionPolicyFactory()
oldPolicy := NewAdmissionPolicyFactory().Build()
newPolicy := NewAdmissionPolicyFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestAdmissionPolicyValidateUpdateWithInvalidOldPolicy(t *testing.T) {
oldPolicy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
newPolicy := admissionPolicyFactory()
oldPolicy := NewClusterAdmissionPolicyFactory().Build()
newPolicy := NewAdmissionPolicyFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.Empty(t, warnings)
require.ErrorContains(t, err, "object is not of type AdmissionPolicy")
Expand Down
14 changes: 7 additions & 7 deletions api/policies/v1/admissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import (
)

func TestAdmissionPolicyGroupDefault(t *testing.T) {
policy := admissionPolicyGroupFactory()
policy := AdmissionPolicyGroup{}
policy.Default()

require.Equal(t, constants.DefaultPolicyServer, policy.GetPolicyServer())
require.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestAdmissionPolicyGroupValidateCreate(t *testing.T) {
policy := admissionPolicyGroupFactory()
policy := NewAdmissionPolicyGroupFactory().Build()
warnings, err := policy.ValidateCreate()
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateCreateWithNoMembers(t *testing.T) {
policy := admissionPolicyGroupFactory()
policy := NewAdmissionPolicyGroupFactory().Build()
policy.Spec.Policies = nil
warnings, err := policy.ValidateCreate()
require.Error(t, err)
Expand All @@ -47,16 +47,16 @@ func TestClusterAdmissionPolicyValidateCreateWithNoMembers(t *testing.T) {
}

func TestAdmissionPolicyGroupValidateUpdate(t *testing.T) {
oldPolicy := admissionPolicyGroupFactory()
newPolicy := admissionPolicyGroupFactory()
oldPolicy := NewAdmissionPolicyGroupFactory().Build()
newPolicy := NewAdmissionPolicyGroupFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestAdmissionPolicyGroupValidateUpdateWithInvalidOldPolicy(t *testing.T) {
oldPolicy := clusterAdmissionPolicyGroupFactory()
newPolicy := admissionPolicyGroupFactory()
oldPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
newPolicy := NewAdmissionPolicyGroupFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.Empty(t, warnings)
require.ErrorContains(t, err, "object is not of type AdmissionPolicyGroup")
Expand Down
12 changes: 6 additions & 6 deletions api/policies/v1/clusteradmissionpolicy_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ import (
)

func TestClusterAdmissionPolicyDefault(t *testing.T) {
policy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
policy := ClusterAdmissionPolicy{}
policy.Default()

require.Equal(t, constants.DefaultPolicyServer, policy.GetPolicyServer())
require.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestClusterAdmissionPolicyValidateCreate(t *testing.T) {
policy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
policy := NewClusterAdmissionPolicyFactory().Build()
warnings, err := policy.ValidateCreate()
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateUpdate(t *testing.T) {
oldPolicy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
newPolicy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
oldPolicy := NewClusterAdmissionPolicyFactory().Build()
newPolicy := NewClusterAdmissionPolicyFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClusterAdmissionPolicyValidateUpdateWithInvalidOldPolicy(t *testing.T) {
oldPolicy := admissionPolicyFactory()
newPolicy := clusterAdmissionPolicyFactory(nil, nil, "", "protect")
oldPolicy := NewAdmissionPolicyFactory().Build()
newPolicy := NewClusterAdmissionPolicyFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.Empty(t, warnings)
require.ErrorContains(t, err, "object is not of type ClusterAdmissionPolicy")
Expand Down
14 changes: 7 additions & 7 deletions api/policies/v1/clusteradmissionpolicygroup_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import (
)

func TestClusterClusterAdmissionPolicyDefault(t *testing.T) {
policy := clusterAdmissionPolicyGroupFactory()
policy := ClusterAdmissionPolicyGroup{}
policy.Default()

require.Equal(t, constants.DefaultPolicyServer, policy.GetPolicyServer())
require.Contains(t, policy.GetFinalizers(), constants.KubewardenFinalizer)
}

func TestClusterClusterAdmissionPolicyValidateCreate(t *testing.T) {
policy := clusterAdmissionPolicyGroupFactory()
policy := NewClusterAdmissionPolicyGroupFactory().Build()
warnings, err := policy.ValidateCreate()
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClusterClusterAdmissionPolicyValidateCreateWithNoMembers(t *testing.T) {
policy := clusterAdmissionPolicyGroupFactory()
policy := NewClusterAdmissionPolicyGroupFactory().Build()
policy.Spec.Policies = nil
warnings, err := policy.ValidateCreate()
require.Error(t, err)
Expand All @@ -47,16 +47,16 @@ func TestClusterClusterAdmissionPolicyValidateCreateWithNoMembers(t *testing.T)
}

func TestClusterClusterAdmissionPolicyValidateUpdate(t *testing.T) {
oldPolicy := clusterAdmissionPolicyGroupFactory()
newPolicy := clusterAdmissionPolicyGroupFactory()
oldPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
newPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.NoError(t, err)
require.Empty(t, warnings)
}

func TestClusterClusterAdmissionPolicyValidateUpdateWithInvalidOldPolicy(t *testing.T) {
oldPolicy := admissionPolicyGroupFactory()
newPolicy := clusterAdmissionPolicyGroupFactory()
oldPolicy := NewAdmissionPolicyGroupFactory().Build()
newPolicy := NewClusterAdmissionPolicyGroupFactory().Build()
warnings, err := newPolicy.ValidateUpdate(oldPolicy)
require.Empty(t, warnings)
require.ErrorContains(t, err, "object is not of type ClusterAdmissionPolicyGroup")
Expand Down
Loading

0 comments on commit 6306fe5

Please sign in to comment.