-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathsgcollect_test.go
224 lines (204 loc) · 6.79 KB
/
sgcollect_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
/*
Copyright 2018-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package rest
import (
"fmt"
"os"
"regexp"
"strings"
"testing"
"github.com/couchbase/sync_gateway/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSgcollectFilename(t *testing.T) {
filename := sgcollectFilename()
// Check it doesn't have forbidden chars
assert.False(t, strings.ContainsAny(filename, "\\/:*?\"<>|"))
pattern := `^sgcollectinfo\-\d{4}\-\d{2}\-\d{2}t\d{6}\-sga?@(\d{1,3}\.){4}zip$`
matched, err := regexp.Match(pattern, []byte(filename))
assert.NoError(t, err, "unexpected regexp error")
assert.True(t, matched, fmt.Sprintf("Filename: %s did not match pattern: %s", filename, pattern))
}
func TestSgcollectOptionsValidateValid(t *testing.T) {
tests := []struct {
name string
options *sgCollectOptions
}{
{
name: "defaults",
options: &sgCollectOptions{},
},
{
name: "upload with customer name",
options: &sgCollectOptions{Upload: true, Customer: "alice"},
},
{
name: "custom upload with customer name",
options: &sgCollectOptions{Upload: true, Customer: "alice", UploadHost: "example.org/custom-s3-bucket-url"},
},
{
name: "directory that exists",
options: &sgCollectOptions{OutputDirectory: "."},
},
{
name: "valid redact level",
options: &sgCollectOptions{RedactLevel: "partial"},
},
{
name: "valid keep_zip option",
options: &sgCollectOptions{KeepZip: true},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Nil(t, test.options.Validate())
})
}
}
func TestSgcollectOptionsValidateInvalid(t *testing.T) {
binaryPath, err := os.Executable()
assert.NoError(t, err, "unexpected error getting executable path")
tests := []struct {
name string
options *sgCollectOptions
errContains string
}{
{
name: "directory doesn't exist",
options: &sgCollectOptions{OutputDirectory: "/path/to/output/dir"},
errContains: "no such file or directory",
},
{
name: "path not a directory",
options: &sgCollectOptions{OutputDirectory: binaryPath},
errContains: "not a directory",
},
{
name: "invalid redact level",
options: &sgCollectOptions{RedactLevel: "asdf"},
errContains: "'redact_level' must be",
},
{
name: "no customer",
options: &sgCollectOptions{Upload: true},
errContains: "'customer' must be set",
},
{
name: "no customer with ticket",
options: &sgCollectOptions{Upload: true, Ticket: "12345"},
errContains: "'customer' must be set",
},
{
name: "customer no upload",
options: &sgCollectOptions{Upload: false, Customer: "alice"},
errContains: "'upload' must be set to true",
},
{
name: "ticket no upload",
options: &sgCollectOptions{Upload: false, Ticket: "12345"},
errContains: "'upload' must be set to true",
},
{
name: "customer upload host no upload",
options: &sgCollectOptions{Upload: false, Customer: "alice", UploadHost: "example.org/custom-s3-bucket-url"},
errContains: "'upload' must be set to true",
},
{
name: "non-digit ticket number",
options: &sgCollectOptions{Upload: true, Customer: "alice", Ticket: "abc"},
errContains: "'ticket' must be",
},
}
for _, test := range tests {
t.Run(test.name, func(ts *testing.T) {
errs := test.options.Validate()
require.NotNil(t, errs)
multiError, ok := errs.(*base.MultiError)
require.True(t, ok)
// make sure we get at least one error for the given invalid options.
require.True(t, multiError.Len() > 0)
// check each error matches the expected string.
for _, err := range multiError.Errors {
assert.Contains(ts, err.Error(), test.errContains)
}
})
}
}
func TestSgcollectOptionsArgs(t *testing.T) {
binPath, err := os.Executable()
assert.NoError(t, err, "unexpected error getting executable path")
tests := []struct {
options *sgCollectOptions
expectedArgs []string
}{
{
options: &sgCollectOptions{},
expectedArgs: []string{},
},
{
options: &sgCollectOptions{Upload: true},
expectedArgs: []string{"--upload-host", defaultSGUploadHost},
},
{
options: &sgCollectOptions{Upload: true, Ticket: "123456", KeepZip: true},
expectedArgs: []string{"--upload-host", defaultSGUploadHost, "--ticket", "123456", "--keep-zip"},
},
{
options: &sgCollectOptions{Upload: true, RedactLevel: "partial"},
expectedArgs: []string{"--upload-host", defaultSGUploadHost, "--log-redaction-level", "partial"},
},
{
options: &sgCollectOptions{Upload: true, RedactLevel: "partial", RedactSalt: "asdf"},
expectedArgs: []string{"--upload-host", defaultSGUploadHost, "--log-redaction-level", "partial", "--log-redaction-salt", "asdf"},
},
{
// Check that the default upload host is set
options: &sgCollectOptions{Upload: true, Customer: "alice"},
expectedArgs: []string{"--upload-host", defaultSGUploadHost, "--customer", "alice"},
},
{
options: &sgCollectOptions{Upload: true, Customer: "alice", UploadHost: "example.org/custom-s3-bucket-url"},
expectedArgs: []string{"--upload-host", "example.org/custom-s3-bucket-url", "--customer", "alice"},
},
{
options: &sgCollectOptions{Upload: true, Customer: "alice", UploadHost: "https://example.org/custom-s3-bucket-url", UploadProxy: "http://proxy.example.org:8080"},
expectedArgs: []string{"--upload-host", "https://example.org/custom-s3-bucket-url", "--upload-proxy", "http://proxy.example.org:8080", "--customer", "alice"},
},
{
// Upload false, so don't pass upload host through. same for keep zip
options: &sgCollectOptions{Upload: false, Customer: "alice", UploadHost: "example.org/custom-s3-bucket-url", KeepZip: false},
expectedArgs: []string{"--customer", "alice"},
},
{
// Directory exists
options: &sgCollectOptions{OutputDirectory: "."},
expectedArgs: []string{},
},
{
// Directory doesn't exist
options: &sgCollectOptions{OutputDirectory: "/path/to/output/dir"},
expectedArgs: []string{},
},
{
// Path not a directory
options: &sgCollectOptions{OutputDirectory: binPath},
expectedArgs: []string{},
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(ts *testing.T) {
// We'll run validate to populate some default fields,
// but ignore errors raised by it for this test.
_ = test.options.Validate()
args := test.options.Args()
assert.Equal(ts, test.expectedArgs, args)
})
}
}