|
1 | 1 | package configmanager_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + |
4 | 7 | . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "github.com/devstream-io/devstream/internal/pkg/configmanager" |
5 | 11 | ) |
6 | 12 |
|
| 13 | +const configFileStr = `--- |
| 14 | +config: |
| 15 | + state: |
| 16 | + backend: local |
| 17 | + options: |
| 18 | + stateFile: devstream.state |
| 19 | +
|
| 20 | +vars: |
| 21 | + foo1: bar1 |
| 22 | + foo2: bar2 |
| 23 | + registryType: dockerhub |
| 24 | + argocdNamespace: argocd |
| 25 | +
|
| 26 | +apps: |
| 27 | +- name: service-a |
| 28 | + spec: |
| 29 | + language: python |
| 30 | + framework: django |
| 31 | + repo: |
| 32 | + scmType: github |
| 33 | + owner: devstream-io |
| 34 | + org: devstream-io # choose between owner and org |
| 35 | + url: github.com/devstream-io/service-a # optional,if url is specified,we can infer scm/owner/org/name from url |
| 36 | + apiURL: gitlab.com/some/path/to/your/api # optional, if you want to create a repo from repo template |
| 37 | + # if repoTemplate is not empty,we could help user to create repo from scaffoldingRepo |
| 38 | + repoTemplate: # optional |
| 39 | + scmType: github |
| 40 | + owner: devstream-io |
| 41 | + org: devstream-io # choose between owner and org |
| 42 | + name: dtm-scaffolding-golang |
| 43 | + url: github.com/devstream-io/dtm-scaffolding-golang # optional,if url is specified,we can infer scm/owner/org/name from url |
| 44 | + ci: |
| 45 | + - type: template |
| 46 | + templateName: ci-pipeline-for-gh-actions |
| 47 | + options: # overwrite options in pipelineTemplates |
| 48 | + docker: |
| 49 | + registry: |
| 50 | + type: [[ registryType ]] # while overridden, use global variables |
| 51 | + vars: # optional, use to render vars in template(valid only if the ci.type is template) |
| 52 | + dockerUser: dockerUser1 |
| 53 | + app: service-a |
| 54 | + cd: |
| 55 | + - type: template |
| 56 | + templateName: cd-pipeline-for-argocdapp |
| 57 | + options: # overwrite options in pipelineTemplates |
| 58 | + destination: |
| 59 | + namespace: devstream-io |
| 60 | + vars: # optional, use to render vars in template(valid only if the cd.type is template) |
| 61 | + app: service-a |
| 62 | +
|
| 63 | +tools: |
| 64 | +- name: plugin1 |
| 65 | + instanceID: default |
| 66 | + options: |
| 67 | + foo1: [[ foo1 ]] |
| 68 | +- name: plugin2 |
| 69 | + instanceID: tluafed |
| 70 | + options: |
| 71 | + foo: bar |
| 72 | + foo2: [[ foo2 ]] |
| 73 | +
|
| 74 | +pipelineTemplates: |
| 75 | +- name: ci-pipeline-for-gh-actions |
| 76 | + type: github-actions # corresponding to a plugin |
| 77 | + options: |
| 78 | + branch: main # optional, default is main |
| 79 | + docker: |
| 80 | + registry: |
| 81 | + type: dockerhub |
| 82 | + username: [[ dockerUser ]] |
| 83 | + repository: [[ app ]] |
| 84 | +- name: cd-pipeline-for-argocdapp |
| 85 | + type: argocdapp |
| 86 | + options: |
| 87 | + app: |
| 88 | + namespace: [[ argocdNamespace ]] # you can use global vars in templates |
| 89 | + destination: |
| 90 | + server: https://kubernetes.default.svc |
| 91 | + namespace: default |
| 92 | + source: |
| 93 | + valuefile: values.yaml |
| 94 | + path: helm/[[ app ]] |
| 95 | + repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}} |
| 96 | +` |
| 97 | + |
7 | 98 | var _ = Describe("LoadConfig", func() { |
| 99 | + var tmpWorkDir string |
| 100 | + |
| 101 | + tool1 := configmanager.Tool{ |
| 102 | + Name: "plugin1", |
| 103 | + InstanceID: "default", |
| 104 | + Options: configmanager.RawOptions{ |
| 105 | + "foo1": "bar1", |
| 106 | + "instanceID": "default", |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + tool2 := configmanager.Tool{ |
| 111 | + Name: "plugin2", |
| 112 | + InstanceID: "tluafed", |
| 113 | + Options: configmanager.RawOptions{ |
| 114 | + "foo": "bar", |
| 115 | + "foo2": "bar2", |
| 116 | + "instanceID": "default1", |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + tool3 := configmanager.Tool{ |
| 121 | + Name: "github-actions", |
| 122 | + InstanceID: "service-a", |
| 123 | + DependsOn: []string{ |
| 124 | + "repo-scaffolding.service-a", |
| 125 | + }, |
| 126 | + Options: configmanager.RawOptions{ |
| 127 | + "pipeline": configmanager.RawOptions{ |
| 128 | + "docker": configmanager.RawOptions{ |
| 129 | + "registry": configmanager.RawOptions{ |
| 130 | + "repository": "service-a", |
| 131 | + "type": "dockerhub", |
| 132 | + "username": "dockerUser1", |
| 133 | + }, |
| 134 | + }, |
| 135 | + "branch": "main", |
| 136 | + "configLocation": "git@github.com:devstream-io/ci-template.git//github-actions", |
| 137 | + }, |
| 138 | + "scm": configmanager.RawOptions{ |
| 139 | + "apiURL": "gitlab.com/some/path/to/your/api", |
| 140 | + "owner": "devstream-io", |
| 141 | + "org": "devstream-io", |
| 142 | + "name": "service-a", |
| 143 | + "scmType": "github", |
| 144 | + "url": "https://github.com/devstream-io/service-a", |
| 145 | + }, |
| 146 | + "instanceID": "service-a", |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + tool4 := configmanager.Tool{ |
| 151 | + Name: "argocdapp", |
| 152 | + InstanceID: "service-a", |
| 153 | + DependsOn: []string{ |
| 154 | + "repo-scaffolding.service-a", |
| 155 | + }, |
| 156 | + Options: configmanager.RawOptions{ |
| 157 | + "pipeline": configmanager.RawOptions{ |
| 158 | + "destination": configmanager.RawOptions{ |
| 159 | + "namespace": "devstream-io", |
| 160 | + "server": "https://kubernetes.default.svc", |
| 161 | + }, |
| 162 | + "app": configmanager.RawOptions{ |
| 163 | + "namespace": "argocd", |
| 164 | + }, |
| 165 | + "source": configmanager.RawOptions{ |
| 166 | + "valuefile": "values.yaml", |
| 167 | + "path": "helm/service-a", |
| 168 | + "repoURL": "${{repo-scaffolding.myapp.outputs.repoURL}}", |
| 169 | + }, |
| 170 | + "configLocation": "", |
| 171 | + }, |
| 172 | + "scm": configmanager.RawOptions{ |
| 173 | + "url": "https://github.com/devstream-io/service-a", |
| 174 | + "apiURL": "gitlab.com/some/path/to/your/api", |
| 175 | + "owner": "devstream-io", |
| 176 | + "org": "devstream-io", |
| 177 | + "name": "service-a", |
| 178 | + "scmType": "github", |
| 179 | + }, |
| 180 | + "instanceID": "service-a", |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + tool5 := configmanager.Tool{ |
| 185 | + Name: "repo-scaffolding", |
| 186 | + InstanceID: "service-a", |
| 187 | + Options: configmanager.RawOptions{ |
| 188 | + "destinationRepo": configmanager.RawOptions{ |
| 189 | + "needAuth": true, |
| 190 | + "org": "devstream-io", |
| 191 | + "repo": "service-a", |
| 192 | + "branch": "main", |
| 193 | + "repoType": "github", |
| 194 | + "url": "github.com/devstream-io/service-a", |
| 195 | + }, |
| 196 | + "sourceRepo": configmanager.RawOptions{ |
| 197 | + "repoType": "github", |
| 198 | + "url": "github.com/devstream-io/dtm-scaffolding-golang", |
| 199 | + "needAuth": true, |
| 200 | + "org": "devstream-io", |
| 201 | + "repo": "dtm-scaffolding-golang", |
| 202 | + "branch": "main", |
| 203 | + }, |
| 204 | + "vars": configmanager.RawOptions{ |
| 205 | + "foo1": "bar1", |
| 206 | + "foo2": "bar2", |
| 207 | + "registryType": "dockerhub", |
| 208 | + "framework": "django", |
| 209 | + "language": "python", |
| 210 | + "argocdNamespace": "argocd", |
| 211 | + }, |
| 212 | + "instanceID": "service-a", |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + BeforeEach(func() { |
| 217 | + tmpWorkDir = GinkgoT().TempDir() |
| 218 | + err := os.WriteFile(filepath.Join(tmpWorkDir, "config.yaml"), []byte(configFileStr), 0644) |
| 219 | + Expect(err).NotTo(HaveOccurred()) |
| 220 | + }) |
| 221 | + |
| 222 | + When("load a config file", func() { |
| 223 | + It("should return 5 tools", func() { |
| 224 | + mgr := configmanager.NewManager(filepath.Join(tmpWorkDir, "config.yaml")) |
| 225 | + cfg, err := mgr.LoadConfig() |
| 226 | + Expect(err).NotTo(HaveOccurred()) |
| 227 | + Expect(cfg).NotTo(BeNil()) |
| 228 | + |
| 229 | + GinkgoWriter.Printf("Config: %v", cfg) |
| 230 | + |
| 231 | + // config/state |
| 232 | + Expect(*cfg.Config.State).To(Equal(configmanager.State{ |
| 233 | + Backend: "local", |
| 234 | + Options: configmanager.StateConfigOptions{ |
| 235 | + StateFile: "devstream.state", |
| 236 | + }, |
| 237 | + })) |
| 238 | + |
| 239 | + // vars |
| 240 | + Expect(len(cfg.Vars)).To(Equal(4)) |
| 241 | + Expect(cfg.Vars["foo1"]).To(Equal("bar1")) |
8 | 242 |
|
| 243 | + // tools |
| 244 | + Expect(len(cfg.Tools)).To(Equal(5)) |
| 245 | + for _, t := range cfg.Tools { |
| 246 | + switch t.Name { |
| 247 | + case "plugin1": |
| 248 | + Expect(t).Should(Equal(tool1)) |
| 249 | + case "plugin2": |
| 250 | + Expect(t).Should(Equal(tool2)) |
| 251 | + case "github-actions": |
| 252 | + Expect(t).Should(Equal(tool3)) |
| 253 | + case "argocdapp": |
| 254 | + Expect(t).Should(Equal(tool4)) |
| 255 | + case "repo-scaffolding": |
| 256 | + Expect(t).Should(Equal(tool5)) |
| 257 | + default: |
| 258 | + Fail("Unexpected plugin name.") |
| 259 | + } |
| 260 | + } |
| 261 | + }) |
| 262 | + }) |
9 | 263 | }) |
0 commit comments