diff --git a/internal/kube/resources/deployment_test.go b/internal/kube/resources/deployment_test.go index 18625b897..3dd28b315 100644 --- a/internal/kube/resources/deployment_test.go +++ b/internal/kube/resources/deployment_test.go @@ -14,8 +14,49 @@ import ( k8s_fake "k8s.io/client-go/kubernetes/fake" ) -var ( - testDeployment = appsv1.Deployment{ +func Test_CreateDeployment(t *testing.T) { + t.Parallel() + t.Run("create deployment", func(t *testing.T) { + kubeClient := &kube_fake.KubeClient{ + TestKubernetesInterface: k8s_fake.NewSimpleClientset(), + } + trueValue := true + err := CreateDeployment(context.Background(), kubeClient, CreateDeploymentOpts{ + Name: "test-name", + Namespace: "default", + Image: "test:image", + ImagePullSecret: "test-image-pull-secret", + InjectIstio: types.NullableBool{Value: &trueValue}, + SecretMounts: []string{"test-name"}, + ConfigmapMounts: []string{"test-name"}, + }) + require.NoError(t, err) + + deploy, err := kubeClient.Static().AppsV1().Deployments("default").Get(context.Background(), "test-name", metav1.GetOptions{}) + require.NoError(t, err) + require.Equal(t, fixDeployment(), deploy) + }) + + t.Run("already exists error", func(t *testing.T) { + kubeClient := &kube_fake.KubeClient{ + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixDeployment()), + } + trueValue := true + err := CreateDeployment(context.Background(), kubeClient, CreateDeploymentOpts{ + Name: "test-name", + Namespace: "default", + Image: "test:image", + ImagePullSecret: "test-image-pull-secret", + InjectIstio: types.NullableBool{Value: &trueValue}, + SecretMounts: []string{"test-name"}, + ConfigmapMounts: []string{"test-name"}, + }) + require.ErrorContains(t, err, `deployments.apps "test-name" already exists`) + }) +} + +func fixDeployment() *appsv1.Deployment { + return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "test-name", Namespace: "default", @@ -105,45 +146,4 @@ var ( }, }, } -) - -func Test_CreateDeployment(t *testing.T) { - t.Parallel() - t.Run("create deployment", func(t *testing.T) { - kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(), - } - trueValue := true - err := CreateDeployment(context.Background(), kubeClient, CreateDeploymentOpts{ - Name: "test-name", - Namespace: "default", - Image: "test:image", - ImagePullSecret: "test-image-pull-secret", - InjectIstio: types.NullableBool{Value: &trueValue}, - SecretMounts: []string{"test-name"}, - ConfigmapMounts: []string{"test-name"}, - }) - require.NoError(t, err) - - deploy, err := kubeClient.Static().AppsV1().Deployments(testDeployment.GetNamespace()).Get(context.Background(), testDeployment.GetName(), metav1.GetOptions{}) - require.NoError(t, err) - require.Equal(t, &testDeployment, deploy) - }) - - t.Run("already exists error", func(t *testing.T) { - kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(&testDeployment), - } - trueValue := true - err := CreateDeployment(context.Background(), kubeClient, CreateDeploymentOpts{ - Name: "test-name", - Namespace: "default", - Image: "test:image", - ImagePullSecret: "test-image-pull-secret", - InjectIstio: types.NullableBool{Value: &trueValue}, - SecretMounts: []string{"test-name"}, - ConfigmapMounts: []string{"test-name"}, - }) - require.ErrorContains(t, err, `deployments.apps "test-name" already exists`) - }) } diff --git a/internal/kube/resources/rbac_test.go b/internal/kube/resources/rbac_test.go index b22dfff1a..6f88c7081 100644 --- a/internal/kube/resources/rbac_test.go +++ b/internal/kube/resources/rbac_test.go @@ -12,76 +12,33 @@ import ( k8s_fake "k8s.io/client-go/kubernetes/fake" ) -var ( - testServiceAccount = corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-name", - Namespace: "default", - }, - } - - testClusterRole = rbacv1.ClusterRole{ - ObjectMeta: metav1.ObjectMeta{ - Name: "clusterRole", - }, - } - - testClusterRoleBinding = rbacv1.ClusterRoleBinding{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-name-binding", - }, - Subjects: []rbacv1.Subject{ - { - Kind: "ServiceAccount", - Name: "test-name", - Namespace: "default", - }}, - - RoleRef: rbacv1.RoleRef{ - Kind: "ClusterRole", - Name: "clusterRole", - }, - } - - testTokenSecret = corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-name", - Namespace: "default", - Annotations: map[string]string{ - "kubernetes.io/service-account.name": "test-name", - }, - }, - Type: corev1.SecretTypeServiceAccountToken, - } -) - func Test_CreateClusterRoleBinding(t *testing.T) { t.Parallel() t.Run("create ClusterRoleBinding", func(t *testing.T) { kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(&testClusterRole), + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixClusterRole()), } - err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", testClusterRole.GetName()) + err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", "clusterRole") require.NoError(t, err) - binding, err := kubeClient.Static().RbacV1().ClusterRoleBindings().Get(context.Background(), testClusterRoleBinding.GetName(), metav1.GetOptions{}) + binding, err := kubeClient.Static().RbacV1().ClusterRoleBindings().Get(context.Background(), "test-name-binding", metav1.GetOptions{}) require.NoError(t, err) - require.Equal(t, &testClusterRoleBinding, binding) + require.Equal(t, fixClusterRoleBinding(), binding) }) t.Run("ClusterRole not found error", func(t *testing.T) { kubeClient := &kube_fake.KubeClient{ TestKubernetesInterface: k8s_fake.NewSimpleClientset(), } - err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", testClusterRole.GetName()) + err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", "clusterRole") require.ErrorContains(t, err, `clusterroles.rbac.authorization.k8s.io "clusterRole" not found`) }) t.Run("ignore already exists error", func(t *testing.T) { kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(&testClusterRole, &testClusterRoleBinding), + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixClusterRole(), fixClusterRoleBinding()), } - err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", testClusterRole.GetName()) + err := CreateClusterRoleBinding(context.Background(), kubeClient, "test-name", "default", "clusterRole") require.NoError(t, err) }) } @@ -92,19 +49,19 @@ func Test_CreateServiceAccountToken(t *testing.T) { kubeClient := &kube_fake.KubeClient{ TestKubernetesInterface: k8s_fake.NewSimpleClientset(), } - err := CreateServiceAccountToken(context.Background(), kubeClient, testTokenSecret.GetName(), testTokenSecret.GetNamespace()) + err := CreateServiceAccountToken(context.Background(), kubeClient, "test-name", "default") require.NoError(t, err) - secret, err := kubeClient.Static().CoreV1().Secrets(testTokenSecret.GetNamespace()).Get(context.Background(), testTokenSecret.GetName(), metav1.GetOptions{}) + secret, err := kubeClient.Static().CoreV1().Secrets("default").Get(context.Background(), "test-name", metav1.GetOptions{}) require.NoError(t, err) - require.Equal(t, &testTokenSecret, secret) + require.Equal(t, fixTokenSecret(), secret) }) t.Run("ignore already exists error", func(t *testing.T) { kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(&testTokenSecret), + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixTokenSecret()), } - err := CreateServiceAccountToken(context.Background(), kubeClient, testTokenSecret.GetName(), testTokenSecret.GetNamespace()) + err := CreateServiceAccountToken(context.Background(), kubeClient, "test-name", "default") require.NoError(t, err) }) } @@ -114,19 +71,68 @@ func Test_CreateServiceAccount(t *testing.T) { kubeClient := &kube_fake.KubeClient{ TestKubernetesInterface: k8s_fake.NewSimpleClientset(), } - err := CreateServiceAccount(context.Background(), kubeClient, testServiceAccount.GetName(), testServiceAccount.GetNamespace()) + err := CreateServiceAccount(context.Background(), kubeClient, "test-name", "default") require.NoError(t, err) - serviceAccount, err := kubeClient.Static().CoreV1().ServiceAccounts(testServiceAccount.GetNamespace()).Get(context.Background(), testServiceAccount.GetName(), metav1.GetOptions{}) + serviceAccount, err := kubeClient.Static().CoreV1().ServiceAccounts("default").Get(context.Background(), "test-name", metav1.GetOptions{}) require.NoError(t, err) - require.Equal(t, &testServiceAccount, serviceAccount) + require.Equal(t, fixServiceAccount(), serviceAccount) }) t.Run("ignore already exists error", func(t *testing.T) { kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(&testServiceAccount), + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixServiceAccount()), } - err := CreateServiceAccount(context.Background(), kubeClient, testServiceAccount.GetName(), testServiceAccount.GetNamespace()) + err := CreateServiceAccount(context.Background(), kubeClient, "test-name", "default") require.NoError(t, err) }) } + +func fixTokenSecret() *corev1.Secret { + return &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-name", + Namespace: "default", + Annotations: map[string]string{ + "kubernetes.io/service-account.name": "test-name", + }, + }, + Type: corev1.SecretTypeServiceAccountToken, + } +} + +func fixClusterRoleBinding() *rbacv1.ClusterRoleBinding { + return &rbacv1.ClusterRoleBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-name-binding", + }, + Subjects: []rbacv1.Subject{ + { + Kind: "ServiceAccount", + Name: "test-name", + Namespace: "default", + }}, + + RoleRef: rbacv1.RoleRef{ + Kind: "ClusterRole", + Name: "clusterRole", + }, + } +} + +func fixServiceAccount() *corev1.ServiceAccount { + return &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-name", + Namespace: "default", + }, + } +} + +func fixClusterRole() *rbacv1.ClusterRole { + return &rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{ + Name: "clusterRole", + }, + } +} diff --git a/internal/kube/resources/service_test.go b/internal/kube/resources/service_test.go index dc72b1b34..d49b9c476 100644 --- a/internal/kube/resources/service_test.go +++ b/internal/kube/resources/service_test.go @@ -25,46 +25,17 @@ func Test_CreateService(t *testing.T) { err := CreateService(context.Background(), kubeClient, "test-svc", "default", 8080) require.NoError(t, err) - expectedSvc := &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-svc", - Namespace: "default", - Labels: map[string]string{ - "app.kubernetes.io/name": "test-svc", - "app.kubernetes.io/created-by": "kyma-cli", - }, - }, - Spec: corev1.ServiceSpec{ - Selector: map[string]string{ - "app": "test-svc", - }, - Ports: []corev1.ServicePort{ - { - Port: 8080, - TargetPort: intstr.FromInt32(8080), - }, - }, - }, - } - svc, err := kubeClient.Static().CoreV1().Services("default").Get(context.Background(), "test-svc", metav1.GetOptions{}) require.NoError(t, err) - require.Equal(t, expectedSvc, svc) + require.Equal(t, fixService(), svc) }) t.Run("service already exists error", func(t *testing.T) { - existingService := &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "existing", - Namespace: "default", - }, - } - kubeClient := &kube_fake.KubeClient{ - TestKubernetesInterface: k8s_fake.NewSimpleClientset(existingService), + TestKubernetesInterface: k8s_fake.NewSimpleClientset(fixService()), } - err := CreateService(context.Background(), kubeClient, "existing", "default", 8080) + err := CreateService(context.Background(), kubeClient, "test-svc", "default", 8080) require.ErrorContains(t, err, `services "existing" already exists`) }) } @@ -134,3 +105,27 @@ func fixAPIRule(apiRuleName, namespace, host string, port uint32) unstructured.U }, } } + +func fixService() *corev1.Service { + return &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-svc", + Namespace: "default", + Labels: map[string]string{ + "app.kubernetes.io/name": "test-svc", + "app.kubernetes.io/created-by": "kyma-cli", + }, + }, + Spec: corev1.ServiceSpec{ + Selector: map[string]string{ + "app": "test-svc", + }, + Ports: []corev1.ServicePort{ + { + Port: 8080, + TargetPort: intstr.FromInt32(8080), + }, + }, + }, + } +}