Skip to content

Commit e68b0b9

Browse files
committed
add some basic tests
1 parent 0fe92df commit e68b0b9

14 files changed

+1989
-38
lines changed

.env.template

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PORT=2000
22
KUBERO_WEBHOOK_SECRET=mysecret
3+
KUBERO_USERS=base64encodedJson
34

45
# webhook configuration
56
KUBERO_WEBHOOK_URL=https://kuberoXXXXXXXXXXXXX.loca.lt/api/repo/webhooks

.env.test

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PORT=2000
2+
KUBERO_WEBHOOK_SECRET=mysecret
3+
#KUBERO_USERS=base64encodedJson
4+
5+
# webhook configuration
6+
KUBERO_WEBHOOK_URL=https://kuberoXXXXXXXXXXXXX.loca.lt/api/repo/webhooks
7+
8+
KUBECONFIG_PATH=./kubeconfig
9+
KUBERO_CONFIG_PATH=./config.yaml
10+
KUBERO_CONTEXT=kind-kubero
11+
KUBERO_NAMESPACE=kubero-dev
12+
KUBERO_SESSION_KEY=randomString
13+
DEBUG=*.*
14+
15+
##########################################
16+
# git repository configuration
17+
#
18+
#GITHUB_PERSONAL_ACCESS_TOKEN=
19+
20+
#GITEA_PERSONAL_ACCESS_TOKEN=
21+
#GITEA_BASEURL=http://localhost:3000
22+
23+
#GOGS_PERSONAL_ACCESS_TOKEN=
24+
#GOGS_BASEURL=http://localhost:3000
25+
26+
#GITLAB_BASEURL=http://localhost:3080
27+
#GITLAB_PERSONAL_ACCESS_TOKEN=glpat-
28+
29+
#BITBUCKET_USERNAME=XXXXXXXXX
30+
#BITBUCKET_APP_PASSWORD=
31+
32+
33+
################################################
34+
# authentication section
35+
#
36+
#GITHUB_CLIENT_SECRET=
37+
#GITHUB_CLIENT_ID=
38+
#GITHUB_CLIENT_CALLBACKURL=http://kubero.lacolhost.com/api/auth/github/callback
39+
#GITHUB_CLIENT_ORG=
40+
41+
#OAUTO2_CLIENT_NAME=Gitea
42+
#OAUTO2_CLIENT_AUTH_URL=http://gitea.lacolhost.com:3000/login/oauth/authorize
43+
#OAUTO2_CLIENT_TOKEN_URL=http://gitea.lacolhost.com:3000/login/oauth/access_token
44+
#OAUTH2_CLIENT_ID=
45+
#OAUTH2_CLIENT_SECRET=
46+
#OAUTH2_CLIENT_CALLBACKURL=http://kubero.lacolhost.com/api/auth/oauth2/callback

jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
4+
//verbose: true,
5+
preset: 'ts-jest',
6+
testEnvironment: 'node',
7+
setupFiles: ["<rootDir>/jest.setup.js"],
8+
9+
transform: {
10+
'^.+\\.tsx?$': 'ts-jest',
11+
},
12+
};

jest.setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("dotenv").config()

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"swaggerwatch": "npx swagger-ui-watcher swagger.json",
1212
"swaggergen": "node ./swagger.js",
1313
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q --ext 'ts,json' dist/index.js\"",
14-
"debug": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\""
14+
"debug": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\"",
15+
"test": "ENV_FILE=.env.test jest"
1516
},
1617
"dependencies": {
1718
"@kubernetes/client-node": "^0.16.3",
@@ -51,6 +52,7 @@
5152
"@types/debug": "^4.1.7",
5253
"@types/express": "^4.17.13",
5354
"@types/express-session": "^1.17.5",
55+
"@types/jest": "^29.2.4",
5456
"@types/lodash.set": "^4.3.7",
5557
"@types/node": "^17.0.34",
5658
"@types/passport": "^1.0.10",
@@ -61,7 +63,10 @@
6163
"@types/swagger-ui-express": "^4.1.3",
6264
"@types/uuid": "^8.3.4",
6365
"concurrently": "^7.2.0",
66+
"jest": "^29.3.1",
6467
"nodemon": "^2.0.16",
65-
"swagger-autogen": "^2.21.5"
68+
"swagger-autogen": "^2.21.5",
69+
"ts-jest": "^29.0.3",
70+
"ts-node": "^10.9.1"
6671
}
6772
}

src/configure.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const after = (app: Express, server: Server) => {
4545
const addons = new Addons({
4646
kubectl: kubero.kubectl
4747
});
48+
addons.loadOperators();
4849
app.locals.addons = addons;
4950
app.use('/api', RouterAddons);
5051
app.use('/api', RouterAuth);

src/git/repo.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { GithubApi } from './github';
2+
import { GogsApi } from './gogs';
3+
import { GitlabApi } from './gitlab';
4+
import { BitbucketApi } from './bitbucket';
5+
import { GiteaApi } from './gitea';
6+
7+
describe('GithubApi', () => {
8+
it('should load config', () => {
9+
const github = new GithubApi("token");
10+
expect(github).toBeTruthy();
11+
});
12+
});
13+
14+
describe('GogsApi', () => {
15+
it('should load config', () => {
16+
const gogs = new GogsApi("http://localhost:3000", "token");
17+
expect(gogs).toBeTruthy();
18+
});
19+
});
20+
21+
describe('GitlabApi', () => {
22+
it('should load config', () => {
23+
const gitlab = new GitlabApi("https://gitlab.com", "token");
24+
expect(gitlab).toBeTruthy();
25+
});
26+
});
27+
28+
describe('GiteaApi', () => {
29+
it('should load config', () => {
30+
const gitea = new GiteaApi("https://codeberg.org", "token");
31+
expect(gitea).toBeTruthy();
32+
});
33+
});
34+
35+
describe('Bitbucket', () => {
36+
it('should load config', () => {
37+
const bitbucket = new BitbucketApi("username", "password");
38+
expect(bitbucket).toBeTruthy();
39+
});
40+
});

src/modules/addons.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Addons} from './addons';
2+
import { Kubectl } from './kubectl';
3+
import YAML from 'yaml';
4+
import * as fs from 'fs';
5+
import { IKuberoConfig} from '../types';
6+
7+
describe('Addons', () => {
8+
it('should load addons', async () => {
9+
const numberOfAddons = 5;
10+
11+
const path = process.env.KUBERO_CONFIG_PATH as string || './config.yaml';
12+
const config = YAML.parse(fs.readFileSync(path, 'utf8')) as IKuberoConfig;
13+
const kubectl = new Kubectl(config);
14+
15+
const addons = new Addons({kubectl: kubectl});
16+
await addons.loadOperators();
17+
expect(addons).toBeTruthy();
18+
expect(addons.addonsList.length).toBe(numberOfAddons);
19+
expect(addons.getAddonsList()).resolves.toBeDefined();
20+
});
21+
});

src/modules/addons.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,26 @@ export class Addons {
6363
options: AddonOptions
6464
) {
6565
this.kubectl = options.kubectl
66-
this.loadOperators()
6766
}
6867

69-
private loadOperators(): void {
70-
this.kubectl.getOperators()
71-
.then(operators => {
68+
public async loadOperators(): Promise<void> {
7269

73-
this.operatorsList = operators;
70+
this.operatorsList = await this.kubectl.getOperators()
7471

75-
const postgresCluster = new PostgresCluster(operators)
76-
this.addonsList.push(postgresCluster)
72+
const postgresCluster = new PostgresCluster(this.operatorsList)
73+
this.addonsList.push(postgresCluster)
7774

78-
const redisCluster = new RedisCluster(operators)
79-
this.addonsList.push(redisCluster)
75+
const redisCluster = new RedisCluster(this.operatorsList)
76+
this.addonsList.push(redisCluster)
8077

81-
const redis = new Redis(operators)
82-
this.addonsList.push(redis)
78+
const redis = new Redis(this.operatorsList)
79+
this.addonsList.push(redis)
8380

84-
const mongoDB = new MongoDB(operators)
85-
this.addonsList.push(mongoDB)
81+
const mongoDB = new MongoDB(this.operatorsList)
82+
this.addonsList.push(mongoDB)
8683

87-
const minio = new Minio(operators)
88-
this.addonsList.push(minio)
89-
})
90-
.catch(err => {
91-
console.error(err)
92-
})
84+
const minio = new Minio(this.operatorsList)
85+
this.addonsList.push(minio)
9386
}
9487

9588
public async getAddonsList(): Promise<IPlugin[]> {

src/modules/auth.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Auth} from './auth';
2+
3+
describe('Auth', () => {
4+
it('requires no authentication', () => {
5+
process.env.KUBERO_USERS = "";
6+
const auth = new Auth();
7+
auth.init();
8+
expect(auth.getAuthMiddleware().name).toBe('noAuthMiddleware');
9+
expect(auth.getBearerMiddleware().name).toBe('authenticate');
10+
});
11+
12+
it('requires authentication', () => {
13+
process.env.KUBERO_USERS = 'WwogIHsKICAgICJpZCI6IDEsCiAgICAibWV0aG9kIjogImxvY2FsIiwKICAgICJ1c2VybmFtZSI6ICJhc2RmIiwKICAgICJwYXNzd29yZCI6ICI4YTg0MjNiYTc4YzhmM2RhNjBhNjAyNDkzNjYzYzFjZGMyNDhhODk1NDFiMTI5ODBlMjkyMzk5YzBmMGNhZDIxIiwKICAgICJpbnNlY3VyZSI6IGZhbHNlCiAgfSwKICB7CiAgICAiaWQiOiAyLAogICAgIm1ldGhvZCI6ICJsb2NhbCIsCiAgICAidXNlcm5hbWUiOiAicXdlciIsCiAgICAicGFzc3dvcmQiOiAicXdlciIsCiAgICAiaW5zZWN1cmUiOiB0cnVlCiAgfQpd';
14+
const auth = new Auth();
15+
auth.init();
16+
expect(auth.getAuthMiddleware().name).toBe('authMiddleware');
17+
expect(auth.getBearerMiddleware().name).toBe('authenticate');
18+
});
19+
});

src/modules/kubectl.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Kubectl } from './kubectl';
2+
import YAML from 'yaml';
3+
import * as fs from 'fs';
4+
import { IKuberoConfig} from '../types';
5+
6+
describe('Kubectl', () => {
7+
it('should load config', () => {
8+
9+
const path = process.env.KUBERO_CONFIG_PATH as string || './config.yaml';
10+
const config = YAML.parse(fs.readFileSync(path, 'utf8')) as IKuberoConfig;
11+
const kubectl = new Kubectl(config);
12+
13+
expect(kubectl).toBeTruthy();
14+
expect(kubectl.config).toBeTruthy();
15+
expect(kubectl.log).toBeTruthy();
16+
17+
expect(kubectl.getPipelinesList()).resolves.toBeDefined();
18+
19+
expect(kubectl.getKubeVersion()).resolves.toBeDefined();
20+
});
21+
});

src/routes/addons.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import {RouterAddons, authMiddleware} from './addons';
3+
4+
describe('Addons API', () => {
5+
it('addons should respond', () => {
6+
expect(RouterAddons.get('/api/addons')).toBeTruthy();
7+
expect(RouterAddons.get('/api/cli/addons')).toBeTruthy();
8+
expect(RouterAddons.get('/api/addons/operators')).toBeTruthy();
9+
});
10+
});
11+
12+
describe('Auth API', () => {
13+
process.env.KUBERO_USERS = "";
14+
it('auth should respond', () => {
15+
expect(authMiddleware.name).toBe('noAuthMiddleware');
16+
});
17+
});

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/* Modules */
2828
"module": "commonjs", /* Specify what module code is generated. */
29-
//"rootDir": "./src/", /* Specify the root folder within your source files. */
29+
"rootDir": "./src/", /* Specify the root folder within your source files. */
3030
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3232
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */

0 commit comments

Comments
 (0)