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
|
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/* eslint-disable no-console */
/* eslint-disable n/no-unpublished-import */
/* eslint-disable n/no-extraneous-import */
import Docker from 'dockerode'
import waitOn from 'wait-on'
import { c as createTar } from 'tar'
import path, { basename } from 'path'
import { execSync } from 'child_process'
import { existsSync } from 'fs'
export const docker = new Docker()
const CONTAINER_NAME = `nextcloud-cypress-tests_${basename(process.cwd()).replace(' ', '')}`
const SERVER_IMAGE = 'ghcr.io/nextcloud/continuous-integration-shallow-server'
/**
* Start the testing container
*
* @param {string} branch the branch of your current work
*/
export const startNextcloud = async function(branch: string = getCurrentGitBranch()): Promise<any> {
try {
try {
// Pulling images
console.log('\nPulling images... ⏳')
await new Promise((resolve, reject): any => docker.pull(SERVER_IMAGE, (err, stream) => {
if (err) {
reject(err)
}
if (stream === null) {
reject(new Error('Could not connect to docker, ensure docker is running.'))
return
}
// https://github.com/apocas/dockerode/issues/357
docker.modem.followProgress(stream, onFinished)
function onFinished(err) {
if (!err) {
resolve(true)
return
}
reject(err)
}
}))
console.log('└─ Done')
} catch (e) {
console.log('└─ Failed to pull images')
throw e
}
// Remove old container if exists
console.log('\nChecking running containers... 🔍')
try {
const oldContainer = docker.getContainer(CONTAINER_NAME)
const oldContainerData = await oldContainer.inspect()
if (oldContainerData) {
console.log('├─ Existing running container found')
console.log('├─ Removing... ⏳')
// Forcing any remnants to be removed just in case
await oldContainer.remove({ force: true })
console.log('└─ Done')
}
} catch (error) {
console.log('└─ None found!')
}
// Starting container
console.log('\nStarting Nextcloud container... 🚀')
console.log(`├─ Using branch '${branch}'`)
const container = await docker.createContainer({
Image: SERVER_IMAGE,
name: CONTAINER_NAME,
HostConfig: {
Mounts: [{
Target: '/var/www/html/data',
Source: '',
Type: 'tmpfs',
ReadOnly: false,
}],
},
Env: [
`BRANCH=${branch}`,
'APCU=1',
],
})
await container.start()
// Set proper permissions for the data folder
await runExec(container, ['chown', '-R', 'www-data:www-data', '/var/www/html/data'], false, 'root')
await runExec(container, ['chmod', '0770', '/var/www/html/data'], false, 'root')
// Init Nextcloud
// await runExec(container, ['initnc.sh'], true, 'root')
// Get container's IP
const ip = await getContainerIP(container)
console.log(`├─ Nextcloud container's IP is ${ip} 🌏`)
return ip
} catch (err) {
console.log('└─ Unable to start the container 🛑')
console.log('\n', err, '\n')
stopNextcloud()
throw new Error('Unable to start the container')
}
}
/**
* Configure Nextcloud
*/
export const configureNextcloud = async function() {
console.log('\nConfiguring nextcloud...')
const container = docker.getContainer(CONTAINER_NAME)
await runExec(container, ['php', 'occ', '--version'], true)
// Be consistent for screenshots
await runExec(container, ['php', 'occ', 'config:system:set', 'default_language', '--value', 'en'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'force_language', '--value', 'en'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'default_locale', '--value', 'en_US'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'force_locale', '--value', 'en_US'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'enforce_theme', '--value', 'light'], true)
// Speed up test and make them less flaky. If a cron execution is needed, it can be triggered manually.
await runExec(container, ['php', 'occ', 'background:cron'], true)
// Checking apcu
const distributed = await runExec(container, ['php', 'occ', 'config:system:get', 'memcache.distributed'])
const local = await runExec(container, ['php', 'occ', 'config:system:get', 'memcache.local'])
const hashing = await runExec(container, ['php', 'occ', 'config:system:get', 'hashing_default_password'])
console.log('├─ Checking APCu configuration... 👀')
if (!distributed.trim().includes('Memcache\\APCu')
|| !local.trim().includes('Memcache\\APCu')
|| !hashing.trim().includes('true')) {
console.log('└─ APCu is not properly configured 🛑')
throw new Error('APCu is not properly configured')
}
console.log('│ └─ OK !')
// Saving DB state
console.log('├─ Creating init DB snapshot...')
await runExec(container, ['cp', '/var/www/html/data/owncloud.db', '/var/www/html/data/owncloud.db-init'], true)
console.log('├─ Creating init data backup...')
await runExec(container, ['tar', 'cf', 'data-init.tar', 'admin'], true, undefined, '/var/www/html/data')
console.log('└─ Nextcloud is now ready to use 🎉')
}
/**
* Applying local changes to the container
* Only triggered if we're not in CI. Otherwise the
* continuous-integration-shallow-server image will
* already fetch the proper branch.
*/
export const applyChangesToNextcloud = async function() {
console.log('\nApply local changes to nextcloud...')
const htmlPath = '/var/www/html'
const folderPaths = [
'./3rdparty',
'./apps',
'./core',
'./dist',
'./lib',
'./ocs',
'./ocs-provider',
'./resources',
'./console.php',
'./cron.php',
'./index.php',
'./occ',
'./public.php',
'./remote.php',
'./status.php',
'./version.php',
].filter((folderPath) => {
const fullPath = path.resolve(__dirname, '..', folderPath)
if (existsSync(fullPath)) {
console.log(`├─ Copying ${folderPath}`)
return true
}
return false
})
// Don't try to apply changes, when there are none. Otherwise we
// still execute the 'chown' command, which is not needed.
if (folderPaths.length === 0) {
console.log('└─ No local changes found to apply')
return
}
const container = docker.getContainer(CONTAINER_NAME)
// Tar-streaming the above folders into the container
const serverTar = createTar({ gzip: false }, folderPaths)
await container.putArchive(serverTar, {
path: htmlPath,
})
// Making sure we have the proper permissions
await runExec(container, ['chown', '-R', 'www-data:www-data', htmlPath], false, 'root')
console.log('└─ Changes applied successfully 🎉')
}
/**
* Force stop the testing container
*/
export const stopNextcloud = async function() {
try {
const container = docker.getContainer(CONTAINER_NAME)
console.log('Stopping Nextcloud container...')
container.remove({ force: true })
console.log('└─ Nextcloud container removed 🥀')
} catch (err) {
console.log(err)
}
}
/**
* Get the testing container's IP
*
* @param {Docker.Container} container the container to get the IP from
*/
export const getContainerIP = async function(
container = docker.getContainer(CONTAINER_NAME),
): Promise<string> {
let ip = ''
let tries = 0
while (ip === '' && tries < 10) {
tries++
await container.inspect(function(err, data) {
if (err) {
throw err
}
ip = data?.NetworkSettings?.IPAddress || ''
})
if (ip !== '') {
break
}
await sleep(1000 * tries)
}
return ip
}
// Would be simpler to start the container from cypress.config.ts,
// but when checking out different branches, it can take a few seconds
// Until we can properly configure the baseUrl retry intervals,
// We need to make sure the server is already running before cypress
// https://github.com/cypress-io/cypress/issues/22676
export const waitOnNextcloud = async function(ip: string) {
console.log('├─ Waiting for Nextcloud to be ready... ⏳')
await waitOn({
resources: [`http://${ip}/index.php`],
// wait for nextcloud to be up and return any non error status
validateStatus: (status) => status >= 200 && status < 400,
// timout in ms
timeout: 5 * 60 * 1000,
// timeout for a single HTTP request
httpTimeout: 60 * 1000,
})
console.log('└─ Done')
}
const runExec = async function(
container: Docker.Container,
command: string[],
verbose = false,
user = 'www-data',
workdir?: string,
): Promise<string> {
const exec = await container.exec({
Cmd: command,
WorkingDir: workdir,
AttachStdout: true,
AttachStderr: true,
User: user,
})
return new Promise((resolve, reject) => {
let output = ''
exec.start({}, (err, stream) => {
if (err) {
reject(err)
}
if (stream) {
stream.setEncoding('utf-8')
stream.on('data', str => {
str = str.trim()
// Remove non printable characters
.replace(/[^\x0A\x0D\x20-\x7E]+/g, '')
// Remove non alphanumeric leading characters
.replace(/^[^a-z]/gi, '')
output += str
if (verbose && str !== '') {
console.log(`├─ ${str.replace(/\n/gi, '\n├─ ')}`)
}
})
stream.on('end', () => resolve(output))
}
})
})
}
const sleep = function(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds))
}
const getCurrentGitBranch = function() {
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim() || 'master'
}
|