This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmarathon_test.go
146 lines (116 loc) · 5.23 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVerifyRunningTask(t *testing.T) {
appID, appVersion, taskID := "/demo/webapp", "2015-12-04T12:25:08.426Z", "demo_webapp.0f810e10-9a82-11e5-94c7-6a515f434e2d"
response, err := ioutil.ReadFile("./resources/test/marathon-apps-response.json")
assert.Nil(t, err)
ok, err := verifyRunningTask(appID, appVersion, taskID, response)
assert.True(t, ok)
assert.Nil(t, err)
// Verify that appID is checked
ok, err = verifyRunningTask("/demo/webap1", appVersion, taskID, response)
assert.False(t, ok)
assert.NotNil(t, err)
// Verify that appVersion is checked
ok, err = verifyRunningTask(appID, "2014-12-04T12:25:08.426Z", taskID, response)
assert.False(t, ok)
assert.NotNil(t, err)
// Verify that taskID is checked
ok, err = verifyRunningTask(appID, appVersion, "demo_webap1.0f810e10-9a82-11e5-94c7-6a515f434e2d", response)
assert.False(t, ok)
assert.NotNil(t, err)
// Verify bad responses
ok, err = verifyRunningTask(appID, appVersion, taskID, []byte(`{}`))
assert.False(t, ok)
assert.NotNil(t, err)
// Verify bad responses
ok, err = verifyRunningTask(appID, appVersion, taskID, []byte(`{"app": {}}`))
assert.False(t, ok)
assert.NotNil(t, err)
// Verify bad responses
ok, err = verifyRunningTask(appID, appVersion, taskID, []byte(`%"#¤%"#¤`))
assert.False(t, ok)
assert.NotNil(t, err)
}
func TestParseApplicationVersion(t *testing.T) {
appID, appVersion, taskID := "/demo/webapp", "2015-12-04T12:25:08.426Z", "demo_webapp.0f810e10-9a82-11e5-94c7-6a515f434e2d"
response, err := ioutil.ReadFile("./resources/test/marathon-versions-response.json")
assert.Nil(t, err)
app, err := parseApplicationVersion(appID, appVersion, taskID, response)
assert.Nil(t, err)
assert.Equal(t, appID, app.ID)
assert.Equal(t, appVersion, app.Version)
assert.Equal(t, taskID, app.TaskID)
assert.Equal(t, "omO6DSEw/mZDG9NuhyEC4uYbgwwqEivOuX0EqX9+Ql0=", encode(app.DeployKey[:]))
assert.Equal(t, "kVOhhw2wAJuAofxO7h4EM0xboxGAwnsq9J6fluFY5CQ=", encode(app.ServiceKey[:]))
// Verify that appID is checked
app, err = parseApplicationVersion("/demo/webap1", "2015-12-04T12:25:08.426Z", taskID, response)
assert.Nil(t, app)
assert.NotNil(t, err)
// Verify that appVersion is checked
app, err = parseApplicationVersion(appID, "2014-12-04T12:25:08.426Z", taskID, response)
assert.Nil(t, app)
assert.NotNil(t, err)
// Verify bad responses
app, err = parseApplicationVersion(appID, appVersion, taskID, []byte(`{}`))
assert.NotNil(t, err)
// Verify bad responses
app, err = parseApplicationVersion(appID, appVersion, taskID, []byte(`{"id": "/demo/webapp"}`))
assert.NotNil(t, err)
// Verify bad responses
app, err = parseApplicationVersion(appID, appVersion, taskID, []byte(`%"#¤%"#¤`))
assert.NotNil(t, err)
}
func TestParseApplicationWithoutServiceKey(t *testing.T) {
appID, appVersion, taskID := "/demo/webapp2", "2015-12-04T12:25:08.426Z", "demo_webapp.0f810e10-9a82-11e5-94c7-6a515f434e2d"
response, err := ioutil.ReadFile("./resources/test/marathon-versions-nosvckey.json")
assert.Nil(t, err)
app, err := parseApplicationVersion(appID, appVersion, taskID, response)
assert.Nil(t, err)
assert.Equal(t, appID, app.ID)
assert.Equal(t, appVersion, app.Version)
assert.Equal(t, taskID, app.TaskID)
assert.Equal(t, "omO6DSEw/mZDG9NuhyEC4uYbgwwqEivOuX0EqX9+Ql0=", encode(app.DeployKey[:]))
assert.Nil(t, app.ServiceKey)
}
func TestParseApplicationWithoutDeployKey(t *testing.T) {
appID, appVersion, taskID := "/demo/webapp", "2015-12-04T12:25:08.426Z", "demo_webapp.0f810e10-9a82-11e5-94c7-6a515f434e2d"
response, err := ioutil.ReadFile("./resources/test/marathon-versions-nodepkey.json")
assert.Nil(t, err)
_, err = parseApplicationVersion(appID, appVersion, taskID, response)
assert.NotNil(t, err)
assert.Equal(t, "App is missing $DEPLOY_PUBLIC_KEY in the Marathon config \"env\" section", err.Error())
}
func TestGetMarathonApp(t *testing.T) {
appsResponse, err := ioutil.ReadFile("./resources/test/marathon-apps-response.json")
assert.Nil(t, err)
versionsResponse, err := ioutil.ReadFile("./resources/test/marathon-versions-response.json")
assert.Nil(t, err)
// Start in-test HTTP server that emulates Marathon
marathon := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.URL.Path == "/v2/apps/demo/webapp" && r.URL.RawQuery == "embed=apps.tasks":
fmt.Fprintln(w, string(appsResponse))
case r.URL.Path == "/v2/apps/demo/webapp/versions/2015-12-04T12:25:08.426Z":
fmt.Fprintln(w, string(versionsResponse))
default:
http.Error(w, fmt.Sprintf("Bad URL %s", r.URL.Path), http.StatusNotFound)
}
}))
defer marathon.Close()
app, err := getMarathonApp(marathon.URL, "/demo/webapp", "2015-12-04T12:25:08.426Z", "demo_webapp.0f810e10-9a82-11e5-94c7-6a515f434e2d")
assert.Nil(t, err)
assert.Equal(t, "/demo/webapp", app.ID)
assert.Equal(t, "2015-12-04T12:25:08.426Z", app.Version)
assert.Equal(t, "omO6DSEw/mZDG9NuhyEC4uYbgwwqEivOuX0EqX9+Ql0=", encode(app.DeployKey[:]))
assert.Equal(t, "kVOhhw2wAJuAofxO7h4EM0xboxGAwnsq9J6fluFY5CQ=", encode(app.ServiceKey[:]))
assert.Equal(t, "ENC[NACL,", app.Env["DATABASE_PASSWORD"][0:9])
}