forked from gengo/goship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goship_test.go
410 lines (371 loc) · 12.7 KB
/
goship_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/coreos/go-etcd/etcd"
goship "github.com/gengo/goship/lib"
"github.com/google/go-github/github"
"github.com/gorilla/sessions"
)
type githubClientMock struct {
}
func (c githubClientMock) ListTeams(owner string, repo string, opt *github.ListOptions) ([]github.Team, *github.Response, error) {
a := github.Team{ID: github.Int(1), Name: github.String("team_1"), Permission: github.String("pull")}
b := github.Team{ID: github.Int(2), Name: github.String("team_2"), Permission: github.String("push")}
if repo == "repo_1" {
return []github.Team{a}, nil, nil
}
if repo == "repo_2" {
return []github.Team{b}, nil, nil
}
if repo == "repo_3" {
return []github.Team{a, b}, nil, nil
}
return []github.Team{}, nil, nil
}
func (c githubClientMock) IsTeamMember(team int, user string) (bool, *github.Response, error) {
if user == "read_only_user" && team == 1 {
return true, nil, nil
}
if user == "push_user" && team == 2 {
return true, nil, nil
}
if user == "push_and_pull_only_user" && (team == 1 || team == 2) {
return true, nil, nil
}
return false, nil, nil
}
func (c githubClientMock) IsCollaborator(owner, repo, user string) (bool, *github.Response, error) {
return true, nil, nil
}
func newMockGithubClient() githubClientMock {
return githubClientMock{}
}
func TestUserOnNoTeam(t *testing.T) {
g := newMockGithubClient()
authentication.authorization = true
var want = false
got, err := userHasDeployPermission(g, "owner_1", "repo_1", "read_only_user")
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("User is Read Only = %v, want %v", got, want)
}
}
func TestUserIsReadOnly(t *testing.T) {
g := newMockGithubClient()
authentication.authorization = true
var want = false
got, err := userHasDeployPermission(g, "owner_1", "repo_1", "push_user")
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("User is Read Only = %v, want %v", got, want)
}
}
func TestUserHasPushPermission(t *testing.T) {
g := newMockGithubClient()
var want = true
got, err := userHasDeployPermission(g, "some_owner", "repo_2", "push_and_pull_only_user")
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("User has Push Permission = %v, want %v", got, want)
}
}
func TestPushPullUserHasPushPermission(t *testing.T) {
g := newMockGithubClient()
var want = true
got, err := userHasDeployPermission(g, "some_owner", "repo_3", "push_and_pull_only_user")
if err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("User has Push Permission = %v, want %v", got, want)
}
}
func TestStripANSICodes(t *testing.T) {
tests := []struct {
give string
want string
}{
{"\x1B[0m", ""},
{"some text \x1B[23m\x1B[2;13m", "some text "},
{"no code", "no code"},
{"\x1B[13m\x1B[23m\x1B[3m", ""},
}
for _, tt := range tests {
got := stripANSICodes(tt.give)
if got != tt.want {
t.Errorf("stripANSICodes(%q) got %q, want %q", tt.give, got, tt.want)
}
}
}
var githubDiffURLTests = []struct {
h goship.Host
p goship.Project
e goship.Environment
want string
}{
{goship.Host{LatestCommit: "abc123"}, goship.Project{nil, "test project", "https://github.com/test/foo", "foo", "test", []goship.Environment{}}, goship.Environment{LatestGitHubCommit: "abc123"}, ""},
{goship.Host{LatestCommit: "abc123"}, goship.Project{nil, "test project", "https://github.com/test/foo", "foo", "test", []goship.Environment{}}, goship.Environment{LatestGitHubCommit: "abc456"}, "https://github.com/test/foo/compare/abc123...abc456"},
}
func TestGitHubDiffURL(t *testing.T) {
for _, tt := range githubDiffURLTests {
if got := tt.h.LatestGitHubDiffURL(tt.p, tt.e); got != tt.want {
t.Errorf("gitHubDiffURL = %s, want %s", got, tt.want)
}
}
}
var deployableTests = []struct {
e goship.Environment
want bool
}{
{goship.Environment{LatestGitHubCommit: "abc123", Hosts: []goship.Host{{LatestCommit: "abc456"}}}, true},
{goship.Environment{LatestGitHubCommit: "abc456", Hosts: []goship.Host{{LatestCommit: "abc456"}}}, false},
}
func TestDeployable(t *testing.T) {
for _, tt := range deployableTests {
if got := tt.e.Deployable(); got != tt.want {
t.Errorf("Deployable = %t, want %t", got, tt.want)
}
}
}
var wantConfig = goship.Config{
Projects: []goship.Project{
{Name: "Test Project One", GitHubURL: "https://github.com/test_owner/test_repo_name", RepoName: "test_repo_name", RepoOwner: "test_owner",
Environments: []goship.Environment{{Name: "live", Deploy: "/deploy/test_project_one.sh", RepoPath: "/repos/test_repo_name/.git",
Hosts: []goship.Host{{URI: "test-project-one.test.com"}}, Branch: "master", IsDeployable: false}}},
{Name: "Test Project Two", GitHubURL: "https://github.com/test_owner/test_repo_name_two", RepoName: "test_repo_name_two", RepoOwner: "test_owner",
Environments: []goship.Environment{{Name: "live", Deploy: "/deploy/test_project_two.sh", RepoPath: "/repos/test_repo_name_two/.git",
Hosts: []goship.Host{{URI: "test-project-two.test.com"}}, Branch: "master", LatestGitHubCommit: "", IsDeployable: false}}}},
DeployUser: "deploy_user",
Notify: "/notify/notify.sh",
Pivotal: &goship.PivotalConfiguration{Project: "111111", Token: "test"}}
func TestSetComment(t *testing.T) {
err := goship.SetComment(&MockEtcdClient{}, "test_project", "test_environment", "A comment")
if err != nil {
t.Fatalf("Can't set Comment %s", err)
}
}
func TestLockingEnvironment(t *testing.T) {
err := goship.LockEnvironment(&MockEtcdClient{}, "test_project", "test_environment", "true")
if err != nil {
t.Fatalf("Can't lock %s", err)
}
}
func TestUnlockingEnvironment(t *testing.T) {
err := goship.LockEnvironment(&MockEtcdClient{}, "test_project", "test_environment", "false")
if err != nil {
t.Fatalf("Can't unlock %s", err)
}
}
func compareStrings(name, got, want string, t *testing.T) {
if got != want {
t.Errorf("got %s = %s; want %s", name, got, want)
}
}
func TestCanParseETCD(t *testing.T) {
got, err := goship.ParseETCD(&MockEtcdClient{})
if err != nil {
t.Fatalf("Can't parse %s %s", t, err)
}
compareStrings("deploy user", got.DeployUser, "test_user", t)
compareStrings("token", got.Pivotal.Token, "XXXXXX", t)
compareStrings("project", got.Pivotal.Project, "111111", t)
compareStrings("project name", got.Projects[0].Name, "pivotal_project", t)
compareStrings("repo path", got.Projects[0].Environments[0].RepoPath, "/repos/test_repo_name/.git", t)
compareStrings("repo branch", got.Projects[0].Environments[0].Branch, "master", t)
compareStrings("host name", got.Projects[0].Environments[0].Hosts[0].URI, "test-qa-01.somewhere.com", t)
}
var now = time.Now()
var formatTimeTests = []struct {
t time.Time
want string
}{
{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), "Nov 10, 2009 at 11:00pm (UTC)"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()-1, 0, now.Location()), "1 second ago"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()-30, 0, now.Location()), "30 seconds ago"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute()-1, now.Second(), 0, now.Location()), "1 minute ago"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute()-30, now.Second(), 0, now.Location()), "30 minutes ago"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-1, now.Minute(), now.Second(), 0, now.Location()), "1 hour ago"},
{time.Date(now.Year(), now.Month(), now.Day(), now.Hour()-3, now.Minute(), now.Second(), 0, now.Location()), "3 hours ago"},
}
func TestFormatTime(t *testing.T) {
for _, tt := range formatTimeTests {
if got := formatTime(tt.t); got != tt.want {
t.Errorf("formatTime(%s) = %s, want %s", tt.t, got, tt.want)
}
}
}
func TestProjectFromName(t *testing.T) {
var want = goship.Project{Name: "TestProject"}
projects := []goship.Project{want}
got, err := goship.ProjectFromName(projects, "TestProject")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, &want) {
t.Errorf("goship.GetProjectFromName = %v, want %v", got, want)
}
got, err = goship.ProjectFromName(projects, "BadProject")
if err == nil {
t.Errorf("goship.GetProjectFromName error case did not error")
}
}
func TestCleanProjects(t *testing.T) {
authentication.authorization = true
req, _ := http.NewRequest("GET", "", nil)
p, err := goship.ParseETCD(&MockEtcdClient{})
if err != nil {
t.Fatalf("Can't parse %s %s", t, err)
}
u := User{}
u.UserName = "bob"
got := len(p.Projects)
if got < 1 {
t.Errorf("clean projects test expects projects to have at least one project [%d]", got)
}
got = len(removeUnauthorizedProjects(p.Projects, req, u))
if got != 0 {
t.Errorf("clean projects failed to clean project for unauth user.. [%d]", got)
}
}
func TestGetUser(t *testing.T) {
authentication.authorization = true
req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
session, err := store.Get(req, sessionName)
if err != nil {
t.Errorf("Can't get a session store")
}
session.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
session.Values["userName"] = "T-800"
session.Values["avatarURL"] = "http://fake.com/1234"
session.Save(req, w)
user, err := getUser(req)
if err != nil {
t.Errorf("Failed to get User from GetUser [%s]", err)
}
if user.UserName != session.Values["userName"] {
t.Errorf("Failed to get User Name, expected %s got [%s]", session.Values["userName"], user.UserName)
}
if user.UserAvatar != session.Values["avatarURL"] {
t.Errorf("Failed to get User Avatar, expected %s got [%s]", session.Values["avatarURL"], user.UserAvatar)
}
}
func TestGetEnvironmentFromName(t *testing.T) {
var (
want = goship.Environment{Name: "TestEnvironment"}
envs = []goship.Environment{want}
)
projects := []goship.Project{goship.Project{Name: "TestProject", Environments: envs}}
got, err := goship.EnvironmentFromName(projects, "TestProject", "TestEnvironment")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, &want) {
t.Errorf("goship.EnvironmentFromName = %v, want %v", got, want)
}
got, err = goship.EnvironmentFromName(projects, "BadProject", "BadEnvironment")
if err == nil {
t.Errorf("goship.EnvironmentFromName error case did not error")
}
}
type MockEtcdClient struct{}
func (*MockEtcdClient) Set(s, c string, x uint64) (*etcd.Response, error) {
m := make(map[string]*etcd.Response)
m["/projects/test_project/environments/test_environment/comment"] = &etcd.Response{
Action: "Set",
Node: &etcd.Node{
Key: "/projects/test_project/environments/test_environment/", Value: "XXXX",
},
PrevNode: &etcd.Node{
Key: "/projects/test_project/environments/test_environment/", Value: "YYYY",
},
}
mockResponse := m[s]
return mockResponse, nil
}
//Mock calls to ETCD here. Each etcd Response should return the structs you need.
func (*MockEtcdClient) Get(s string, t bool, x bool) (*etcd.Response, error) {
m := make(map[string]*etcd.Response)
m["/"] = &etcd.Response{Action: "Get", Node: &etcd.Node{
Key: "projects", Value: "",
Nodes: etcd.Nodes{
{Key: "deploy_user", Value: "test_user", Dir: false},
{Key: "pivotal_token", Value: "XXXXXX", Dir: false},
{Key: "pivotal_project", Value: "111111", Dir: false},
}, Dir: true,
}, EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
m["/projects"] = &etcd.Response{
Action: "Get",
Node: &etcd.Node{
Key: "projects", Value: "",
Nodes: etcd.Nodes{
{Key: "/projects/pivotal_project", Dir: true},
}, Dir: true,
},
EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
m["/projects/pivotal_project"] = &etcd.Response{
Action: "Get",
Node: &etcd.Node{
Key: "/projects/pivotal_project", Value: "",
Nodes: etcd.Nodes{
{
Key: "project_name", Value: "/projects/pivotal_project/project_name/TC", Dir: true,
},
},
Dir: true,
},
EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
m["/projects/pivotal_project/environments"] = &etcd.Response{
Action: "Get",
Node: &etcd.Node{
Key: "/projects/pivotal_project/environments",
Value: "",
Nodes: etcd.Nodes{
{Key: "qa", Dir: true},
},
Dir: true,
},
EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
m["/projects/pivotal_project/environments/qa"] = &etcd.Response{
Action: "Get",
Node: &etcd.Node{
Key: "qa",
Value: "",
Nodes: etcd.Nodes{
{Key: "repo_path", Value: "/repos/test_repo_name/.git", Dir: false},
{Key: "branch", Value: "master", Dir: false},
}, Dir: true,
}, EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
m["/projects/pivotal_project/environments/qa/hosts"] = &etcd.Response{
Action: "Get",
Node: &etcd.Node{
Key: "/projects/pivotal_project/environments/qa/hosts", Value: "",
Nodes: etcd.Nodes{
{Key: "test-qa-01.somewhere.com", Dir: true},
},
Dir: true,
},
EtcdIndex: 1, RaftIndex: 1, RaftTerm: 1,
}
mockResponse := m[s]
return mockResponse, nil
}