This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch camel-main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 152ac2161cf0d7fe8fd8fc0d14c7b85042ad643a
Author: Antonin Stefanutti <anto...@stefanutti.fr>
AuthorDate: Tue Jun 8 10:19:45 2021 +0200

    chore(trait): Polish Pod trait unit tests
---
 pkg/trait/pod_test.go | 97 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 63 insertions(+), 34 deletions(-)

diff --git a/pkg/trait/pod_test.go b/pkg/trait/pod_test.go
index 58bb35d..48d20d0 100755
--- a/pkg/trait/pod_test.go
+++ b/pkg/trait/pod_test.go
@@ -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) {
@@ -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"))
@@ -58,33 +63,21 @@ 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_SE [...]
-       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{
@@ -92,7 +85,44 @@ func createPodTest(templateString string) (*podTrait, 
*Environment, *appsv1.Depl
                        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",
+                                                               },
+                                                       },
+                                               },
+                                       },
+                               },
+                       },
                },
        }
 
@@ -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!"
@@ -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)

Reply via email to