-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdata_test.go
247 lines (220 loc) · 7.38 KB
/
testdata_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
package main
import (
"flag"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
connspy "github.com/j0hnsmith/connspy/http"
"github.com/jarcoal/httpmock"
"github.com/fatih/color"
goldie "github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/require"
)
func init() {
color.NoColor = false
}
func sniffHTTP() func() {
realClient := http.DefaultClient
http.DefaultClient = connspy.NewClient(nil, nil)
return func() {
http.DefaultClient = realClient
}
}
var liveTest = flag.Bool("live", false, "enable live testing")
var acronisTestURL = url.URL{
Scheme: "http",
Host: "dev-cloud.acronis.com",
}
const (
acronisTestUser = "testuser"
acronisTestPass = "hunter2"
)
var testAcronisAPI_Auth_testdata = map[string]interface{}{
"access_token": "testAcronisValidToken",
"id_token": "testAcronisValidTokenInstanceID",
"token_type": "bearer",
}
// httpmockRegisterFiles walks a directory and sets up file responders
//
// generally it registeres items at path of (with whatever topdir):
// [topdir]/[method]/[domain]/uri
//
// as a specific example, topdir of testdata/mock/http it assumes paths like
// testdata/mock/http/GET/dev-cloud.acronis.com/api/2/clients/testuser
func httpmockRegisterFiles(t *testing.T, topdir string) {
// ensure trailing / on topdir
if !strings.HasSuffix(topdir, string(os.PathSeparator)) {
topdir += string(os.PathSeparator)
}
// for each file in the given topdir path
require.NoError(t, filepath.Walk(topdir,
func(fpath string, info os.FileInfo, err error) error {
// if it's not a file, skip it
if info == nil || !info.Mode().IsRegular() {
return nil
}
// read in the target file, should be no error possible
fileBytes, err := ioutil.ReadFile(fpath)
if err != nil {
panic(err)
}
// the responder that we'll assign multiple times
responder := func(req *http.Request) (*http.Response, error) {
// check the auth token, if no match 401 reject
reqToken := req.Header.Get("Authorization")
reqToken = strings.TrimSpace(reqToken) // trim spaces around both
reqToken = strings.TrimPrefix(reqToken, "Bearer") // strip bearer
reqToken = strings.TrimSpace(reqToken) // remove any more spaces
// if the auth token was there and matched, return the fileBytes
if reqToken == testAcronisAPI_Auth_testdata["access_token"] {
return httpmock.NewBytesResponse(http.StatusOK, fileBytes), nil
}
// otherwise 401 unauthorized
return httpmock.NewJsonResponse(
http.StatusUnauthorized,
map[string]map[string]string{"error": {"message": "OK"}},
)
}
// now to find the url and method
// chop the topdir off the file
relFPath := strings.TrimPrefix(fpath, topdir)
filepathParts := strings.Split(relFPath, string(os.PathSeparator))
method := filepathParts[0] // first element
url := path.Join(filepathParts[1:]...) // rest is the url
// then, register that responder on every protocol imagined
httpmock.RegisterResponder(method, url, responder)
httpmock.RegisterResponder(method, "http://"+url, responder)
httpmock.RegisterResponder(method, "https://"+url, responder)
return nil
},
))
return
}
var testAcronisAPI_ClientTenant_testdata = struct {
Type string `json:"type"`
TenantID string `json:"tenant_id"`
Data struct {
ClientName string `json:"client_name"`
AgentType string `json:"agent_type"`
Hostname string `json:"hostname"`
} `json:"data"`
Status string `json:"status"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}{
Type: "api_client",
Data: struct {
ClientName string `json:"client_name"`
AgentType string `json:"agent_type"`
Hostname string `json:"hostname"`
}{ClientName: "Acronis-Exporter dev client"},
TenantID: "c8e6259d-a4d7-4ffc-8614-79c1d143cc54", // random uuid
Status: "enabled",
}
func goldenAssert(t *testing.T, filename string, actual []byte) {
goldie.New(t,
goldie.WithFixtureDir("testdata/fixtures"),
goldie.WithNameSuffix(".golden"),
goldie.WithTestNameForDir(true),
goldie.WithDiffEngine(goldie.ColoredDiff),
).Assert(t, filename, actual)
}
// note, returns backwards of t.Failed()
func assertGoldenFile(t *testing.T, filename string) {
fileBytes, err := ioutil.ReadFile(filename)
require.NoError(t, err)
goldenAssert(t, filepath.Base(filename), fileBytes)
}
func readerGoldenAssert(t *testing.T, name string, actual io.Reader) {
fileBytes, err := ioutil.ReadAll(actual)
require.NoError(t, err)
goldenAssert(t, name, fileBytes)
}
var testReadTask_testdata = map[string]error{
"testdata/mock/byTask/020c2794-e24c-4c78-af8f-f5f4f6cca110.json": nil,
"testdata/mock/byTask/391cf484-f9a9-4491-b379-d18cec00fa55.json": nil,
"testdata/mock/byTask/680d2028-0279-4642-90e8-ffa9fcc08a5c.json": nil,
"testdata/mock/byTask/7130f8f5-192f-4017-b668-d0cad9b672a0.json": nil,
"testdata/mock/byTask/7974c902-5165-41bd-8077-65b4ab90a62b.json": nil,
"testdata/mock/byTask/bd78859e-531a-4173-ba84-3d6d5bd62fff.json": nil,
"testdata/mock/byTask/eff6f78f-584e-46e7-9b78-9da3771cf2ba.json": nil,
"testdata/mock/byTask/fdec0d76-e405-4cd9-b657-37a9cdf314c7.json": nil,
"testdata/mock/byTask/missing.json": os.ErrNotExist,
}
var testWriteTask_testdata = map[string]struct {
taskPath string
cacheDir string
expErr string
}{
"normal": {
taskPath: "testdata/mock/byTask/020c2794-e24c-4c78-af8f-f5f4f6cca110.json",
cacheDir: "testdata/cache/writeTask",
},
"badDest": {
taskPath: "testdata/mock/byTask/020c2794-e24c-4c78-af8f-f5f4f6cca110.json",
cacheDir: "testdata/missing",
expErr: "open testdata/missing/badDest.json: no such file or directory",
},
}
var testFilterTaskUpdatesOnly_testdata = map[string]struct {
taskPath []string
cacheDir string
lastTs time.Time
expErr string
}{
"normal": {
taskPath: []string{
"testdata/mock/byTask/020c2794-e24c-4c78-af8f-f5f4f6cca110.json",
},
cacheDir: "testdata/cache/filterUpdates",
},
"olderFirst": {
taskPath: []string{
"testdata/mock/byTask/7130f8f5-192f-4017-b668-d0cad9b672a0.json",
"testdata/mock/byTask/391cf484-f9a9-4491-b379-d18cec00fa55.json",
},
cacheDir: "testdata/cache/filterUpdates",
},
"newerFirst": {
taskPath: []string{
"testdata/mock/byTask/391cf484-f9a9-4491-b379-d18cec00fa55.json",
"testdata/mock/byTask/7130f8f5-192f-4017-b668-d0cad9b672a0.json",
},
cacheDir: "testdata/cache/filterUpdates",
},
"badDest": {
taskPath: []string{"testdata/mock/byTask/020c2794-e24c-4c78-af8f-f5f4f6cca110.json"},
cacheDir: "testdata/missing",
expErr: "open testdata/missing/badDest.json: no such file or directory",
},
}
var testTaskToRegistry_testdata = map[string]string{
"first": "testdata/mock/byTask/7130f8f5-192f-4017-b668-d0cad9b672a0.json",
"second": "testdata/mock/byTask/391cf484-f9a9-4491-b379-d18cec00fa55.json",
}
var testProbeHandler_testdata = []string{
"020c2794-e24c-4c78-af8f-f5f4f6cca110",
"391cf484-f9a9-4491-b379-d18cec00fa55",
"680d2028-0279-4642-90e8-ffa9fcc08a5c",
"7130f8f5-192f-4017-b668-d0cad9b672a0",
"7974c902-5165-41bd-8077-65b4ab90a62b",
"bd78859e-531a-4173-ba84-3d6d5bd62fff",
"eff6f78f-584e-46e7-9b78-9da3771cf2ba",
"fdec0d76-e405-4cd9-b657-37a9cdf314c7",
"missing",
}
var testTenantIDToUUID_testdata = map[string]struct {
id string
uuid string
}{
"C3R2PB": {id: "1272636", uuid: "1ca2ea47-e6f1-48af-9328-41757c298d03"},
"RZU0ND": {id: "1272639", uuid: "e8846c9a-41db-4534-bcbb-29b21a5eb34d"},
}