Skip to content

Commit

Permalink
finish test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maples7 committed Aug 7, 2017
1 parent 14ee08c commit 8a4cbeb
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 188 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.travis.yml
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "6.1"
before_script:
- cd ./test/kong-mock-server && git pull origin master
after_script:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
cache:
directories:
- node_modules
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ For example, `kcm-config.json` in the current working directory:

The key would be also used as folder name with all configs of corresponding Kong instance in it, so do **NOT** use any illegal characters for a folder name.

**NOTE**: the protocol like `http` or `https` can NOT be omitted.

## About Version

This tool are fully tested under version **0.10.x** of Kong, any other versions are NOT guaranteed. Theoretically, once the admin APIs of Kong remain unchanged, this tool would work perfectly. But again, nothing is determined for sure.
Expand Down
11 changes: 2 additions & 9 deletions bin/kcm-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ let retPromise = null;

function initApply(instance, host) {
const instancePath = getAbsolutePath(instance);
if (!_.isString(host)) {
exit('host mast be a string, for example: https://localhost:8444');
}
host = _.trimEnd(host, '/');
if (fs.existsSync(instancePath)) {
console.log(chalk.green(`Ready to apply configs for ${instance}...`));
Expand All @@ -34,7 +31,7 @@ function initApply(instance, host) {
}
}

if (program.host && program.instance) {
if (program.host) {
retPromise = initApply(program.instance, program.host);
} else if (program.file) {
const configs = getConfigs(program.file);
Expand All @@ -43,18 +40,14 @@ if (program.host && program.instance) {
retPromise = Promise.map(Object.keys(configs), key =>
initApply(key, configs[key])
);
} else if (program.instance) {
} else {
if (!configs[program.instance]) {
exit(
`instance ${program.instance} not found in CLI config file ${program.file}`
);
}
retPromise = initApply(program.instance, configs[program.instance]);
} else {
exit('params error');
}
} else {
exit('params error');
}

retPromise
Expand Down
7 changes: 1 addition & 6 deletions bin/kcm-dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ try {
let retPromise = null;

if (program.host) {
program.instance = program.instance || 'main';
retPromise = dump(program.host, program.instance);
} else if (program.file) {
const configs = getConfigs(program.file);
Expand All @@ -32,18 +31,14 @@ if (program.host) {
console.log(chalk.green(`kong insatnce ${key} finished!`))
)
);
} else if (program.instance) {
} else {
if (!configs[program.instance]) {
exit(
`instance ${program.instance} not found in CLI config file ${program.file}`
);
}
retPromise = dump(configs[program.instance], program.instance);
} else {
exit('params error');
}
} else {
exit('params error');
}

retPromise
Expand Down
12 changes: 9 additions & 3 deletions lib/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ module.exports = function apply(dir, host) {
}).then(response => {
if (response.statusCode !== 204) {
throw new Error(
`Fail to delete plguin ${itemToDeleteId}: ${response}`
`Fail to delete ${obj} ${itemToDeleteId}: ${JSON.stringify(
response
)}`
);
} else {
debug(`[${obj}] Success to DELETE item ${itemToDeleteId}!`);
Expand Down Expand Up @@ -113,7 +115,9 @@ module.exports = function apply(dir, host) {
}).then(response => {
if (response.statusCode !== 201) {
throw new Error(
`Fail to add new target ${newItemId}: ${response}`
`Fail to add new ${obj} ${newItemId}: ${JSON.stringify(
response
)}`
);
} else {
fse.removeSync(newItemPath);
Expand Down Expand Up @@ -155,7 +159,9 @@ module.exports = function apply(dir, host) {
}).then(response => {
if (response.statusCode !== 200) {
throw new Error(
`Fail to patch plugin ${patchItemId}: ${response}`
`Fail to patch ${obj} ${patchItemId}: ${JSON.stringify(
response
)}`
);
} else {
debug(
Expand Down
6 changes: 1 addition & 5 deletions lib/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,13 @@ module.exports = function dump(host, name) {
const targetDir = `${currentDir}_${id}_targets`;
makeDir(targetDir);
debug(`Ready to dump targets of upstream ${id}`);
return dumpItems(`${url}${id}/targets/`, targetDir, [], false);
return dumpItems(`${url}${id}/targets/`, targetDir, [], 'targets');
}
}
});
});
}

if (!_.isString(host)) {
exit('host mast be a string, for example: https://localhost:8444');
}

console.log(chalk.green(`Ready to dump configs of kong instance ${name}...`));
host = _.trimEnd(host, '/');
makeDir(name);
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"kcm": "./bin/kcm.js"
},
"scripts": {
"test": "ava"
"test": "app=./test/kong-mock-server/index.js && cmd=./node_modules/pm2/bin/pm2 && INIT_DATA=true ${cmd} start ${app} -f && nyc ava && ${cmd} delete ${app}",
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls"
},
"ava": {
"files": [
Expand Down Expand Up @@ -47,6 +48,9 @@
},
"devDependencies": {
"ava": "^0.21.0",
"coveralls": "^2.13.1",
"nyc": "^11.1.0",
"pm2": "^2.6.1",
"shelljs": "^0.7.8"
}
}
136 changes: 115 additions & 21 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,138 @@
const fs = require('fs');
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');
// wait initial data finished
shell.exec('sleep 1');
shell.cd('test');
fse.writeJsonSync(
'./kcm-config.json',
{
main: 'localhost:3001',
sec_test: 'localhost:3001'
main: 'http://localhost:3001',
sec_test: 'http://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.serial('DEBUG=kcm:dump kcm dump --all', t => {
t.plan(5);
const ret = shell.exec('DEBUG=kcm:dump kcm dump --all');

t.is(ret.code, 0);
const api1 = require('./sec_test/apis/60d8c00b-1d2e-4dab-8dfc-b3a8e04aa891.json');
t.is(api1.name, 'api1');
t.is(api1.upstream_url, 'httpbin.com');
const target1 = require('./main/upstreams_13611da7-703f-44f8-b790-fc1e7bf51b3e_targets/4661f55e-95c2-4011-8fd6-c5c56df1c9db.json');
t.is(target1.id, '4661f55e-95c2-4011-8fd6-c5c56df1c9db');
t.is(target1.weight, 15);
});

test.serial('kcm dump --host http://localhost:3001', t => {
t.plan(3);
const ret = shell.exec('kcm dump --host http://localhost:3001');

t.is(ret.code, 0);
const plugin1 = require('./main/plugins/3d324d84-1sdb-30a5-c043-63b19db421d1.json');
t.is(plugin1.name, 'halo-auth');
t.is(plugin1.enabled, true);
});

test.serial('kcm dump --file ./kcm-config.json', t => {
t.plan(3);
const ret = shell.exec('kcm dump --file ./kcm-config.json');

t.is(ret.code, 0);
const cluster = require('./main/cluster/064f9f98619d_0.0.0.0:7946_d5593e0d422840519b0ec828a73af045.json');
t.is(cluster.status, 'alive');
t.is(cluster.address, '1.2.3.4:7946');
});

test.serial('kcm dump --instance wrongins', t => {
t.plan(1);
const ret = shell.exec('kcm dump --instance wrongins');

t.is(ret.code, 1);
});

test.serial('DEBUG=kcm:apply kcm apply', t => {
t.plan(5);
// rm a consumer - DELETE
shell.rm('-rf', './main/consumers/2d324024-8fdb-20a5-g044-62b19db411d1.json');

// disable a plugin - PATCH
const plugin1Path =
'./main/plugins/3d324d84-1sdb-30a5-c043-63b19db421d1.json';
const plugin1 = require(plugin1Path);
plugin1.enabled = false;
fse.writeJsonSync(plugin1Path, plugin1, { spaces: 2 });

// add a new snis - POST
fse.writeJsonSync(
'./main/snis/httpbin.com.json',
{
name: 'httpbin.com',
ssl_certificate_id: '16c39eab-49d9-40f9-a55e-c4ee47fada68',
created_at: 1485531710212
},
{ spaces: 2 }
);

const ret = shell.exec('DEBUG=kcm:apply kcm apply');

t.is(ret.code, 0);
// verify DELETE
const consumers = fs.readdirSync('./main/consumers');
t.is(consumers.length, 1);
t.is(consumers[0], '4d924084-1adb-40a5-c042-63b19db421d1.json');
// verify PATCH
const newPlugin1 = require(plugin1Path);
t.is(newPlugin1.enabled, false);
// verify POST
const snis = fs.readdirSync('./main/snis');
t.is(snis.length, 3);
});

test('DEBUG=kcm* kcm dump --all', t => {
const ret = shell.exec('DEBUG=kcm* kcm dump --all');
test.serial('kcm apply --host http://localhost:3001 --instance main', t => {
t.plan(2);
shell.rm('-rf', './main/plugins/4d924084-1adb-40a5-c042-63b19db421d1.json');

const ret = shell.exec('kcm apply --host http://localhost:3001 --instance main');

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');
const plugins = fs.readdirSync('./main/plugins');
t.is(plugins.length, 1);
});

test.serial('kcm dump --instance sec_test', t => {
t.plan(1);
const ret = shell.exec('kcm dump --instance sec_test');
t.is(ret.code, 0);
});

// At this point, local configs of `main` and `sec_test` are synced

test.serial('kcm apply --all', t => {
t.plan(1);
const ret = shell.exec('kcm apply --all');
t.is(ret.code, 0);
});

test.serial('kcm apply --instance wrongins', t => {
t.plan(1);
const ret = shell.exec('kcm apply --instance wrongins');
t.is(ret.code, 1);
});

test.serial('kcm apply --instance sec_test', t => {
t.plan(1);
shell.rm('-rf', './sec_test');
const ret = shell.exec('kcm apply --instance sec_test');
t.is(ret.code, 1);
});
32 changes: 0 additions & 32 deletions test/init-data/apis.json

This file was deleted.

16 changes: 0 additions & 16 deletions test/init-data/certificates.json

This file was deleted.

12 changes: 0 additions & 12 deletions test/init-data/consumers.json

This file was deleted.

Loading

0 comments on commit 8a4cbeb

Please sign in to comment.