-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
177 lines (149 loc) · 4.36 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package builder
import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"
dtime "github.com/deis/deis/pkg/time"
)
type ClosingBuffer struct {
*bytes.Buffer
}
func (cb *ClosingBuffer) Close() error {
// we don't have to do anything here, since the buffer is just some data in memory
return nil
}
func stringInSlice(list []string, s string) bool {
for _, li := range list {
if li == s {
return true
}
}
return false
}
func TestYamlToJSONGood(t *testing.T) {
goodProcfiles := [][]byte{
[]byte(`web: while true; do echo hello; sleep 1; done`),
[]byte(`web: while true; do echo hello; sleep 1; done
worker: while true; do echo hello; sleep 1; done`),
// test a procfile with quoted strings
[]byte(`web: /bin/bash -c "while true; do echo hello; sleep 1; done"`),
}
goodProcess := "while true; do echo hello; sleep 1; done"
for _, procfile := range goodProcfiles {
data, err := YamlToJSON(procfile)
if err != nil {
t.Errorf("expected procfile to be valid, got '%v'", err)
}
var p ProcessType
if err := json.Unmarshal([]byte(data), &p); err != nil {
t.Errorf("expected to be able to unmarshal object, got '%v'", err)
}
if !strings.Contains(p["web"], goodProcess) {
t.Errorf("expected web process == '%s', got '%s'", goodProcess, p["web"])
}
}
}
func TestParseConfigGood(t *testing.T) {
// mock the controller response
resp := bytes.NewBufferString(`{"owner": "test",
"app": "example-go",
"values": {"FOO": "bar", "CAR": 1234},
"memory": {},
"cpu": {},
"tags": {},
"created": "2014-01-01T00:00:00UTC",
"updated": "2014-01-01T00:00:00UTC",
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`)
config, err := ParseConfig(resp.Bytes())
if err != nil {
t.Error(err)
}
if config.Values["FOO"] != "bar" {
t.Errorf("expected FOO='bar', got FOO='%v'", config.Values["FOO"])
}
if car, ok := config.Values["CAR"].(float64); ok {
if car != 1234 {
t.Errorf("expected CAR=1234, got CAR=%d", config.Values["CAR"])
}
} else {
t.Error("expected CAR to be of type float64")
}
}
func TestParseDomainGood(t *testing.T) {
// mock controller build-hook response
resp := []byte(`{"release": {"version": 1},
"domains": ["test.example.com", "test2.example.com"]}`)
domain, err := ParseDomain(resp)
if err != nil {
t.Errorf("expected to parse domain, got '%v'", err)
}
if domain != "test.example.com" {
t.Errorf("expected 'test.example.com', got '%s'", domain)
}
}
func TestParseReleaseVersionGood(t *testing.T) {
// mock controller build-hook response
resp := []byte(`{"release": {"version": 1},
"domains": ["test.example.com", "test2.example.com"]}`)
version, err := ParseReleaseVersion(resp)
if err != nil {
t.Errorf("expected to parse version, got '%v'", err)
}
if version != 1 {
t.Errorf("expected '1', got '%d'", version)
}
}
func TestGetDefaultTypeGood(t *testing.T) {
goodData := [][]byte{[]byte(`default_process_types:
web: while true; do echo hello; sleep 1; done`),
[]byte(`foo: bar
default_process_types:
web: while true; do echo hello; sleep 1; done`),
[]byte(``)}
for _, data := range goodData {
defaultType, err := GetDefaultType(data)
if err != nil {
t.Error(err)
}
if defaultType != `{"web":"while true; do echo hello; sleep 1; done"}` && string(data) != "" {
t.Errorf("incorrect default type, got %s", defaultType)
}
if string(data) == "" && defaultType != "{}" {
t.Errorf("incorrect default type, got %s", defaultType)
}
}
}
func TestParseControllerConfigGood(t *testing.T) {
// mock controller config response
resp := []byte(`{"owner": "test",
"app": "example-go",
"values": {"FOO": "bar", "CAR": "star"},
"memory": {},
"cpu": {},
"tags": {},
"created": "2014-01-01T00:00:00UTC",
"updated": "2014-01-01T00:00:00UTC",
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
}`)
config, err := ParseControllerConfig(resp)
if err != nil {
t.Errorf("expected to pass, got '%v'", err)
}
if len(config) != 2 {
t.Errorf("expected 2, got %d", len(config))
}
if !stringInSlice(config, " -e CAR=\"star\"") {
t.Error("expected ' -e CAR=\"star\"' in slice")
}
}
func TestTimeSerialize(t *testing.T) {
time, err := json.Marshal(&dtime.Time{Time: time.Now().UTC()})
if err != nil {
t.Errorf("expected to be able to serialize time as json, got '%v'", err)
}
if !strings.Contains(string(time), "UTC") {
t.Errorf("could not find 'UTC' in datetime, got '%s'", string(time))
}
}