github.com/argoproj/argo-cd@v1.8.7/test/e2e/custom_tool_test.go (about) 1 package e2e 2 3 import ( 4 "sort" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/argoproj/gitops-engine/pkg/health" 10 . "github.com/argoproj/gitops-engine/pkg/sync/common" 11 "github.com/stretchr/testify/assert" 12 13 . "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 14 . "github.com/argoproj/argo-cd/test/e2e/fixture" 15 . "github.com/argoproj/argo-cd/test/e2e/fixture/app" 16 . "github.com/argoproj/argo-cd/util/errors" 17 ) 18 19 // make sure we can echo back the Git creds 20 func TestCustomToolWithGitCreds(t *testing.T) { 21 Given(t). 22 // path does not matter, we ignore it 23 ConfigManagementPlugin( 24 ConfigManagementPlugin{ 25 Name: Name(), 26 Generate: Command{ 27 Command: []string{"sh", "-c"}, 28 Args: []string{`echo "{\"kind\": \"ConfigMap\", \"apiVersion\": \"v1\", \"metadata\": { \"name\": \"$ARGOCD_APP_NAME\", \"namespace\": \"$ARGOCD_APP_NAMESPACE\", \"annotations\": {\"GitAskpass\": \"$GIT_ASKPASS\", \"GitUsername\": \"$GIT_USERNAME\", \"GitPassword\": \"$GIT_PASSWORD\"}}}"`}, 29 }, 30 }, 31 ). 32 CustomCACertAdded(). 33 // add the private repo with credentials 34 HTTPSRepoURLAdded(true). 35 RepoURLType(RepoURLTypeHTTPS). 36 Path("https-kustomize-base"). 37 When(). 38 Create(). 39 Sync(). 40 Then(). 41 Expect(OperationPhaseIs(OperationSucceeded)). 42 Expect(SyncStatusIs(SyncStatusCodeSynced)). 43 Expect(HealthIs(health.HealthStatusHealthy)). 44 And(func(app *Application) { 45 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitAskpass}") 46 assert.NoError(t, err) 47 assert.Equal(t, "git-ask-pass.sh", output) 48 }). 49 And(func(app *Application) { 50 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitUsername}") 51 assert.NoError(t, err) 52 assert.Equal(t, GitUsername, output) 53 }). 54 And(func(app *Application) { 55 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitPassword}") 56 assert.NoError(t, err) 57 assert.Equal(t, GitPassword, output) 58 }) 59 } 60 61 // make sure we can echo back the Git creds 62 func TestCustomToolWithGitCredsTemplate(t *testing.T) { 63 Given(t). 64 // path does not matter, we ignore it 65 ConfigManagementPlugin( 66 ConfigManagementPlugin{ 67 Name: Name(), 68 Generate: Command{ 69 Command: []string{"sh", "-c"}, 70 Args: []string{`echo "{\"kind\": \"ConfigMap\", \"apiVersion\": \"v1\", \"metadata\": { \"name\": \"$ARGOCD_APP_NAME\", \"namespace\": \"$ARGOCD_APP_NAMESPACE\", \"annotations\": {\"GitAskpass\": \"$GIT_ASKPASS\", \"GitUsername\": \"$GIT_USERNAME\", \"GitPassword\": \"$GIT_PASSWORD\"}}}"`}, 71 }, 72 }, 73 ). 74 CustomCACertAdded(). 75 // add the git creds template 76 HTTPSCredentialsUserPassAdded(). 77 // add the private repo without credentials 78 HTTPSRepoURLAdded(false). 79 RepoURLType(RepoURLTypeHTTPS). 80 Path("https-kustomize-base"). 81 When(). 82 Create(). 83 Sync(). 84 Then(). 85 Expect(OperationPhaseIs(OperationSucceeded)). 86 Expect(SyncStatusIs(SyncStatusCodeSynced)). 87 Expect(HealthIs(health.HealthStatusHealthy)). 88 And(func(app *Application) { 89 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitAskpass}") 90 assert.NoError(t, err) 91 assert.Equal(t, "git-ask-pass.sh", output) 92 }). 93 And(func(app *Application) { 94 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitUsername}") 95 assert.NoError(t, err) 96 assert.Equal(t, GitUsername, output) 97 }). 98 And(func(app *Application) { 99 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.GitPassword}") 100 assert.NoError(t, err) 101 assert.Equal(t, GitPassword, output) 102 }) 103 } 104 105 // make sure we can echo back the env 106 func TestCustomToolWithEnv(t *testing.T) { 107 Given(t). 108 // path does not matter, we ignore it 109 ConfigManagementPlugin( 110 ConfigManagementPlugin{ 111 Name: Name(), 112 Generate: Command{ 113 Command: []string{"sh", "-c"}, 114 Args: []string{`echo "{\"kind\": \"ConfigMap\", \"apiVersion\": \"v1\", \"metadata\": { \"name\": \"$ARGOCD_APP_NAME\", \"namespace\": \"$ARGOCD_APP_NAMESPACE\", \"annotations\": {\"Foo\": \"$FOO\", \"KubeVersion\": \"$KUBE_VERSION\", \"KubeApiVersion\": \"$KUBE_API_VERSIONS\",\"Bar\": \"baz\"}}}"`}, 115 }, 116 }, 117 ). 118 // does not matter what the path is 119 Path("guestbook"). 120 When(). 121 CreateFromFile(func(app *Application) { 122 app.Spec.Source.Plugin.Env = Env{{ 123 Name: "FOO", 124 Value: "bar", 125 }} 126 }). 127 Sync(). 128 Then(). 129 Expect(OperationPhaseIs(OperationSucceeded)). 130 Expect(SyncStatusIs(SyncStatusCodeSynced)). 131 Expect(HealthIs(health.HealthStatusHealthy)). 132 And(func(app *Application) { 133 time.Sleep(1 * time.Second) 134 }). 135 And(func(app *Application) { 136 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.Bar}") 137 assert.NoError(t, err) 138 assert.Equal(t, "baz", output) 139 }). 140 And(func(app *Application) { 141 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.Foo}") 142 assert.NoError(t, err) 143 assert.Equal(t, "bar", output) 144 }). 145 And(func(app *Application) { 146 expectedKubeVersion := GetVersions().ServerVersion.Format("%s.%s") 147 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.KubeVersion}") 148 assert.NoError(t, err) 149 assert.Equal(t, expectedKubeVersion, output) 150 }). 151 And(func(app *Application) { 152 expectedApiVersion := GetApiVersions() 153 expectedApiVersionSlice := strings.Split(expectedApiVersion, ",") 154 sort.Strings(expectedApiVersionSlice) 155 156 output, err := Run("", "kubectl", "-n", DeploymentNamespace(), "get", "cm", Name(), "-o", "jsonpath={.metadata.annotations.KubeApiVersion}") 157 assert.NoError(t, err) 158 outputSlice := strings.Split(output, ",") 159 sort.Strings(outputSlice) 160 161 assert.EqualValues(t, expectedApiVersionSlice, outputSlice) 162 }) 163 } 164 165 //make sure we can sync and diff with --local 166 func TestCustomToolSyncAndDiffLocal(t *testing.T) { 167 Given(t). 168 // path does not matter, we ignore it 169 ConfigManagementPlugin( 170 ConfigManagementPlugin{ 171 Name: Name(), 172 Generate: Command{ 173 Command: []string{"sh", "-c"}, 174 Args: []string{`echo "{\"kind\": \"ConfigMap\", \"apiVersion\": \"v1\", \"metadata\": { \"name\": \"$ARGOCD_APP_NAME\", \"namespace\": \"$ARGOCD_APP_NAMESPACE\", \"annotations\": {\"Foo\": \"$FOO\", \"KubeVersion\": \"$KUBE_VERSION\", \"KubeApiVersion\": \"$KUBE_API_VERSIONS\",\"Bar\": \"baz\"}}}"`}, 175 }, 176 }, 177 ). 178 // does not matter what the path is 179 Path("guestbook"). 180 When(). 181 Create("--config-management-plugin", Name()). 182 Sync("--local", "testdata/guestbook"). 183 Then(). 184 Expect(OperationPhaseIs(OperationSucceeded)). 185 Expect(SyncStatusIs(SyncStatusCodeSynced)). 186 Expect(HealthIs(health.HealthStatusHealthy)). 187 And(func(app *Application) { 188 time.Sleep(1 * time.Second) 189 }). 190 And(func(app *Application) { 191 FailOnErr(RunCli("app", "sync", app.Name, "--local", "testdata/guestbook")) 192 }). 193 And(func(app *Application) { 194 FailOnErr(RunCli("app", "diff", app.Name, "--local", "testdata/guestbook")) 195 }) 196 }