Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: anushkamittal2001 <[email protected]>
  • Loading branch information
anushkamittal2001 committed Sep 26, 2023
1 parent 15c95ae commit e453f71
Showing 1 changed file with 6 additions and 103 deletions.
109 changes: 6 additions & 103 deletions .github/workflows/load-testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
run-kyverno-load-testing:
runs-on: ubuntu-latest
steps:
steps:
- name: Check out code
uses: actions/checkout@v2

Expand All @@ -26,47 +26,16 @@ jobs:
AUTH_HEADER="Authorization: token ${{ secrets.GITHUB_TOKEN }}"
# Clone the entire external repository
git clone --branch $EXTERNAL_BRANCH --depth 1 "https://github.com/$EXTERNAL_REPO.git" load-testing
git clone "https://github.com/$EXTERNAL_REPO.git"
working-directory: ${{ github.workspace }}

- name: Create and Add my-test.js
run: |
# Navigate to the cloned external repository (load-testing)
cd load-testing/k6
cd k6
# Replace 'YOUR_CONTENT_HERE' with the provided JavaScript code
echo "import http from 'k6/http';
import { check } from 'k6';
import { buildKubernetesBaseUrl, generatePod, getParamsWithAuth, getTestNamespace, randomString } from './util.js';

const baseUrl = buildKubernetesBaseUrl();
const namespace = getTestNamespace();

http.setResponseCallback(http.expectedStatuses(400));

export default function() {
const podName = \`test-\${randomString(8)}\`;
const pod = generatePod(podName);
pod.metadata.labels = {
app: 'k6-test',
}

pod.metadata.labels[\"environment.tess.io/name\"] = 'feature'

const params = getParamsWithAuth();
params.headers['Content-Type'] = 'application/json';

const createRes = http.post(\`\${baseUrl}/api/v1/namespaces/\${namespace}/pods\`, JSON.stringify(pod), params);
console.log(\"received response \" + createRes.status + \" \" + createRes.status_text)
check(createRes, {
'verify response code of POST is 400': r => r.status === 400
});

}

export function teardown() {

}" > tests/my-test.js
echo "import http from 'k6/http'; import { check } from 'k6';import { buildKubernetesBaseUrl, generatePod, getParamsWithAuth, getTestNamespace, randomString } from './util.js';const baseUrl = buildKubernetesBaseUrl();const namespace = getTestNamespace();http.setResponseCallback(http.expectedStatuses(400));export default function() { const podName = \`test-\${randomString(8)}\`;const pod = generatePod(podName);pod.metadata.labels = {app: 'k6-test',} pod.metadata.labels[\"environment.tess.io/name\"] = 'feature' const params = getParamsWithAuth(); params.headers['Content-Type'] = 'application/json';const createRes = http.post(\`\${baseUrl}/api/v1/namespaces/\${namespace}/pods\`, JSON.stringify(pod), params);console.log(\"received response \" + createRes.status + \" \" + createRes.status_text)check(createRes, {'verify response code of POST is 400': r => r.status === 400});}export function teardown() {}" > tests/my-test.js
# Add the my-test.js file to the repository
git add my-test.js
Expand All @@ -76,76 +45,10 @@ export function teardown() {
- name: Create and Add util.js
run: |
# Navigate to the cloned external repository (load-testing)
cd load-testing/k6
cd k6
# Replace 'YOUR_CONTENT_HERE' with the provided JavaScript code
echo "export const generatePod = (name = 'test', image = 'nginx') => {
return {
kind: 'Pod',
apiVersion: 'v1',
metadata: {
name: name
},
spec: {
containers: [
{
name: 'test',
image,
securityContext: {
}
}
],
}
}
}

export const generateConfigmap = (name = 'test') => {
return {
kind: "ConfigMap",
apiVersion: "v1",
metadata: {
name: name
}
}
}

export const generateSecret = (name = 'test') => {
return {
kind: "Secret",
apiVersion: "v1",
metadata: {
name: name
}
}
}

export const buildKubernetesBaseUrl = () => {
return `https://${__ENV.KUBERNETES_SERVICE_HOST}:${__ENV.KUBERNETES_SERVICE_PORT}`;
}

export const getTestNamespace = () => {
return __ENV.POD_NAMESPACE;
}

export const getParamsWithAuth = () => {
return {
headers: {
'Authorization': `Bearer ${__ENV.KUBERNETES_TOKEN}`
}
}
}

export const randomString = (length) => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let result = '';
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}" > tests/util.js
echo "export const generatePod = (name = 'test', image = 'nginx') => {return {kind: 'Pod', apiVersion: 'v1', metadata: { name: name }, spec: { containers: [ { name: 'test', image, securityContext: { } } ], } }}export const generateConfigmap = (name = 'test') => { return { kind: "ConfigMap", apiVersion: "v1", metadata: { name: name } }}export const generateSecret = (name = 'test') => { return { kind: "Secret", apiVersion: "v1", metadata: { name: name } }}export const buildKubernetesBaseUrl = () => { return `https://${__ENV.KUBERNETES_SERVICE_HOST}:${__ENV.KUBERNETES_SERVICE_PORT}`;}export const getTestNamespace = () => { return __ENV.POD_NAMESPACE;}export const getParamsWithAuth = () => { return { headers: { 'Authorization': `Bearer ${__ENV.KUBERNETES_TOKEN}` } }}export const randomString = (length) => { const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; let result = ''; let counter = 0; while (counter < length) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); counter += 1; } return result;}" > tests/util.js
# Add the util.js file to the repository
git add util.js
Expand Down

0 comments on commit e453f71

Please sign in to comment.