forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps_test.go
More file actions
169 lines (135 loc) · 4.62 KB
/
apps_test.go
File metadata and controls
169 lines (135 loc) · 4.62 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
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
package controllers_test
import (
"encoding/json"
"fmt"
"net/url"
"os"
"testing"
"github.com/convox/rack/api/controllers"
"github.com/convox/rack/api/models"
"github.com/convox/rack/api/structs"
"github.com/convox/rack/test"
"github.com/stretchr/testify/assert"
)
func init() {
os.Setenv("RACK", "convox-test")
models.PauseNotifications = true
test.HandlerFunc = controllers.HandlerFunc
}
func TestAppList(t *testing.T) {
aws := test.StubAws(test.DescribeStackCycleWithoutQuery("convox-test-bar"))
defer aws.Close()
body := test.HTTPBody("GET", "http://convox/apps", nil, nil)
var resp []map[string]string
err := json.Unmarshal([]byte(body), &resp)
if assert.NoError(t, err) {
assert.Equal(t, "bar", resp[0]["name"])
assert.Equal(t, "running", resp[0]["status"])
}
}
func TestAppGet(t *testing.T) {
models.Test(t, func() {
app := &structs.App{
Name: "myapp",
Release: "R1234",
Status: "running",
}
models.TestProvider.On("AppGet", "myapp").Return(app, nil)
hf := test.NewHandlerFunc(controllers.HandlerFunc)
if assert.Nil(t, hf.Request("GET", "/apps/myapp", nil)) {
hf.AssertCode(t, 200)
hf.AssertJSON(t, "{\"name\":\"myapp\",\"release\":\"R1234\",\"status\":\"running\"}")
}
})
}
func TestAppGetWithAppNotFound(t *testing.T) {
models.Test(t, func() {
models.TestProvider.On("AppGet", "myapp").Return(nil, errorNotFound(fmt.Sprintf("no such app: myapp")))
hf := test.NewHandlerFunc(controllers.HandlerFunc)
if assert.Nil(t, hf.Request("GET", "/apps/myapp", nil)) {
hf.AssertCode(t, 404)
hf.AssertJSON(t, "{\"error\":\"no such app: myapp\"}")
}
})
}
// Test the primary path: creating an app on a `convox` rack
// Return to testing against a `convox-test` rack afterwards
func TestAppCreate(t *testing.T) {
r := os.Getenv("RACK")
os.Setenv("RACK", "convox")
defer os.Setenv("RACK", r)
aws := test.StubAws(
test.DescribeStackNotFound("application"),
test.CreateAppStackCycle("convox-application"),
test.DescribeAppStackCycle("convox-application"),
)
defer aws.Close()
val := url.Values{"name": []string{"application"}}
body := test.HTTPBody("POST", "http://convox/apps", val, nil)
if assert.NotEqual(t, "", body) {
var resp map[string]string
err := json.Unmarshal([]byte(body), &resp)
if assert.NoError(t, err) {
assert.Equal(t, "application", resp["name"])
assert.Equal(t, "running", resp["status"])
}
}
}
func TestAppCreateWithAlreadyExists(t *testing.T) {
aws := test.StubAws(
test.DescribeStackNotFound("application"),
test.CreateAppStackExistsCycle("convox-test-application"),
test.DescribeAppStackCycle("convox-test-application"),
)
defer aws.Close()
val := url.Values{"name": []string{"application"}}
body := test.AssertStatus(t, 403, "POST", "http://convox/apps", val, nil)
assert.Equal(t, "{\"error\":\"there is already an app named application (running)\"}", body)
}
func TestAppCreateWithAlreadyExistsUnbound(t *testing.T) {
aws := test.StubAws(
test.DescribeAppStackCycle("application"),
)
defer aws.Close()
val := url.Values{"name": []string{"application"}}
body := test.AssertStatus(t, 403, "POST", "http://convox/apps", val, nil)
assert.Equal(t, "{\"error\":\"there is already a legacy app named application (running). We recommend you delete this app and create it again.\"}", body)
}
func TestAppCreateWithRackName(t *testing.T) {
aws := test.StubAws(
test.DescribeAppStackCycle("foobar"),
)
defer aws.Close()
val := url.Values{"name": []string{"convox-test"}}
body := test.AssertStatus(t, 403, "POST", "http://convox/apps", val, nil)
assert.Equal(t, "{\"error\":\"application name cannot match rack name (convox-test). Please choose a different name for your app.\"}", body)
}
// TODO: test bucket cleanup. this is handled via goroutines.
/* NOTE: the S3 stuff fucks up b.c the client ries to prepend the
bucket name to the ephermeral host, so you get `app-XXX.127.0.0.1`
*/
func TestAppDelete(t *testing.T) {
aws := test.StubAws(
test.DescribeAppStackCycle("convox-test-bar"),
test.DeleteStackCycle("convox-test-bar"),
)
defer aws.Close()
// setup expectations on current provider
models.TestProvider.On("AppDelete", "bar").Return(nil)
body := test.HTTPBody("DELETE", "http://convox/apps/bar", nil, nil)
var resp map[string]bool
err := json.Unmarshal([]byte(body), &resp)
if assert.NoError(t, err) {
assert.Equal(t, true, resp["success"])
}
}
func TestAppDeleteWithAppNotFound(t *testing.T) {
aws := test.StubAws(
test.DescribeStackNotFound("convox-test-bar"),
test.DescribeStackNotFound("bar"),
)
defer aws.Close()
test.AssertStatus(t, 404, "DELETE", "http://convox/apps/bar", nil, nil)
}
func TestAppLogs(t *testing.T) {
}