Skip to content

Commit

Permalink
chore(cypress): enable redis
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 21, 2024
1 parent 7820536 commit e29bedc
Showing 1 changed file with 56 additions and 28 deletions.
84 changes: 56 additions & 28 deletions cypress/dockerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { existsSync } from 'fs'
export const docker = new Docker()

const CONTAINER_NAME = 'nextcloud-cypress-tests-server'
const CONTAINER_NAME_REDIS = `${CONTAINER_NAME}-redis`
const SERVER_IMAGE = 'ghcr.io/nextcloud/continuous-integration-shallow-server'

/**
Expand All @@ -29,30 +30,8 @@ export const startNextcloud = async function(branch: string = getCurrentGitBranc
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)

/**
*
* @param err
*/
function onFinished(err) {
if (!err) {
resolve(true)
return
}
reject(err)
}
}))
await pullImage(SERVER_IMAGE)
await pullImage('redis')
console.log('└─ Done')
} catch (e) {
console.log('└─ Failed to pull images')
Expand All @@ -65,12 +44,20 @@ export const startNextcloud = async function(branch: string = getCurrentGitBranc
const oldContainer = docker.getContainer(CONTAINER_NAME)
const oldContainerData = await oldContainer.inspect()
if (oldContainerData) {
console.log('├─ Existing running container found')
console.log('├─ Removing... ⏳')
console.log('├─ Removing existing container... ⏳')
// Forcing any remnants to be removed just in case
await oldContainer.remove({ force: true })
console.log('└─ Done')
}

const oldRedisContainer = docker.getContainer(CONTAINER_NAME_REDIS)
const oldRedisContainerData = await oldRedisContainer.inspect()
if (oldRedisContainerData) {
console.log('├─ Removing existing redis container... ⏳')
// Forcing any remnants to be removed just in case
await oldRedisContainer.remove({ force: true })
}

console.log('└─ Done')
} catch (error) {
console.log('└─ None found!')
}
Expand All @@ -90,6 +77,12 @@ export const startNextcloud = async function(branch: string = getCurrentGitBranc
})
await container.start()

const redis = await docker.createContainer({
Image: 'redis',
name: CONTAINER_NAME_REDIS,
})
await redis.start()

// Get container's IP
const ip = await getContainerIP(container)

Expand All @@ -103,6 +96,29 @@ export const startNextcloud = async function(branch: string = getCurrentGitBranc
}
}

const pullImage = async function(name: string) {
return new Promise((resolve, reject): any => docker.pull(name, (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)
}
}))
}

/**
* Configure Nextcloud
*/
Expand All @@ -117,9 +133,18 @@ export const configureNextcloud = async function() {
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)

// Enable redis
const redis = docker.getContainer(CONTAINER_NAME_REDIS)
const ip = await getContainerIP(redis)
await runExec(container, ['php', 'occ', 'config:system:set', 'memcache.distributed', '--value', '\\OC\\Memcache\\Redis'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'memcache.locking', '--value', '\\OC\\Memcache\\Redis'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'redis', 'host', '--value', ip], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'redis', 'port', '--value', '6379', '--type', 'integer'], true)

console.log('└─ Nextcloud is now ready to use 🎉')
}

Expand Down Expand Up @@ -189,7 +214,10 @@ export const stopNextcloud = async function() {
const container = docker.getContainer(CONTAINER_NAME)
console.log('Stopping Nextcloud container...')
container.remove({ force: true })
console.log('└─ Nextcloud container removed 🥀')
console.log('├─ Removing redis container...')
const redis = docker.getContainer(CONTAINER_NAME_REDIS)
redis.remove({ force: true })
console.log('└─ All containers removed 🥀')
} catch (err) {
console.log(err)
}
Expand Down

0 comments on commit e29bedc

Please sign in to comment.