forked from taskcluster/generic-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_helper_test.go
More file actions
280 lines (257 loc) · 9.07 KB
/
aws_helper_test.go
File metadata and controls
280 lines (257 loc) · 9.07 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/taskcluster/taskcluster-client-go/tcpurgecache"
"github.com/taskcluster/taskcluster-client-go/tcqueue"
)
type MockAWSProvisionedEnvironment struct {
PublicFiles []map[string]string
PrivateFiles []map[string]string
PublicConfig map[string]interface{}
PrivateConfig map[string]interface{}
WorkerTypeSecretFunc func(t *testing.T, w http.ResponseWriter)
WorkerTypeDefinitionUserDataFunc func(t *testing.T) interface{}
Terminating bool
PretendMetadata string
OldDeploymentID string
NewDeploymentID string
// Set when provisioner secret (credentials) gets deleted
SecretDeleted bool
}
func (m *MockAWSProvisionedEnvironment) ValidPublicConfig(t *testing.T) map[string]interface{} {
result := map[string]interface{}{
// Need common caches directory across tests, since files
// directory-caches.json and file-caches.json are not per-test.
"cachesDir": filepath.Join(cwd, "caches"),
"cleanUpTaskDirs": false,
"deploymentId": m.OldDeploymentID,
"disableReboots": true,
// Need common downloads directory across tests, since files
// directory-caches.json and file-caches.json are not per-test.
"downloadsDir": filepath.Join(cwd, "downloads"),
"idleTimeoutSecs": 60,
"numberOfTasksToRun": 1,
// should be enough for tests, and travis-ci.org CI environments
// don't have a lot of free disk
"queueBaseURL": tcqueue.New(nil, os.Getenv("TASKCLUSTER_ROOT_URL")).BaseURL,
"purgeCacheBaseURL": tcpurgecache.New(nil, os.Getenv("TASKCLUSTER_ROOT_URL")).BaseURL,
"requiredDiskSpaceMegabytes": 16,
// "secretsBaseURL": "http://localhost:13243/secrets",
"sentryProject": "generic-worker-tests",
"shutdownMachineOnIdle": false,
"shutdownMachineOnInternalError": false,
"ed25519SigningKeyLocation": filepath.Join(testdataDir, "ed25519_private_key"),
"subdomain": "taskcluster-worker.net",
"tasksDir": filepath.Join(testdataDir, t.Name()),
"workerTypeMetadata": map[string]interface{}{
"machine-setup": map[string]string{
"pretend-metadata": m.PretendMetadata,
},
},
}
EngineTestSettings(result)
return result
}
func (m *MockAWSProvisionedEnvironment) ValidPrivateConfig(t *testing.T) map[string]interface{} {
return map[string]interface{}{
"livelogSecret": "I have to confess, when me and my friends sort of used to run through the fields of wheat, um, the farmers weren't too pleased about that.",
}
}
func WriteJSON(t *testing.T, w http.ResponseWriter, resp interface{}) {
bytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
t.Fatalf("Strange - I can't convert %#v to json: %v", resp, err)
}
w.Write(bytes)
}
func (m *MockAWSProvisionedEnvironment) workerTypeDefinition(t *testing.T, w http.ResponseWriter) {
resp := map[string]interface{}{
"userData": map[string]interface{}{
"genericWorker": map[string]interface{}{
"config": map[string]interface{}{
"deploymentId": m.NewDeploymentID,
},
},
},
}
WriteJSON(t, w, resp)
}
func (m *MockAWSProvisionedEnvironment) credentials(t *testing.T, w http.ResponseWriter) {
resp := map[string]interface{}{
"credentials": map[string]string{
"clientId": os.Getenv("TASKCLUSTER_CLIENT_ID"),
"certificate": os.Getenv("TASKCLUSTER_CERTIFICATE"),
"accessToken": os.Getenv("TASKCLUSTER_ACCESS_TOKEN"),
},
"scopes": []string{},
}
WriteJSON(t, w, resp)
}
func (m *MockAWSProvisionedEnvironment) workerTypeSecret(t *testing.T, w http.ResponseWriter) {
if m.WorkerTypeSecretFunc != nil {
m.WorkerTypeSecretFunc(t, w)
return
}
resp := map[string]interface{}{
"secret": m.PrivateHostSetup(t),
"expires": "2077-08-19T00:00:00.000Z",
}
WriteJSON(t, w, resp)
}
func (m *MockAWSProvisionedEnvironment) userData(t *testing.T, w http.ResponseWriter, workerType string) {
var data interface{}
if m.WorkerTypeDefinitionUserDataFunc != nil {
data = m.WorkerTypeDefinitionUserDataFunc(t)
} else {
data = m.WorkerTypeDefinitionUserData(t)
}
resp := map[string]interface{}{
"data": data,
"capacity": 1,
"workerType": workerType,
"provisionerId": "test-provisioner",
"region": "test-worker-group",
"availabilityZone": "neuss-germany",
"instanceType": "p3.teenyweeny",
"spotBid": 3.5,
"price": 3.02,
// "taskclusterRootUrl": os.Getenv("TASKCLUSTER_ROOT_URL"), // don't use tcclient.RootURLFromEnvVars() since we don't want ClientID of CI
"taskclusterRootUrl": "http://localhost:13243",
"launchSpecGenerated": time.Now(),
"lastModified": time.Now().Add(time.Minute * -30),
// "provisionerBaseUrl": "http://localhost:13243/provisioner",
"securityToken": "12345",
}
WriteJSON(t, w, resp)
}
func (m *MockAWSProvisionedEnvironment) WorkerTypeDefinitionUserData(t *testing.T) map[string]map[string]interface{} {
workerTypeDefinitionUserData := map[string]map[string]interface{}{
"genericWorker": map[string]interface{}{},
}
if m.PublicConfig != nil {
workerTypeDefinitionUserData["genericWorker"]["config"] = m.PublicConfig
} else {
workerTypeDefinitionUserData["genericWorker"]["config"] = m.ValidPublicConfig(t)
}
if m.PublicFiles != nil {
workerTypeDefinitionUserData["genericWorker"]["files"] = m.PublicFiles
}
return workerTypeDefinitionUserData
}
func (m *MockAWSProvisionedEnvironment) PrivateHostSetup(t *testing.T) interface{} {
privateHostSetup := map[string]interface{}{}
if m.PrivateConfig != nil {
privateHostSetup["config"] = m.PrivateConfig
} else {
privateHostSetup["config"] = m.ValidPrivateConfig(t)
}
if m.PrivateFiles != nil {
privateHostSetup["files"] = m.PrivateFiles
}
return privateHostSetup
}
func (m *MockAWSProvisionedEnvironment) Setup(t *testing.T) (teardown func(), err error) {
td := setupEnvironment(t)
workerType := testWorkerType()
configureForAWS = true
oldEC2MetadataBaseURL := EC2MetadataBaseURL
EC2MetadataBaseURL = "http://localhost:13243/latest"
// Create custom *http.ServeMux rather than using http.DefaultServeMux, so
// registered handler functions won't interfere with future tests that also
// use http.DefaultServeMux.
ec2MetadataHandler := http.NewServeMux()
ec2MetadataHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
switch req.URL.EscapedPath() {
// simulate provisioner endpoints
case "/api/aws-provisioner/v1/worker-type/" + workerType:
m.workerTypeDefinition(t, w)
case "/api/aws-provisioner/v1/secret/12345":
switch req.Method {
case "GET":
m.credentials(t, w)
case "DELETE":
fmt.Fprint(w, "Credentials deleted, yay!")
m.SecretDeleted = true
default:
w.WriteHeader(400)
}
// simulate taskcluster secrets endpoints
case "/api/secrets/v1/secret/worker-type%3Atest-provisioner%2F" + workerType:
m.workerTypeSecret(t, w)
// simulate AWS endpoints
case "/latest/meta-data/ami-id":
fmt.Fprint(w, "test-ami")
case "/latest/meta-data/spot/termination-time":
if m.Terminating {
fmt.Fprint(w, "time to die")
} else {
w.WriteHeader(404)
}
case "/latest/meta-data/placement/availability-zone":
fmt.Fprint(w, "outer-space")
case "/latest/meta-data/instance-type":
fmt.Fprint(w, "p3.teenyweeny")
case "/latest/meta-data/instance-id":
fmt.Fprint(w, "test-instance-id")
case "/latest/meta-data/public-hostname":
fmt.Fprint(w, "MadamaButterfly")
case "/latest/meta-data/local-ipv4":
fmt.Fprint(w, "87.65.43.21")
case "/latest/meta-data/public-ipv4":
fmt.Fprint(w, "12.34.56.78")
case "/latest/user-data":
m.userData(t, w, workerType)
default:
w.WriteHeader(400)
fmt.Fprintf(w, "Cannot serve URL %v", req.URL)
}
})
s := &http.Server{
Addr: "localhost:13243",
Handler: ec2MetadataHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
s.ListenAndServe()
t.Log("HTTP server for mock Provisioner and EC2 metadata endpoints stopped")
}()
config, err = loadConfig(filepath.Join(testdataDir, t.Name(), "generic-worker.config"), true, false)
return func() {
td()
err := s.Shutdown(context.Background())
if err != nil {
t.Fatalf("Error shutting down http server: %v", err)
}
if !m.SecretDeleted {
t.Fatal("Provisioner secret (credentials) not deleted")
}
EC2MetadataBaseURL = oldEC2MetadataBaseURL
configureForAWS = false
}, err
}
func (m *MockAWSProvisionedEnvironment) ExpectError(t *testing.T, errorText string) (teardown func()) {
var err error
teardown, err = m.Setup(t)
if err == nil || !strings.Contains(err.Error(), errorText) {
t.Fatalf("Was expecting error to include %q but got: %v", errorText, err)
}
return
}
func (m *MockAWSProvisionedEnvironment) ExpectNoError(t *testing.T) (teardown func()) {
var err error
teardown, err = m.Setup(t)
if err != nil {
t.Fatalf("Was expecting no error but got: %v", err)
}
return
}