-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
106 lines (88 loc) · 2.8 KB
/
main_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
package main
import (
"context"
"sort"
"testing"
"time"
codeship "github.com/codeship/codeship-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAllocatedAtSort(t *testing.T) {
now := time.Now()
build1 := codeship.Build{AllocatedAt: now.Add(time.Duration(-1) * time.Minute)}
build2 := codeship.Build{AllocatedAt: now}
build3 := codeship.Build{AllocatedAt: now.Add(time.Duration(-5) * time.Minute)}
bl := []codeship.Build{build1, build2, build3}
sort.Sort(allocatedAtSort(bl))
assert.Equal(t, bl[0], build3)
assert.Equal(t, bl[1], build1)
assert.Equal(t, bl[2], build2)
}
type mockBuildGetter struct {
buildStatus string
}
func (m mockBuildGetter) ListBuilds(ctx context.Context, projectUUID string, opts ...codeship.PaginationOption) (codeship.BuildList, codeship.Response, error) {
now := time.Now()
build1 := codeship.Build{UUID: "2", Status: "testing", Branch: "test-branch", AllocatedAt: now.Add(time.Duration(-1) * time.Minute)}
build2 := codeship.Build{UUID: "3", Status: "success", Branch: "test-branch", AllocatedAt: now}
build3 := codeship.Build{UUID: "1", Status: "testing", Branch: "test-branch", AllocatedAt: now.Add(time.Duration(-5) * time.Minute)}
build4 := codeship.Build{UUID: "4", Status: "testing", Branch: "another-branch"}
return codeship.BuildList{
Builds: []codeship.Build{build1, build2, build3, build4},
}, codeship.Response{}, nil
}
func (m mockBuildGetter) GetBuild(ctx context.Context, projectUUID, buildUUID string) (codeship.Build, codeship.Response, error) {
return codeship.Build{
Branch: "test-branch",
Status: m.buildStatus,
}, codeship.Response{}, nil
}
func TestWaitOnPreviousBuilds(t *testing.T) {
monitor := &monitor{
buildGetter: mockBuildGetter{},
}
err := monitor.waitOnPreviousBuilds(context.TODO(), "project-uuid", "build-uuid", "test-branch")
require.NoError(t, err)
}
func TestBuildFinished(t *testing.T) {
testCases := []struct {
name string
buildStatus string
finished bool
}{
{
name: "success status",
buildStatus: "success",
finished: true,
}, {
name: "testing status",
buildStatus: "testing",
finished: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b := codeship.Build{
ProjectUUID: "project-uuid",
UUID: "build-uuid",
}
monitor := &monitor{
buildGetter: mockBuildGetter{
buildStatus: tc.buildStatus,
},
}
finished, err := monitor.buildFinished(context.TODO(), b)
require.NoError(t, err)
assert.Equal(t, finished, tc.finished)
})
}
}
func TestBuildsToWatch(t *testing.T) {
monitor := &monitor{
buildGetter: mockBuildGetter{},
}
builds, err := monitor.buildsToWatch(context.TODO(), "project-id", "test-branch")
require.NoError(t, err)
assert.Equal(t, len(builds), 2)
}