Skip to content

Commit 882a16b

Browse files
authored
Merge pull request #1247 from merico-dev/test-ht
Refactor: Remove `types` Package(vars.go and error.go) in `backend` Package
2 parents f3ecd89 + 0f1a728 commit 882a16b

7 files changed

Lines changed: 50 additions & 95 deletions

File tree

internal/pkg/backend/backend.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package backend
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/devstream-io/devstream/internal/pkg/backend/k8s"
78
"github.com/devstream-io/devstream/internal/pkg/backend/local"
89
"github.com/devstream-io/devstream/internal/pkg/backend/s3"
9-
"github.com/devstream-io/devstream/internal/pkg/backend/types"
1010
"github.com/devstream-io/devstream/internal/pkg/configmanager"
1111
)
1212

@@ -18,18 +18,27 @@ type Backend interface {
1818
Write(data []byte) error
1919
}
2020

21+
type Type string
22+
23+
const (
24+
LocalBackend Type = "local"
25+
S3Backend Type = "s3"
26+
K8sBackend Type = "k8s"
27+
K8sBackendAlias Type = "kubernetes"
28+
)
29+
2130
// GetBackend will return a Backend by the given name.
2231
func GetBackend(state configmanager.State) (Backend, error) {
23-
typeName := types.Type(strings.ToLower(state.Backend))
32+
typeName := Type(strings.ToLower(string(state.Backend)))
2433

2534
switch typeName {
26-
case types.Local:
35+
case LocalBackend:
2736
return local.NewLocal(state.Options.StateFile)
28-
case types.S3:
37+
case S3Backend:
2938
return s3.NewS3Backend(state.Options.Bucket, state.Options.Region, state.Options.Key)
30-
case types.K8s, types.K8sAlias:
39+
case K8sBackend, K8sBackendAlias:
3140
return k8s.NewBackend(state.Options.Namespace, state.Options.ConfigMap)
3241
default:
33-
return nil, types.NewInvalidBackendErr(state.Backend)
42+
return nil, fmt.Errorf("the backend type < %s > is illegal", state.Backend)
3443
}
3544
}

internal/pkg/backend/backend_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/devstream-io/devstream/internal/pkg/backend"
1010
"github.com/devstream-io/devstream/internal/pkg/backend/local"
11-
"github.com/devstream-io/devstream/internal/pkg/backend/types"
1211
"github.com/devstream-io/devstream/internal/pkg/configmanager"
1312
)
1413

@@ -32,7 +31,6 @@ var _ = Describe("GetBackend", func() {
3231
state := configmanager.State{Backend: "not_exist_plug"}
3332
_, err := backend.GetBackend(state)
3433
Expect(err).Error().Should(HaveOccurred())
35-
Expect(types.IsInvalidBackendErr(err)).To(BeTrue())
3634
})
3735
})
3836

@@ -41,7 +39,6 @@ var _ = Describe("GetBackend", func() {
4139
state := configmanager.State{Backend: "s3"}
4240
_, err := backend.GetBackend(state)
4341
Expect(err).Error().Should(HaveOccurred())
44-
Expect(types.IsBackendOptionErr(err)).To(BeTrue())
4542
})
4643
})
4744
})

internal/pkg/backend/s3/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package s3
33
import (
44
"fmt"
55

6-
"github.com/devstream-io/devstream/internal/pkg/backend/types"
76
"github.com/devstream-io/devstream/pkg/util/log"
87
)
98

@@ -23,7 +22,7 @@ func validate(bucket, region, key string) error {
2322
for _, err := range errs {
2423
log.Error(err)
2524
}
26-
return types.NewBackendOptionErr(types.S3)
25+
return fmt.Errorf("s3 Backend config error")
2726
}
2827

2928
return nil

internal/pkg/backend/s3/validate_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ package s3
33
import (
44
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
6-
7-
"github.com/devstream-io/devstream/internal/pkg/backend/types"
86
)
97

108
var _ = Describe("validate", func() {
119
It("should return error s3 option not config", func() {
1210
err := validate("", "", "")
1311
Expect(err).Error().Should(HaveOccurred())
14-
Expect(types.IsBackendOptionErr(err)).To(BeTrue())
1512
})
1613

1714
It("should return true if config s3 valid", func() {

internal/pkg/backend/types/error.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

internal/pkg/backend/types/vars.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

internal/pkg/configmanager/configmanager_test.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,7 @@ import (
1111
"github.com/devstream-io/devstream/internal/pkg/configmanager"
1212
)
1313

14-
var _ = Describe("LoadConfig", func() {
15-
const (
16-
mainConfigFile = "config.yaml"
17-
varFile = "var.yaml"
18-
toolFile = "tool.yaml"
19-
appFile = "app.yaml"
20-
templateFile = "template.yaml"
21-
pluginDir = "./plugins"
22-
)
23-
24-
var tmpDir string
25-
var mainConfig string
26-
const appConfig = `apps:
14+
const appConfig = `apps:
2715
- name: service-A
2816
spec:
2917
language: python
@@ -60,7 +48,7 @@ var _ = Describe("LoadConfig", func() {
6048
vars: # optional, use to render vars in template(valid only if the cd.type is template)
6149
app: service-A
6250
`
63-
const toolConfig = `tools:
51+
const toolConfig = `tools:
6452
- name: plugin1
6553
instanceID: default
6654
options:
@@ -71,11 +59,11 @@ var _ = Describe("LoadConfig", func() {
7159
key1: value1
7260
key2: [[ var2 ]]
7361
`
74-
const varConfig = `var2: value-of-var2
62+
const varConfig = `var2: value-of-var2
7563
var3: dockerhub-overwrite
7664
argocdNamespace: argocd
7765
`
78-
const templateConfig = `pipelineTemplates:
66+
const templateConfig = `pipelineTemplates:
7967
- name: ci-pipeline-1
8068
type: github-actions # corresponding to a plugin
8169
options:
@@ -98,19 +86,31 @@ argocdNamespace: argocd
9886
path: helm/[[ app ]]
9987
repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}}
10088
`
101-
var (
102-
state = &configmanager.State{
103-
Backend: "local",
104-
Options: configmanager.StateConfigOptions{
105-
StateFile: "devstream.state",
106-
},
107-
}
10889

109-
expectedConfig = &configmanager.Config{
110-
PluginDir: pluginDir,
111-
State: state,
112-
}
113-
)
90+
const (
91+
mainConfigFile = "config.yaml"
92+
varFile = "var.yaml"
93+
toolFile = "tool.yaml"
94+
appFile = "app.yaml"
95+
templateFile = "template.yaml"
96+
pluginDir = "./plugins"
97+
)
98+
99+
var _ = Describe("LoadConfig", func() {
100+
var tmpDir string
101+
var mainConfig string
102+
var state = &configmanager.State{
103+
Backend: "local",
104+
Options: configmanager.StateConfigOptions{
105+
StateFile: "devstream.state",
106+
},
107+
}
108+
109+
var expectedConfig = &configmanager.Config{
110+
PluginDir: pluginDir,
111+
State: state,
112+
}
113+
114114
BeforeEach(func() {
115115
// create files
116116
tmpDir = GinkgoT().TempDir()
@@ -125,6 +125,7 @@ argocdNamespace: argocd
125125
Expect(err).Should(Succeed())
126126
}
127127
})
128+
128129
When("only with config file", func() {
129130
BeforeEach(func() {
130131
mainConfig = fmt.Sprintf(`# main config
@@ -271,6 +272,7 @@ var1: value-of-var1 # var1 is a global var
271272
Expect(config.Tools[4]).Should(Equal(expectedTools5))
272273
})
273274
})
275+
274276
When("global file and configFile all has config", func() {
275277
BeforeEach(func() {
276278
mainConfig = fmt.Sprintf(`# main config
@@ -306,6 +308,7 @@ tools:
306308
Expect(len(config.Tools)).Should(Equal(4))
307309
})
308310
})
311+
309312
When("with global config", func() {
310313
BeforeEach(func() {
311314
mainConfig = `
@@ -397,7 +400,6 @@ tools:
397400
Expect(len(config.Tools)).Should(Equal(3))
398401
})
399402
})
400-
401403
})
402404

403405
var _ = Describe("Manager struct", func() {
@@ -434,6 +436,7 @@ tools:
434436
Expect(err.Error()).Should(ContainSubstring("no such file or directory"))
435437
})
436438
})
439+
437440
When("GetGlobalVars failed", func() {
438441
BeforeEach(func() {
439442
err := os.WriteFile(fLoc, []byte(`
@@ -449,6 +452,7 @@ state:
449452
Expect(err).Error().Should(HaveOccurred())
450453
})
451454
})
455+
452456
When("GetTools failed", func() {
453457
BeforeEach(func() {
454458
err := os.WriteFile(fLoc, []byte(`
@@ -464,6 +468,7 @@ state:
464468
Expect(err).Error().Should(HaveOccurred())
465469
})
466470
})
471+
467472
When("getApps failed", func() {
468473
BeforeEach(func() {
469474
err := os.WriteFile(fLoc, []byte(`

0 commit comments

Comments
 (0)