Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: update mutate tests #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions k6/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

NAMESPACE="load-tests"
kubectl delete ns "$NAMESPACE"

if [[ $# -lt 3 ]]; then
echo "Usage: $0 <script> <vus> <iterations>" 1>&2
Expand All @@ -27,6 +28,14 @@ if [[ $SCRIPT == *"kyverno-pss.js" ]]; then
kubectl wait --for=condition=Ready --timeout=120s cpol -l app.kubernetes.io/name=kyverno-policies
fi

if [[ $SCRIPT == *"kyverno-mutate.js" ]]; then
rm /tmp/policies.json
echo "installing 10 mutate policies" 1>&2
node tests/utils/create-mutate-policies.js
kubectl create -f /tmp/policies.json
kubectl wait --for=condition=Ready --timeout=120s cpol -l app.kubernetes.io/name=kyverno-policies
fi

echo "Deploying namespace..."
kubectl create ns "$NAMESPACE"

Expand Down Expand Up @@ -77,4 +86,10 @@ if [[ $SCRIPT == *"kyverno-pss.js" ]]; then
helm uninstall kyverno-policies -n kyverno-policies
fi

if [[ $SCRIPT == *"kyverno-mutate.js" ]]; then
echo "cleaning up 10 mutate policies" 1>&2
kubectl delete -f /tmp/policies.json
rm /tmp/policies.json
fi

exit $EXIT_CODE
51 changes: 1 addition & 50 deletions k6/tests/kyverno-mutate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.2/index.js";
import {
buildKubernetesBaseUrl,
generatePod,
mutatePolicy,
getParamsWithAuth,
getTestNamespace,
randomString,
Expand All @@ -16,37 +17,6 @@ const namespace = getTestNamespace();
const params = getParamsWithAuth();
params.headers["Content-Type"] = "application/json";

export function setup() {
const mutatePolicy = {
apiVersion: "kyverno.io/v1",
kind: "ClusterPolicy",
metadata: { name: "add-labels" },
spec: {
rules: [
{
match: { any: [{ resources: { kinds: ["Pod"] } }] },
mutate: {
patchStrategicMerge: {
metadata: { labels: { "+(team)": "bravo" } },
},
},
name: "add-team",
},
],
},
};

const createRes = http.post(
`${baseUrl}/apis/kyverno.io/v1/clusterpolicies`,
JSON.stringify(mutatePolicy),
params
);

check(createRes, {
"verify response code of POST is 201": (r) => r.status === 201,
});
}

export default function () {
const podName = `test-${randomString(8)}`;
const pod = generatePod(podName);
Expand All @@ -64,22 +34,3 @@ export default function () {
"verify response code of POST is 201": (r) => r.status === 201,
});
}

export function teardown() {
const deleteRes = http.del(
`${baseUrl}/apis/kyverno.io/v1/clusterpolicies/add-labels`,
null,
params
);

check(deleteRes, {
"verify response code of DELETE is 200": (r) => r.status === 200,
});
}

export function handleSummary(data) {
return {
stdout: textSummary(data, { indent: " ", enableColors: false }),
"summary.json": JSON.stringify(data),
};
}
51 changes: 51 additions & 0 deletions k6/tests/utils/create-mutate-policies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');

const mutatePolicy = (policyName) => {
return {
apiVersion: "kyverno.io/v1",
kind: "ClusterPolicy",
metadata: {
name: policyName,
labels: {
"app.kubernetes.io/name": "kyverno-policies"
}
},
spec: {
rules: [
{
match: {
any: [{
resources: {
kinds: ["Pod"]
}
}]
},
mutate: {
patchStrategicMerge: {
metadata: {
labels: {
["+(test" + policyName + ")"]: "bravo"
}
}
}
},
name: "policy-" + policyName
}
]
}
}
};

for (let i = 0; i < 10; i++) {
const policyName = "policy-" + i;
const policyData = mutatePolicy(policyName);

fs.appendFile('/tmp/policies.json', JSON.stringify(policyData, null, 2) + '\n', (err) => {
if (err) {
console.error(err);
return;
}
});
}

console.log('Policies data has been written to /tmp/policies.json file');