-
Notifications
You must be signed in to change notification settings - Fork 8
/
store-cli_test.go
253 lines (233 loc) · 7.31 KB
/
store-cli_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
package main
import (
"fmt"
"net/url"
"os"
"path/filepath"
"testing"
)
func TestMain(m *testing.M) {
// run test
retCode := m.Run()
// teardown functions
os.Exit(retCode)
}
func TestSkipCache(t *testing.T) {
// Success test cases
testCases := []struct {
storeType string
scope string
action string
prNum string
expected bool
}{
{"cache", "pipeline", "get", "", false},
{"cache", "pipeline", "get", "123", false},
{"cache", "pipeline", "set", "123", true},
{"cache", "pipeline", "remove", "123", true},
{"cache", "event", "get", "", false},
{"cache", "job", "get", "", false},
{"cache", "job", "set", "123", true},
{"artifact", "event", "get", "", false},
{"log", "build", "set", "123", false},
}
for _, tc := range testCases {
os.Setenv("SD_PULL_REQUEST", tc.prNum)
skip := skipCache(tc.storeType, tc.scope, tc.action)
if skip != tc.expected {
t.Fatalf("%s %s for scope %s, expected '%t' got '%t'", tc.action, tc.storeType, tc.scope, tc.expected, skip)
}
}
}
func TestMakeURL(t *testing.T) {
os.Setenv("SD_STORE_URL", "http://store.screwdriver.cd/v1/")
os.Setenv("SD_BUILD_ID", "10038")
os.Setenv("SD_JOB_ID", "888")
os.Setenv("SD_EVENT_ID", "499")
os.Setenv("SD_PIPELINE_ID", "100")
abspath, _ := filepath.Abs("./")
abspath = url.PathEscape(abspath)
// Success test cases
testCases := []struct {
storeType string
scope string
key string
expected string
}{
{"cache", "job", "/myprcache", "http://store.screwdriver.cd/v1/caches/jobs/987/%2Fmyprcache"},
{"cache", "event", "/mycache", "http://store.screwdriver.cd/v1/caches/events/499/%2Fmycache"},
{"cache", "event", "mycache", fmt.Sprintf("%s%s", "http://store.screwdriver.cd/v1/caches/events/499/", "mycache")},
{"cache", "event", "./mycache", fmt.Sprintf("%s%s", "http://store.screwdriver.cd/v1/caches/events/499/", "mycache")},
{"cache", "event", "/tmp/mycache/1/2/3/4/", "http://store.screwdriver.cd/v1/caches/events/499/%2Ftmp%2Fmycache%2F1%2F2%2F3%2F4"},
{"cache", "event", "/!-_.*'()&@:,.$=+?; space", "http://store.screwdriver.cd/v1/caches/events/499/%2F%21-_.%2A%27%28%29&@:%2C.$=+%3F%3B%20space"},
{"artifact", "event", "artifact-1", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/artifact-1"},
{"artifact", "build", "test", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/test"},
{"artifact", "", ".test", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/.test"},
{"artifact", "", "./test", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/test"},
{"artifact", "", "test/foo", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/test%2Ffoo"},
{"artifact", "", "test/foo./bar", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/test%2Ffoo.%2Fbar"},
{"artifact", "", "test/foo/あいうえお.txt", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/test%2Ffoo%2F%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A.txt"},
{"artifact", "", "!-_.*'()&@:,.$=+?; space", "http://store.screwdriver.cd/v1/builds/10038/ARTIFACTS/%21-_.%2A%27%28%29&@:%2C.$=+%3F%3B%20space"},
{"log", "build", "testlog", "http://store.screwdriver.cd/v1/builds/10038-testlog"},
{"log", "build", "step-bookend", "http://store.screwdriver.cd/v1/builds/10038-step-bookend"},
{"log", "pipeline", "test-2", "http://store.screwdriver.cd/v1/builds/10038-test-2"},
}
for _, tc := range testCases {
if tc.key == "/myprcache" {
os.Setenv("SD_PULL_REQUEST", "900")
os.Setenv("SD_PR_PARENT_JOB_ID", "987")
}
i, _ := makeURL(tc.storeType, tc.scope, tc.key)
if i.String() != tc.expected {
t.Fatalf("Expected '%s' got '%s'", tc.expected, i)
}
}
// Error test case
var err error
storeType := "invalid"
scope := "pipelines"
key := "test2"
_, err = makeURL(storeType, scope, key)
if err == nil {
t.Fatalf("Expected error, got nil")
}
}
func TestGetTimeout(t *testing.T) {
testCases := []struct {
name string
flagTimeout string
envName string
defaultTimeout int
expected int
envValue string
shouldError bool
}{
{
name: "use flag timeout",
flagTimeout: "50",
envName: "SD_STORE_CLI_DOWNLOAD_HTTP_TIMEOUT",
defaultTimeout: 60,
expected: 50,
envValue: "",
shouldError: false,
},
{
name: "use environment timeout",
flagTimeout: "",
envName: "SD_STORE_CLI_DOWNLOAD_HTTP_TIMEOUT",
defaultTimeout: 60,
expected: 70,
envValue: "70",
shouldError: false,
},
{
name: "use default upload timeout",
flagTimeout: "",
envName: "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT",
defaultTimeout: UPLOAD_HTTP_TIMEOUT,
expected: 60,
envValue: "",
shouldError: false,
},
{
name: "use default download timeout",
flagTimeout: "",
envName: "SD_STORE_CLI_DOWNLOAD_HTTP_TIMEOUT",
defaultTimeout: DOWNLOAD_HTTP_TIMEOUT,
expected: 300,
envValue: "",
shouldError: false,
},
{
name: "use default remove timeout",
flagTimeout: "",
envName: "SD_STORE_CLI_REMOVE_HTTP_TIMEOUT",
defaultTimeout: REMOVE_HTTP_TIMEOUT,
expected: 300,
envValue: "",
shouldError: false,
},
{
name: "set flagTimeout to zero",
flagTimeout: "0",
envName: "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT",
defaultTimeout: UPLOAD_HTTP_TIMEOUT,
expected: 0,
envValue: "",
shouldError: false,
},
{
name: "set envValue to zero",
flagTimeout: "",
envName: "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT",
defaultTimeout: UPLOAD_HTTP_TIMEOUT,
expected: 0,
envValue: "0",
shouldError: false,
},
{
name: "Error case set flagTimeout to string",
flagTimeout: "dummystring",
envName: "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT",
defaultTimeout: UPLOAD_HTTP_TIMEOUT,
expected: 0,
envValue: "",
shouldError: true,
},
{
name: "Error case set envValue to string",
flagTimeout: "",
envName: "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT",
defaultTimeout: UPLOAD_HTTP_TIMEOUT,
expected: 0,
envValue: "dummystring",
shouldError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.envValue != "" {
os.Setenv(tc.envName, tc.envValue)
}
got, err := getTimeout(tc.flagTimeout, tc.envName, tc.defaultTimeout)
if err != nil {
if tc.shouldError {
return
}
t.Fatalf("getTimeout() error = %v", err)
return
}
// If we reach here, the test get error.
if tc.shouldError {
t.Fatal("getTimeout() expected an error, but got nil")
}
if got != tc.expected {
t.Fatalf("getTimeout() got = %v, want %v", got, tc.expected)
}
// Clear environment value for the next test case.
os.Unsetenv(tc.envName)
})
}
}
func TestIsEnableExpectHeader(t *testing.T) {
tests := []struct {
envValue string
expected bool
}{
{"true", true},
{"false", false},
{"1", false},
{"invalid", false},
{"", false},
}
for _, tt := range tests {
t.Run("env:"+tt.envValue, func(t *testing.T) {
os.Setenv("SD_ENABLE_EXPECT_HEADER", tt.envValue)
result := IsEnableExpectHeader()
if result != tt.expected {
t.Errorf("expected %t, got %t", tt.expected, result)
}
})
}
os.Unsetenv("SD_ENABLE_EXPECT_HEADER")
}