Skip to content

Commit 4a78ad2

Browse files
authored
Int/rgo tests (#615)
* test generateArgoApplication() all at once * trim space off from doc; add more tests * add simple e2e test
1 parent 7b8ad25 commit 4a78ad2

File tree

5 files changed

+225
-1
lines changed

5 files changed

+225
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
schema: v2
3+
namespace: myns
4+
repository: stable
5+
context: mycontext
6+
repositories:
7+
fairwinds-stable:
8+
url: https://charts.fairwinds.com/stable
9+
minimum_versions:
10+
helm: 3.4.1
11+
reckoner: 4.3.1
12+
gitops:
13+
argocd:
14+
kind: Application
15+
apiVersion: argoproj.io/v1alpha1
16+
metadata:
17+
namespace: argocd
18+
annotations:
19+
one_key: one_value
20+
spec:
21+
destination:
22+
namespace: some_value
23+
server: https://kubernetes.default.svc
24+
project: default
25+
source:
26+
# path: manifests
27+
repoURL: https://github.com/someuser/clustername.git
28+
directory:
29+
recurse: true
30+
# plugin:
31+
# name: argocd-vault-plugin
32+
syncPolicy:
33+
automated:
34+
prune: true
35+
syncOptions:
36+
- CreateNamespace=true
37+
- PruneLast=true
38+
namespace_management:
39+
default:
40+
metadata:
41+
annotations:
42+
insights.fairwinds.com/adminOnly: "true"
43+
labels:
44+
ManagedBy: Fairwinds
45+
settings:
46+
overwrite: true
47+
releases:
48+
- name: rbac-manager
49+
namespace: rbac-manager
50+
namespace_management:
51+
settings:
52+
overwrite: true
53+
chart: rbac-manager
54+
version: 1.11.1
55+
repository: fairwinds-stable
56+
57+
gitops:
58+
argocd:
59+
metadata:
60+
annotations:
61+
notifications.argoproj.io/subscribe.on-sync-succeeded.slack: some_channel
62+
spec:
63+
project: different_project
64+
source:
65+
path: some_totally_different_path
66+
repoURL: https://github.com/another_user/another_repo.git
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "2"
2+
name: Shell Executor Secret
3+
vars:
4+
course: ../course_files/27_gitops.yaml
5+
namespace: 27-gitops
6+
release: shell-executor-chart
7+
testcases:
8+
- name: 27 - template course
9+
steps:
10+
- script: |
11+
reckoner template -a {{.course}} --output-dir 27_gitops_output
12+
assertions:
13+
- result.code ShouldEqual 0

pkg/reckoner/argocd_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package reckoner
2+
3+
import (
4+
"testing"
5+
6+
"github.com/fairwindsops/reckoner/pkg/course"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_generateArgoApplication(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
cFile course.FileV2
14+
want course.ArgoApplication
15+
wantErr bool
16+
}{
17+
{
18+
name: "ensure_defaults",
19+
cFile: course.FileV2{
20+
Releases: []*course.Release{
21+
{
22+
Name: "somename",
23+
Namespace: "somens",
24+
Repository: "somerepo",
25+
GitOps: course.GitOps{ // release-specific *addition*
26+
ArgoCD: course.ArgoApplication{
27+
Metadata: course.ArgoApplicationMetadata{
28+
Annotations: map[string]string{
29+
"notifications.argoproj.io/subscribe.on-sync-succeeded.slack": "fairwindsops-infra-argocd",
30+
},
31+
},
32+
Spec: course.ArgoApplicationSpec{
33+
Source: course.ArgoApplicationSpecSource{
34+
Directory: course.ArgoApplicationSpecSourceDirectory{
35+
Recurse: true, // release-specific *override*
36+
},
37+
},
38+
},
39+
},
40+
},
41+
},
42+
},
43+
GitOps: course.GitOps{
44+
ArgoCD: course.ArgoApplication{
45+
Spec: course.ArgoApplicationSpec{
46+
Source: course.ArgoApplicationSpecSource{
47+
Directory: course.ArgoApplicationSpecSourceDirectory{
48+
Recurse: false, // exists here only to be overridden by the release-specific instance
49+
},
50+
// Path: "somepath", // omitting this tests more functionality
51+
RepoURL: "https://domain.tld/someorg/somerepo.git",
52+
},
53+
// Destination: course.ArgoApplicationSpecDestination{}, // omitting this tests defaults
54+
},
55+
},
56+
},
57+
},
58+
want: course.ArgoApplication{
59+
Kind: "Application",
60+
APIVersion: "argoproj.io/v1alpha1",
61+
Metadata: course.ArgoApplicationMetadata{
62+
Name: "somename",
63+
Annotations: map[string]string{
64+
"notifications.argoproj.io/subscribe.on-sync-succeeded.slack": "fairwindsops-infra-argocd",
65+
},
66+
},
67+
Spec: course.ArgoApplicationSpec{
68+
Source: course.ArgoApplicationSpecSource{
69+
Directory: course.ArgoApplicationSpecSourceDirectory{
70+
Recurse: true,
71+
},
72+
Path: "somename",
73+
RepoURL: "https://domain.tld/someorg/somerepo.git",
74+
},
75+
Destination: course.ArgoApplicationSpecDestination{
76+
Server: "https://kubernetes.default.svc",
77+
Namespace: "somens",
78+
},
79+
Project: "default",
80+
},
81+
},
82+
wantErr: false,
83+
},
84+
}
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
got, err := generateArgoApplication(*tt.cFile.Releases[0], tt.cFile)
88+
if (err != nil) != tt.wantErr {
89+
t.Errorf("generateArgoApplication() error = %v, wantErr %v", err, tt.wantErr)
90+
return
91+
}
92+
assert.Equal(t, tt.want, got)
93+
})
94+
}
95+
}

pkg/reckoner/split.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func splitYAML(in []byte) (out [][]byte, err error) {
6262
}
6363

6464
// we believe we have a valid YAML object
65-
out = append(out, b.Bytes()) // so append it to the list to be returned later
65+
out = append(out, bytes.TrimSpace(b.Bytes())) // so append it to the list to be returned later
6666
}
6767

6868
return out, nil // list of YAML objects, each a []byte

pkg/reckoner/split_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package reckoner
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_splitYAML(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
yamlFile []byte
13+
want [][]byte
14+
wantNumberOfDocs int
15+
wantErr bool
16+
}{
17+
{
18+
name: "one_document",
19+
yamlFile: []byte("---\nfield: \"value\"\n"),
20+
want: [][]byte{
21+
[]byte("field: value"),
22+
},
23+
wantErr: false,
24+
},
25+
{
26+
name: "multi_documents",
27+
yamlFile: []byte("---\nfield:\n nested: value\n---\nanother: second\n"),
28+
want: [][]byte{
29+
[]byte("field:\n nested: value"),
30+
[]byte("another: second"),
31+
},
32+
wantErr: false,
33+
},
34+
}
35+
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
docs, err := splitYAML(tt.yamlFile)
39+
if (err != nil) != tt.wantErr {
40+
t.Errorf("splitYAML() error = %v, wantErr %v", err, tt.wantErr)
41+
return
42+
}
43+
if len(tt.want) != len(docs) {
44+
t.Errorf("splitYAML() produced different number of YAML documents than expected; wanted = %v, got %v", tt.wantNumberOfDocs, len(docs))
45+
}
46+
47+
assert.Equal(t, tt.want, docs)
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)