|
| 1 | +import { Before } from "@badeball/cypress-cucumber-preprocessor"; |
| 2 | + |
| 3 | +const echoToTerminal = (message: string) => { |
| 4 | + const escaped = message.replace(/"/g, '\\"'); |
| 5 | + cy.exec( |
| 6 | + `echo "${escaped}" >> /home/vmiticka/Programming/freeipa-webui/cypress/support/logs.log` |
| 7 | + ); |
| 8 | +}; |
| 9 | + |
| 10 | +export const cleanup = ( |
| 11 | + commands_find: string[], |
| 12 | + commands_delete: string[], |
| 13 | + omit: string[] |
| 14 | +) => { |
| 15 | + const command = commands_find.join(" "); |
| 16 | + echoToTerminal( |
| 17 | + `cleanup starting: find=[${command}] delete=[${commands_delete.join(" ")}] omit=[${omit}]` |
| 18 | + ); |
| 19 | + cy.ipa(command, undefined, { |
| 20 | + failOnNonZeroExit: false, |
| 21 | + }).then((result) => { |
| 22 | + const items: string[] = result.stdout |
| 23 | + .split("\n") |
| 24 | + .map((line) => line.split(":")[1]?.trim() || "") |
| 25 | + .filter((u) => u.length > 0 && !omit.includes(u)); |
| 26 | + |
| 27 | + const sample = items.slice(0, 10); |
| 28 | + const suffix = items.length > 10 ? " ..." : ""; |
| 29 | + echoToTerminal( |
| 30 | + `about to delete ${items.length} item(s): ${sample.join(", ")}${suffix}` |
| 31 | + ); |
| 32 | + |
| 33 | + //items.forEach((u) => { |
| 34 | + // cy.ipa(commands_delete.join(" "), u, { failOnNonZeroExit: false }); |
| 35 | + // }); |
| 36 | + |
| 37 | + echoToTerminal( |
| 38 | + `done cleanup: delete=[${commands_delete.join(" ")}] total=${items.length}` |
| 39 | + ); |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +// Special-case cleanup for services: keep core principals for the test server |
| 44 | +const cleanupServices = () => { |
| 45 | + const host = Cypress.env("SERVER_NAME") as string; |
| 46 | + const keepPrefixes = [ |
| 47 | + `HTTP/${host}`, |
| 48 | + `DNS/${host}`, |
| 49 | + `dogtag/${host}`, |
| 50 | + `ipa-dnskeysyncd/${host}`, |
| 51 | + `ldap/${host}`, |
| 52 | + ]; |
| 53 | + |
| 54 | + echoToTerminal( |
| 55 | + `cleanup starting: services keep=[${keepPrefixes.join(", ")}]` |
| 56 | + ); |
| 57 | + |
| 58 | + cy.exec(`podman exec webui ipa service-find --pkey-only`, { |
| 59 | + failOnNonZeroExit: false, |
| 60 | + }).then((result) => { |
| 61 | + const items: string[] = result.stdout |
| 62 | + .split("\n") |
| 63 | + .map((line) => line.split(":")[1]?.trim() || "") |
| 64 | + .filter( |
| 65 | + (u) => |
| 66 | + u.length > 0 && !keepPrefixes.some((prefix) => u.startsWith(prefix)) |
| 67 | + ); |
| 68 | + |
| 69 | + const sample = items.slice(0, 10); |
| 70 | + const suffix = items.length > 10 ? " ..." : ""; |
| 71 | + echoToTerminal( |
| 72 | + `about to delete ${items.length} service(s): ${sample.join(", ")}${suffix}` |
| 73 | + ); |
| 74 | + |
| 75 | + items.forEach((u) => { |
| 76 | + cy.ipa("service-del", u, { failOnNonZeroExit: false }); |
| 77 | + }); |
| 78 | + |
| 79 | + echoToTerminal(`done cleanup: service-del total=${items.length}`); |
| 80 | + }); |
| 81 | +}; |
| 82 | + |
| 83 | +Before(() => { |
| 84 | + cy.log("running cleanup"); |
| 85 | + cleanup(["automember-find", "--pkey-only"], ["automember-del"], []); |
| 86 | + cleanup(["certmaprule-find", "--pkey-only"], ["certmaprule-del"], []); |
| 87 | + cleanup(["dnsforwardzone-find", "--pkey-only"], ["dnsforwardzone-del"], []); |
| 88 | + cleanup(["dnsrecord-find", "--pkey-only"], ["dnsrecord-del"], []); |
| 89 | + cleanup(["dnszone-find", "--pkey-only"], ["dnszone-del"], []); |
| 90 | + cleanup( |
| 91 | + ["group-find", "--pkey-only"], |
| 92 | + ["group-del"], |
| 93 | + ["admins", "editors", "ipausers", "trust admins"] |
| 94 | + ); |
| 95 | + cleanup(["hbacrule-find", "--pkey-only"], ["hbacrule-del"], []); |
| 96 | + cleanup(["hbacsvc-find", "--pkey-only"], ["hbacsvc-del"], []); |
| 97 | + cleanup(["hbacsvcgroup-find", "--pkey-only"], ["hbacsvcgroup-del"], []); |
| 98 | + cleanup(["host-find", "--pkey-only"], ["host-del"], []); |
| 99 | + cleanup(["hostgroup-find", "--pkey-only"], ["hostgroup-del"], []); |
| 100 | + cleanup(["idoverridegroup-find", "--pkey-only"], ["idoverridegroup-del"], []); |
| 101 | + cleanup(["idoverrideuser-find", "--pkey-only"], ["idoverrideuser-del"], []); |
| 102 | + cleanup(["idp-find", "--pkey-only"], ["idp-del"], []); |
| 103 | + cleanup(["idrange-find", "--pkey-only"], ["idrange-del"], []); |
| 104 | + cleanup(["idview-find", "--pkey-only"], ["idview-del"], []); |
| 105 | + cleanup(["netgroup-find", "--pkey-only"], ["netgroup-del"], []); |
| 106 | + cleanup(["pwpolicy-find", "--pkey-only"], ["pwpolicy-del"], []); |
| 107 | + cleanupServices(); |
| 108 | + cleanup(["stageuser-find", "--pkey-only"], ["stageuser-del"], []); |
| 109 | + cleanup(["sudocmd-find", "--pkey-only"], ["sudocmd-del"], []); |
| 110 | + cleanup(["sudocmdgroup-find", "--pkey-only"], ["sudocmdgroup-del"], []); |
| 111 | + cleanup(["sudorule-find", "--pkey-only"], ["sudorule-del"], []); |
| 112 | + cleanup(["trust-find", "--pkey-only"], ["trust-del"], []); |
| 113 | + cleanup(["user-find", "--pkey-only"], ["user-del"], ["admin"]); |
| 114 | +}); |
0 commit comments