diff --git a/magefile.go b/magefile.go index dd31cce..60627a1 100644 --- a/magefile.go +++ b/magefile.go @@ -76,6 +76,28 @@ func Prepare() error { return nil } +// Remove user data +func UnPrepare() error { + mg.Deps(EnsureK6, mkOutputDir) + + env := addHarborSizeToEnv(addHarborEnv(nil)) + + scripts, err := filepath.Glob("./scripts/data/teardown/*.js") + if err != nil { + return err + } + + for _, script := range scripts { + args := getK6RunArgs(script) + + if err := sh.RunWithV(env, K6Command, args...); err != nil { + return err + } + } + + return nil +} + // Execute a specific test func Run(test string) error { mg.Deps(EnsureK6, mkOutputDir) diff --git a/scripts/data/teardown/01-project.js b/scripts/data/teardown/01-project.js new file mode 100644 index 0000000..8fce6e2 --- /dev/null +++ b/scripts/data/teardown/01-project.js @@ -0,0 +1,41 @@ +// prepare projects +import { Rate } from 'k6/metrics' +import counter from 'k6/x/counter' +import { Harbor } from 'k6/x/harbor' + +import { Settings } from '../config.js' +import { numberToPadString } from '../helpers.js' + +const settings = Settings() + +const totalIterations = settings.ProjectsCount + +export let successRate = new Rate('success') + +export let options = { + setupTimeout: '6h', + duration: '24h', + vus: Math.min(settings.VUS, totalIterations), + iterations: totalIterations, + thresholds: { + 'success': ['rate>=1'], + 'iteration_duration{scenario:default}': [ + `max>=0`, + ], + 'iteration_duration{group:::setup}': [`max>=0`], + } +}; + +const harbor = new Harbor(settings.Harbor); + +export default function () { + const suffix = numberToPadString(counter.up(), settings.ProjectsCount) + + try { + harbor.deleteProject({ projectName: `${settings.ProjectPrefix}-${suffix}` }) + successRate.add(true) + } catch (e) { + successRate.add(false) + console.error(e.message) + } +} diff --git a/scripts/data/teardown/02-user.js b/scripts/data/teardown/02-user.js new file mode 100644 index 0000000..a2c1e50 --- /dev/null +++ b/scripts/data/teardown/02-user.js @@ -0,0 +1,41 @@ +// prepare users +import { Rate } from 'k6/metrics' +import counter from 'k6/x/counter' +import { Harbor } from 'k6/x/harbor' + +import { Settings } from '../config.js' +import { numberToPadString } from '../helpers.js' + +const settings = Settings() + +const totalIterations = settings.UsersCount + +export let successRate = new Rate('success') + +export let options = { + setupTimeout: '6h', + duration: '24h', + vus: Math.min(settings.VUS, totalIterations), + iterations: totalIterations, + thresholds: { + 'success': ['rate>=1'], + 'iteration_duration{scenario:default}': [ + `max>=0`, + ], + 'iteration_duration{group:::setup}': [`max>=0`], + } +}; + +const harbor = new Harbor(settings.Harbor) + +export default function () { + const suffix = numberToPadString(counter.up(), settings.UsersCount) + + try { + harbor.deleteUser(`${settings.UserPrefix}-${suffix}`) + successRate.add(true) + } catch (e) { + successRate.add(false) + console.error(e.message) + } +}