-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils_test.go
More file actions
122 lines (102 loc) · 2.75 KB
/
utils_test.go
File metadata and controls
122 lines (102 loc) · 2.75 KB
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
package pkg
import (
"bytes"
"encoding/json"
"strings"
"testing"
"time"
dtime "github.com/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 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 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))
}
}