Skip to content

Commit

Permalink
refactor: parallel test v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Mar 23, 2018
1 parent 36c4d6b commit 08612de
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 100 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"test": "bash test/e2e.sh; bash test/unit.sh"
"test": "node test/parallel.js"
},
"keywords": [
"cli",
Expand Down Expand Up @@ -173,7 +173,7 @@
"moment": "^2.19",
"needle": "^2.2.0",
"nssocket": "0.6.0",
"pidusage": "^2.0.5",
"pidusage": "^2.0.6",
"pm2-axon": "3.1.0",
"pm2-axon-rpc": "0.5.0",
"pm2-deploy": "^0.3.9",
Expand Down
2 changes: 1 addition & 1 deletion test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WORKDIR /var/pm2
ENV NODE_ENV test
ENV PM2_DISCRETE_MODE true

RUN apk update && apk add bash git curl python3 php5 && rm -rf /var/cache/apk/*
RUN apk update && apk add bash git curl python python3 php5 && rm -rf /var/cache/apk/*
RUN ln -s /usr/bin/php5 /usr/bin/php
RUN npm install -g [email protected]

Expand Down
32 changes: 0 additions & 32 deletions test/e2e/agent/interact.sh

This file was deleted.

2 changes: 1 addition & 1 deletion test/e2e/cli/app-configuration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exists 'probe Event Loop Latency default value' "alert: {}"

# Override value for test-probe
$pm2 set probe-test.probes.test-probe.value 30
sleep 2
sleep 1

exists 'probe Event Loop Latency alerted' "value: 30"

Expand Down
24 changes: 13 additions & 11 deletions test/e2e/cli/cli-actions-2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source "${SRC}/../include.sh"

cd $file_path

############# TEST
############# Start / Stop / Restart

echo -e "\033[1mRunning tests:\033[0m"

Expand All @@ -17,7 +17,8 @@ spec "Should stop an app by script.js"
$pm2 restart echo.js
spec "Should restart an app by script.js (TRANSITIONAL STATE)"

###############
############### Start edge case

$pm2 delete all

echo "Start application with filename starting with a numeric"
Expand All @@ -28,28 +29,29 @@ should 'should app be stopped' 'stopped' 1
$pm2 restart 001-test
should 'should app be online once restart called' 'online' 1

$pm2 delete all

############## PID

$pm2 delete all
$pm2 start 001-test.js --name "test"
should 'should app be online' 'online' 1
$pm2 pid > /tmp/pid-tmp
$pm2 pid test
$pm2 delete all

###############

$pm2 delete all
echo "Start application with filename starting with a numeric"
$pm2 start throw-string.js -l err-string.log --merge-logs --no-automation
>err-string.log
sleep 2
sleep 1
grep 'throw-string.js' err-string.log
spec "Should have written raw stack when throwing a string"

$pm2 delete all

####

$pm2 delete all

$pm2 start echo.js --name gege
should 'should app be online' 'online' 1
$pm2 stop gege
Expand Down Expand Up @@ -78,7 +80,7 @@ $pm2 start echo.js
ispec "Should not re start app"

########### DELETED STUFF BY ID
$pm2 kill
$pm2 delete all

$pm2 start echo.js
$pm2 delete 0
Expand All @@ -100,7 +102,7 @@ $pm2 list
should 'should has been deleted process by script' "name: 'echo'" 0

######## Actions on app name as number (#1937)
$pm2 kill
$pm2 delete all
$pm2 start echo.js --name "455"
should 'should restart processes' 'restart_time: 0' 1
$pm2 restart 455
Expand All @@ -113,9 +115,9 @@ $pm2 delete 455
should 'should has been deleted process by id' "name: '455'" 0

########### OPTIONS OUTPUT FILES
$pm2 kill
$pm2 delete all

$pm2 start echo.js -o outech.log -e errech.log --name gmail -i 10
$pm2 start echo.js -o outech.log -e errech.log --name gmail -i 1
sleep 1
cat outech-0.log > /dev/null
spec "file outech-0.log exist"
Expand Down
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions test/e2e/file-descriptor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

#
# LSOF check
#

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/../include.sh"

cd $file_path

echo "################## RELOAD ###################"

# lsof -c PM2 > /tmp/no_pm2_out.dat

# $pm2 list

# sleep 1
# lsof -c PM2 > /tmp/empty_pm2_out.dat

# $pm2 start echo.js -i 3
# $pm2 start killtoofast.js -i 3
# $pm2 delete all

# sleep 3
# lsof -c PM2 > /tmp/empty_pm2_out2.dat

# OUT1=`cat /tmp/empty_pm2_out.dat | wc -l`
# OUT2=`cat /tmp/empty_pm2_out2.dat | wc -l`

# if [ $OUT1 -eq $OUT2 ]; then
# success "All file descriptors have been closed"
# else
# fail "Some file descriptors are still open"
# fi

# $pm2 start killtoofast.js -i 6
# $pm2 kill

# rm /tmp/no_pm2_out.dat
# rm /tmp/no_pm2_out2.dat
# rm /tmp/empty_pm2_out.dat
# rm /tmp/empty_pm2_out2.dat

# sleep 6
> /tmp/no_pm_pm2_out.dat
> /tmp/no_pm_pm2_out2.dat

lsof -c PM2 > /tmp/no_pm2_out2.dat
diff /tmp/no_pm2_out.dat /tmp/no_pm2_out2.dat

if [ $? == "0" ]; then
success "All file descriptors have been closed"
else
fail "Some file descriptors are still open"
fi

rm /tmp/no_pm2_out.dat
rm /tmp/no_pm2_out2.dat
rm /tmp/empty_pm2_out.dat
rm /tmp/empty_pm2_out2.dat
53 changes: 0 additions & 53 deletions test/e2e/misc/file-descriptor.sh

This file was deleted.

2 changes: 2 additions & 0 deletions test/e2e/misc/versioning-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "${SRC}/../include.sh"

cd $file_path

rm -rf app-playground

git clone https://github.com/keymetrics/app-playground.git

cd app-playground
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
111 changes: 111 additions & 0 deletions test/parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

const async = require('async')
const fs = require('fs')
const exec = require('child_process').exec
const path = require('path')
const chalk = require('chalk')
const Table = require('cli-table-redemption');

const testFolder = './test/e2e/'

const CONCURRENT_TEST = 6
const DOCKER_IMAGE_NAME = 'pm2-test'

var timings = {};

function run(cmd, cb) {
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.log(`Retrying ${cmd}`)
return exec(cmd, function(err, stdout, stderr) {
if (err) return cb(stdout.split('\n'));
return cb(null);
})
}
return cb(null)
})
}

function buildContainer(cb) {
exec(`docker build -t ${DOCKER_IMAGE_NAME} -f test/Dockerfile .`, cb)
}

function listAllTest(cb) {
var test_suite = []

fs.readdir(testFolder, (err, folders) => {
async.forEachLimit(folders, 4, (folder, next) => {
var fold = path.join(testFolder, folder)
fs.readdir(fold, (err, files) => {
if (err) return next()
files.forEach((file) => {
test_suite.push(path.join(fold, file))
})
next()
})
}, function() {
launchTestSuite(test_suite, cb)
})
})
}

function launchTestSuite(files, cb) {
async.forEachLimit(files, CONCURRENT_TEST, function(file, next) {
var cmd = `docker run -v ${path.resolve(__dirname, '..')}:/var/pm2 ${DOCKER_IMAGE_NAME} bash ${file}`

console.log(chalk.bold(`Running test ${file}`))
timings[file] = new Date().getTime()

run(cmd, function(err) {
if (err) {
// Display Error
console.error(chalk.bold.red(`${'='.repeat(25)} Test File ${file} has failed ${'='.repeat(25)}`))
console.error(chalk.bold('Output (stderr):'))
err.forEach(function(line) {
console.error(line)
})
console.error(chalk.bold.red(`${'='.repeat(80)}`))
return next(err)
}

timings[file] = new Date().getTime() - timings[file]

console.log(chalk.bold.green(`✓ Test ${file} success`))
return next();
})
}, (err) => {
if (err) {
console.log('Test Suite has failed')
cb(err)
}
console.log('Test Suite passed succesfully')
cb()
})
}

buildContainer(function(err) {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Container ${DOCKER_IMAGE_NAME} has been built`)

return listAllTest(function(err) {

var table = new Table({
head: ['Test', 'Duration'],
style : {'padding-left' : 1, head : ['cyan', 'bold'], compact : true}
});

Object.keys(timings).forEach(function(test) {
table.push(test, timings[test])
})

console.log(table.toString());

if (err) {
return console.error(chalk.bold.red('Test suite failed'))
}
console.log(chalk.bold.blue('Test suite succeeded'))
})
})

0 comments on commit 08612de

Please sign in to comment.