Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Export runCommand and runOccCommand #688

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/commands/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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(' ')

getContainerName()
.then((containerName) => {
return cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${env} ${containerName} ${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 }