Browse Source

Run a cleanup command before running tests

The cleanup will appear as a regular test (so that it can also be
easily run manually and cleanup errors can be seen nicely) and will
delete all the replicas, migrations, endpoints, users created by Cypress
and projects created by Cypress.
The cleanup will happen only if the current user is 'cypress'.

Also includes and updated version of Cypress: 3.0.1 -> 3.0.2.
Sergiu Miclea 7 years ago
parent
commit
3b773f2136

+ 2 - 2
package.json

@@ -37,7 +37,7 @@
     "@storybook/react": "^3.2.15",
     "babel-eslint": "^8.0.1",
     "babel-jest": "^21.2.0",
-    "cypress": "3.0.1",
+    "cypress": "3.0.2",
     "enzyme": "^3.1.0",
     "enzyme-adapter-react-16": "^1.0.4",
     "eslint": "^4.8.0",
@@ -103,4 +103,4 @@
     "webpack-blocks-happypack": "^0.1.3",
     "webpack-blocks-split-vendor": "^0.2.1"
   }
-}
+}

+ 32 - 0
private/cypress/integration/0 - cleanup/Cleanup.js

@@ -0,0 +1,32 @@
+/*
+Copyright (C) 2018  Cloudbase Solutions SRL
+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 <http://www.gnu.org/licenses/>.
+*/
+
+// @flow
+
+describe('Cleaning up Cypress environment', () => {
+  before(() => {
+    cy.login()
+  })
+
+  beforeEach(() => {
+    Cypress.Cookies.preserveOnce('token', 'projectId')
+  })
+
+  it('Loaded the UI', () => {
+    cy.get('[data-test-id="navigation-item-replicas"]').should('exist')
+    cy.cleanup()
+  })
+
+  it('Successfully cleaned up the Cypress environment', () => { })
+})

+ 4 - 0
private/cypress/integration/1 - login/Invalid Login.js

@@ -19,6 +19,10 @@ import config from '../../config'
 declare var cy: any
 
 describe('Coriolis Login Failed', () => {
+  before(() => {
+    cy.logout()
+  })
+
   it('Displays incorrect password', () => {
     cy.server()
     cy.route({ url: '**/identity/**', method: 'POST' }).as('login')

+ 99 - 0
private/cypress/support/commands.js

@@ -18,6 +18,7 @@ import config from '../config.js'
 
 const identityUrl = `${config.coriolisUrl}identity/auth/tokens`
 const projectsUrl = `${config.coriolisUrl}identity/auth/projects`
+const coriolisUrl = `${config.coriolisUrl}coriolis`
 
 declare var expect: any
 
@@ -88,3 +89,101 @@ Cypress.Commands.add('login', () => {
     })
   })
 })
+
+Cypress.Commands.add('logout', () => {
+  let token
+  return cy.getCookies().then(cookies => {
+    let tokenCookie = cookies.find(c => c.name === 'token')
+    if (tokenCookie) {
+      token = tokenCookie
+    }
+  }).then(() => {
+    if (!token) {
+      return Promise.resolve()
+    }
+    return cy.request({
+      method: 'DELETE',
+      url: `${config.coriolisUrl}identity/auth/tokens`,
+      headers: { 'X-Subject-Token': token, 'X-Auth-Token': token },
+    })
+  })
+})
+
+Cypress.Commands.add('cleanup', () => {
+  if (config.username !== 'cypress') {
+    return Promise.resolve()
+  }
+
+  let token
+  let projectId
+  return cy.getCookies().then(cookies => {
+    token = cookies.find(c => c.name === 'token').value
+    projectId = cookies.find(c => c.name === 'projectId').value
+  }).then(() => {
+    if (!token || !projectId) {
+      return Promise.resolve()
+    }
+
+    // Delete replicas
+    return cy.request({
+      method: 'GET',
+      url: `${coriolisUrl}/${projectId}/replicas/detail`,
+      headers: { 'X-Auth-Token': token },
+    }).then(response => response.body.replicas)
+      .then(replicas => Promise.all(replicas.map(replica => cy.request({
+        method: 'DELETE',
+        url: `${coriolisUrl}/${projectId}/replicas/${replica.id}`,
+        headers: { 'X-Auth-Token': token },
+      }))))
+  }).then(() => {
+    // Delete migrations
+    return cy.request({
+      method: 'GET',
+      url: `${coriolisUrl}/${projectId}/migrations/detail`,
+      headers: { 'X-Auth-Token': token },
+    }).then(response => response.body.migrations)
+      .then(migrations => Promise.all(migrations.map(migration => cy.request({
+        method: 'DELETE',
+        url: `${coriolisUrl}/${projectId}/migrations/${migration.id}`,
+        headers: { 'X-Auth-Token': token },
+      }))))
+  }).then(() => {
+    // Delete endpoints
+    return cy.request({
+      method: 'GET',
+      url: `${coriolisUrl}/${projectId}/endpoints`,
+      headers: { 'X-Auth-Token': token },
+    }).then(response => response.body.endpoints)
+      .then(endpoints => Promise.all(endpoints.map(endpoint => cy.request({
+        method: 'DELETE',
+        url: `${coriolisUrl}/${projectId}/endpoints/${endpoint.id}`,
+        headers: { 'X-Auth-Token': token },
+      }))))
+  }).then(() => {
+    // Delete users created by Cypress
+    return cy.request({
+      method: 'GET',
+      url: `${config.coriolisUrl}identity/users`,
+      headers: { 'X-Auth-Token': token },
+    }).then(response => response.body.users
+      .filter(user => user.description && /user created by cypress/gi.test(user.description))
+    ).then(users => Promise.all(users.map(user => cy.request({
+      method: 'DELETE',
+      url: `${config.coriolisUrl}identity/users/${user.id}`,
+      headers: { 'X-Auth-Token': token },
+    }))))
+  }).then(() => {
+    // Delete projects created by Cypress
+    return cy.request({
+      method: 'GET',
+      url: `${config.coriolisUrl}identity/projects`,
+      headers: { 'X-Auth-Token': token },
+    }).then(response => response.body.projects
+      .filter(project => project.description && /project created by cypress/gi.test(project.description))
+    ).then(projects => Promise.all(projects.map(project => cy.request({
+      method: 'DELETE',
+      url: `${config.coriolisUrl}identity/projects/${project.id}`,
+      headers: { 'X-Auth-Token': token },
+    }))))
+  })
+})

+ 65 - 13
yarn.lock

@@ -201,9 +201,9 @@
   version "4.14.87"
   resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.87.tgz#55f92183b048c2c64402afe472f8333f4e319a6b"
 
-"@types/minimatch@3.0.1":
-  version "3.0.1"
-  resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.1.tgz#b683eb60be358304ef146f5775db4c0e3696a550"
+"@types/minimatch@3.0.3":
+  version "3.0.3"
+  resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
 
 "@types/mocha@2.2.44":
   version "2.2.44"
@@ -1978,9 +1978,9 @@ bytes@3.0.0:
   version "3.0.0"
   resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
 
-cachedir@1.2.0:
-  version "1.2.0"
-  resolved "https://registry.yarnpkg.com/cachedir/-/cachedir-1.2.0.tgz#e9a0a25bb21a2b7a0f766f07c41eb7a311919b97"
+cachedir@1.3.0:
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/cachedir/-/cachedir-1.3.0.tgz#5e01928bf2d95b5edd94b0942188246740e0dbc4"
   dependencies:
     os-homedir "^1.0.1"
 
@@ -2467,6 +2467,16 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0:
     shebang-command "^1.2.0"
     which "^1.2.9"
 
+cross-spawn@^6.0.0:
+  version "6.0.5"
+  resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
+  dependencies:
+    nice-try "^1.0.4"
+    path-key "^2.0.1"
+    semver "^5.5.0"
+    shebang-command "^1.2.0"
+    which "^1.2.9"
+
 crypt@~0.0.1:
   version "0.0.2"
   resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
@@ -2647,9 +2657,9 @@ cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0":
   dependencies:
     cssom "0.3.x"
 
-cypress@3.0.1:
-  version "3.0.1"
-  resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.0.1.tgz#6a8938ce8a551e4ae1bd5fb2ceab038d4ad39c4d"
+cypress@3.0.2:
+  version "3.0.2"
+  resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.0.2.tgz#90caef84c91bd52b9cdf123aa76213249a289694"
   dependencies:
     "@cypress/listr-verbose-renderer" "0.4.1"
     "@cypress/xvfb" "1.2.3"
@@ -2659,17 +2669,18 @@ cypress@3.0.1:
     "@types/chai-jquery" "1.1.35"
     "@types/jquery" "3.2.16"
     "@types/lodash" "4.14.87"
-    "@types/minimatch" "3.0.1"
+    "@types/minimatch" "3.0.3"
     "@types/mocha" "2.2.44"
     "@types/sinon" "4.0.0"
     "@types/sinon-chai" "2.7.29"
     bluebird "3.5.0"
-    cachedir "1.2.0"
+    cachedir "1.3.0"
     chalk "2.4.1"
     check-more-types "2.24.0"
     commander "2.11.0"
     common-tags "1.4.0"
     debug "3.1.0"
+    execa "0.10.0"
     executable "4.1.1"
     extract-zip "1.6.6"
     fs-extra "4.0.1"
@@ -2684,7 +2695,7 @@ cypress@3.0.1:
     minimist "1.2.0"
     progress "1.1.8"
     ramda "0.24.1"
-    request "2.81.0"
+    request "2.87.0"
     request-progress "0.3.1"
     supports-color "5.1.0"
     tmp "0.0.31"
@@ -3355,6 +3366,18 @@ exec-sh@^0.2.0:
   dependencies:
     merge "^1.1.3"
 
+execa@0.10.0:
+  version "0.10.0"
+  resolved "https://registry.yarnpkg.com/execa/-/execa-0.10.0.tgz#ff456a8f53f90f8eccc71a96d11bdfc7f082cb50"
+  dependencies:
+    cross-spawn "^6.0.0"
+    get-stream "^3.0.0"
+    is-stream "^1.1.0"
+    npm-run-path "^2.0.0"
+    p-finally "^1.0.0"
+    signal-exit "^3.0.0"
+    strip-eof "^1.0.0"
+
 execa@^0.7.0:
   version "0.7.0"
   resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
@@ -5621,6 +5644,10 @@ negotiator@0.6.1:
   version "0.6.1"
   resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"
 
+nice-try@^1.0.4:
+  version "1.0.4"
+  resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"
+
 nise@^1.2.0:
   version "1.2.0"
   resolved "https://registry.yarnpkg.com/nise/-/nise-1.2.0.tgz#079d6cadbbcb12ba30e38f1c999f36ad4d6baa53"
@@ -6045,7 +6072,7 @@ path-is-inside@^1.0.1, path-is-inside@^1.0.2:
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53"
 
-path-key@^2.0.0:
+path-key@^2.0.0, path-key@^2.0.1:
   version "2.0.1"
   resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
 
@@ -7139,6 +7166,31 @@ request@2.81.0:
     tunnel-agent "^0.6.0"
     uuid "^3.0.0"
 
+request@2.87.0:
+  version "2.87.0"
+  resolved "https://registry.yarnpkg.com/request/-/request-2.87.0.tgz#32f00235cd08d482b4d0d68db93a829c0ed5756e"
+  dependencies:
+    aws-sign2 "~0.7.0"
+    aws4 "^1.6.0"
+    caseless "~0.12.0"
+    combined-stream "~1.0.5"
+    extend "~3.0.1"
+    forever-agent "~0.6.1"
+    form-data "~2.3.1"
+    har-validator "~5.0.3"
+    http-signature "~1.2.0"
+    is-typedarray "~1.0.0"
+    isstream "~0.1.2"
+    json-stringify-safe "~5.0.1"
+    mime-types "~2.1.17"
+    oauth-sign "~0.8.2"
+    performance-now "^2.1.0"
+    qs "~6.5.1"
+    safe-buffer "^5.1.1"
+    tough-cookie "~2.3.3"
+    tunnel-agent "^0.6.0"
+    uuid "^3.1.0"
+
 "request@>= 2.52.0", request@^2.79.0, request@^2.83.0:
   version "2.83.0"
   resolved "https://registry.yarnpkg.com/request/-/request-2.83.0.tgz#ca0b65da02ed62935887808e6f510381034e3356"