Skip to content

Commit

Permalink
finish test sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
Maples7 committed Aug 6, 2017
1 parent 4ce7270 commit 14ee08c
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ This tool are fully tested under version **0.10.x** of Kong, any other versions

If you want to contribute to this project, feel free to raise any PRs.

### Test

Firstly, pull git submodule `kong-mock-server` and install all npm dependencies for test.

Then, make sure you have installed `kong-config-manager` or run `npm link` in the root directory of this project.

Finally, run `npm test`.

## Relatives

- [kong-mock-server](https://github.com/Maples7/kong-mock-server)
Expand Down
7 changes: 6 additions & 1 deletion lib/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ module.exports = function dump(host, name) {
const currentDir = `${name}/${obj}`;
makeDir(currentDir);
debug(`Ready to dump ${obj} object of ${name}...`);
return dumpItems(`${host}/${_.replace(obj, /_/g, '/')}/`, currentDir, [], obj);
return dumpItems(
`${host}/${_.replace(obj, /_/g, '/')}/`,
currentDir,
[],
obj
);
});
};
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
"kcm": "./bin/kcm.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava"
},
"ava": {
"files": [
"test/index.js"
],
"source": [
"lib/*.js",
"enums/*.js",
"bin/*.js"
],
"failFast": true
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -35,6 +46,7 @@
"request-promise": "^4.2.1"
},
"devDependencies": {
"ava": "^0.21.0"
"ava": "^0.21.0",
"shelljs": "^0.7.8"
}
}
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
const test = require('ava');
const fse = require('fs-extra');
const rp = require('request-promise');
const shell = require('shelljs');
const initData = require('./init-data/');
const db = require('./kong-mock-server/lib/db');

test.before(t => {
shell.exec('node ./kong-mock-server/index', (code, stdout, stderr) => {
console.log('Mock Server Exit code:', code);
console.log('Mock Server output:', stdout);
console.log('Mock Server stderr:', stderr);
});
shell.exec('sleep 2');
fse.writeJsonSync(
'./kcm-config.json',
{
main: 'localhost:3001',
sec_test: 'localhost:3001'
},
{
spaces: 2
}
);
});

test.beforeEach(async t => {
const numRemoved = await db.removeAsync({}, { multi: true });
console.log(`nedb was flushed! (${numRemoved} items)`);
await Promise.all(
Object.keys(initData).map(async obj => {
await db.insertAsync(initData.obj);
})
);
});

test('DEBUG=kcm* kcm dump --all', t => {
const ret = shell.exec('DEBUG=kcm* kcm dump --all');

t.is(ret.code, 0);
const api1 = require('./main/apis/60d8c00b-1d2e-4dab-8dfc-b3a8e04aa891.json');
t.is(api1.name, 'api1');
t.is(api1.upstream_url, 'mockbin.com');
});
32 changes: 32 additions & 0 deletions test/init-data/apis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"name": "api1",
"hosts": ["1.com", "2.com"],
"upstream_url": "mockbin.com",
"http_if_terminated": true,
"upstream_read_timeout": 6000,
"preserve_host": false,
"upstream_connect_timeout": 6000,
"strip_uri": true,
"https_only": false,
"retries": 5,
"upstream_send_timeout": 6000,
"created_at": 1502013470934,
"id": "60d8c00b-1d2e-4dab-8dfc-b3a8e04aa891"
},
{
"name": "api2",
"hosts": ["1.com", "2.com", "3.com"],
"upstream_url": "mockbin.com",
"http_if_terminated": true,
"upstream_read_timeout": 6000,
"preserve_host": false,
"upstream_connect_timeout": 6000,
"strip_uri": true,
"https_only": false,
"retries": 5,
"upstream_send_timeout": 6000,
"created_at": 1502013504702,
"id": "fc111281-5a6f-41fd-abf8-d4424250e25d"
}
]
16 changes: 16 additions & 0 deletions test/init-data/certificates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id": "21b69eab-09d9-40f9-a55e-c4ee47fada68",
"cert": "-----BEGIN CERTIFICATE-----...",
"key": "-----BEGIN RSA PRIVATE KEY-----...",
"snis": ["mockbin.com"],
"created_at": 1485521710265
},
{
"id": "95b19eab-09d9-40f9-a55e-c4ee47fada68",
"cert": "-----BEGIN CERTIFICATE-----...",
"key": "-----BEGIN RSA PRIVATE KEY-----...",
"snis": ["example.com"],
"created_at": 1485521710266
}
]
12 changes: 12 additions & 0 deletions test/init-data/consumers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "4d924084-1adb-40a5-c042-63b19db421d1",
"custom_id": "abc123",
"created_at": 1422386534
},
{
"id": "2d324024-8fdb-20a5-g044-62b19db411d1",
"custom_id": "test",
"created_at": 1422386535
}
]
17 changes: 17 additions & 0 deletions test/init-data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');

const ret = {};

fs
.readdirSync(__dirname)
.filter(file => file.indexOf('.') !== 0 && file !== 'index.js')
.forEach(file => {
const item = file.split('.')[0];
ret[item] = require(`./${item}`);
ret[item] = ret[item].map(o => {
o.type = item;
return o;
});
});

module.exports = ret;
21 changes: 21 additions & 0 deletions test/init-data/plugins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"id": "4d924084-1adb-40a5-c042-63b19db421d1",
"api_id": "5fd1z584-1adb-40a5-c042-63b19db49x21",
"name": "rate-limiting",
"config": {
"minute": 20,
"hour": 500
},
"enabled": true,
"created_at": 1422386534
},
{
"id": "3d324d84-1sdb-30a5-c043-63b19db421d1",
"consumer_id": "a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4",
"name": "halo-auth",
"config": {},
"enabled": true,
"created_at": 1422386534
}
]
12 changes: 12 additions & 0 deletions test/init-data/snis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "mockbin.com",
"ssl_certificate_id": "21b69eab-09d9-40f9-a55e-c4ee47fada68",
"created_at": 1485521710265
},
{
"name": "example.com",
"ssl_certificate_id": "34c49eab-09d9-40f9-a55e-c4ee47fada68",
"created_at": 1485521710267
}
]
16 changes: 16 additions & 0 deletions test/init-data/targets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id": "4661f55e-95c2-4011-8fd6-c5c56df1c9db",
"target": "1.2.3.4:80",
"weight": 15,
"upstream_id": "13611da7-703f-44f8-b790-fc1e7bf51b3e",
"created_at": 1485523507446
},
{
"id": "5552f66e-95c2-4011-8fd6-c5c56df1c9db",
"target": "1.2.3.4:80",
"weight": 15,
"upstream_id": "13611da7-703f-44f8-b790-fc1e7bf51b3e",
"created_at": 1485523507446
}
]
16 changes: 16 additions & 0 deletions test/init-data/upstreams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id": "13611da7-703f-44f8-b790-fc1e7bf51b3e",
"name": "service.v1.xyz",
"orderlist": [1, 2, 7, 9, 6, 4, 5, 10, 3, 8],
"slots": 10,
"created_at": 1485521710265
},
{
"id": "24721da8-703f-44f8-b790-fc2e7bf51b3e",
"name": "httpbin.com",
"orderlist": [1, 11, 7, 9, 6, 4, 5, 10, 3, 8],
"slots": 10,
"created_at": 1485521710269
}
]

0 comments on commit 14ee08c

Please sign in to comment.