-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmarathon_test.go
182 lines (176 loc) · 5.75 KB
/
marathon_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
package main
import (
"bytes"
"encoding/json"
"testing"
)
const marathonExampleYAML = `
id: /path/to/apps
apps:
- id: svc
cpus: 0.2
mem: 512
instances: 1
ports:
- 0
container:
type: DOCKER
docker:
image: index.docker.io/library/hello-world
network: HOST
parameters:
- key: log-driver
value: journald
volumes:
- containerPath: /run/pald
hostPath: /run/pald
mode: RO
env:
SOME_ENV_VAR: some-env-var-value
labels:
some_label: some_label_value
healthChecks:
- protocol: HTTP
path: /_healthcheck
command:
value: foo
gracePeriodSeconds: 3
intervalSeconds: 10
portIndex: 0
timeoutSeconds: 10
maxConsecutiveFailures: 3
`
func TestMarathonParseYAML(t *testing.T) {
app, err := marathonParseYAML([]byte(marathonExampleYAML))
if err != nil {
t.Errorf("Error parsing Marathon YAML: %s", err)
}
expect := "/path/to/apps"
if app.ID != expect {
t.Errorf(
"Error parsing Marathon YAML. Expect id '%s', got '%s'",
expect,
app.ID,
)
}
}
func TestMarathonValidate(t *testing.T) {
exampleAppInvalid, _ := marathonParseYAML([]byte(marathonExampleYAML))
exampleAppValid, _ := marathonParseYAML([]byte(marathonExampleYAML))
exampleAppValid.Apps[0].Container.Docker.Image = "index.docker.io/library/hello-world:latest"
tests := []struct {
app marathonGroup
err string
}{
{
app: exampleAppValid,
err: "",
},
{
app: marathonGroup{},
err: "App id '' invalid",
},
{
app: exampleAppInvalid,
err: "App 0 container docker image 'index.docker.io/library/hello-world' must have a tag",
},
}
for i, test := range tests {
e := marathonValidate(test.app)
if e != nil && test.err == "" {
t.Errorf("(%d) Unexpected error: %s", i, e)
} else if e == nil && test.err != "" {
t.Errorf("(%d) Expected error '%s' but no error occurred", i, test.err)
} else if e != nil && e.Error() != test.err {
t.Errorf("(%d) Expected error '%s' but got '%s'", i, test.err, e)
}
}
}
func TestMarathonParseYAMLPorts(t *testing.T) {
tests := []struct {
yaml string
expectError bool
validateFunc func(*marathonGroup) bool
}{
{
yaml: "apps: [{ports: [0, 0]}]",
validateFunc: func(g *marathonGroup) bool { return len(g.Apps[0].Ports) == 2 },
},
{
yaml: "apps: [{portDefinitions: [{port: 0, name: metrics}, {port: 0, name: pprof}]}]",
validateFunc: func(g *marathonGroup) bool { return g.Apps[0].PortDefinitions[0].Name == "metrics" },
},
{
yaml: "apps: [{portDefinitions: [{port: 0}, {port: 0, name: pprof}]}]",
validateFunc: func(g *marathonGroup) bool {
return g.Apps[0].PortDefinitions[0].Name == "" && g.Apps[0].PortDefinitions[1].Name == "pprof"
},
},
{
yaml: "apps: [{portDefinitions: [0, 0]}]",
expectError: true,
validateFunc: nil,
},
}
for i, test := range tests {
config, err := marathonParseYAML([]byte(test.yaml))
switch {
case err != nil && test.expectError:
case err == nil && test.expectError:
t.Errorf("expected error, didn't get it")
case err != nil:
t.Errorf("unexpected error in test %v: %v", i, err)
case !test.validateFunc(&config):
t.Errorf("invalid result for test %v: %#v", i, config)
}
}
}
func TestMarathonYAMLtoJSON(t *testing.T) {
tests := []struct {
yaml []byte
json []byte
}{
{
yaml: []byte(``),
json: []byte(`{"id":""}`),
},
{
yaml: []byte(`apps: [{ports: [0, 0]}]`),
json: []byte(`{"id":"","apps":[{"id":"","instances":0,"cpus":0,"mem":0,"constraints":null,"ports":[0,0],"requirePorts":false,"container":{"type":"","volumes":null}}]}`),
},
// validate that portDefinition doesn't create empty name fields.
{
yaml: []byte(`apps: [{portDefinitions: [{port: 0}]}]`),
json: []byte(`{"id":"","apps":[{"id":"","instances":0,"cpus":0,"mem":0,"constraints":null,"portDefinitions":[{"port":0}],"requirePorts":false,"container":{"type":"","volumes":null}}]}`),
},
{
yaml: []byte(`apps: [{portDefinitions: [{port: 0, name: metrics}, {port: 0, name: pprof}]}]`),
json: []byte(`{"id":"","apps":[{"id":"","instances":0,"cpus":0,"mem":0,"constraints":null,"portDefinitions":[{"port":0,"name":"metrics"},{"port":0,"name":"pprof"}],"requirePorts":false,"container":{"type":"","volumes":null}}]}`),
},
{
yaml: []byte(`apps: [{healthChecks: [{protocol: COMMAND, command: {value: foo}, gracePeriodSeconds: 2, intervalSeconds: 2, portIndex: 1, timeoutSeconds: 2, maxConsecutiveFailures: 2 }]}]`),
json: []byte(`{"id":"","apps":[{"id":"","instances":0,"cpus":0,"mem":0,"constraints":null,"requirePorts":false,"container":{"type":"","volumes":null},"healthChecks":[{"protocol":"COMMAND","command":{"value":"foo"},"gracePeriodSeconds":2,"intervalSeconds":2,"portIndex":1,"timeoutSeconds":2,"maxConsecutiveFailures":2}]}]}`),
},
{
yaml: []byte(`apps: [{healthChecks: [{protocol: HTTP, gracePeriodSeconds: 2, intervalSeconds: 2, portIndex: 1, timeoutSeconds: 2, maxConsecutiveFailures: 2 }]}]`),
json: []byte(`{"id":"","apps":[{"id":"","instances":0,"cpus":0,"mem":0,"constraints":null,"requirePorts":false,"container":{"type":"","volumes":null},"healthChecks":[{"protocol":"HTTP","gracePeriodSeconds":2,"intervalSeconds":2,"portIndex":1,"timeoutSeconds":2,"maxConsecutiveFailures":2}]}]}`),
},
}
for i, test := range tests {
config, err := marathonParseYAML(test.yaml)
if err != nil {
t.Fatalf("(%d) Unexpected error parsing YAML: %s", i, err)
}
marathonJSON, err := json.Marshal(config)
if err != nil {
t.Fatalf("(%d) Unexpected error marshaling JSON: %s", i, err)
}
if !bytes.Equal(marathonJSON, test.json) {
t.Fatalf(
"(%d) JSON mismatch.\nExpected:\t%s\nGot:\t\t%s\n", i,
test.json,
marathonJSON,
)
}
}
}