Skip to content

Commit

Permalink
Add tests skeleton
Browse files Browse the repository at this point in the history
* Add tests skeleton

* Fix JSDoc

* Update dapi tests

* Add tips
  • Loading branch information
shumkov authored and nmarley committed Sep 6, 2018
1 parent 15c8e7f commit f6dab04
Show file tree
Hide file tree
Showing 27 changed files with 1,997 additions and 8 deletions.
34 changes: 34 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"extends": "airbnb-base",
"env": {
"node": true,
"mocha": true
},
"rules": {
"no-plusplus": 0,
"eol-last": [
"error",
"always"
],
"no-await-in-loop": "off",
"import/no-extraneous-dependencies": "off",
"no-restricted-syntax": [
"error",
{
"selector": "LabeledStatement",
"message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
},
{
"selector": "WithStatement",
"message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
}
],
"curly": [
"error",
"all"
]
},
"globals": {
"expect": true
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/ansible/*.retry
/networks/*.inventory

# Node.JS
node_modules

# Vagrant
.vagrant

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ arises or when Evolution is released.

* Note: You may need to run the above command with "pip2" instead of "pip" if
your default Python installation is version 3 (e.g. OSX + Homebrew).

4. Install [Node.JS](https://nodejs.org/en/download/) and dependecies:

```bash
npm install
```

## Configuration

Expand Down Expand Up @@ -91,6 +97,17 @@ To destroy available Dash Network run `bin/destroy` command with particular netw

You may pass `--keep-infrastructure` option to remove software and configuration and keep infrastructure.

## Test network

To test network run `bin/test` command with with particular network name:

```bash
./bin/test <network_name>
```

You may pass `--type` option to run particular type of tests (`smoke`, `e2e`). It possible to specify several types
using comma delimiter.

## Dash Network Services

### DashCore
Expand Down
8 changes: 4 additions & 4 deletions bin/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ea

. ./lib/utils.sh
. ./lib/cli/utils.sh

CMD_USAGE="Usage: ./deploy [options] <network_name>
Expand Down Expand Up @@ -30,9 +30,9 @@ case ${i} in
esac
done

. ./lib/init.sh
. ./lib/terraform.sh
. ./lib/ansible.sh
. ./lib/cli/init.sh
. ./lib/cli/terraform.sh
. ./lib/cli/ansible.sh

echo "Deploying $NETWORK_NAME network...";

Expand Down
8 changes: 4 additions & 4 deletions bin/destroy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ea

. ./lib/utils.sh
. ./lib/cli/utils.sh

CMD_USAGE="Usage: OPTION=value ./destroy <network_name>
Expand All @@ -26,9 +26,9 @@ case ${i} in
esac
done

. ./lib/init.sh
. ./lib/terraform.sh
. ./lib/ansible.sh
. ./lib/cli/init.sh
. ./lib/cli/terraform.sh
. ./lib/cli/ansible.sh

echo "Destroying $NETWORK_NAME network...";

Expand Down
38 changes: 38 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -ea

. ./lib/cli/utils.sh

CMD_USAGE="Usage: ./test [options] <network_name>
Options:
-m=<args> --mocha-args=<args> - Mocha arguments
-t=<types> --types=<types> - Test types: smoke, e2e. You may specify several using comma delimiter
-h --help - Show help"

for i in "$@"
do
case ${i} in
-m=*|--mocha-args=*)
MOCHA_ARGUMENTS="${i#*=}"
;;
-t=*|--types=*)
TEST_TYPES="${i#*=}"
;;
-h|--help)
echo ${CMD_USAGE}
exit 0
;;
*)
NETWORK_NAME="${i#*=}"
;;
esac
done

. ./lib/cli/init.sh
. ./lib/cli/test.sh

echo "Run test against $NETWORK_NAME network...";

run_mocha
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions lib/cli/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function run_mocha() {
if [ ! -f "$INVENTORY_FILE" ]; then
print_error "Ansible inventory file not found. Please read README.md how to create infrastructure with Terraform"
fi

if [ ! -f "$ANSIBLE_CONFIG_PATH" ]; then
print_error "Ansible network config '$ANSIBLE_CONFIG_PATH' is not found. Please read README.md how to configure networks"
fi

# Export ansible inventory file

ANSIBLE_INVENTORY_EXPORT_PATH="networks/$NETWORK_NAME-inventory.json"

ANSIBLE_GROUP_VARS_PATH="ansible/group_vars/all"

ANSIBLE_INVENTORY=$(ansible-inventory --list --export -i "$INVENTORY_FILE")

# Select tests

if [ -z ${TEST_TYPES} ]; then
TEST_TYPES="smoke,e2e"
fi

TEST_TYPES_STRING=""
IFS=',' read -ra TEST_TYPES_ARRAY <<< "$TEST_TYPES"
for TYPE in "${TEST_TYPES_ARRAY[@]}"; do
TEST_TYPES_STRING="$TEST_TYPES_STRING test/$TYPE/**/*.spec.js"
done

# Run mocha tests

node_modules/.bin/_mocha ${MOCHA_ARGUMENTS} ${TEST_TYPES_STRING}
}
File renamed without changes.
10 changes: 10 additions & 0 deletions lib/test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { expect, use } = require('chai');
const dirtyChai = require('dirty-chai');
const chaiAsPromised = require('chai-as-promised');

use(chaiAsPromised);
use(dirtyChai);

process.env.NODE_ENV = 'test';

global.expect = expect;
31 changes: 31 additions & 0 deletions lib/test/getNetworkConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const readYamlFiles = require('./readYamlFiles');

let info;

/**
* Get network information
*
* @return {{ network:string, networkName: string,
* devnetName: string, inventory: object,
* variables: object}}
*/
module.exports = function getNetworkConfig() {
if (info) {
return info;
}

const variables = readYamlFiles(
`${__dirname}/../../${process.env.ANSIBLE_GROUP_VARS_PATH}`,
`${__dirname}/../../${process.env.ANSIBLE_CONFIG_PATH}`,
);

info = {
network: process.env.NETWORK,
networkName: process.env.NETWORK_NAME,
devnetName: process.env.NETWORK_DEVNET_NAME,
inventory: JSON.parse(process.env.ANSIBLE_INVENTORY),
variables,
};

return variables;
};
16 changes: 16 additions & 0 deletions lib/test/readYamlFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require('fs');
const merge = require('lodash.merge');
const yaml = require('js-yaml');

/**
* Read and merge YAML files
* @param files
*/
module.exports = function readYamlFiles(...files) {
const objectsArray = files.map((file) => {
const fileString = fs.readFileSync(file, 'utf8');
return yaml.safeLoad(fileString);
});

return merge(...objectsArray);
};
Loading

0 comments on commit f6dab04

Please sign in to comment.