Skip to content

Commit

Permalink
Fix #2530: fix test to check actual resources on the namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and astefanutti committed Aug 4, 2021
1 parent 07b52bd commit be736c4
Showing 1 changed file with 74 additions and 20 deletions.
94 changes: 74 additions & 20 deletions pkg/trait/pull_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{},
Expand All @@ -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
}

0 comments on commit be736c4

Please sign in to comment.