github.com/argoproj/argo-cd@v1.8.7/test/e2e/jsonnet_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/argoproj/gitops-engine/pkg/sync/common"
     7  	"github.com/argoproj/gitops-engine/pkg/utils/kube"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	. "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    11  	. "github.com/argoproj/argo-cd/test/e2e/fixture"
    12  	. "github.com/argoproj/argo-cd/test/e2e/fixture/app"
    13  	. "github.com/argoproj/argo-cd/util/errors"
    14  )
    15  
    16  func TestJsonnetAppliedCorrectly(t *testing.T) {
    17  	Given(t).
    18  		Path("jsonnet-tla").
    19  		When().
    20  		Create().
    21  		Sync().
    22  		Then().
    23  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    24  		And(func(app *Application) {
    25  			manifests, err := RunCli("app", "manifests", app.Name, "--source", "live")
    26  			assert.NoError(t, err)
    27  			resources, err := kube.SplitYAML([]byte(manifests))
    28  			assert.NoError(t, err)
    29  
    30  			index := -1
    31  			for i := range resources {
    32  				if resources[i].GetKind() == kube.DeploymentKind {
    33  					index = i
    34  					break
    35  				}
    36  			}
    37  
    38  			assert.True(t, index > -1)
    39  
    40  			deployment := resources[index]
    41  			assert.Equal(t, "jsonnet-guestbook-ui", deployment.GetName())
    42  			assert.Equal(t, int64(1), *kube.GetDeploymentReplicas(deployment))
    43  		})
    44  }
    45  
    46  func TestJsonnetTlaParameterAppliedCorrectly(t *testing.T) {
    47  	Given(t).
    48  		Path("jsonnet-tla").
    49  		When().
    50  		Create("--jsonnet-tla-str", "name=testing-tla", "--jsonnet-tla-code", "replicas=0").
    51  		Sync().
    52  		Then().
    53  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    54  		And(func(app *Application) {
    55  			manifests, err := RunCli("app", "manifests", app.Name, "--source", "live")
    56  			assert.NoError(t, err)
    57  			resources, err := kube.SplitYAML([]byte(manifests))
    58  			assert.NoError(t, err)
    59  
    60  			index := -1
    61  			for i := range resources {
    62  				if resources[i].GetKind() == kube.DeploymentKind {
    63  					index = i
    64  					break
    65  				}
    66  			}
    67  
    68  			assert.True(t, index > -1)
    69  
    70  			deployment := resources[index]
    71  			assert.Equal(t, "testing-tla", deployment.GetName())
    72  			assert.Equal(t, int64(0), *kube.GetDeploymentReplicas(deployment))
    73  		})
    74  }
    75  
    76  func TestJsonnetTlaEnv(t *testing.T) {
    77  	Given(t).
    78  		Path("jsonnet-tla-cm").
    79  		When().
    80  		Create("--jsonnet-tla-str", "foo=$ARGOCD_APP_NAME", "--jsonnet-tla-code", "bar='$ARGOCD_APP_NAME'").
    81  		Sync().
    82  		Then().
    83  		Expect(OperationPhaseIs(OperationSucceeded)).
    84  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    85  		And(func(app *Application) {
    86  			assert.Equal(t, Name(), FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.foo}")).(string))
    87  			assert.Equal(t, Name(), FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.bar}")).(string))
    88  		})
    89  }
    90  func TestJsonnetExtVarEnv(t *testing.T) {
    91  	Given(t).
    92  		Path("jsonnet-ext-var").
    93  		When().
    94  		Create("--jsonnet-ext-var-str", "foo=$ARGOCD_APP_NAME", "--jsonnet-ext-var-code", "bar='$ARGOCD_APP_NAME'").
    95  		Sync().
    96  		Then().
    97  		Expect(OperationPhaseIs(OperationSucceeded)).
    98  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
    99  		And(func(app *Application) {
   100  			assert.Equal(t, Name(), FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.foo}")).(string))
   101  			assert.Equal(t, Name(), FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.bar}")).(string))
   102  		})
   103  }
   104  
   105  //Jsonnet file located in nested sub directory uses import
   106  func TestJsonnetNestedDirWithImports(t *testing.T) {
   107  	Given(t).
   108  		Path("jsonnet-nested-dir-with-imports/apps").
   109  		When().
   110  		Create("--directory-recurse").
   111  		Sync().
   112  		Then().
   113  		Expect(OperationPhaseIs(OperationSucceeded)).
   114  		Expect(SyncStatusIs(SyncStatusCodeSynced)).
   115  		Expect(ResourceSyncStatusIs("Namespace", "hello-world", SyncStatusCodeSynced)).
   116  		Expect(ResourceSyncStatusIs("Namespace", "hello-root", SyncStatusCodeSynced))
   117  }