-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathrawconfig_test.go
More file actions
217 lines (208 loc) · 5.52 KB
/
Copy pathrawconfig_test.go
File metadata and controls
217 lines (208 loc) · 5.52 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package configmanager
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("newRawConfigFromConfigText func", func() {
var testText []byte
When("text is valid", func() {
BeforeEach(func() {
testText = []byte(`---
config:
state:
backend: local
options:
stateFile: devstream.state
vars:
tests: argocd
apps:
- name: service-a
spec:
language: python
framework: [[ var1 ]]/[[ var2 ]]_gg
not_exist: 123`)
})
It("should return config map", func() {
rawMap, err := newRawConfigFromConfigBytes(testText)
Expect(err).ShouldNot(HaveOccurred())
Expect(string(rawMap.apps)).Should(Equal(`
- name: service-a
spec:
language: python
framework: [[ var1 ]]/[[ var2 ]]_gg
`))
Expect(string(rawMap.config)).Should(Equal(`
state:
backend: local
options:
stateFile: devstream.state
`))
Expect(string(rawMap.vars)).Should(Equal(`
tests: argocd
`))
})
})
When("text is not valid", func() {
BeforeEach(func() {
testText = []byte(`
not_valid: not_exist
`)
})
It("should return error", func() {
_, err := newRawConfigFromConfigBytes(testText)
Expect(err).Error().Should(HaveOccurred())
})
})
})
var _ = Describe("rawConfig struct", func() {
var r *rawConfig
Context("checkValid method", func() {
When("config is not exist", func() {
BeforeEach(func() {
r = &rawConfig{
apps: []byte("apps"),
}
})
It("should return error", func() {
e := r.validate()
Expect(e).Error().Should(HaveOccurred())
Expect(e.Error()).Should(Equal("config not valid; check the [config] section of your config file"))
})
})
When("apps and tools is not exist", func() {
BeforeEach(func() {
r = &rawConfig{
config: []byte("config"),
}
})
It("should return error", func() {
e := r.validate()
Expect(e).Error().Should(HaveOccurred())
Expect(e.Error()).Should(Equal("config not valid; check the [tools and apps] section of your config file"))
})
})
})
Context("getTemplateMap method", func() {
BeforeEach(func() {
r = &rawConfig{
pipelineTemplates: []byte(`---
- name: ci-pipeline-for-gh-actions
type: github-actions # corresponding to a plugin
options:
docker:
registry:
username: [[ dockerUser ]]
repository: [[ app ]]
- name: cd-pipeline-for-argocdapp
type: argocdapp
options:
app:
namespace: [[ argocdNamespace ]]/[[ test ]] # you can use global vars in templates
`)}
})
It("should get template", func() {
m, err := r.getTemplatePipelineMap()
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(m).Should(Equal(map[string]string{
"ci-pipeline-for-gh-actions": "name: ci-pipeline-for-gh-actions\ntype: github-actions\noptions:\n docker:\n registry:\n repository: '[[ app ]]'\n username: '[[ dockerUser ]]'\n",
"cd-pipeline-for-argocdapp": "name: cd-pipeline-for-argocdapp\ntype: argocdapp\noptions:\n app:\n namespace: '[[ argocdNamespace ]]/[[ test ]]'\n",
}))
})
})
Context("getApps method", func() {
When("app file is not valid", func() {
BeforeEach(func() {
r = &rawConfig{
apps: []byte(`
---
- name: app-1
cd:
- type: template
vars:
app: [[ appName ]]`)}
})
It("should get apps", func() {
vars := map[string]any{}
_, err := r.getAppsWithVars(vars)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("has no entry"))
})
})
})
Context("getConfig method", func() {
BeforeEach(func() {
r = &rawConfig{
config: []byte(`
state:
backend: local
options:
stateFile: devstream.state`)}
})
When("get core config from config file", func() {
It("should works fine", func() {
cc, err := r.getConfig()
Expect(err).NotTo(HaveOccurred())
Expect(cc).NotTo(BeNil())
Expect(cc.State.Backend).To(Equal("local"))
Expect(cc.State.Options.StateFile).To(Equal("devstream.state"))
})
})
})
Context("getTools method", func() {
BeforeEach(func() {
r = &rawConfig{
tools: []byte(`
- name: name1
instanceID: ins-1
dependsOn: []
options:
foo: [[ foo ]]`)}
})
When("get tools from config file", func() {
It("should return config with vars", func() {
tools, err := r.getToolsWithVars(map[string]any{"foo": interface{}("bar")})
Expect(err).NotTo(HaveOccurred())
Expect(tools).NotTo(BeNil())
Expect(len(tools)).To(Equal(1))
Expect(len(tools[0].Options)).To(Equal(1))
Expect(tools[0].Options["foo"]).To(Equal(interface{}("bar")))
})
})
})
Context("getPipelineTemplateMap method", func() {
BeforeEach(func() {
r = &rawConfig{
pipelineTemplates: []byte(`
- name: tpl1
type: github-actions
options:
foo: bar`)}
})
When("get tools from config file", func() {
It("should return config with vars", func() {
pipelineTemplatesMap, err := r.getTemplatePipelineMap()
Expect(err).NotTo(HaveOccurred())
Expect(pipelineTemplatesMap).NotTo(BeNil())
Expect(len(pipelineTemplatesMap)).To(Equal(1))
Expect(pipelineTemplatesMap["tpl1"]).To(Equal("name: tpl1\ntype: github-actions\noptions:\n foo: bar\n"))
})
})
})
Context("getVars method", func() {
BeforeEach(func() {
r = &rawConfig{
vars: []byte(`---
foo1: bar1
foo2: 123
foo3: bar3`)}
})
It("should works fine", func() {
varMap, err := r.getVars()
Expect(err).NotTo(HaveOccurred())
Expect(varMap).NotTo(BeNil())
Expect(len(varMap)).To(Equal(3))
Expect(varMap["foo1"]).To(Equal(interface{}("bar1")))
Expect(varMap["foo2"]).To(Equal(interface{}(123)))
})
})
})