-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_test.go
121 lines (95 loc) · 3.86 KB
/
check_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
package concourse_tfe_resource
import (
"encoding/json"
"errors"
"github.com/hashicorp/go-tfe"
"go.uber.org/mock/gomock"
"strconv"
"testing"
)
func runList(start int, len int) (list tfe.RunList) {
for i := 0; i < len; i++ {
list.Items = append(list.Items, &tfe.Run{ID: strconv.Itoa(i + start)})
}
return
}
func TestCheckWithNilVersion(t *testing.T) {
setup(t)
result := checkOutputJSON{}
firstCall := runList(0, 5)
input := inputJSON{Source: sourceJSON{Workspace: "foo"}}
runs.EXPECT().List(gomock.Any(), gomock.Eq(workspace.ID), gomock.Any()).Return(&firstCall, nil)
runs.EXPECT().List(gomock.Any(), gomock.Eq(workspace.ID), gomock.Any()).Return(&tfe.RunList{}, nil)
output, _ := check(input)
json.Unmarshal([]byte(output), &result)
if len(result) != 5 {
t.Errorf("check with nil version returned %d elements", len(result))
} else if result[0].Ref != "4" {
t.Errorf("check with nil version didn't return the first result")
}
}
func TestCheckWithExistingVersion(t *testing.T) {
setup(t)
result := checkOutputJSON{}
firstCall := runList(0, 5)
input := inputJSON{Source: sourceJSON{Workspace: "foo"}}
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Any()).Return(&firstCall, nil)
input.Version.Ref = "2"
output, _ := check(input)
json.Unmarshal([]byte(output), &result)
if len(result) != 3 {
t.Errorf("check with third oldest version returned %d elements", len(result))
} else if result[0].Ref != "2" {
t.Errorf("check with third oldest version didn't return the expected elements")
}
}
func TestCheckWithVersionOnSecondPage(t *testing.T) {
setup(t)
result := checkOutputJSON{}
firstCall := runList(0, 5)
secondCall := runList(5, 5)
input := inputJSON{Source: sourceJSON{Workspace: "foo"}}
rlo1 := tfe.RunListOptions{ListOptions: tfe.ListOptions{PageSize: 100, PageNumber: 0}}
rlo2 := tfe.RunListOptions{ListOptions: tfe.ListOptions{PageSize: 100, PageNumber: 1}}
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Eq(&rlo1)).Return(&firstCall, nil)
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Eq(&rlo2)).Return(&secondCall, nil)
input.Version.Ref = "8"
output, _ := check(input)
json.Unmarshal([]byte(output), &result)
if len(result) != 9 {
t.Errorf("check for 9th most recent version (multiple calls) returned %d results", len(result))
} else if result[0].Ref != "8" {
t.Errorf("multiple call check returns incorrect results")
}
}
func TestCheckWithNonexistentVersion(t *testing.T) {
setup(t)
result := checkOutputJSON{}
firstCall := runList(0, 5)
input := inputJSON{Source: sourceJSON{Workspace: "foo"}}
// if the provided version does not seem to exist, return the current version
rlo1 := tfe.RunListOptions{ListOptions: tfe.ListOptions{PageSize: 100, PageNumber: 0}}
rlo2 := tfe.RunListOptions{ListOptions: tfe.ListOptions{PageSize: 100, PageNumber: 1}}
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Eq(&rlo1)).Return(&firstCall, nil)
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Eq(&rlo2)).Return(&tfe.RunList{}, nil)
input.Version.Ref = "8"
output, _ := check(input)
json.Unmarshal([]byte(output), &result)
if len(result) != 1 {
t.Errorf("check with non-present version returned %d elements", len(result))
} else if result[len(result)-1].Ref != "0" {
t.Errorf("check with non-present version didn't return the first result")
}
}
func TestCheckWithFailingListCall(t *testing.T) {
setup(t)
result := checkOutputJSON{}
firstCall := runList(0, 5)
input := inputJSON{Source: sourceJSON{Workspace: "foo"}}
rlo1 := tfe.RunListOptions{ListOptions: tfe.ListOptions{PageSize: 100, PageNumber: 0}}
runs.EXPECT().List(gomock.Any(), gomock.Eq("foo"), gomock.Eq(&rlo1)).Return(&firstCall, errors.New("NO"))
output, err := check(input)
if output != nil || err == nil || err.Error() != "error listing runs: NO" {
t.Errorf("unexpected:\n\tresult = \"%s\"\n\terr = \"%s\"", result, err)
}
}