-
Notifications
You must be signed in to change notification settings - Fork 7
/
dependency_test.go
114 lines (94 loc) · 2.85 KB
/
dependency_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package dev_test
import (
"fmt"
"testing"
"github.com/docker/docker/api/types"
"github.com/spf13/afero"
"github.com/wish/dev"
"github.com/wish/dev/cmd"
"github.com/wish/dev/config"
"github.com/wish/dev/test"
"gotest.tools/v3/env"
)
var orderCalled int
type MockDep struct {
Name string
Type string
Order int
ProjectConfig *config.Project
RegistryConfig *config.Registry
NetworkConfig *types.NetworkCreate
}
func (md *MockDep) PreRun(command string, appConfig *config.Dev, project *dev.Project) {
md.Order = orderCalled
orderCalled++
}
func (md *MockDep) Dependencies() []string {
switch md.Type {
case "network":
return []string{}
case "registry":
return []string{}
case "project":
return md.ProjectConfig.Dependencies
case "default":
fmt.Println("unsupported dependency type")
}
return []string{}
}
func (md *MockDep) GetName() string {
return md.Name
}
func createObjectMap(devConfig *config.Dev, t *testing.T) (map[string]dev.Dependency, map[string]*MockDep) {
objMap := make(map[string]dev.Dependency)
depMap := make(map[string]*MockDep)
for name, opts := range devConfig.Projects {
dep := &MockDep{Name: name, Type: "project", ProjectConfig: opts, Order: -1}
objMap[name] = dep
depMap[name] = dep
}
for name, opts := range devConfig.Networks {
dep := &MockDep{Name: name, Type: "network", NetworkConfig: opts, Order: -1}
objMap[name] = dep
depMap[name] = dep
}
for name, opts := range devConfig.Registries {
dep := &MockDep{Name: name, Type: "registry", RegistryConfig: opts, Order: -1}
objMap[name] = dep
depMap[name] = dep
}
return objMap, depMap
}
func TestInitDeps(t *testing.T) {
defer env.Patch(t, "DEV_CONFIG", "/home/test/.dev.yaml")()
defer env.Patch(t, "PATH", "/usr/bin:/usr/local/bin:/sbin")()
cmd.Reset()
cmd.AppConfig.SetFs(afero.NewMemMapFs())
test.CreateDockerComposeBinary(cmd.AppConfig.GetFs(), "/usr/local/bin")
test.CreateConfigFile(cmd.AppConfig.GetFs(), test.SmallCoConfig, "/home/test/.dev.yaml")
cmd.Initialize()
objMap, depMap := createObjectMap(cmd.AppConfig, t)
proj := dev.NewProject(cmd.AppConfig.Projects["postgresql"])
dev.InitDeps(objMap, cmd.AppConfig, "UP", proj)
sharedDep := depMap["shared"]
if sharedDep.Order == -1 {
t.Errorf("shared dependency not initialized error")
}
if sharedDep.Order != 2 {
t.Errorf("Expected order of initialization of shared to be 2, but got %d", sharedDep.Order)
}
appNet := depMap["app-net"]
if appNet.Order == -1 {
t.Errorf("app-net dependency not initialized error")
}
if appNet.Order > 1 {
t.Errorf("Expected order of initialization of app-net to be <= 1 , but got %d", appNet.Order)
}
ecr := depMap["ecr"]
if ecr.Order == -1 {
t.Errorf("app-net dependency not initialized error")
}
if ecr.Order > 1 {
t.Errorf("Expected order of initialization of ecr to be <= 1 , but got %d", ecr.Order)
}
}