diff --git a/lib/commands/index.ts b/lib/commands/index.ts index 8424cede..67323ab2 100644 --- a/lib/commands/index.ts +++ b/lib/commands/index.ts @@ -6,4 +6,4 @@ export * from './getNc' export * from './sessions' export * from './users' -export * from './snapshots' +export * from './state' diff --git a/lib/commands/snapshots.ts b/lib/commands/snapshots.ts deleted file mode 100644 index 58747776..00000000 --- a/lib/commands/snapshots.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ -import { basename } from 'path' - -const getContainerName = function(): Cypress.Chainable { - return cy.exec('pwd').then(({ stdout }) => { - const app = basename(stdout).replace(' ', '') - return cy.wrap(`nextcloud-cypress-tests_${app}`) - }) -} - -export const createDBSnapshot = function(snapshot?: string): Cypress.Chainable { - const hash = new Date().toISOString().replace(/[^0-9]/g, '') - getContainerName().then(name => { - cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${snapshot ?? hash}`) - }) - cy.log(`Created snapshot ${snapshot ?? hash}`) - return cy.wrap(snapshot ?? hash) -} - -export const restoreDBSnapshot = function(snapshot: string = 'init') { - getContainerName().then(name => { - cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`) - }) - cy.log(`Restored snapshot ${snapshot}`) -} diff --git a/lib/commands/state.ts b/lib/commands/state.ts new file mode 100644 index 00000000..24c65369 --- /dev/null +++ b/lib/commands/state.ts @@ -0,0 +1,60 @@ +/** + * @copyright 2024 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import { basename } from 'path' + +const getContainerName = function(): Cypress.Chainable { + return cy.exec('pwd').then(({ stdout }) => { + const app = basename(stdout).replace(' ', '') + return cy.wrap(`nextcloud-cypress-tests_${app}`) + }) +} + +export function saveState(): Cypress.Chainable { + 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}`) +} diff --git a/lib/index.ts b/lib/index.ts index 23c34654..6de8af3f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -2,7 +2,7 @@ * 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" @@ -53,7 +53,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) */ @@ -71,14 +71,14 @@ 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> /** - * + * * Query metadata for, and in behalf, of a given user * * @param user User whom metadata to query @@ -86,15 +86,19 @@ declare global { getUserData(user: User): Cypress.Chainable> /** - * Create a snapshot of the current database - */ - createDBSnapshot(snapshot?: string): Cypress.Chainable, + * Create a snapshot of the current DB and data folder state. + * + * @return string the ID of the snapshot + */ + saveState(): Cypress.Chainable /** - * 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 } } } @@ -117,8 +121,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 }