forked from argoproj/argo-cd
/
fixture.go
457 lines (381 loc) · 14.8 KB
/
fixture.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package fixture
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/argoproj/pkg/errors"
jsonpatch "github.com/evanphx/json-patch"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/argoproj/argo-cd/common"
. "github.com/argoproj/argo-cd/errors"
argocdclient "github.com/argoproj/argo-cd/pkg/apiclient"
sessionpkg "github.com/argoproj/argo-cd/pkg/apiclient/session"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned"
"github.com/argoproj/argo-cd/util"
grpcutil "github.com/argoproj/argo-cd/util/grpc"
"github.com/argoproj/argo-cd/util/rand"
"github.com/argoproj/argo-cd/util/settings"
)
const (
defaultApiServer = "localhost:8080"
adminPassword = "password"
testingLabel = "e2e.argoproj.io"
ArgoCDNamespace = "argocd-e2e"
// ensure all repos are in one directory tree, so we can easily clean them up
TmpDir = "/tmp/argo-e2e"
repoDir = "testdata.git"
submoduleDir = "submodule.git"
submoduleParentDir = "submoduleParent.git"
GuestbookPath = "guestbook"
)
var (
id string
deploymentNamespace string
name string
KubeClientset kubernetes.Interface
DynamicClientset dynamic.Interface
AppClientset appclientset.Interface
ArgoCDClientset argocdclient.Client
apiServerAddress string
token string
plainText bool
)
type RepoURLType string
const (
RepoURLTypeFile = "file"
RepoURLTypeHTTPS = "https"
RepoURLTypeHTTPSClientCert = "https-cc"
RepoURLTypeHTTPSSubmodule = "https-sub"
RepoURLTypeHTTPSSubmoduleParent = "https-par"
RepoURLTypeSSH = "ssh"
RepoURLTypeSSHSubmodule = "ssh-sub"
RepoURLTypeSSHSubmoduleParent = "ssh-par"
RepoURLTypeHelm = "helm"
GitUsername = "admin"
GitPassword = "password"
)
// getKubeConfig creates new kubernetes client config using specified config path and config overrides variables
func getKubeConfig(configPath string, overrides clientcmd.ConfigOverrides) *rest.Config {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = configPath
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)
restConfig, err := clientConfig.ClientConfig()
CheckError(err)
return restConfig
}
// creates e2e tests fixture: ensures that Application CRD is installed, creates temporal namespace, starts repo and api server,
// configure currently available cluster.
func init() {
// ensure we log all shell execs
log.SetLevel(log.DebugLevel)
// set-up variables
config := getKubeConfig("", clientcmd.ConfigOverrides{})
AppClientset = appclientset.NewForConfigOrDie(config)
KubeClientset = kubernetes.NewForConfigOrDie(config)
DynamicClientset = dynamic.NewForConfigOrDie(config)
apiServerAddress = os.Getenv(argocdclient.EnvArgoCDServer)
if apiServerAddress == "" {
apiServerAddress = defaultApiServer
}
tlsTestResult, err := grpcutil.TestTLS(apiServerAddress)
CheckError(err)
ArgoCDClientset, err = argocdclient.NewClient(&argocdclient.ClientOptions{Insecure: true, ServerAddr: apiServerAddress, PlainText: !tlsTestResult.TLS})
CheckError(err)
closer, client, err := ArgoCDClientset.NewSessionClient()
CheckError(err)
defer util.Close(closer)
sessionResponse, err := client.Create(context.Background(), &sessionpkg.SessionCreateRequest{Username: "admin", Password: adminPassword})
CheckError(err)
ArgoCDClientset, err = argocdclient.NewClient(&argocdclient.ClientOptions{
Insecure: true,
ServerAddr: apiServerAddress,
AuthToken: sessionResponse.Token,
PlainText: !tlsTestResult.TLS,
})
CheckError(err)
token = sessionResponse.Token
plainText = !tlsTestResult.TLS
log.WithFields(log.Fields{"apiServerAddress": apiServerAddress}).Info("initialized")
}
func Name() string {
return name
}
func repoDirectory() string {
return path.Join(TmpDir, repoDir)
}
func submoduleDirectory() string {
return path.Join(TmpDir, submoduleDir)
}
func submoduleParentDirectory() string {
return path.Join(TmpDir, submoduleParentDir)
}
func RepoURL(urlType RepoURLType) string {
switch urlType {
// Git server via SSH
case RepoURLTypeSSH:
return "ssh://root@localhost:2222/tmp/argo-e2e/testdata.git"
// Git submodule repo
case RepoURLTypeSSHSubmodule:
return "ssh://root@localhost:2222/tmp/argo-e2e/submodule.git"
// Git submodule parent repo
case RepoURLTypeSSHSubmoduleParent:
return "ssh://root@localhost:2222/tmp/argo-e2e/submoduleParent.git"
// Git server via HTTPS
case RepoURLTypeHTTPS:
return "https://localhost:9443/argo-e2e/testdata.git"
// Git server via HTTPS - Client Cert protected
case RepoURLTypeHTTPSClientCert:
return "https://localhost:9444/argo-e2e/testdata.git"
case RepoURLTypeHTTPSSubmodule:
return "https://localhost:9443/argo-e2e/submodule.git"
// Git submodule parent repo
case RepoURLTypeHTTPSSubmoduleParent:
return "https://localhost:9443/argo-e2e/submoduleParent.git"
// Default - file based Git repository
case RepoURLTypeHelm:
return "https://localhost:9444/argo-e2e/testdata.git/helm-repo"
default:
return fmt.Sprintf("file://%s", repoDirectory())
}
}
func RepoBaseURL(urlType RepoURLType) string {
return path.Base(RepoURL(urlType))
}
func DeploymentNamespace() string {
return deploymentNamespace
}
// creates a secret for the current test, this currently can only create a single secret
func CreateSecret(username, password string) string {
secretName := fmt.Sprintf("argocd-e2e-%s", name)
FailOnErr(Run("", "kubectl", "create", "secret", "generic", secretName,
"--from-literal=username="+username,
"--from-literal=password="+password,
"-n", ArgoCDNamespace))
FailOnErr(Run("", "kubectl", "label", "secret", secretName, testingLabel+"=true", "-n", ArgoCDNamespace))
return secretName
}
func updateSettingConfigMap(updater func(cm *corev1.ConfigMap) error) {
cm, err := KubeClientset.CoreV1().ConfigMaps(ArgoCDNamespace).Get(common.ArgoCDConfigMapName, v1.GetOptions{})
errors.CheckError(err)
if cm.Data == nil {
cm.Data = make(map[string]string)
}
errors.CheckError(updater(cm))
_, err = KubeClientset.CoreV1().ConfigMaps(ArgoCDNamespace).Update(cm)
errors.CheckError(err)
}
func SetResourceOverrides(overrides map[string]v1alpha1.ResourceOverride) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
if len(overrides) > 0 {
yamlBytes, err := yaml.Marshal(overrides)
if err != nil {
return err
}
cm.Data["resource.customizations"] = string(yamlBytes)
} else {
delete(cm.Data, "resource.customizations")
}
return nil
})
}
func SetAccounts(accounts map[string][]string) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
for k, v := range accounts {
cm.Data[fmt.Sprintf("accounts.%s", k)] = strings.Join(v, ",")
}
return nil
})
}
func SetConfigManagementPlugins(plugin ...v1alpha1.ConfigManagementPlugin) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
yamlBytes, err := yaml.Marshal(plugin)
if err != nil {
return err
}
cm.Data["configManagementPlugins"] = string(yamlBytes)
return nil
})
}
func SetResourceFilter(filters settings.ResourcesFilter) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
exclusions, err := yaml.Marshal(filters.ResourceExclusions)
if err != nil {
return err
}
inclusions, err := yaml.Marshal(filters.ResourceInclusions)
if err != nil {
return err
}
cm.Data["resource.exclusions"] = string(exclusions)
cm.Data["resource.inclusions"] = string(inclusions)
return nil
})
}
func SetHelmRepos(repos ...settings.HelmRepoCredentials) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
yamlBytes, err := yaml.Marshal(repos)
if err != nil {
return err
}
cm.Data["helm.repositories"] = string(yamlBytes)
return nil
})
}
func SetRepos(repos ...settings.RepositoryCredentials) {
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
yamlBytes, err := yaml.Marshal(repos)
if err != nil {
return err
}
cm.Data["repositories"] = string(yamlBytes)
return nil
})
}
func SetProjectSpec(project string, spec v1alpha1.AppProjectSpec) {
proj, err := AppClientset.ArgoprojV1alpha1().AppProjects(ArgoCDNamespace).Get(project, v1.GetOptions{})
errors.CheckError(err)
proj.Spec = spec
_, err = AppClientset.ArgoprojV1alpha1().AppProjects(ArgoCDNamespace).Update(proj)
errors.CheckError(err)
}
func EnsureCleanState(t *testing.T) {
start := time.Now()
policy := v1.DeletePropagationBackground
// delete resources
// kubectl delete apps --all
CheckError(AppClientset.ArgoprojV1alpha1().Applications(ArgoCDNamespace).DeleteCollection(&v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{}))
// kubectl delete appprojects --field-selector metadata.name!=default
CheckError(AppClientset.ArgoprojV1alpha1().AppProjects(ArgoCDNamespace).DeleteCollection(
&v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{FieldSelector: "metadata.name!=default"}))
// kubectl delete secrets -l e2e.argoproj.io=true
CheckError(KubeClientset.CoreV1().Secrets(ArgoCDNamespace).DeleteCollection(
&v1.DeleteOptions{PropagationPolicy: &policy}, v1.ListOptions{LabelSelector: testingLabel + "=true"}))
FailOnErr(Run("", "kubectl", "delete", "ns", "-l", testingLabel+"=true", "--field-selector", "status.phase=Active", "--wait=false"))
FailOnErr(Run("", "kubectl", "delete", "crd", "-l", testingLabel+"=true", "--wait=false"))
// reset settings
updateSettingConfigMap(func(cm *corev1.ConfigMap) error {
cm.Data = map[string]string{}
return nil
})
SetProjectSpec("default", v1alpha1.AppProjectSpec{
OrphanedResources: nil,
SourceRepos: []string{"*"},
Destinations: []v1alpha1.ApplicationDestination{{Namespace: "*", Server: "*"}},
ClusterResourceWhitelist: []v1.GroupKind{{Group: "*", Kind: "*"}},
})
// remove tmp dir
CheckError(os.RemoveAll(TmpDir))
// random id - unique across test runs
postFix := "-" + strings.ToLower(rand.RandString(5))
id = t.Name() + postFix
name = dnsFriendly(t.Name(), "")
deploymentNamespace = dnsFriendly(fmt.Sprintf("argocd-e2e-%s", t.Name()), postFix)
// create tmp dir
FailOnErr(Run("", "mkdir", "-p", TmpDir))
// create TLS and SSH certificate directories
FailOnErr(Run("", "mkdir", "-p", TmpDir+"/app/config/tls"))
FailOnErr(Run("", "mkdir", "-p", TmpDir+"/app/config/ssh"))
// set-up tmp repo, must have unique name
FailOnErr(Run("", "cp", "-Rf", "testdata", repoDirectory()))
FailOnErr(Run(repoDirectory(), "chmod", "777", "."))
FailOnErr(Run(repoDirectory(), "git", "init"))
FailOnErr(Run(repoDirectory(), "git", "add", "."))
FailOnErr(Run(repoDirectory(), "git", "commit", "-q", "-m", "initial commit"))
// create namespace
FailOnErr(Run("", "kubectl", "create", "ns", DeploymentNamespace()))
FailOnErr(Run("", "kubectl", "label", "ns", DeploymentNamespace(), testingLabel+"=true"))
log.WithFields(log.Fields{"duration": time.Since(start), "name": t.Name(), "id": id, "username": "admin", "password": "password"}).Info("clean state")
}
func RunCli(args ...string) (string, error) {
return RunCliWithStdin("", args...)
}
func RunCliWithStdin(stdin string, args ...string) (string, error) {
if plainText {
args = append(args, "--plaintext")
}
args = append(args, "--server", apiServerAddress, "--auth-token", token, "--insecure")
return RunWithStdin(stdin, "", "../../dist/argocd", args...)
}
func Patch(path string, jsonPatch string) {
log.WithFields(log.Fields{"path": path, "jsonPatch": jsonPatch}).Info("patching")
filename := filepath.Join(repoDirectory(), path)
bytes, err := ioutil.ReadFile(filename)
CheckError(err)
patch, err := jsonpatch.DecodePatch([]byte(jsonPatch))
CheckError(err)
isYaml := strings.HasSuffix(filename, ".yaml")
if isYaml {
log.Info("converting YAML to JSON")
bytes, err = yaml.YAMLToJSON(bytes)
CheckError(err)
}
log.WithFields(log.Fields{"bytes": string(bytes)}).Info("JSON")
bytes, err = patch.Apply(bytes)
CheckError(err)
if isYaml {
log.Info("converting JSON back to YAML")
bytes, err = yaml.JSONToYAML(bytes)
CheckError(err)
}
CheckError(ioutil.WriteFile(filename, bytes, 0644))
FailOnErr(Run(repoDirectory(), "git", "diff"))
FailOnErr(Run(repoDirectory(), "git", "commit", "-am", "patch"))
}
func Delete(path string) {
log.WithFields(log.Fields{"path": path}).Info("deleting")
CheckError(os.Remove(filepath.Join(repoDirectory(), path)))
FailOnErr(Run(repoDirectory(), "git", "diff"))
FailOnErr(Run(repoDirectory(), "git", "commit", "-am", "delete"))
}
func AddFile(path, contents string) {
log.WithFields(log.Fields{"path": path}).Info("adding")
CheckError(ioutil.WriteFile(filepath.Join(repoDirectory(), path), []byte(contents), 0644))
FailOnErr(Run(repoDirectory(), "git", "diff"))
FailOnErr(Run(repoDirectory(), "git", "add", "."))
FailOnErr(Run(repoDirectory(), "git", "commit", "-am", "add file"))
}
// create the resource by creating using "kubectl apply", with bonus templating
func Declarative(filename string, values interface{}) (string, error) {
bytes, err := ioutil.ReadFile(path.Join("testdata", filename))
CheckError(err)
tmpFile, err := ioutil.TempFile("", "")
CheckError(err)
_, err = tmpFile.WriteString(Tmpl(string(bytes), values))
CheckError(err)
return Run("", "kubectl", "-n", ArgoCDNamespace, "apply", "-f", tmpFile.Name())
}
func CreateSubmoduleRepos(repoType string) {
// set-up submodule repo
FailOnErr(Run("", "cp", "-Rf", "testdata/git-submodule/", submoduleDirectory()))
FailOnErr(Run(submoduleDirectory(), "chmod", "777", "."))
FailOnErr(Run(submoduleDirectory(), "git", "init"))
FailOnErr(Run(submoduleDirectory(), "git", "add", "."))
FailOnErr(Run(submoduleDirectory(), "git", "commit", "-q", "-m", "initial commit"))
// set-up submodule parent repo
FailOnErr(Run("", "mkdir", submoduleParentDirectory()))
FailOnErr(Run(submoduleParentDirectory(), "chmod", "777", "."))
FailOnErr(Run(submoduleParentDirectory(), "git", "init"))
FailOnErr(Run(submoduleParentDirectory(), "git", "add", "."))
FailOnErr(Run(submoduleParentDirectory(), "git", "submodule", "add", "-b", "master", "../submodule.git", "submodule/test"))
if repoType == "ssh" {
FailOnErr(Run(submoduleParentDirectory(), "git", "config", "--file=.gitmodules", "submodule.submodule/test.url", RepoURL(RepoURLTypeSSHSubmodule)))
} else if repoType == "https" {
FailOnErr(Run(submoduleParentDirectory(), "git", "config", "--file=.gitmodules", "submodule.submodule/test.url", RepoURL(RepoURLTypeHTTPSSubmodule)))
}
FailOnErr(Run(submoduleParentDirectory(), "git", "add", "--all"))
FailOnErr(Run(submoduleParentDirectory(), "git", "commit", "-q", "-m", "commit with submodule"))
}