-
Notifications
You must be signed in to change notification settings - Fork 46
/
setup.coffee
398 lines (354 loc) · 9.93 KB
/
setup.coffee
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
Bluebird = require('bluebird')
_ = require('lodash')
{ chai } = require('mochainon')
chai.use(require('chai-samsam'))
exports.IS_BROWSER = IS_BROWSER = window?
{ getInitialOrganization } = require('./utils')
if IS_BROWSER
require('js-polyfills/es6')
getSdk = window.balenaSdk
env = window.__env__
apiUrl = env.TEST_API_URL || 'https://api.balena-cloud.com'
opts =
apiUrl: apiUrl
builderUrl: env.TEST_BUILDER_URL || apiUrl.replace('api.', 'builder.')
else
getSdk = require('../..')
settings = require('balena-settings-client')
env = process.env
apiUrl = env.TEST_API_URL || settings.get('apiUrl')
opts =
apiUrl: apiUrl
builderUrl: env.TEST_BUILDER_URL || apiUrl.replace('api.', 'builder.')
dataDirectory: settings.get('dataDirectory')
_.assign opts,
isBrowser: IS_BROWSER,
retries: 3
console.log("Running SDK tests against: #{opts.apiUrl}")
console.log("TEST_USERNAME: #{env?.TEST_USERNAME}")
buildCredentials = ->
if not env
throw new Error('Missing environment object?!')
credentials =
email: env.TEST_EMAIL
password: env.TEST_PASSWORD
username: env.TEST_USERNAME
paid:
email: env.TEST_PAID_EMAIL
password: env.TEST_PAID_PASSWORD
register:
email: env.TEST_REGISTER_EMAIL
password: env.TEST_REGISTER_PASSWORD
username: env.TEST_REGISTER_USERNAME
if not _.every [
credentials.email?
credentials.password?
credentials.username?
credentials.register.email?
credentials.register.password?
credentials.register.username?
]
throw new Error('Missing environment credentials')
return credentials
exports.getSdk = getSdk
exports.sdkOpts = opts
exports.balena = balena = getSdk(opts)
exports.resetUser = ->
return balena.auth.isLoggedIn().then (isLoggedIn) ->
return if not isLoggedIn
Bluebird.all [
balena.pine.delete
resource: 'application'
options:
$filter: 1: 1
balena.pine.delete
resource: 'user__has__public_key'
options:
$filter: 1: 1
balena.pine.delete
resource: 'api_key'
# only delete named user api keys
options: $filter: name: $ne: null
# Api keys can't delete api keys, just ignore failures here
.catchReturn()
]
exports.credentials = buildCredentials()
exports.givenLoggedInUserWithApiKey = (beforeFn) ->
beforeFn ->
balena.auth.login
email: exports.credentials.email
password: exports.credentials.password
.then ->
return balena.request.send
method: 'POST'
url: '/api-key/user/full'
baseUrl: opts.apiUrl
body:
name: 'apiKey'
.get('body')
.tap(balena.auth.logout)
.then(balena.auth.loginWithToken)
.then(exports.resetUser)
afterFn = if beforeFn == beforeEach then afterEach else after
afterFn ->
exports.resetUser()
exports.givenLoggedInUser = (beforeFn) ->
beforeFn ->
balena.auth.login
email: exports.credentials.email
password: exports.credentials.password
.then(exports.resetUser)
afterFn = if beforeFn == beforeEach then afterEach else after
afterFn ->
exports.resetUser()
exports.loginPaidUser = ->
balena.auth.login
email: exports.credentials.paid.email
password: exports.credentials.paid.password
resetApplications = ->
balena.pine.delete
resource: 'application'
options:
$filter: 1: 1
exports.givenInitialOrganization = (beforeFn) ->
beforeFn ->
getInitialOrganization().then (initialOrg) =>
@initialOrg = initialOrg
exports.givenAnApplication = (beforeFn) ->
exports.givenInitialOrganization(beforeFn)
beforeFn ->
balena.models.application.create
name: 'FooBar'
applicationType: 'microservices-starter'
deviceType: 'raspberry-pi'
organization: @initialOrg.id
.then (@application) =>
chai.expect(@application.is_for__device_type).to.be.an('object')
.that.has.property('__id').that.is.a('number')
balena.pine.get
resource: 'device_type'
id: @application.is_for__device_type.__id
options:
$select: 'slug'
.then (@applicationDeviceType) =>
chai.expect(@applicationDeviceType).to.be.an('object')
.that.has.property('slug').that.is.a('string')
afterFn = if beforeFn == beforeEach then afterEach else after
afterFn resetApplications
resetDevices = ->
balena.pine.delete
resource: 'device'
options:
$filter: 1: 1
exports.givenADevice = (beforeFn, extraDeviceProps) ->
beforeFn ->
uuid = balena.models.device.generateUniqueKey()
balena.models.device.register(@application.app_name, uuid)
.tap (deviceInfo) =>
if !@currentRelease || !@currentRelease.commit
return
balena.pine.patch
resource: 'device'
body:
is_running__release: @currentRelease.id
options:
$filter:
uuid: deviceInfo.uuid
.tap (deviceInfo) ->
if !extraDeviceProps
return
balena.pine.patch
resource: 'device'
body: extraDeviceProps
options:
$filter:
uuid: deviceInfo.uuid
.then (deviceInfo) ->
balena.models.device.get(deviceInfo.uuid)
.then (device) =>
@device = device
.tap (device) =>
if !@currentRelease || !@currentRelease.commit
return
Bluebird.all [
# Create image installs for the images on the device
balena.pine.post
resource: 'image_install'
body:
installs__image: @oldWebImage.id
is_provided_by__release: @oldRelease.id
device: device.id
download_progress: null
status: 'Running'
install_date: '2017-10-01'
,
balena.pine.post
resource: 'image_install'
body:
installs__image: @newWebImage.id
is_provided_by__release: @currentRelease.id
device: device.id
download_progress: 50,
status: 'Downloading'
install_date: '2017-10-30'
,
balena.pine.post
resource: 'image_install'
body:
installs__image: @oldDbImage.id
is_provided_by__release: @oldRelease.id
device: device.id
download_progress: 100,
status: 'deleted',
install_date: '2017-09-30'
,
balena.pine.post
resource: 'image_install'
body:
installs__image: @newDbImage.id
is_provided_by__release: @currentRelease.id
device: device.id
download_progress: null,
status: 'Running',
install_date: '2017-10-30'
]
.spread (oldWebInstall, newWebInstall, oldDbInstall, newDbInstall) =>
@oldWebInstall = oldWebInstall
@newWebInstall = newWebInstall
@newDbInstall = newDbInstall
afterFn = if beforeFn == beforeEach then afterEach else after
afterFn resetDevices
exports.givenAnApplicationWithADevice = (beforeFn) ->
exports.givenAnApplication(beforeFn)
exports.givenADevice(beforeFn)
exports.givenMulticontainerApplicationWithADevice = (beforeFn) ->
exports.givenMulticontainerApplication(beforeFn)
exports.givenADevice(beforeFn)
exports.givenMulticontainerApplication = (beforeFn) ->
exports.givenAnApplication(beforeFn)
beforeFn ->
balena.auth.getUserId().then (userId) =>
Bluebird.all [
# Register web & DB services
balena.pine.post
resource: 'service'
body:
application: @application.id
service_name: 'web'
,
balena.pine.post
resource: 'service'
body:
application: @application.id
service_name: 'db'
,
# Register an old & new release of this application
Bluebird.mapSeries [
resource: 'release'
body:
belongs_to__application: @application.id
is_created_by__user: userId
commit: 'old-release-commit'
status: 'success'
source: 'cloud'
composition: {}
start_timestamp: 1234
,
resource: 'release'
body:
belongs_to__application: @application.id
is_created_by__user: userId
commit: 'new-release-commit'
status: 'success'
source: 'cloud'
composition: {}
start_timestamp: 54321
], (pineParams) -> balena.pine.post pineParams
]
.spread (webService, dbService, [oldRelease, newRelease]) =>
@webService = webService
@dbService = dbService
@oldRelease = oldRelease
@currentRelease = newRelease
Bluebird.all [
# Register an old & new web image build from the old and new
# releases, a db build in the new release only
balena.pine.post
resource: 'image'
body:
is_a_build_of__service: webService.id
project_type: 'dockerfile'
content_hash: 'abc'
build_log: 'old web log'
start_timestamp: 1234
push_timestamp: 1234
status: 'success'
,
balena.pine.post
resource: 'image'
body:
is_a_build_of__service: webService.id
project_type: 'dockerfile'
content_hash: 'def'
build_log: 'new web log'
start_timestamp: 54321
push_timestamp: 54321
status: 'success'
,
balena.pine.post
resource: 'image'
body:
is_a_build_of__service: dbService.id
project_type: 'dockerfile'
content_hash: 'jkl'
build_log: 'old db log'
start_timestamp: 123
push_timestamp: 123
status: 'success'
,
balena.pine.post
resource: 'image'
body:
is_a_build_of__service: dbService.id
project_type: 'dockerfile'
content_hash: 'ghi'
build_log: 'new db log'
start_timestamp: 54321
push_timestamp: 54321
status: 'success'
]
.spread (oldWebImage, newWebImage, oldDbImage, newDbImage) =>
@oldWebImage = oldWebImage
@newWebImage = newWebImage
@oldDbImage = oldDbImage
@newDbImage = newDbImage
Bluebird.all [
# Tie the images to their corresponding releases
balena.pine.post
resource: 'image__is_part_of__release'
body:
image: oldWebImage.id
is_part_of__release: oldRelease.id
,
balena.pine.post
resource: 'image__is_part_of__release'
body:
image: oldDbImage.id
is_part_of__release: oldRelease.id
,
balena.pine.post
resource: 'image__is_part_of__release'
body:
image: newWebImage.id
is_part_of__release: newRelease.id
,
balena.pine.post
resource: 'image__is_part_of__release'
body:
image: newDbImage.id
is_part_of__release: newRelease.id
,
]
afterFn = if beforeFn == beforeEach then afterEach else after
afterFn ->
@currentRelease = null