Skip to content

Commit 140863e

Browse files
committed
feat!: Add save/restore state commands
Signed-off-by: Louis Chemineau <[email protected]>
1 parent d6a7b65 commit 140863e

File tree

5 files changed

+62
-45
lines changed

5 files changed

+62
-45
lines changed

cypress/e2e/snapshots.cy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Create a snapshot and a user', function() {
99
let snapshot: string
1010

1111
it('Create a snapshot', function() {
12-
cy.createDBSnapshot().then(_snapshot => {
12+
cy.saveState().then(_snapshot => {
1313
snapshot = _snapshot
1414
})
1515
})
@@ -30,7 +30,7 @@ describe('Create a snapshot and a user', function() {
3030
})
3131

3232
it('Restore the snapshot', function() {
33-
cy.restoreDBSnapshot(snapshot)
33+
cy.restoreState(snapshot)
3434
})
3535

3636
it('Fail login with the user', function() {

lib/commands/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
export * from './getNc'
77
export * from './sessions'
88
export * from './users'
9-
export * from './snapshots'
9+
export * from './state'

lib/commands/snapshots.ts

-28
This file was deleted.

lib/commands/state.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import { basename } from 'path'
6+
7+
const getContainerName = function(): Cypress.Chainable<string> {
8+
return cy.exec('pwd').then(({ stdout }) => {
9+
const app = basename(stdout).replace(' ', '')
10+
return cy.wrap(`nextcloud-cypress-tests_${app}`)
11+
})
12+
}
13+
14+
export function saveState(): Cypress.Chainable<string> {
15+
const snapshot = Math.random().toString(36).substring(7)
16+
17+
getContainerName().then(name => {
18+
// DB
19+
cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${snapshot}`)
20+
21+
// Data
22+
cy.exec(`docker exec --user www-data rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
23+
cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${name} tar cf /var/www/html/data/data-${snapshot}.tar .`)
24+
})
25+
26+
cy.log(`Created snapshot ${snapshot}`)
27+
28+
return cy.wrap(snapshot)
29+
}
30+
31+
export function restoreState(snapshot: string = 'init') {
32+
getContainerName().then(name => {
33+
// DB
34+
cy.exec(`docker exec --user www-data ${name} cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)
35+
36+
// Data
37+
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')`)
38+
cy.exec(`docker exec --user www-data --workdir /var/www/html/data ${name} tar -xf '/var/www/html/data/data-${snapshot}.tar'`)
39+
})
40+
41+
cy.log(`Restored snapshot ${snapshot}`)
42+
}

lib/index.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5-
import { getNc } from "./commands"
5+
import { getNc, restoreState, saveState } from "./commands"
66
import { login, logout } from "./commands/sessions"
7-
import { createDBSnapshot, restoreDBSnapshot } from "./commands/snapshots"
87
import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
98
import type { Selector } from "./selectors"
109

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

7271
/**
7372
* Enable or disable a given user
74-
*
73+
*
7574
* @param user user whom to enable or disable
7675
* @param enable True to enable, false to disable (default is enable)
7776
*/
7877
enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>
7978

8079
/**
81-
*
80+
*
8281
* Query metadata for, and in behalf, of a given user
8382
*
8483
* @param user User whom metadata to query
8584
*/
8685
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>
8786

8887
/**
89-
* Create a snapshot of the current database
90-
*/
91-
createDBSnapshot(snapshot?: string): Cypress.Chainable<string>,
88+
* Create a snapshot of the current DB and data folder state.
89+
*
90+
* @return string the ID of the snapshot
91+
*/
92+
saveState(): Cypress.Chainable<string>
9293

9394
/**
94-
* Restore a snapshot of the database
95-
* Default is the post-setup state
96-
*/
97-
restoreDBSnapshot(snapshot?: string): Cypress.Chainable,
95+
* Restore a snapshot of the database
96+
* Default is the post-setup state
97+
*
98+
* @param snapshot string the ID of the snapshot
99+
*/
100+
restoreState(snapshot: string): Cypress.Chainable<void>
98101
}
99102
}
100103
}
@@ -117,8 +120,8 @@ export const addCommands = function() {
117120
Cypress.Commands.add('modifyUser', modifyUser)
118121
Cypress.Commands.add('enableUser', enableUser)
119122
Cypress.Commands.add('getUserData', getUserData)
120-
Cypress.Commands.add('createDBSnapshot', createDBSnapshot)
121-
Cypress.Commands.add('restoreDBSnapshot', restoreDBSnapshot)
123+
Cypress.Commands.add('saveState', saveState)
124+
Cypress.Commands.add('restoreState', restoreState)
122125
}
123126

124127
export { User }

0 commit comments

Comments
 (0)