-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
223 lines (208 loc) · 5.35 KB
/
config_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package glx_test
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/spf13/afero"
"github.com/srvc/glx"
)
func TestNewConfig(t *testing.T) {
yaml1 := `
<: &accounts-k8s
name: accounts
hostname: accounts.glx.local
kubernetes:
context: k8s.srvc.tools
namespace: accounts
labels:
role: api
ports:
80: 8080
<: &admin-k8s
name: admin
hostname: admin.glx.local
kubernetes:
context: k8s.srvc.tools
namespace: admin
labels:
role: web
ports:
80: 3000
`
yaml2 := `
<: &blog-local
name: blog
hostname: blog.glx.local
local:
port_env:
PORT: 80
path: example.com/blog
cmd: ["go", "run", "./cmd/server"]
`
yaml3 := `
<: &admin-docker
name: admin
hostname: admin.glx.local
docker:
path: example.com/admin
build:
dockerfile: ./dev.dockerfile
run:
cmd: ["bin/rails", "s"]
volumes:
- .:/app
- bundle:/usr/local/bundle
- tmp:/app/tmp
- log:/app/log
port_envs:
- PORT: 80
`
yaml4 := `
root: /Users/glx/src
projects:
- name: blog
apps:
- *blog-local
- *admin-k8s
- *accounts-k8s
- name: admin
apps:
- *blog-local
- *admin-docker
- *accounts-k8s
`
wantCfg := &glx.Config{
Root: "/Users/glx/src",
Projects: []*glx.Project{
{
Name: "blog",
Apps: []*glx.App{
{
Name: "blog",
Hostname: "blog.glx.local",
Local: &glx.LocalApp{
PortEnv: map[string]glx.Port{"PORT": glx.Port(80)},
Path: "example.com/blog",
Cmd: []string{"go", "run", "./cmd/server"},
},
},
{
Name: "admin",
Hostname: "admin.glx.local",
Kubernetes: &glx.KubernetesApp{
Context: "k8s.srvc.tools",
Namespace: "admin",
Labels: map[string]string{"role": "web"},
Ports: map[glx.Port]glx.Port{80: 3000},
},
},
{
Name: "accounts",
Hostname: "accounts.glx.local",
Kubernetes: &glx.KubernetesApp{
Context: "k8s.srvc.tools",
Namespace: "accounts",
Labels: map[string]string{"role": "api"},
Ports: map[glx.Port]glx.Port{80: 8080},
},
},
},
},
{
Name: "admin",
Apps: []*glx.App{
{
Name: "blog",
Hostname: "blog.glx.local",
Local: &glx.LocalApp{
PortEnv: map[string]glx.Port{"PORT": glx.Port(80)},
Path: "example.com/blog",
Cmd: []string{"go", "run", "./cmd/server"},
},
},
{
Name: "admin",
Hostname: "admin.glx.local",
Docker: &glx.DockerApp{
Ports: []glx.Port{80},
Path: "example.com/admin",
Cmd: []string{"bin/rails", "s"},
},
},
{
Name: "accounts",
Hostname: "accounts.glx.local",
Kubernetes: &glx.KubernetesApp{
Context: "k8s.srvc.tools",
Namespace: "accounts",
Labels: map[string]string{"role": "api"},
Ports: map[glx.Port]glx.Port{80: 8080},
},
},
},
},
},
}
configDir := filepath.Join(os.Getenv("HOME"), ".config", "glx")
cases := []struct {
test string
setup func(t *testing.T, fs afero.Fs)
}{
{
test: "with glx.yaml",
setup: func(t *testing.T, fs afero.Fs) {
afero.WriteFile(fs, filepath.Join(configDir, "glx.k8s.yaml"), []byte(yaml1), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.local.yaml"), []byte(yaml2), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.docker.yaml"), []byte(yaml3), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.yaml"), []byte(yaml4), 0644)
},
},
{
test: "without glx.yaml",
setup: func(t *testing.T, fs afero.Fs) {
afero.WriteFile(fs, filepath.Join(configDir, "glx.k8s.yaml"), []byte(yaml1), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.local.yaml"), []byte(yaml2), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.docker.yaml"), []byte(yaml3), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.projects.yaml"), []byte(yaml4), 0644)
},
},
{
test: "with glx.yml",
setup: func(t *testing.T, fs afero.Fs) {
afero.WriteFile(fs, filepath.Join(configDir, "glx.k8s.yml"), []byte(yaml1), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.local.yml"), []byte(yaml2), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.docker.yml"), []byte(yaml3), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.yml"), []byte(yaml4), 0644)
},
},
{
test: "without glx.yml",
setup: func(t *testing.T, fs afero.Fs) {
afero.WriteFile(fs, filepath.Join(configDir, "glx.k8s.yml"), []byte(yaml1), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.local.yml"), []byte(yaml2), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.docker.yml"), []byte(yaml3), 0644)
afero.WriteFile(fs, filepath.Join(configDir, "glx.projects.yml"), []byte(yaml4), 0644)
},
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.test, func(t *testing.T) {
baseFs := afero.NewMemMapFs()
tc.setup(t, baseFs)
fs, err := glx.NewUnionFs(baseFs)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
viper := glx.NewViper(fs)
cfg, err := glx.NewConfig(viper)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if diff := cmp.Diff(wantCfg, cfg); diff != "" {
t.Errorf("loaded config mismatch (-want +got):\n%s", diff)
}
})
}
}