-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils_test.go
156 lines (142 loc) · 3.21 KB
/
utils_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
package drycc
import (
"fmt"
"os"
"path/filepath"
"testing"
)
type versionComparison struct {
Client string
Server string
Error error
}
func TestCheckAPIVersions(t *testing.T) {
comparisons := []versionComparison{
{"1.2", "2.1", ErrAPIMismatch},
{"2.1", "1.2", ErrAPIMismatch},
{"2.1", "2.2", ErrAPIMismatch},
{"2.3", "2.0", nil},
}
for _, check := range comparisons {
err := CheckAPICompatibility(check.Client, check.Server)
if err != check.Error {
t.Errorf("%v: Expected %v, Got %v", check, check.Error, err)
}
}
}
func TestParseEnv(t *testing.T) {
expects := map[string]string{
"F1": "111",
"F2": "222",
"F3": "333",
}
f, err := os.CreateTemp("", "env")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(f.Name())
for key, value := range expects {
f.WriteString(fmt.Sprintf("%s=%s\n", key, value))
}
f.Seek(0, 0)
config, err := ParseEnv(f.Name())
if err != nil {
t.Fatal(err)
}
if len(config) != 3 {
t.Errorf("Expected %d, Got %d", 3, len(config))
}
for key, value := range expects {
if v, ok := config[key]; !ok || config[key] != value {
t.Errorf("Expected %s, Got %s", v, value)
}
}
}
func TestParseDryccfile(t *testing.T) {
webPipeline := `
kind: pipeline
ptype: web
build:
docker: Dockerfile
arg:
CODENAME: bookworm
env:
VERSION: 1.2.1
run:
command:
- ./deployment-tasks.sh
image: worker
timeout: 100
config:
- jvmconfig
deploy:
command:
- bash
- -ec
args:
- bundle exec puma -C config/puma.rb
`
taskPipeline := `
kind: pipeline
ptype: task
build:
docker: Dockerfile
arg:
CODENAME: bookworm
env:
VERSION: 1.2.1
run:
command:
- ./deployment-tasks.sh
image: worker
timeout: 100
config:
- jvmconfig
deploy:
command:
- bash
- -ec
args:
- bundle exec puma -C config/puma.rb
`
tmp, err := os.MkdirTemp("", "drycc")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
configDir := filepath.Join(tmp, "config")
os.MkdirAll(configDir, 0777)
os.WriteFile(filepath.Join(tmp, "web.yml"), []byte(webPipeline), 0777)
os.WriteFile(filepath.Join(tmp, "task.yaml"), []byte(taskPipeline), 0777)
os.WriteFile(filepath.Join(configDir, "global"), []byte("DEBUG=true\n"), 0777)
os.WriteFile(filepath.Join(configDir, "web"), []byte("PORT=8000\nJVM_OPTIONS=-Xms16G\n"), 0777)
dryccfile, err := ParseDryccfile(tmp)
if err != nil {
t.Fatal(err)
}
if config, ok := dryccfile["config"]; ok {
webConfig := config.(map[string]interface{})["web"].(map[string]interface{})
globalConfig := config.(map[string]interface{})["global"].(map[string]interface{})
if globalConfig["DEBUG"] != "true" {
t.Errorf("Expected %s, Got %s", "true", globalConfig["DEBUG"])
}
if webConfig["PORT"].(string) != "8000" {
t.Errorf("Expected %s, Got %s", "8000", webConfig["PORT"])
}
if webConfig["JVM_OPTIONS"].(string) != "-Xms16G" {
t.Errorf("Expected %s, Got %s", "-Xms16G", webConfig["JVM_OPTIONS"])
}
} else {
t.Error("Config not found")
}
if pipeline, ok := dryccfile["pipeline"]; ok {
if _, ok := pipeline.(map[string]interface{})["web.yml"]; !ok {
t.Error("web.yml not found")
}
if _, ok := pipeline.(map[string]interface{})["task.yaml"]; !ok {
t.Error("task.yaml not found")
}
} else {
t.Error("Pipeline not found")
}
}