mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-18 04:43:12 +02:00
Support authentication and TLS in k6 load testing script (#391)
This commit is contained in:
@@ -2,12 +2,15 @@ import {check} from 'k6';
|
||||
import http from 'k6/http';
|
||||
import {WebSocket} from 'k6/experimental/websockets';
|
||||
import crypto from 'k6/crypto';
|
||||
import encoding from 'k6/encoding';
|
||||
import {uuidv4} from 'https://jslib.k6.io/k6-utils/1.6.0/index.js';
|
||||
import {expect} from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
|
||||
|
||||
const BASE_URL = __ENV.ORCHARD_BASE_URL || 'http://127.0.0.1:6120/v1';
|
||||
const WS_BASE_URL = __ENV.ORCHARD_WS_BASE_URL || BASE_URL.replace(/^http/, 'ws');
|
||||
const BASE_URL = __ENV.BASE_URL || 'http://127.0.0.1:6120/v1';
|
||||
const WS_BASE_URL = __ENV.WS_BASE_URL || BASE_URL.replace(/^http/, 'ws');
|
||||
const WS_BYTES = __ENV.WS_BYTES || 64 * 1024;
|
||||
const SERVICE_ACCOUNT_NAME = __ENV.SERVICE_ACCOUNT_NAME;
|
||||
const SERVICE_ACCOUNT_TOKEN = __ENV.SERVICE_ACCOUNT_TOKEN;
|
||||
|
||||
export const options = {
|
||||
scenarios: {
|
||||
@@ -51,6 +54,7 @@ function createVM(vmName) {
|
||||
const resp = http.post(`${BASE_URL}/vms`, body, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authHeaders(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -68,7 +72,11 @@ async function portForward(vmName) {
|
||||
|
||||
console.debug(`connecting to ${url}`);
|
||||
|
||||
const ws = new WebSocket(url);
|
||||
const ws = new WebSocket(url, [], {
|
||||
headers: {
|
||||
...authHeaders(),
|
||||
}
|
||||
});
|
||||
ws.binaryType = 'arraybuffer';
|
||||
|
||||
const sentBytes = new Uint8Array(crypto.randomBytes(WS_BYTES));
|
||||
@@ -108,7 +116,12 @@ async function portForward(vmName) {
|
||||
}
|
||||
|
||||
function deleteVM(vmName) {
|
||||
const resp = http.del(`${BASE_URL}/vms/${vmName}`);
|
||||
const resp = http.del(`${BASE_URL}/vms/${vmName}`, null, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authHeaders(),
|
||||
}
|
||||
});
|
||||
|
||||
const ok = check(resp, {
|
||||
'VM deletion succeeded': (r) => r.status === 200,
|
||||
@@ -118,3 +131,16 @@ function deleteVM(vmName) {
|
||||
console.error(`Failed to delete a VM: HTTP ${resp.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
function authHeaders() {
|
||||
if (!SERVICE_ACCOUNT_NAME || !SERVICE_ACCOUNT_TOKEN) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const credentials = `${SERVICE_ACCOUNT_NAME}:${SERVICE_ACCOUNT_TOKEN}`;
|
||||
const encodedCredentials = encoding.b64encode(credentials);
|
||||
|
||||
return {
|
||||
Authorization: `Basic ${encodedCredentials}`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user