Skip to content

Commit 76d2ed8

Browse files
committed
test: update ut of configmanager
Signed-off-by: Bird <[email protected]>
1 parent c8c3548 commit 76d2ed8

File tree

2 files changed

+91
-33
lines changed

2 files changed

+91
-33
lines changed

internal/pkg/configmanager/rawconfig_test.go

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,78 @@
11
package configmanager
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
. "github.com/onsi/ginkgo/v2"
58
. "github.com/onsi/gomega"
69
)
710

811
var _ = Describe("newRawConfigFromConfigText func", func() {
912
var testText []byte
10-
When("text is valid", func() {
11-
BeforeEach(func() {
12-
testText = []byte(`---
13-
config:
13+
const (
14+
validConfigTextWithoutKey = `
1415
state:
1516
backend: local
1617
options:
17-
stateFile: devstream.state
18-
vars:
19-
tests: argocd
20-
apps:
18+
stateFile: devstream.state`
19+
validVarsTextWithoutKey = `
20+
tests: argocd`
21+
validAppsTextWithoutKey = `
2122
- name: service-a
2223
spec:
2324
language: python
24-
framework: [[ var1 ]]/[[ var2 ]]_gg
25-
not_exist: 123`)
25+
framework: [[ var1 ]]/[[ var2 ]]_gg`
26+
)
27+
When("text is valid", func() {
28+
BeforeEach(func() {
29+
testText = []byte(fmt.Sprintf(`---
30+
config:%s
31+
vars:%s
32+
apps:%s`, validConfigTextWithoutKey, validVarsTextWithoutKey, validAppsTextWithoutKey))
2633
})
2734
It("should return config map", func() {
2835
rawMap, err := newRawConfigFromConfigBytes(testText)
2936
Expect(err).ShouldNot(HaveOccurred())
30-
Expect(string(rawMap.apps)).Should(Equal(`
31-
- name: service-a
32-
spec:
33-
language: python
34-
framework: [[ var1 ]]/[[ var2 ]]_gg
35-
`))
36-
Expect(string(rawMap.config)).Should(Equal(`
37-
state:
38-
backend: local
39-
options:
40-
stateFile: devstream.state
41-
`))
42-
Expect(string(rawMap.vars)).Should(Equal(`
43-
tests: argocd
44-
`))
37+
removeLineFeed := func(s string) string {
38+
return strings.ReplaceAll(s, "\n", "")
39+
}
40+
Expect(removeLineFeed(string(rawMap.apps))).Should(Equal(removeLineFeed(validAppsTextWithoutKey)))
41+
Expect(removeLineFeed(string(rawMap.config))).Should(Equal(removeLineFeed(validConfigTextWithoutKey)))
42+
Expect(removeLineFeed(string(rawMap.vars))).Should(Equal(removeLineFeed(validVarsTextWithoutKey)))
4543
})
4644
})
47-
When("text is not valid", func() {
48-
BeforeEach(func() {
49-
testText = []byte(`
50-
not_valid: not_exist
51-
`)
45+
When("text is invalid", func() {
46+
When("invalid keys are used", func() {
47+
BeforeEach(func() {
48+
testText = []byte(fmt.Sprintf(`---
49+
config:
50+
%s
51+
vars:
52+
%s
53+
app:
54+
%s`, validConfigTextWithoutKey, validVarsTextWithoutKey, validAppsTextWithoutKey))
55+
})
56+
It("should return error", func() {
57+
_, err := newRawConfigFromConfigBytes(testText)
58+
Expect(err).Should(HaveOccurred())
59+
})
5260
})
53-
It("should return error", func() {
54-
_, err := newRawConfigFromConfigBytes(testText)
55-
Expect(err).Error().Should(HaveOccurred())
61+
62+
When("there are no enough keys", func() {
63+
BeforeEach(func() {
64+
testText = []byte(fmt.Sprintf(`
65+
config:
66+
%s
67+
`, validConfigTextWithoutKey))
68+
})
69+
It("should return error", func() {
70+
_, err := newRawConfigFromConfigBytes(testText)
71+
Expect(err).Error().Should(HaveOccurred())
72+
})
5673
})
5774
})
75+
5876
})
5977

6078
var _ = Describe("rawConfig struct", func() {

internal/pkg/configmanager/tool_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,43 @@ var _ = Describe("Tool struct", func() {
165165
})
166166
})
167167
})
168+
169+
var _ = Describe("DuplicatedCheck", func() {
170+
var (
171+
errs []error
172+
tools Tools
173+
toolsWithoutDuplicated = Tools{
174+
{Name: "test_tool", InstanceID: "0"},
175+
{Name: "test_tool", InstanceID: "1"},
176+
{Name: "test_tool", InstanceID: "2"},
177+
}
178+
179+
toolsWithDuplicated = Tools{
180+
{Name: "test_tool", InstanceID: "0"},
181+
{Name: "test_tool", InstanceID: "1"},
182+
{Name: "test_tool", InstanceID: "0"},
183+
}
184+
)
185+
186+
JustBeforeEach(func() {
187+
errs = tools.DuplicatedCheck()
188+
})
189+
190+
When("tools has duplicated name and instanceID", func() {
191+
BeforeEach(func() {
192+
tools = toolsWithDuplicated
193+
})
194+
It("should return error", func() {
195+
Expect(errs).Should(HaveLen(1))
196+
})
197+
})
198+
199+
When("tools don't have duplicated name and instanceID", func() {
200+
BeforeEach(func() {
201+
tools = toolsWithoutDuplicated
202+
})
203+
It("should return nil", func() {
204+
Expect(errs).Should(BeEmpty())
205+
})
206+
})
207+
})

0 commit comments

Comments
 (0)