Skip to content

Commit

Permalink
chore(trait): Polish Pod trait unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jun 8, 2021
1 parent b21d9ed commit 4e7e6ac
Showing 1 changed file with 63 additions and 34 deletions.
97 changes: 63 additions & 34 deletions pkg/trait/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package trait

import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util/kubernetes"
"testing"

"github.com/stretchr/testify/assert"

"gopkg.in/yaml.v2"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/kubernetes"
)

func TestConfigurePodTraitDoesSucceed(t *testing.T) {
Expand Down Expand Up @@ -38,10 +43,10 @@ func TestSimpleChange(t *testing.T) {
func TestMergeArrays(t *testing.T) {
templateString :=
"{containers: [{name: second-container, " +
"env: [{name: SOME_VARIABLE, value: SOME_VALUE}, {name: SOME_VARIABLE2, value: SOME_VALUE2}]}, " +
"{name: integration, env: [{name: TEST_ADDED_CUSTOM_VARIABLE, value: value}]}" +
"]" +
"}"
"env: [{name: SOME_VARIABLE, value: SOME_VALUE}, {name: SOME_VARIABLE2, value: SOME_VALUE2}]}, " +
"{name: integration, env: [{name: TEST_ADDED_CUSTOM_VARIABLE, value: value}]}" +
"]" +
"}"
templateSpec := testPodTemplateSpec(t, templateString)

assert.NotNil(t, getContainer(templateSpec.Spec.Containers, "second-container"))
Expand All @@ -58,41 +63,66 @@ func TestChangeEnvVariables(t *testing.T) {
"]}"
templateSpec := testPodTemplateSpec(t, templateString)

//check if env var was added in second container
// Check if env var was added in second container
assert.Equal(t, containsEnvVariables(templateSpec, "second", "TEST_VARIABLE"), "TEST_VALUE")
assert.Equal(t, 3, len(getContainer(templateSpec.Spec.Containers, "second").Env))

//check if env var was changed
// Check if env var was changed
assert.Equal(t, containsEnvVariables(templateSpec, "integration", "CAMEL_K_DIGEST"), "new_value")
}

func createPodTest(templateString string) (*podTrait, *Environment, *appsv1.Deployment) {
func createPodTest(podSpecTemplate string) (*podTrait, *Environment, *appsv1.Deployment) {
trait := newPodTrait().(*podTrait)
enabled := true
trait.Enabled = &enabled

specTemplateYamlString := "{metadata: {name: example-template, creationTimestamp: null, " +
"labels: {camel.apache.org/integration: test}}, " +
"spec: {volumes: [" +
"{name: i-source-000," + "configMap: {name: test-source-000, items: [{key: content, path: test.groovy}], defaultMode: 420}}, " +
"{name: application-properties, configMap: {name: test-application-properties, items: [{key: application.properties, path: application.properties}], defaultMode: 420}}], " +
"containers: [" +
"{name: second, env: [{name: SOME_VARIABLE, value: SOME_VALUE}, {name: SOME_VARIABLE2, value: SOME_VALUE2}]}," +
"{name: integration, command: [/bin/sh, '-c'], env: [{name: CAMEL_K_DIGEST, value: vO3wwJHC7-uGEiFFVac0jq6rZT5EZNw56Ae5gKKFZZsk}, {name: CAMEL_K_CONF, value: /etc/camel/conf/application.properties}, {name: CAMEL_K_CONF_D, value: /etc/camel/conf.d},{name: CAMEL_K_VERSION, value: 1.3.0-SNAPSHOT}, {name: CAMEL_K_INTEGRATION, value: test}, {name: CAMEL_K_RUNTIME_VERSION, value: 1.5.0}, {name: CAMEL_K_MOUNT_PATH_CONFIGMAPS, value: /etc/camel/conf.d/_configmaps}, {name: CAMEL_K_MOUNT_PATH_SECRETS, value: /etc/camel/conf.d/_secrets}, {name: NAMESPACE, valueFrom: {fieldRef: {apiVersion: v1, fieldPath: metadata.namespace}}}, {name: POD_NAME, valueFrom: {fieldRef: {apiVersion: v1, fieldPath: metadata.name}}}], imagePullPolicy: IfNotPresent, volumeMounts: [{name: i-source-000, mountPath: /etc/camel/sources/i-source-000}, {name: application-properties, mountPath: /etc/camel/conf}], terminationMessagePolicy: File, image: 'image-registry.openshift-image-registry.svc:5000/podtrait/camel-k-kit-bvd7utv170hult6ju26g@sha256:1c091437ef986f2852733da5f3fce7a5f48a5ea51e409f0bdcb0c13ff620e6b2', workingDir: /deployments, args: ['echo exec java -cp ./resources:/etc/camel/conf:/etc/camel/resources:/etc/camel/sources/i-source-000 io.quarkus.runner.GeneratedMain']}], restartPolicy: Always, terminationGracePeriodSeconds: 90, dnsPolicy: ClusterFirst, securityContext: {}, schedulerName: default-scheduler}}"
var template corev1.PodTemplateSpec
_ = yaml.Unmarshal([]byte(specTemplateYamlString), &template)

var podTemplateIt v1.PodSpec
if templateString != "" {
_ = yaml.Unmarshal([]byte(templateString), &podTemplateIt)
trait.Enabled = util.BoolP(true)

var podSpec v1.PodSpec
if podSpecTemplate != "" {
_ = yaml.Unmarshal([]byte(podSpecTemplate), &podSpec)
}

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-template-test-integration",
},
Spec: appsv1.DeploymentSpec{
Template: template,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "example-template",
Labels: map[string]string{
v1.IntegrationLabel: "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "integration",
Env: []corev1.EnvVar{
{
Name: "CAMEL_K_DIGEST",
Value: "vO3wwJHC7-uGEiFFVac0jq6rZT5EZNw56Ae5gKKFZZsk",
},
{
Name: "CAMEL_K_CONF",
Value: "/etc/camel/conf/application.properties",
},
},
},
{
Name: "second",
Env: []corev1.EnvVar{
{
Name: "SOME_VARIABLE",
Value: "SOME_VALUE",
},
{
Name: "SOME_VARIABLE2",
Value: "SOME_VALUE2",
},
},
},
},
},
},
},
}

Expand All @@ -106,23 +136,23 @@ func createPodTest(templateString string) (*podTrait, *Environment, *appsv1.Depl
},
Spec: v1.IntegrationSpec{
PodTemplate: &v1.PodSpecTemplate{
Spec: podTemplateIt,
Spec: podSpec,
},
},
},

Resources: kubernetes.NewCollection(deployment),
}

return trait, environment, deployment
}

func containsEnvVariables(template corev1.PodTemplateSpec, containerName string, name string) string {
container := getContainer(template.Spec.Containers, containerName)

for i := range container.Env {
envv := container.Env[i]
if envv.Name == name {
return envv.Value
env := container.Env[i]
if env.Name == name {
return env.Value
}
}
return "not found!"
Expand All @@ -139,7 +169,6 @@ func getContainer(containers []corev1.Container, name string) *corev1.Container

func testPodTemplateSpec(t *testing.T, template string) corev1.PodTemplateSpec {
trait, environment, _ := createPodTest(template)
//trait.Template = template

_, err := trait.Configure(environment)
assert.Nil(t, err)
Expand Down

0 comments on commit 4e7e6ac

Please sign in to comment.