Skip to content

Commit cf6c62a

Browse files
committed
test: add some uts for configmanager
Signed-off-by: Daniel Hu <[email protected]>
1 parent d172e4c commit cf6c62a

File tree

9 files changed

+121
-77
lines changed

9 files changed

+121
-77
lines changed

cmd/plugin/argocdapp/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ const NAME = "argocdapp"
1313
// Plugin is the type used by DevStream core. It's a string.
1414
type Plugin string
1515

16-
// Create implements the installation of an ArgoCD app.
16+
// Create implements the create of an argocdapp.
1717
func (p Plugin) Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1818
return argocdapp.Create(options)
1919
}
2020

21-
// Update implements the installation of an ArgoCD app.
21+
// Update implements the update of an argocdapp.
2222
func (p Plugin) Update(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2323
return argocdapp.Update(options)
2424
}
2525

26-
// Read implements the healthy check of ArgoCD app.
26+
// Read implements the read of argocdapp.
2727
func (p Plugin) Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2828
return argocdapp.Read(options)
2929
}
3030

31-
// Delete Deletes the installation of an ArgoCD app.
31+
// Delete implements the delete of argocdapp.
3232
func (p Plugin) Delete(options configmanager.RawOptions) (bool, error) {
3333
return argocdapp.Delete(options)
3434
}

cmd/plugin/github-actions/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ const NAME = "github-actions"
1313
// Plugin is the type used by DevStream core. It's a string.
1414
type Plugin string
1515

16-
// Create implements the installation of some GitHub Actions workflows.
16+
// Create implements the create of github-actions.
1717
func (p Plugin) Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1818
return general.Create(options)
1919
}
2020

21-
// Update implements the installation of some GitHub Actions workflows.
21+
// Update implements the update of github-actions.
2222
func (p Plugin) Update(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2323
return general.Update(options)
2424
}
2525

26-
// Read implements the healthy check of GitHub Actions workflows.
26+
// Read implements the read of github-actions.
2727
func (p Plugin) Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2828
return general.Read(options)
2929
}
3030

31-
// Delete implements the installation of some GitHub Actions workflows.
31+
// Delete implements the delete of github-actions.
3232
func (p Plugin) Delete(options configmanager.RawOptions) (bool, error) {
3333
return general.Delete(options)
3434
}

cmd/plugin/githubactions-golang/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ const NAME = "githubactions-golang"
1313
// Plugin is the type used by DevStream core. It's a string.
1414
type Plugin string
1515

16-
// Create implements the installation of some GitHub Actions workflows.
16+
// Create implements the create of githubactions-golang.
1717
func (p Plugin) Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1818
return golang.Create(options)
1919
}
2020

21-
// Update implements the installation of some GitHub Actions workflows.
21+
// Update implements the update of githubactions-golang.
2222
func (p Plugin) Update(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2323
return golang.Update(options)
2424
}
2525

26-
// Read implements the healthy check of GitHub Actions workflows.
26+
// Read implements the read of githubactions-golang.
2727
func (p Plugin) Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2828
return golang.Read(options)
2929
}
3030

31-
// Delete implements the installation of some GitHub Actions workflows.
31+
// Delete implements the delete of githubactions-golang.
3232
func (p Plugin) Delete(options configmanager.RawOptions) (bool, error) {
3333
return golang.Delete(options)
3434
}

cmd/plugin/githubactions-nodejs/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ const NAME = "githubactions-nodejs"
1313
// Plugin is the type used by DevStream core. It's a string.
1414
type Plugin string
1515

16-
// Create implements the installation of some GitHub Actions workflows.
16+
// Create implements the create of githubactions-nodejs.
1717
func (p Plugin) Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1818
return nodejs.Create(options)
1919
}
2020

21-
// Update implements the installation of some GitHub Actions workflows.
21+
// Update implements the update of githubactions-nodejs.
2222
func (p Plugin) Update(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2323
return nodejs.Update(options)
2424
}
2525

26-
// Read implements the healthy check of GitHub Actions workflows.
26+
// Read implements the read of githubactions-nodejs.
2727
func (p Plugin) Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2828
return nodejs.Read(options)
2929
}
3030

31-
// Delete implements the installation of some GitHub Actions workflows.
31+
// Delete implements the delete of githubactions-nodejs.
3232
func (p Plugin) Delete(options configmanager.RawOptions) (bool, error) {
3333
return nodejs.Delete(options)
3434
}

cmd/plugin/githubactions-python/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ const NAME = "githubactions-python"
1313
// Plugin is the type used by DevStream core. It's a string.
1414
type Plugin string
1515

16-
// Create implements the installation of some GitHub Actions workflows.
16+
// Create implements the create of some githubactions-python.
1717
func (p Plugin) Create(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
1818
return python.Create(options)
1919
}
2020

21-
// Update implements the installation of some GitHub Actions workflows.
21+
// Update implements the update of some githubactions-python.
2222
func (p Plugin) Update(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2323
return python.Update(options)
2424
}
2525

26-
// Read implements the healthy check of GitHub Actions workflows.
26+
// Read implements the read of githubactions-python.
2727
func (p Plugin) Read(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
2828
return python.Read(options)
2929
}
3030

31-
// Delete implements the installation of some GitHub Actions workflows.
31+
// Delete implements the delete of some githubactions-python.
3232
func (p Plugin) Delete(options configmanager.RawOptions) (bool, error) {
3333
return python.Delete(options)
3434
}

internal/pkg/configmanager/configmanager_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ pipelineTemplates:
9393
repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}}
9494
`
9595

96+
var tmpWorkDir string
97+
9698
var _ = Describe("LoadConfig", func() {
97-
var tmpWorkDir string
9899

99100
tool1 := &Tool{
100101
Name: "plugin1",
@@ -270,6 +271,28 @@ var _ = Describe("LoadConfig", func() {
270271
})
271272
})
272273

274+
var _ = Describe("getConfigFromFile", func() {
275+
BeforeEach(func() {
276+
tmpWorkDir = GinkgoT().TempDir()
277+
err := os.WriteFile(filepath.Join(tmpWorkDir, "config.yaml"), []byte(configFileStr), 0644)
278+
Expect(err).NotTo(HaveOccurred())
279+
})
280+
281+
When("get config from file", func() {
282+
It("should return a config", func() {
283+
mgr := NewManager(filepath.Join(tmpWorkDir, "config.yaml"))
284+
cfg, err := mgr.getConfigFromFile()
285+
Expect(err).NotTo(HaveOccurred())
286+
Expect(cfg.Config.State.Backend).To(Equal("local"))
287+
Expect(cfg.Vars["foo1"]).To(Equal("bar1"))
288+
Expect(len(cfg.Apps)).To(Equal(1))
289+
Expect(cfg.Apps[0].Name).To(Equal("service-a"))
290+
Expect(len(cfg.Tools)).To(Equal(2))
291+
Expect(cfg.Tools[1].Name).To(Equal("plugin2"))
292+
})
293+
})
294+
})
295+
273296
var _ = Describe("escapeBrackets", func() {
274297
When("escape brackets", func() {
275298
It("should works right", func() {

internal/pkg/configmanager/tool.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ func (t *Tool) String() string {
5555

5656
type Tools []*Tool
5757

58-
func (ts Tools) validateAll() error {
58+
func (tools Tools) validateAll() error {
5959
var errs []error
60-
errs = append(errs, ts.validate()...)
61-
errs = append(errs, ts.validateDependency()...)
60+
errs = append(errs, tools.validate()...)
61+
errs = append(errs, tools.validateDependsOnConfig()...)
6262
return multierr.Combine(errs...)
6363
}
6464

65-
func (ts Tools) validate() (errs []error) {
66-
for _, tool := range ts {
65+
func (tools Tools) validate() (errs []error) {
66+
for _, tool := range tools {
6767
errs = append(errs, tool.validate()...)
6868
}
6969
return
@@ -116,36 +116,36 @@ func (t *Tool) GetPluginMD5FileNameWithOSAndArch(os, arch string) string {
116116
return t.GetPluginNameWithOSAndArch(os, arch) + ".md5"
117117
}
118118

119-
func (ts Tools) validateDependency() []error {
120-
errors := make([]error, 0)
119+
// validateDependsOnConfig is used to validate all tools' DependsOn config
120+
func (tools Tools) validateDependsOnConfig() (retErrs []error) {
121+
retErrs = make([]error, 0)
122+
toolKeySet := make(map[string]struct{})
121123

122-
// config "set" (map)
123-
toolMap := make(map[string]bool)
124-
// creating the set
125-
for _, tool := range ts {
126-
toolMap[tool.KeyWithNameAndInstanceID()] = true
124+
// record all tools' key with name.instanceID
125+
for _, tool := range tools {
126+
toolKeySet[tool.KeyWithNameAndInstanceID()] = struct{}{}
127127
}
128128

129-
for _, tool := range ts {
130-
// no dependency, pass
129+
validateOneTool := func(tool *Tool) (errs []error) {
130+
errs = make([]error, 0)
131131
if len(tool.DependsOn) == 0 {
132-
continue
132+
return
133133
}
134-
135-
// for each dependency
136-
for _, dependency := range tool.DependsOn {
137-
// skip empty string
138-
dependency = strings.TrimSpace(dependency)
139-
if dependency == "" {
134+
for _, d := range tool.DependsOn {
135+
if strings.TrimSpace(d) == "" {
140136
continue
141137
}
142138

143-
// generate an error if the dependency isn't in the config set,
144-
if _, ok := toolMap[dependency]; !ok {
145-
errors = append(errors, fmt.Errorf("tool %s's dependency %s doesn't exist in the config", tool.InstanceID, dependency))
139+
if _, ok := toolKeySet[d]; !ok {
140+
errs = append(errs, fmt.Errorf("t %s's DependsOn %s doesn't exist", tool.InstanceID, d))
146141
}
147142
}
143+
return
144+
}
145+
146+
for _, t := range tools {
147+
retErrs = append(retErrs, validateOneTool(t)...)
148148
}
149149

150-
return errors
150+
return
151151
}

internal/pkg/configmanager/tool_test.go

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,62 @@ import (
88
. "github.com/onsi/gomega"
99
)
1010

11-
var _ = Describe("Dependency", func() {
12-
Context("singe dep", func() {
13-
tools := Tools{
14-
{InstanceID: "argocd", Name: "argocd"},
15-
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd"}},
16-
}
17-
errors := tools.validateDependency()
18-
Expect(len(errors)).To(Equal(0))
19-
})
11+
var tools Tools
2012

21-
Context("dep not exist", func() {
22-
tools := Tools{
23-
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd"}},
24-
}
25-
errors := tools.validateDependency()
26-
Expect(len(errors)).To(Equal(1))
13+
var _ = Describe("validateDependsOnConfig", func() {
14+
When("empty dep", func() {
15+
BeforeEach(func() {
16+
tools = Tools{
17+
{InstanceID: "ins-1", Name: "plugin1"},
18+
{InstanceID: "ins-2", Name: "plugin2"},
19+
}
20+
})
21+
It("should not have errors", func() {
22+
errs := tools.validateDependsOnConfig()
23+
Expect(len(errs)).To(Equal(0))
24+
})
2725
})
2826

29-
Context("multi-dep", func() {
30-
tools := Tools{
31-
{InstanceID: "argocd", Name: "argocd"},
32-
{InstanceID: "repo", Name: "github"},
33-
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{"argocd.argocd", "github.repo"}},
34-
}
35-
errors := tools.validateDependency()
36-
Expect(len(errors)).To(Equal(0))
27+
When("singe dep", func() {
28+
BeforeEach(func() {
29+
tools = Tools{
30+
{InstanceID: "ins-1", Name: "plugin1"},
31+
{InstanceID: "ins-2", Name: "plugin2"},
32+
}
33+
})
34+
It("should not have errors", func() {
35+
tools[1].DependsOn = []string{"plugin1.ins-1"}
36+
errs := tools.validateDependsOnConfig()
37+
Expect(len(errs)).To(Equal(0))
38+
})
39+
It("should has some errors", func() {
40+
tools[1].DependsOn = []string{"plugin1.ins-2"}
41+
errs := tools.validateDependsOnConfig()
42+
Expect(len(errs)).To(Equal(1))
43+
})
3744
})
3845

39-
Context("empty dep", func() {
40-
tools := Tools{
41-
{InstanceID: "argocd", Name: "argocd"},
42-
{InstanceID: "argocdapp", Name: "argocdapp", DependsOn: []string{}},
43-
}
44-
errors := tools.validateDependency()
45-
Expect(len(errors)).To(Equal(0))
46+
When("multi-dep", func() {
47+
BeforeEach(func() {
48+
tools = Tools{
49+
{InstanceID: "ins-1", Name: "plugin1"},
50+
{InstanceID: "ins-2", Name: "plugin2"},
51+
{InstanceID: "ins-3", Name: "plugin3"},
52+
}
53+
})
54+
It("should not have errors", func() {
55+
tools[2].DependsOn = []string{"plugin1.ins-1"}
56+
tools[2].DependsOn = []string{"plugin2.ins-2"}
57+
tools[1].DependsOn = []string{"plugin1.ins-1"}
58+
errs := tools.validateDependsOnConfig()
59+
Expect(len(errs)).To(Equal(0))
60+
})
61+
It("should has some errors", func() {
62+
tools[1].DependsOn = []string{"plugin1.ins-3"}
63+
tools[2].DependsOn = []string{"plugin1.ins-2", "plugin2.ins-1"}
64+
errs := tools.validateDependsOnConfig()
65+
Expect(len(errs)).To(Equal(3))
66+
})
4667
})
4768
})
4869

internal/pkg/pluginengine/change.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *Change) String() string {
3030
c.ActionName, c.Tool.Name, c.Tool.InstanceID)
3131
}
3232

33-
// execute changes in the plan in batch.
33+
// execute changes the plan in batch.
3434
// If any error occurs, it will stop executing the next batches and return the error.
3535
func execute(smgr statemanager.Manager, changes []*Change, reverse bool) map[string]error {
3636
errorsMap := make(map[string]error)

0 commit comments

Comments
 (0)