forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitops_test.go
More file actions
203 lines (176 loc) · 5.59 KB
/
Copy pathgitops_test.go
File metadata and controls
203 lines (176 loc) · 5.59 KB
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
package gitops
import (
"context"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/weaveworks/eksctl/pkg/git"
"github.com/weaveworks/eksctl/pkg/gitops/fileprocessor"
)
type mockCloner struct {
mock.Mock
}
func (m *mockCloner) CloneRepoInTmpDir(cloneDirPrefix string, options git.CloneOptions) (string, error) {
args := m.Called(cloneDirPrefix, options)
return args.String(0), args.Error(1)
}
var _ = Describe("gitops profile", func() {
var (
gitCloner *mockCloner
memFs afero.Fs
io afero.Afero
testDir string
profile *Profile
outputDir string
processor fileprocessor.FileProcessor
)
Context("generating a profile", func() {
BeforeEach(func() {
// In memory filesystem for the tests
memFs = afero.NewMemMapFs()
io = afero.Afero{Fs: memFs}
// Create test data files instead of cloning
testDir, _ = io.TempDir("", "test-dir-")
createTestFiles(testDir, memFs)
// mock git clone
gitCloner = new(mockCloner)
gitCloner.On("CloneRepoInTmpDir", mock.Anything, mock.Anything, mock.Anything).Return(testDir, nil)
// output path
outputDir, _ = io.TempDir("", "test-output-dir-")
processor = &fileprocessor.GoTemplateProcessor{
Params: fileprocessor.TemplateParameters{ClusterName: "test-cluster"},
}
profile = &Profile{
Path: outputDir,
GitOpts: git.Options{
Branch: "master",
URL: "git@github.com:someorg/test-gitops-repo.git",
},
IO: io,
FS: memFs,
GitCloner: gitCloner,
Processor: processor,
}
})
AfterEach(func() {
io.RemoveAll(testDir)
io.RemoveAll(outputDir)
})
It("processes go templates and writes them to the output directory", func() {
err := profile.Generate(context.Background())
Expect(err).ToNot(HaveOccurred())
template1, err := io.ReadFile(filepath.Join(outputDir, "a/good-template1.yaml"))
Expect(err).ToNot(HaveOccurred())
Expect(template1).To(MatchYAML([]byte("cluster: test-cluster")))
template2, err := io.ReadFile(filepath.Join(outputDir, "a/b/good-template2.yaml"))
Expect(err).ToNot(HaveOccurred())
Expect(template2).To(MatchYAML([]byte("name: test-cluster")))
})
It("can load files and ignore .git/ files", func() {
err := profile.ignoreFiles(testDir)
Expect(err).ToNot(HaveOccurred())
files, err := profile.loadFiles(testDir)
Expect(err).ToNot(HaveOccurred())
Expect(files).To(HaveLen(4))
Expect(files).To(ConsistOf(
fileprocessor.File{
Path: filepath.Join(testDir, "a/good-template1.yaml.tmpl"),
Data: []byte("cluster: {{ .ClusterName }}"),
},
fileprocessor.File{
Path: filepath.Join(testDir, "a/b/good-template2.yaml.tmpl"),
Data: []byte("name: {{ .ClusterName }}"),
},
fileprocessor.File{
Path: filepath.Join(testDir, "a/not-a-template2.yaml"),
Data: []byte("somekey2: value2"),
},
fileprocessor.File{
Path: filepath.Join(testDir, "not-a-template.yaml"),
Data: []byte("somekey: value"),
}))
})
Context("processing templates", func() {
It("processes go templates and leaves the rest intact", func() {
templateContent := []byte(`
apiVersion: v1
kind: Namespace
metadata:
labels:
name: {{ .ClusterName }}
name: flux`)
expectedProcessedTemplate := []byte(`
apiVersion: v1
kind: Namespace
metadata:
labels:
name: test-cluster
name: flux`)
pureYaml := []byte("this: is just yaml")
inputFiles := []fileprocessor.File{
{
Data: templateContent,
Path: "dir0/some-file.yaml.tmpl",
},
{
Data: templateContent,
Path: "dir0/dir1/some-file2.yaml.tmpl",
},
{
Data: pureYaml,
Path: "dir0/dir1/non-template.yaml",
},
{
Data: templateContent,
Path: "dir0/dir1/dir2/some-file3.yaml.tmpl",
},
}
files, err := profile.processFiles(inputFiles, "dir0")
Expect(err).ToNot(HaveOccurred())
Expect(files).To(HaveLen(4))
Expect(files).To(ConsistOf(
fileprocessor.File{
Path: "some-file.yaml",
Data: expectedProcessedTemplate,
},
fileprocessor.File{
Path: "dir1/some-file2.yaml",
Data: expectedProcessedTemplate,
},
fileprocessor.File{
Path: "dir1/non-template.yaml",
Data: pureYaml,
},
fileprocessor.File{
Path: "dir1/dir2/some-file3.yaml",
Data: expectedProcessedTemplate,
},
))
})
})
})
})
func createTestFiles(testDir string, memFs afero.Fs) {
createFile(memFs, filepath.Join(testDir, "not-a-template.yaml"), "somekey: value")
createFile(memFs, filepath.Join(testDir, "a/not-a-template2.yaml"), "somekey2: value2")
createFile(memFs, filepath.Join(testDir, "a/good-template1.yaml.tmpl"), "cluster: {{ .ClusterName }}")
createFile(memFs, filepath.Join(testDir, "a/b/good-template2.yaml.tmpl"), "name: {{ .ClusterName }}")
memFs.Mkdir(".git", 0755)
createFile(memFs, filepath.Join(testDir, ".git/some-git-file"), "this is a git file and should be ignored")
createFile(memFs, filepath.Join(testDir, ".git/some-git-file.yaml"), "this is a git file and should be ignored")
createFile(memFs, filepath.Join(testDir, ".git/some-git-file.yaml.tmpl"), "this is a git file and should be ignored")
createFile(memFs, filepath.Join(testDir, ".eksctlignore"), "path/to/ignore")
createFile(memFs, filepath.Join(testDir, "path/to/ignore"), "this file should be ignored")
}
func createFile(memFs afero.Fs, path string, content string) error {
file, err := memFs.Create(path)
if err != nil {
return err
}
if _, err := file.WriteString(content); err != nil {
return err
}
return nil
}