Skip to content

Commit

Permalink
Merge pull request #72 from pablochacin/add-helpers
Browse files Browse the repository at this point in the history
Add helpers
  • Loading branch information
javaducky authored Oct 11, 2022
2 parents c6d495d + 0e11b19 commit e201063
Show file tree
Hide file tree
Showing 16 changed files with 1,591 additions and 356 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ This API offers methods for creating, retrieving, listing and deleting resources
| | namespace |
| list | kind| returns a collection of resources of a given kind
| | namespace |
| update | spec object | updates an existing resource


The kinds of resources currently supported are:
Expand Down Expand Up @@ -154,6 +155,68 @@ export default function () {
}
```
## Helpers
The `xk6-kubernetes` extension offers helpers to facilitate common tasks when setting up a tests. All helper functions work in a namespace to facilitate the development of tests segregated by namespace. The helpers are accessed using the following method:
| Method | Parameters| Description |
| -------------| ---| ------ |
| helpers | namespace | returns helpers that operate in the given namespace. If none is specified, "default" is used |
The methods above return an object that implements the following helper functions:
| Method | Parameters| Description |
| ------------ | --------| ------ |
| getExternalIP | service | returns the external IP of a service if any is assigned before timeout expires|
| | timeout in seconds | |
| waitPodRunning | pod name | waits until the pod is in 'Running' state or the timeout expires. Returns a boolean indicating of the pod was ready or not. Throws an error if the pod is Failed. |
| | timeout in seconds | |
| waitServiceReady | service name | waits until the given service has at least one endpoint ready or the timeout expires |
| | timeout in seconds | |
### Examples
### Creating a pod in a random namespace and wait until it is running
```javascript
import { Kubernetes } from 'k6/x/kubernetes';
let podSpec = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
}
}
export default function () {
const kubernetes = new Kubernetes();
// create pod
kubernetes.create(pod)
// get helpers for test namespace
const helpers = kubernetes.helpers()
// wait for pod to be running
const timeout = 10
if (!helpers.waitPodRunning(pod.metadata.name, timeout)) {
console.log(`"pod ${pod.metadata.name} not ready after ${timeout} seconds`)
}
}
## Resource kind helpers
This API offers a helper for each kind of Kubernetes resources supported (Pods, Deployments, Secrets, et cetera). For each one, an interface for creating, getting, listing and deleting objects is offered.
Expand Down
38 changes: 38 additions & 0 deletions examples/wait_pod_running.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Kubernetes } from 'k6/x/kubernetes';

let pod = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
}
}

export default function () {
const kubernetes = new Kubernetes();

const ns = "default"

// create pod in test namespace
pod.metadata.namespace = ns
kubernetes.create(pod)

// get helpers for test namespace
const helpers = kubernetes.helpers(ns)

// wait for pod to be running
const timeout = 10
if (!helpers.waitPodRunning(pod.metadata.name, timeout)) {
console.log(`"pod ${pod.metadata.name} not ready after ${timeout} seconds`)
}
}
5 changes: 3 additions & 2 deletions internal/testutils/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
)

// NewFakeDynamic creates a new instance of a fake dynamic client with a default scheme
func NewFakeDynamic() (*dynamicfake.FakeDynamicClient, error) {
func NewFakeDynamic(objs ...runtime.Object) (*dynamicfake.FakeDynamicClient, error) {
scheme := runtime.NewScheme()
err := fake.AddToScheme(scheme)
if err != nil {
return nil, err
}
return dynamicfake.NewSimpleDynamicClient(scheme), nil

return dynamicfake.NewSimpleDynamicClient(scheme, objs...), nil
}
44 changes: 44 additions & 0 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,47 @@ if (k8s.list(podSpec.kind, podSpec.metadata.namespace).length != 0) {
`)
require.NoError(t, err)
}

// TestHelpersAreScriptable runs helpers
func TestHelpersAreScriptable(t *testing.T) {
t.Parallel()

rt := setupTestEnv(t)

_, err := rt.RunString(`
const k8s = new Kubernetes()
let pod = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
},
status: {
phase: "Running"
}
}
// create pod in test namespace
k8s.create(pod)
// get helpers for test namespace
const helpers = k8s.helpers()
// wait for pod to be running
if (!helpers.waitPodRunning(pod.metadata.name, 5)) {
throw new Error("should not timeout")
}
`)
require.NoError(t, err)
}
Loading

0 comments on commit e201063

Please sign in to comment.