Automation

Created by

Remus Golgot

Last updated Jan 17, 2019

https://www.terlici.com/2015/09/29/automated-testing-with-node-express-github-and-jenkins.html

https://lethalbrains.com/integrating-jenkins-with-github-node-js-and-slack-e8fc4d2271b5

Prerequisites :

sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer
sudo apt install oracle-java8-set-default
cd /tmp && wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo 'deb https://pkg.jenkins.io/debian-stable binary/' | sudo tee -a /etc/apt/sources.list.d/jenkins.list
sudo apt update
sudo apt install jenkins

  • add jenkins user in the sudoers file

    • visudo -f /etc/sudoers

    • (last line of the file ) : jenkins ALL = (ALL) NOPASSWD:ALL

  • Jenkins configurations

    • add jenkins secret

    • install default plugins

    • create admin user

    • make sure the port Jenkins is running on is different than 8080 ( which is by default used by the private IPFS node)

      • sudo nano /etc/default/jenkins
        #port for HTTP connector (default 8080; disable with -1) Http_port = 8888
  • libpq-dev

    • sudo apt-get update

    • sudo apt-get install -yyq postgresql postgresql-contrib libpq-dev

  • .npm permissions

    • mkdir ~/.npm-global

    • npm config set prefix '~/.npm-global'

    • export PATH=~/.npm-global/bin:$PATH

  • mocha-junit-reporter

  • Groovy

    • sudo apt-get install groovy

    • install the Groovy Jenkins plugin

  • make sure the jenkins service is running

    • service jenkins status
  • make sure the postgresql service is running

    • service postgresql status

Build Job

  • if successful, triggers Run Node and Run Tests jobs

  • stops internally after npm install is complete

  • shell script step :

if (sudo netstat -ltnp | grep -w '4100') then sudo fuser -k 4100/tcp; fi
echo 'initializing database ...'
sudo -u postgres psql -c 'drop database persona_localnet'
sudo -u postgres psql -c 'create database persona_localnet'
sudo mkdir -p /home/persona
export PJ_HOME=/home/persona/personatestnet
sudo rm -rf $PJ_HOME
echo 'cloning repo ...'
cd /home/persona
sudo git clone https://github.com/PersonaIam/personatestnet.git
cd personatestnet
echo 'npm install ... '
sudo npm cache clean --force
sudo chown -R jenkins $PJ_HOME
sudo chown -R jenkins /var/lib/jenkins/
npm install --save-dev mochawesome
sudo npm install --prefix /home/persona --save-dev node-json2html
npm install

Run Node Job

  • triggered by the Build job ( only if the upstream job is successful )

  • stopped by the Stop Node job

  • shell script step

if (sudo netstat -ltnp | grep -w '4100') then sudo fuser -k 4100/tcp; fi

sleep 5s

export PJ_HOME=/home/persona/personatestnet

cd $PJ_HOME

sudo npm run start:localnet

Run Tests Job

  • triggered by the Build job ( only if the upstream job is successful )

  • stops internally when all the tests are executed

  • triggers Stop Node job

  • shell script step #1

sleep 300s

export PJ_HOME=/home/persona/personatestnet

echo $PJ_HOME

cd $PJ_HOME

node_modules/mocha/bin/mocha test/api/business --reporter mochawesome

  • shell script step #2

sudo su

cp /home/persona/personatestnet/mochawesome-report/mochawesome.html '/var/lib/jenkins/workspace/Run Tests'

cp /home/persona/personatestnet/mochawesome-report/mochawesome.json '/var/lib/jenkins/workspace/Run Tests'

node /home/persona/main.js

javascript fileEdit file

main.js ( reformats the mochawesome JSON result file into multiple, prettier, HTML files )

Stop Node Job

  • triggered by the Run Tests job ( even if the upstream job fails )

  • stops the Run Node job

  • Groovy System shell script

import jenkins.*

import jenkins.model.*

Jenkins.instance.getItemByFullName("Run Node").getLastBuild().doStop();

Last Automation Execution

Detailed Report (contains all the tests, including the prerequisites as well as duplication for some of the business scenarios)

http://5.135.75.75:8888/job/Run Tests/ws/detailedReport.html

Business Report (contains only the business scenarios)

http://5.135.75.75:8888/job/Run Tests/ws/businessReport.html

(5.135.75.75 credentials : admin / 7fb9189bbda9494f8dc8507d440be783)