From c53118c8c179a21082da23c3399709429d5b74b1 Mon Sep 17 00:00:00 2001 From: anushkamittal2001 Date: Tue, 26 Sep 2023 16:16:14 +0530 Subject: [PATCH] Refactoring Signed-off-by: anushkamittal2001 --- .github/workflows/load-testing.yaml | 142 ++++------------------------ 1 file changed, 20 insertions(+), 122 deletions(-) diff --git a/.github/workflows/load-testing.yaml b/.github/workflows/load-testing.yaml index 834b17f400d3..a36b4d9f7c4a 100644 --- a/.github/workflows/load-testing.yaml +++ b/.github/workflows/load-testing.yaml @@ -12,160 +12,58 @@ on: jobs: run-kyverno-load-testing: runs-on: ubuntu-latest - steps: + steps: - name: Check out code uses: actions/checkout@v2 - name: Clone External Repository run: | - # Define the external repository and branch EXTERNAL_REPO="kyverno/load-testing" EXTERNAL_BRANCH="main" - # Authenticate using your PAT 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: Set Git Config + run: | + git config --global user.email "anushka@nirmata.com" + git config --global user.name "anushka" working-directory: ${{ github.workspace }} - name: Create and Add my-test.js run: | - # Navigate to the cloned external repository (load-testing) - cd load-testing/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 - }); + cd k6 + cd tests -} - -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() {}" > my-test.js - # Add the my-test.js file to the repository git add my-test.js git commit -m "Add my-test.js" working-directory: ${{ github.workspace }}/load-testing - name: Create and Add util.js run: | - # Navigate to the cloned external repository (load-testing) - cd load-testing/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 - } - } -} + cd k6 + cd tests -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;}" > util.js - # Add the util.js file to the repository git add util.js git commit -m "Add util.js" working-directory: ${{ github.workspace }}/load-testing - name: Make ./start.sh Script Executable run: | - # Navigate to the cloned external repository (load-testing) - cd load-testing/k6 + cd k6 - # Make the script executable chmod +x start.sh working-directory: ${{ github.workspace }}/load-testing - - name: Run ./start.sh Script - run: | - # Navigate to the cloned external repository (load-testing) - cd load-testing/k6 + # - name: Run ./start.sh Script + # run: | + # cd k6 - # Run the ./start.sh script with the specified arguments - ./start.sh tests/my-test.js 10 100 - working-directory: ${{ github.workspace }}/load-testing \ No newline at end of file + # ./start.sh tests/my-test.js 10 100 + # working-directory: ${{ github.workspace }}/load-testing \ No newline at end of file