Skip to content

Commit

Permalink
feat: Export runCommand and runOccCommand
Browse files Browse the repository at this point in the history
And refactor saveState and restoreState to use those.

Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Nov 19, 2024
1 parent aeb1888 commit 9e34c99
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
21 changes: 21 additions & 0 deletions lib/commands/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { basename } from 'path'

function getContainerName(): Cypress.Chainable<string> {
return cy.exec('pwd').then(({ stdout }) => {
const name = basename(stdout).replace(' ', '')
return cy.wrap(`nextcloud-cypress-tests_${name}`)
})
}

export function runCommand(command: string, options?: Partial<Cypress.ExecOptions>) {
const env = Object.entries(options?.env ?? {})
.map(([name, value]) => `-e '${name}=${value}'`)
.join(' ')

return cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${env} ${getContainerName()} ${command}`, options)
}
2 changes: 2 additions & 0 deletions lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export * from './getNc'
export * from './sessions'
export * from './users'
export * from './state'
export * from './docker'
export * from './occ'
10 changes: 10 additions & 0 deletions lib/commands/occ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { runCommand } from "./docker"

export function runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>) {
runCommand(`php ./occ ${command}`, options)
}
34 changes: 12 additions & 22 deletions lib/commands/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,31 @@
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { basename } from 'path'

const getContainerName = function(): Cypress.Chainable<string> {
return cy.exec('pwd').then(({ stdout }) => {
const app = basename(stdout).replace(' ', '')
return cy.wrap(`nextcloud-cypress-tests_${app}`)
})
}
import { runCommand } from "./docker"

export function saveState(): Cypress.Chainable<string> {
const snapshot = Math.random().toString(36).substring(7)

getContainerName().then(name => {
// DB
cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${snapshot}`)
// DB
runCommand(`cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${snapshot}`)

// Data
cy.exec(`docker exec --user www-data rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${name} tar cf /var/www/html/data/data-${snapshot}.tar .`)
})
// Data
runCommand(`rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
runCommand(`tar cf /var/www/html/data/data-${snapshot}.tar .`)

cy.log(`Created snapshot ${snapshot}`)

return cy.wrap(snapshot)
}

export function restoreState(snapshot: string = 'init') {
getContainerName().then(name => {
// DB
cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)

// Data
cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${name} rm -vfr $(tar --exclude='*/*' -tf '/var/www/html/data/data-${snapshot}.tar')`)
cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${name} tar -xf '/var/www/html/data/data-${snapshot}.tar'`)
})
// DB
runCommand(`cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)

// Data
runCommand(`rm -vfr $(tar --exclude='*/*' -tf '/var/www/html/data/data-${snapshot}.tar')`)
runCommand(`tar -xf '/var/www/html/data/data-${snapshot}.tar'`)

cy.log(`Restored snapshot ${snapshot}`)
}
15 changes: 14 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getNc, restoreState, saveState } from "./commands"
import { getNc, restoreState, runCommand, runOccCommand, saveState } from "./commands"
import { login, logout } from "./commands/sessions"
import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
import type { Selector } from "./selectors"
Expand Down Expand Up @@ -98,6 +98,17 @@ declare global {
* @param snapshot string the ID of the snapshot
*/
restoreState(snapshot: string): Cypress.Chainable<void>

/**
* Run a command in the docker container
*
*/
runCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>

/**
* Run an occ command
*/
runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>
}
}
}
Expand All @@ -122,6 +133,8 @@ export const addCommands = function() {
Cypress.Commands.add('getUserData', getUserData)
Cypress.Commands.add('saveState', saveState)
Cypress.Commands.add('restoreState', restoreState)
Cypress.Commands.add('runCommand', runCommand)
Cypress.Commands.add('runOccCommand', runOccCommand)
}

export { User }

0 comments on commit 9e34c99

Please sign in to comment.