Skip to content

Commit c61a594

Browse files
daniel-hutaosteinliber
authored andcommitted
fix: configmanager ut error
Signed-off-by: Daniel Hu <tao.hu@merico.dev>
1 parent edf2339 commit c61a594

14 files changed

Lines changed: 148 additions & 99 deletions

File tree

cmd/devstream/init.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func initCMDFunc(_ *cobra.Command, _ []string) {
3737
}
3838

3939
var (
40-
tools []configmanager.Tool
40+
tools configmanager.Tools
4141
err error
4242
)
4343

@@ -67,7 +67,7 @@ func initCMDFunc(_ *cobra.Command, _ []string) {
6767
log.Success("Initialize finished.")
6868
}
6969

70-
func GetPluginsFromConfig() (tools []configmanager.Tool, err error) {
70+
func GetPluginsFromConfig() (tools configmanager.Tools, err error) {
7171
cfg, err := configmanager.NewManager(configFilePath).LoadConfig()
7272
if err != nil {
7373
return nil, err
@@ -76,7 +76,7 @@ func GetPluginsFromConfig() (tools []configmanager.Tool, err error) {
7676
return cfg.Tools, nil
7777
}
7878

79-
func GetPluginsFromFlags() (tools []configmanager.Tool, err error) {
79+
func GetPluginsFromFlags() (tools configmanager.Tools, err error) {
8080
// 1. get plugins from flags
8181
var pluginsName []string
8282
if downloadAll {
@@ -110,7 +110,7 @@ func GetPluginsFromFlags() (tools []configmanager.Tool, err error) {
110110

111111
// build the plugin list
112112
for _, pluginName := range pluginsName {
113-
tools = append(tools, configmanager.Tool{Name: pluginName})
113+
tools = append(tools, &configmanager.Tool{Name: pluginName})
114114
}
115115

116116
return tools, nil

internal/pkg/configmanager/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (a *app) generateCICDToolsFromAppConfig(templateMap map[string]string, appV
3333
return nil, err
3434
}
3535
pipelineTool.DependsOn = a.getRepoTemplateDependants()
36-
tools = append(tools, *pipelineTool)
36+
tools = append(tools, pipelineTool)
3737
}
3838
return tools, nil
3939
}

internal/pkg/configmanager/config.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Config) getToolsWithVarsFromApp(a app) (Tools, error) {
3737
return nil, err
3838
}
3939

40-
// 1. render appStr with globalVars
40+
// 1. render appStr with Vars
4141
appRenderStr, err := renderConfigWithVariables(string(appStr), c.Vars)
4242
if err != nil {
4343
log.Debugf("configmanager/app %s render globalVars %+v failed", appRenderStr, c.Vars)
@@ -70,7 +70,7 @@ func (c *Config) getToolsWithVarsFromApp(a app) (Tools, error) {
7070
return nil, fmt.Errorf("app[%s] get pipeline tools failed: %w", rawApp.Name, err)
7171
}
7272
if repoScaffoldingTool != nil {
73-
tools = append(tools, *repoScaffoldingTool)
73+
tools = append(tools, repoScaffoldingTool)
7474
}
7575

7676
log.Debugf("Have got %d tools from app %s.", len(tools), rawApp.Name)
@@ -81,6 +81,26 @@ func (c *Config) getToolsWithVarsFromApp(a app) (Tools, error) {
8181
return tools, nil
8282
}
8383

84+
func (c *Config) renderToolsWithVars() error {
85+
toolsStr, err := yaml.Marshal(c.Tools)
86+
if err != nil {
87+
return err
88+
}
89+
90+
toolsStrWithVars, err := renderConfigWithVariables(string(toolsStr), c.Vars)
91+
if err != nil {
92+
return err
93+
}
94+
95+
var tools Tools
96+
if err = yaml.Unmarshal(toolsStrWithVars, &tools); err != nil {
97+
return err
98+
}
99+
c.Tools = tools
100+
101+
return nil
102+
}
103+
84104
func (c *Config) renderPipelineTemplateMap() error {
85105
c.pipelineTemplateMap = make(map[string]string)
86106
for _, pt := range c.PipelineTemplates {
@@ -93,6 +113,12 @@ func (c *Config) renderPipelineTemplateMap() error {
93113
return nil
94114
}
95115

116+
func (c *Config) renderInstanceIDtoOptions() {
117+
for _, t := range c.Tools {
118+
t.Options["instanceID"] = t.InstanceID
119+
}
120+
}
121+
96122
func (c *Config) validate() error {
97123
if c.Config.State == nil {
98124
return fmt.Errorf("state is not defined")

internal/pkg/configmanager/configmanager.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"io"
66
"os"
7+
"regexp"
78

89
"gopkg.in/yaml.v3"
910

@@ -39,7 +40,13 @@ func (m *Manager) LoadConfig() (*Config, error) {
3940
if err != nil {
4041
return nil, err
4142
}
43+
44+
err = c.renderToolsWithVars()
45+
if err != nil {
46+
return nil, err
47+
}
4248
c.Tools = append(c.Tools, appTools...)
49+
c.renderInstanceIDtoOptions()
4350

4451
// step 3
4552
if err = c.validate(); err != nil {
@@ -51,18 +58,24 @@ func (m *Manager) LoadConfig() (*Config, error) {
5158

5259
// getConfigFromFile gets Config from the config file specified by Manager.ConfigFilePath
5360
func (m *Manager) getConfigFromFile() (*Config, error) {
54-
// 1. read the original config file
5561
configBytes, err := os.ReadFile(m.ConfigFilePath)
5662
if err != nil {
5763
return nil, err
5864
}
5965

60-
// 2. decode total yaml files
66+
configBytesEscaped := escapeBrackets(configBytes)
67+
6168
var c Config
62-
if err = yaml.Unmarshal(configBytes, &c); err != nil && !errors.Is(err, io.EOF) {
69+
if err = yaml.Unmarshal(configBytesEscaped, &c); err != nil && !errors.Is(err, io.EOF) {
6370
log.Errorf("Please verify the format of your config. Error: %s.", err)
6471
return nil, err
6572
}
6673

6774
return &c, nil
6875
}
76+
77+
// escapeBrackets is used to escape []byte(": [[xxx]]xxx\n") to []byte(": \"[[xxx]]\"xxx\n")
78+
func escapeBrackets(param []byte) []byte {
79+
re := regexp.MustCompile(`([^:]+:)(\s*)(\[\[[^\]]+\]\][^\s]*)`)
80+
return re.ReplaceAll(param, []byte("$1$2\"$3\""))
81+
}

internal/pkg/configmanager/configmanager_test.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package configmanager_test
1+
package configmanager
22

33
import (
44
"os"
55
"path/filepath"
66

77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
9-
10-
"github.com/devstream-io/devstream/internal/pkg/configmanager"
119
)
1210

1311
const configFileStr = `---
@@ -98,35 +96,38 @@ pipelineTemplates:
9896
var _ = Describe("LoadConfig", func() {
9997
var tmpWorkDir string
10098

101-
tool1 := configmanager.Tool{
99+
tool1 := &Tool{
102100
Name: "plugin1",
103101
InstanceID: "default",
104-
Options: configmanager.RawOptions{
102+
DependsOn: []string{},
103+
Options: RawOptions{
105104
"foo1": "bar1",
106105
"instanceID": "default",
107106
},
108107
}
109108

110-
tool2 := configmanager.Tool{
109+
tool2 := &Tool{
111110
Name: "plugin2",
112111
InstanceID: "tluafed",
113-
Options: configmanager.RawOptions{
112+
DependsOn: []string{},
113+
Options: RawOptions{
114+
"instanceID": "tluafed",
114115
"foo": "bar",
115116
"foo2": "bar2",
116-
"instanceID": "default1",
117117
},
118118
}
119119

120-
tool3 := configmanager.Tool{
120+
tool3 := &Tool{
121121
Name: "github-actions",
122122
InstanceID: "service-a",
123123
DependsOn: []string{
124124
"repo-scaffolding.service-a",
125125
},
126-
Options: configmanager.RawOptions{
127-
"pipeline": configmanager.RawOptions{
128-
"docker": configmanager.RawOptions{
129-
"registry": configmanager.RawOptions{
126+
Options: RawOptions{
127+
"instanceID": "service-a",
128+
"pipeline": RawOptions{
129+
"docker": RawOptions{
130+
"registry": RawOptions{
130131
"repository": "service-a",
131132
"type": "dockerhub",
132133
"username": "dockerUser1",
@@ -135,81 +136,81 @@ var _ = Describe("LoadConfig", func() {
135136
"branch": "main",
136137
"configLocation": "git@github.com:devstream-io/ci-template.git//github-actions",
137138
},
138-
"scm": configmanager.RawOptions{
139+
"scm": RawOptions{
139140
"apiURL": "gitlab.com/some/path/to/your/api",
140141
"owner": "devstream-io",
141142
"org": "devstream-io",
142143
"name": "service-a",
143144
"scmType": "github",
144145
"url": "https://github.com/devstream-io/service-a",
145146
},
146-
"instanceID": "service-a",
147147
},
148148
}
149149

150-
tool4 := configmanager.Tool{
150+
tool4 := &Tool{
151151
Name: "argocdapp",
152152
InstanceID: "service-a",
153153
DependsOn: []string{
154154
"repo-scaffolding.service-a",
155155
},
156-
Options: configmanager.RawOptions{
157-
"pipeline": configmanager.RawOptions{
158-
"destination": configmanager.RawOptions{
156+
Options: RawOptions{
157+
"instanceID": "service-a",
158+
"pipeline": RawOptions{
159+
"destination": RawOptions{
159160
"namespace": "devstream-io",
160161
"server": "https://kubernetes.default.svc",
161162
},
162-
"app": configmanager.RawOptions{
163+
"app": RawOptions{
163164
"namespace": "argocd",
164165
},
165-
"source": configmanager.RawOptions{
166+
"source": RawOptions{
166167
"valuefile": "values.yaml",
167168
"path": "helm/service-a",
168169
"repoURL": "${{repo-scaffolding.myapp.outputs.repoURL}}",
169170
},
170171
"configLocation": "",
171172
},
172-
"scm": configmanager.RawOptions{
173+
"scm": RawOptions{
173174
"url": "https://github.com/devstream-io/service-a",
174175
"apiURL": "gitlab.com/some/path/to/your/api",
175176
"owner": "devstream-io",
176177
"org": "devstream-io",
177178
"name": "service-a",
178179
"scmType": "github",
179180
},
180-
"instanceID": "service-a",
181181
},
182182
}
183183

184-
tool5 := configmanager.Tool{
184+
tool5 := &Tool{
185185
Name: "repo-scaffolding",
186186
InstanceID: "service-a",
187-
Options: configmanager.RawOptions{
188-
"destinationRepo": configmanager.RawOptions{
187+
DependsOn: []string{},
188+
Options: RawOptions{
189+
"instanceID": "service-a",
190+
"destinationRepo": RawOptions{
189191
"needAuth": true,
190192
"org": "devstream-io",
191193
"repo": "service-a",
192194
"branch": "main",
193195
"repoType": "github",
194196
"url": "github.com/devstream-io/service-a",
195197
},
196-
"sourceRepo": configmanager.RawOptions{
198+
"sourceRepo": RawOptions{
197199
"repoType": "github",
198200
"url": "github.com/devstream-io/dtm-scaffolding-golang",
199201
"needAuth": true,
200202
"org": "devstream-io",
201203
"repo": "dtm-scaffolding-golang",
202204
"branch": "main",
203205
},
204-
"vars": configmanager.RawOptions{
206+
"vars": RawOptions{
205207
"foo1": "bar1",
206208
"foo2": "bar2",
207209
"registryType": "dockerhub",
208210
"framework": "django",
209211
"language": "python",
210212
"argocdNamespace": "argocd",
211213
},
212-
"instanceID": "service-a",
213214
},
214215
}
215216

@@ -221,17 +222,17 @@ var _ = Describe("LoadConfig", func() {
221222

222223
When("load a config file", func() {
223224
It("should return 5 tools", func() {
224-
mgr := configmanager.NewManager(filepath.Join(tmpWorkDir, "config.yaml"))
225+
mgr := NewManager(filepath.Join(tmpWorkDir, "config.yaml"))
225226
cfg, err := mgr.LoadConfig()
226227
Expect(err).NotTo(HaveOccurred())
227228
Expect(cfg).NotTo(BeNil())
228229

229230
GinkgoWriter.Printf("Config: %v", cfg)
230231

231232
// config/state
232-
Expect(*cfg.Config.State).To(Equal(configmanager.State{
233+
Expect(*cfg.Config.State).To(Equal(State{
233234
Backend: "local",
234-
Options: configmanager.StateConfigOptions{
235+
Options: StateConfigOptions{
235236
StateFile: "devstream.state",
236237
},
237238
}))
@@ -261,3 +262,21 @@ var _ = Describe("LoadConfig", func() {
261262
})
262263
})
263264
})
265+
266+
var _ = Describe("escapeBrackets", func() {
267+
When("escape brackets", func() {
268+
It("should works right", func() {
269+
testStrBytes1 := "foo: [[ foo ]]\n"
270+
testStr2 := "foo: xx[[ foo ]]\n"
271+
testStr3 := "foo: [[ foo ]]xx\n"
272+
273+
retStr1 := escapeBrackets([]byte(testStrBytes1))
274+
retStr2 := escapeBrackets([]byte(testStr2))
275+
retStr3 := escapeBrackets([]byte(testStr3))
276+
277+
Expect(string(retStr1)).To(Equal("foo: \"[[ foo ]]\"\n"))
278+
Expect(string(retStr2)).To(Equal("foo: xx[[ foo ]]\n"))
279+
Expect(string(retStr3)).To(Equal("foo: \"[[ foo ]]xx\"\n"))
280+
})
281+
})
282+
})

internal/pkg/configmanager/pipelineTemplate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ var _ = Describe("PipelineTemplate struct", func() {
236236
Expect(tool).Should(Equal(&Tool{
237237
Name: t.Type,
238238
InstanceID: appName,
239+
DependsOn: []string{},
239240
Options: opts,
240241
}))
241242
})

0 commit comments

Comments
 (0)