From be736c4090bc10b267aa408209e41f46941f07ef Mon Sep 17 00:00:00 2001 From: nicolaferraro Date: Thu, 29 Jul 2021 13:46:46 +0200 Subject: [PATCH] Fix #2530: fix test to check actual resources on the namespace --- pkg/trait/pull_secret_test.go | 94 +++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 20 deletions(-) diff --git a/pkg/trait/pull_secret_test.go b/pkg/trait/pull_secret_test.go index a1499e0145..44ee3efa43 100644 --- a/pkg/trait/pull_secret_test.go +++ b/pkg/trait/pull_secret_test.go @@ -18,32 +18,23 @@ limitations under the License. package trait import ( + "context" "testing" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/util/kubernetes" + "github.com/apache/camel-k/pkg/util/test" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/stretchr/testify/assert" ) func TestPullSecret(t *testing.T) { - e := &Environment{} - e.Integration = &v1.Integration{ - Status: v1.IntegrationStatus{ - Phase: v1.IntegrationPhaseDeploying, - }, - } - - deployment := appsv1.Deployment{ - Spec: appsv1.DeploymentSpec{ - Template: corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{}, - }, - }, - } - e.Resources = kubernetes.NewCollection(&deployment) + e, deployment := getEnvironmentAndDeployment(t) trait := newPullSecretTrait().(*pullSecretTrait) trait.SecretName = "xxxy" @@ -57,15 +48,66 @@ func TestPullSecret(t *testing.T) { } func TestPullSecretDoesNothingWhenNotSetOnPlatform(t *testing.T) { + e, _ := getEnvironmentAndDeployment(t) + e.Platform = &v1.IntegrationPlatform{} + + trait := newPullSecretTrait() + enabled, err := trait.Configure(e) + assert.Nil(t, err) + assert.False(t, enabled) +} + +func TestPullSecretAuto(t *testing.T) { + e, _ := getEnvironmentAndDeployment(t) + + trait := newPullSecretTrait().(*pullSecretTrait) + trait.Auto = newFalse() + enabled, err := trait.Configure(e) + assert.Nil(t, err) + assert.False(t, enabled) +} + +func TestPullSecretImagePullerDelegation(t *testing.T) { + e, _ := getEnvironmentAndDeployment(t) + + trait := newPullSecretTrait().(*pullSecretTrait) + trait.Auto = newFalse() + trait.ImagePullerDelegation = newTrue() + enabled, err := trait.Configure(e) + assert.Nil(t, err) + assert.True(t, enabled) + assert.True(t, *trait.ImagePullerDelegation) + + err = trait.Apply(e) + assert.NoError(t, err) + + var roleBinding rbacv1.RoleBinding + roleBindingKey := client.ObjectKey{ + Namespace: "test", + Name: "camel-k-puller-test-default", + } + err = e.Client.Get(e.C, roleBindingKey, &roleBinding) + assert.NoError(t, err) + assert.Len(t, roleBinding.Subjects, 1) +} + +func getEnvironmentAndDeployment(t *testing.T) (*Environment, *appsv1.Deployment) { e := &Environment{} e.Integration = &v1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test", + Name: "myit", + }, Status: v1.IntegrationStatus{ Phase: v1.IntegrationPhaseDeploying, }, } - e.Platform = &v1.IntegrationPlatform{} deployment := appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test", + Name: "myit", + }, Spec: appsv1.DeploymentSpec{ Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{}, @@ -74,8 +116,20 @@ func TestPullSecretDoesNothingWhenNotSetOnPlatform(t *testing.T) { } e.Resources = kubernetes.NewCollection(&deployment) - trait := newPullSecretTrait() - enabled, err := trait.Configure(e) - assert.Nil(t, err) - assert.False(t, enabled) + var err error + e.C = context.TODO() + e.Client, err = test.NewFakeClient(e.Integration, &deployment) + assert.NoError(t, err) + + return e, &deployment +} + +func newFalse() *bool { + b := false + return &b +} + +func newTrue() *bool { + b := true + return &b }