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: Add save/restore state commands #627

Merged
merged 1 commit into from
Sep 17, 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
4 changes: 2 additions & 2 deletions cypress/e2e/snapshots.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Create a snapshot and a user', function() {
let snapshot: string

it('Create a snapshot', function() {
cy.createDBSnapshot().then(_snapshot => {
cy.saveState().then(_snapshot => {
snapshot = _snapshot
})
})
Expand All @@ -30,7 +30,7 @@ describe('Create a snapshot and a user', function() {
})

it('Restore the snapshot', function() {
cy.restoreDBSnapshot(snapshot)
cy.restoreState(snapshot)
})

it('Fail login with the user', function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
export * from './getNc'
export * from './sessions'
export * from './users'
export * from './snapshots'
export * from './state'
28 changes: 0 additions & 28 deletions lib/commands/snapshots.ts

This file was deleted.

42 changes: 42 additions & 0 deletions lib/commands/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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}`)
})
}

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}`)

// 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 .`)
})

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'`)
})

cy.log(`Restored snapshot ${snapshot}`)
}
31 changes: 17 additions & 14 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getNc } from "./commands"
import { getNc, restoreState, saveState } from "./commands"
import { login, logout } from "./commands/sessions"
import { createDBSnapshot, restoreDBSnapshot } from "./commands/snapshots"
import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
import type { Selector } from "./selectors"

Expand Down Expand Up @@ -53,7 +52,7 @@ declare global {
* Query list of users on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*
*
* @param details Set to true to fetch users with detailed information (default false)
* @return List of user IDs or list of Users (if details was set to true)
*/
Expand All @@ -71,30 +70,34 @@ declare global {

/**
* Enable or disable a given user
*
*
* @param user user whom to enable or disable
* @param enable True to enable, false to disable (default is enable)
*/
enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>

/**
*
*
* Query metadata for, and in behalf, of a given user
*
* @param user User whom metadata to query
*/
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Create a snapshot of the current database
*/
createDBSnapshot(snapshot?: string): Cypress.Chainable<string>,
* Create a snapshot of the current DB and data folder state.
*
* @return string the ID of the snapshot
*/
saveState(): Cypress.Chainable<string>

/**
* Restore a snapshot of the database
* Default is the post-setup state
*/
restoreDBSnapshot(snapshot?: string): Cypress.Chainable,
* Restore a snapshot of the database
* Default is the post-setup state
*
* @param snapshot string the ID of the snapshot
*/
restoreState(snapshot: string): Cypress.Chainable<void>
}
}
}
Expand All @@ -117,8 +120,8 @@ export const addCommands = function() {
Cypress.Commands.add('modifyUser', modifyUser)
Cypress.Commands.add('enableUser', enableUser)
Cypress.Commands.add('getUserData', getUserData)
Cypress.Commands.add('createDBSnapshot', createDBSnapshot)
Cypress.Commands.add('restoreDBSnapshot', restoreDBSnapshot)
Cypress.Commands.add('saveState', saveState)
Cypress.Commands.add('restoreState', restoreState)
}

export { User }
Loading