Skip to content

Commit

Permalink
chore: update testing helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed May 31, 2024
1 parent c1e8d1d commit a88d2a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,36 @@ func TestEnsureClusterRole(t *testing.T) {
wrongClusterRole2 := clusterRole.DeepCopy()
wrongClusterRole2.ObjectMeta.Labels["aaa"] = "bbb"

clusterRole.ObjectMeta.Labels[consts.GatewayOperatorManagedByNameLabel] = "test-controlplane"
controlplane := operatorv1beta1.ControlPlane{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway-operator.konghq.com/v1beta1",
Kind: "ControlPlane",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-controlplane",
Namespace: "test-namespace",
UID: types.UID(uuid.NewString()),
},
Spec: operatorv1beta1.ControlPlaneSpec{
ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{
Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: consts.ControlPlaneControllerContainerName,
Image: consts.DefaultControlPlaneImage,
},
},
},
},
},
},
},
}

k8sutils.SetOwnerForObjectThroughLabels(clusterRole, &controlplane)

testCases := []struct {
Name string
controlplane operatorv1beta1.ControlPlane
Expand All @@ -54,98 +83,20 @@ func TestEnsureClusterRole(t *testing.T) {
err error
}{
{
Name: "no existing clusterrole",
controlplane: operatorv1beta1.ControlPlane{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway-operator.konghq.com/v1beta1",
Kind: "ControlPlane",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-controlplane",
Namespace: "test-namespace",
UID: types.UID(uuid.NewString()),
},
Spec: operatorv1beta1.ControlPlaneSpec{
ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{
Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: consts.ControlPlaneControllerContainerName,
Image: consts.DefaultControlPlaneImage,
},
},
},
},
},
},
},
},
Name: "no existing clusterrole",
controlplane: controlplane,
createdorUpdated: true,
expectedClusterRole: *clusterRole,
},
{
Name: "up to date clusterrole",
controlplane: operatorv1beta1.ControlPlane{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway-operator.konghq.com/v1beta1",
Kind: "ControlPlane",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-controlplane",
Namespace: "test-namespace",
UID: types.UID(uuid.NewString()),
},
Spec: operatorv1beta1.ControlPlaneSpec{
ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{
Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: consts.ControlPlaneControllerContainerName,
Image: consts.DefaultControlPlaneImage,
},
},
},
},
},
},
},
},
Name: "up to date clusterrole",
controlplane: controlplane,
existingClusterRole: clusterRole,
expectedClusterRole: *clusterRole,
},
{
Name: "out of date clusterrole, object meta",
controlplane: operatorv1beta1.ControlPlane{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway-operator.konghq.com/v1beta1",
Kind: "ControlPlane",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-controlplane",
Namespace: "test-namespace",
UID: types.UID(uuid.NewString()),
},
Spec: operatorv1beta1.ControlPlaneSpec{
ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{
Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: consts.ControlPlaneControllerContainerName,
Image: consts.DefaultControlPlaneImage,
},
},
},
},
},
},
},
},
Name: "out of date clusterrole, object meta",
controlplane: controlplane,
existingClusterRole: wrongClusterRole2,
createdorUpdated: true,
expectedClusterRole: *clusterRole,
Expand Down
10 changes: 2 additions & 8 deletions pkg/utils/test/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func MustListControlPlaneClusterRoles(t *testing.T, ctx context.Context, control
clusterRoles, err := k8sutils.ListClusterRoles(
ctx,
clients.MgrClient,
client.MatchingLabels{
consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue,
consts.GatewayOperatorManagedByNameLabel: controlplane.Name,
},
client.MatchingLabels(k8sutils.GetManagedByLabelSet(controlplane)),
)
require.NoError(t, err)
return clusterRoles
Expand All @@ -59,10 +56,7 @@ func MustListControlPlaneClusterRoleBindings(t *testing.T, ctx context.Context,
clusterRoleBindings, err := k8sutils.ListClusterRoleBindings(
ctx,
clients.MgrClient,
client.MatchingLabels{
consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue,
consts.GatewayOperatorManagedByNameLabel: controlplane.Name,
},
client.MatchingLabels(k8sutils.GetManagedByLabelSet(controlplane)),
)
require.NoError(t, err)
return clusterRoleBindings
Expand Down

0 comments on commit a88d2a7

Please sign in to comment.