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

[Tests-Only] Add k6 tests for deleting and downloading a file #959

Closed
wants to merge 6 commits into from
Closed
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
20 changes: 20 additions & 0 deletions tests/k6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ $ yarn build
```

## How to run
***Thest tests will have absolute control over your server. Running them in your production server might result in the permanent loss of data***

Use the following command to run the tests
```console
k6 run ./dist/TESTNAME.js
```

## Running with different backends
### 1. Running with OCIS backend
The tests run by default on the oCIS backend. They use the address https://localhost:9200 to run the tests.
If your oCIS instance is running on different address use `OC_HOST_NAME` env variable to specify the address of the server.
eg.
```console
OC_HOST_NAME=http://ocis-server.com k6 run ./dist/TESTNAME.js
```

### 2. Running with OC10 (classic) backend
To run the tests with oc10 classic backend set the address of oc10 server on `OC_HOST_NAME` env variable and also set `TEST_OC10` to `true`

eg.
```console
OC_HOST_NAME=http://owncloud-server.com TEST_OC10=true k6 run ./dist/TESTNAME.js
```
27 changes: 25 additions & 2 deletions tests/k6/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as types from "./types";

export const uploadFile = <RT extends ResponseType | undefined>(account: types.Account, data: bytes, name: string): RefinedResponse<RT> => {
return http.put(
`https://${defaults.host.name}/remote.php/dav/files/${account.login}/${name}`,
`${defaults.host.name}/remote.php/dav/files/${account.login}/${name}`,
data as any,
{
headers: {
Expand All @@ -16,13 +16,36 @@ export const uploadFile = <RT extends ResponseType | undefined>(account: types.A
);
}

export const downloadFile = <RT extends ResponseType | undefined>(account: types.Account, name: string): RefinedResponse<RT> => {
return http.get(
`${defaults.host.name}/remote.php/dav/files/${account.login}/${name}`,
{
headers: {
Authorization: `Basic ${encoding.b64encode(`${account.login}:${account.password}`)}`,
}
}
);
}

export const userInfo = <RT extends ResponseType | undefined>(account: any): RefinedResponse<RT> => {
return http.get(
`https://${defaults.host.name}/ocs/v1.php/cloud/users/${account.login}`,
`${defaults.host.name}/ocs/v1.php/cloud/users/${account.login}`,
{
headers: {
Authorization: `Basic ${encoding.b64encode(`${account.login}:${account.password}`)}`,
},
}
);
}

export const deleteFile = <RT extends ResponseType | undefined>(account: types.Account, name: string): RefinedResponse<RT> => {
return http.del(
`${defaults.host.name}/remote.php/dav/files/${account.login}/${name}`,
{},
{
headers: {
Authorization: `Basic ${encoding.b64encode(`${account.login}:${account.password}`)}`,
}
}
);
}
9 changes: 7 additions & 2 deletions tests/k6/src/lib/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as types from './types';

export const host = {
name: __ENV.OC_HOST_NAME || 'localhost:9200'
export const host: types.Host = {
name: __ENV.OC_HOST_NAME || 'https://localhost:9200',
type: !!__ENV.TEST_OC10 ? types.HostType.Oc10 : types.HostType.Ocis
}

export const accounts: { [key: string]: types.Account; } = {
Expand All @@ -13,4 +14,8 @@ export const accounts: { [key: string]: types.Account; } = {
login: 'richard',
password: 'superfluidity',
},
admin: {
login: 'admin',
password: 'admin',
},
}
1 change: 1 addition & 0 deletions tests/k6/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as defaults from './defaults'
export * as api from './api'
export * as users from './users'
export * as utils from './utils'
16 changes: 16 additions & 0 deletions tests/k6/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
export interface Account {
login: string
password: string
}
export interface UserRequestData {
displayname: string,
email: string,
password: string,
userid: string
}

export enum HostType {
Ocis = 1,
Oc10
}

export interface Host {
name: string,
type: HostType,
}
69 changes: 69 additions & 0 deletions tests/k6/src/lib/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import encoding from 'k6/encoding';
import {fail} from "k6";
import http, { file } from "k6/http";
import * as defaults from "./defaults";
import * as types from "./types";
import { uploadFile } from './api';

const adminUser = defaults.accounts.admin

const files = {
'kb_50.jpg': open('../_files/kb_50.jpg', 'b'),
}

export const k6VirtualUser = (userNumber: number): types.Account => {
return {
login: `k6users_${userNumber}`,
password: `k6users_${userNumber}`,
}
}

export const createBatchUsers = (numberOfUsers: number): void => {
deleteBatchUsers(numberOfUsers, false)
console.log("Cleared all existing users")
console.log("Creating demo users...")
for (let i=1; i <= numberOfUsers; i++ ) {
const userName = `k6users_${i}`
const userData: types.UserRequestData = {
displayname: userName,
email: `${userName}@example.com`,
password: userName,
userid: userName
}
const res = http.post(
`${defaults.host.name}/ocs/v2.php/cloud/users`,
userData as any,
{
headers: {
Authorization: `Basic ${encoding.b64encode(`${adminUser.login}:${adminUser.password}`)}`,
}
}
);
if (res.status != 200) {
fail("Failed while creating user")
}
console.log(`Created user ${userName}, initializing user now...`)

// running on oc10 will also require users to be initialized
uploadFile(k6VirtualUser(i), files['kb_50.jpg'], 'initialfile.txt')
}
}

export const deleteBatchUsers = (numberOfUsers: number, stopOnFailure: boolean = true): void => {
console.log("Clearing all demo users...")
for (let i=1; i <= numberOfUsers; i++ ) {
const userName = `k6users_${i}`
const res = http.del(
`${defaults.host.name}/ocs/v2.php/cloud/users/${userName}`,
{},
{
headers: {
Authorization: `Basic ${encoding.b64encode(`${adminUser.login}:${adminUser.password}`)}`,
}
}
);
if (stopOnFailure && res.status != 200) {
fail("Failed while deleting created user")
}
}
}
52 changes: 52 additions & 0 deletions tests/k6/src/test-basic-file-operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {sleep, check} from 'k6';
import {Options} from "k6/options";
import {api} from "./lib";
import {users} from "./lib"

const files = {
'kb_50.jpg': open('./_files/kb_50.jpg', 'b'),
}

export let options: Options = {
insecureSkipTLSVerify: true,
iterations: 300,
vus: 100,
setupTimeout: '200s',
};

export const setup = (): void => {
users.createBatchUsers(options.vus)
}

export default () => {
// Upload 6 50kb files
for (let i = 1; i <= 6; i++) {
const res = api.uploadFile(users.k6VirtualUser(__VU), files['kb_50.jpg'], `downloadfile_${i}.jpg`)
check(res, {
'status is 201': () => res.status === 201,
});
}

// Download all the uploaded files
for (let i = 1; i <= 6; i++) {
const res = api.downloadFile(users.k6VirtualUser(__VU), `downloadfile_${i}.jpg`)
check(res, {
'status is 200': () => res.status === 200,
});
}

// Delete all the uploaded files
for (let i = 1; i <= 6; i++) {
const res = api.deleteFile(users.k6VirtualUser(__VU), `downloadfile_${i}.jpg`)
check(res, {
'status is 204': () => res.status === 204 ,
});
}
sleep(1);
};

export const teardown = (): void => {
console.log("teardown step")
users.deleteBatchUsers(options.vus)
}

45 changes: 45 additions & 0 deletions tests/k6/src/test-delete-different-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {sleep, check,fail} from 'k6';
import {Options} from "k6/options";
import {api, utils, users} from "./lib";

const files = {
'kb_50.jpg': open('./_files/kb_50.jpg', 'b'),
}

export let options: Options = {
insecureSkipTLSVerify: true,
iterations: 100,
vus: 100,
};

export const setup = (): string[][] => {
users.createBatchUsers(options.vus)
const uploadedFiles: string[][] = []
for (let vu=1; vu <= options.vus; vu++) {
uploadedFiles[vu] = []
const user = users.k6VirtualUser(vu)
for (let iter=0; iter < options.iterations/options.vus; iter++) {
const fileName = `deletefile_${utils.randomString()}.jpg`
const res = api.uploadFile(user, files['kb_50.jpg'], fileName)
if (res.status !== 201) {
fail(`status code was *not* 201, expected: 201, actual: ${res.status}`);
}
uploadedFiles[vu].push(fileName)
}
}
return uploadedFiles
}

export default (files: string[]) => {
const fileName: string = files[__VU][__ITER]

const res = api.deleteFile(users.k6VirtualUser(__VU), fileName)
check(res, {
'status is 204': () => res.status === 204,
})
sleep(1)
}

export const teardown = (): void => {
users.deleteBatchUsers(options.vus)
}
40 changes: 40 additions & 0 deletions tests/k6/src/test-delete-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {sleep, check,fail} from 'k6';
import {Options} from "k6/options";
import {defaults, api} from "./lib";

const files = {
'kb_50.jpg': open('./_files/kb_50.jpg', 'b'),
}

export let options: Options = {
insecureSkipTLSVerify: true,
iterations: 100,
vus: 100,
};

export const setup = (): string[] => {
const uploadedFiles: string[] = []
console.log("setup for delete")
for (let iter=0; iter < options.iterations; iter++) {
const fileName = `deletefile_${iter}.jpg`
const res = api.uploadFile(defaults.accounts.einstein, files['kb_50.jpg'], fileName)
if (!check(res, {
'status code MUST be 201': (res) => res.status == 201,
})) {
fail(`status code was *not* 201, expected: 201, actual: ${res.status}`);
}
uploadedFiles.push(fileName)
}
console.log("uploaded test files")
return uploadedFiles
}

export default (files: string[]) => {
const fileName: string = files[(__ITER * options.vus) + __VU - 1]

const res = api.deleteFile(defaults.accounts.einstein, fileName)
check(res, {
'status is 204': () => res.status === 204,
})
sleep(1);
phil-davis marked this conversation as resolved.
Show resolved Hide resolved
};
46 changes: 46 additions & 0 deletions tests/k6/src/test-download-different-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {sleep, check} from 'k6';
import {Options} from "k6/options";
import {api, users} from "./lib";

const files = {
'kb_50.jpg': open('./_files/kb_50.jpg', 'b'),
}

export let options: Options = {
insecureSkipTLSVerify: true,
iterations: 100,
vus: 100,
};

export const setup = (): void => {
users.createBatchUsers(options.vus)
console.log("setup for download")
for (let vu=1; vu <= options.vus; vu++) {
const res = api.uploadFile(users.k6VirtualUser(vu), files['kb_50.jpg'], 'downloadfile.jpg')
if (res.status !== 201) {
fail("Status is not 201 while uploading the file")
}
}
console.log("uploaded files")
}

export default () => {
const res = api.downloadFile(users.k6VirtualUser(__VU), 'downloadfile.jpg')
check(res, {
'status is 200': () => res.status === 200,
});
sleep(1);
};

export const teardown = (): void => {
console.log("teardown for download")
for (let vu=1; vu <= options.vus; vu++) {
const res = api.deleteFile(users.k6VirtualUser(vu), 'downloadfile.jpg')
if (res.status !== 204) {
fail("Status is not 204 while deleting the file")
}
}
console.log("deleted files")
users.deleteBatchUsers(options.vus)
}

Loading