grafana / xk6-kubernetes

Client extension for interacting with Kubernetes clusters from your k6 tests.
Apache License 2.0
62 stars 20 forks source link

Add helper function for creating random namespaces #73

Open pablochacin opened 1 year ago

pablochacin commented 1 year ago

The main use case for the xk6-kubernetes extension is facilitating the setup of tests, by providing a simple API for creating Kubernetes resources such as secrets, pods, services and others required for running a test application.

When running multiple concurrent tests, it is convenient to isolate tests by using different namespaces when creating the resources needed by the tests. Moreover, it is convenient to use randomly generated namespaces for each test, to prevent interferences with other instances of the same tests running concurrently or that run previously and was not properly teared down.

This means that of such test scripts must include a sequence of code similar to the example below:

import { Kubernetes } from 'k6/x/kubernetes'
import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

const namespace = randomString(8)

const nsObj = {
    apiVersion: "v1",
    kind: "Namespace",
    metadata: {
        name: "test-ns"
    }
}

export  function setup() {
  const k8s = new Kubernetes()
  k8s.create(nsObj)
}

Being this a very common use case, In order to prevent this redundancy of code on each test, it will be convenient to provide a helper function that creates a new namespace with a random name and return its name, reducing the above sequence to this show below:


import { Kubernetes } from 'k6/x/kubernetes'

export function setup() {
  const k8s = new Kubernetes()
  namespace = k8s.randomNamespace()
}