<img src="https://upload.wikimedia.org/wikipedia/commons/e/e9/Jenkins_logo.svg" width="50">
# Smoke tests with Jenkins + Nightfall Client-SDK
## Objective
The main goal of this test is warn the slack team if some error happen at moment of some transaction using the nightfall [client-SDK](https://github.com/maticnetwork/nightfall-sdk).
## Test
### Part 1
----
The smoke test will generate 32 deposits. One of them will be for a new user3 created without a mnemonic.
To be certain that the block will be generated, the test will do more 31 transaction. This time using a user1 with an already existent mnemonic.
After that will be possible to verify if the user3 balance is its balance before + the value of deposit.
### Part 2
----
The second part is do 32 transfers. The first between the user3 for a new user4 created without a mnemonic.
Again to be certain that the block will be generated, the test will do more 31 trasfers. This time from user1 to user2, two existent users that are created passing a existent mnemonic.
After that is possible check the user4 balance that should be the balance before + value of transfer.
____
## Jenkins setup localy (Ubuntu)
### Install Java
```
$ sudo apt update
$ sudo apt install openjdk-11-jre
$ java -version
```
### Install Jenkins
```
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
```
### Start Jenkins
**You can enable the Jenkins service to start at boot with the command:**
```
sudo systemctl enable jenkins
```
**You can start the Jenkins service with the command:**
```
sudo systemctl start jenkins
```
**You can check the status of the Jenkins service using the command:**
```
sudo systemctl status jenkins
```
## Was created a new nodejs project inside CICD github repo
Repo: https://github.com/maticnetwork/nightfall-cicd
```
cd jenkins/jobs && mkdir nightfall-smoke-tests && cd nightfall-smoke-tests
```
```
npm init -y
```
#### Projects dependences
```
npm i aws-sdk nightfall-sdk
```
```
npm i mocha chai --save-dev
```
#### Created the test
The test should do enough deposits to some user1 until the block creation.
After that verify the user balance.
Then shall do enough transfers from user1 to user2 until the second block generation.
Again do the balances verification.
**The test is in:**
_nightfall-cicd/jenkins/jobs/nightfall-smoke-tests/**tests**/e2e/smoke.spec.js_
#### Created a Jenkinsfile in the project root
## Jenkins Setup
Go to the dashboard and create a new item:

Enter a name, select pipeline and press ok:

### Pipeline settings
Insert a description and add the github project that your test will run:

On pipeline settings:

- First chose _Pipeline script from SCM_ in definition
- Insert the repository that contain the Jenkinsfile
- Create a SSH Username with private key credential based in your github ssh key
- Insert a specific branch
Then:

Insert the path where your Jenkinsfile is located in the repository.
And save.
## Slack setup
### Plugin
First verify if you already have the Slack Plugin. If not, install it.

**Go to Dashboard -> Manage Jenkins -> Configure System and do the steps below:**

**On credential, if you not have yet, add a new one:**
- *Obs: The credential is of Secret text type. Ask the password for the Administrator.*

## Global Tool Configuration
Go to Dashboard -> Manage Jenkins -> Global Tool Configuration
### Git setup
Insert the path where your executable of git is located in your machine.

### NodeJS setup
For that is necessary install the NodeJS Plugin. Go to Dashboard -> Manage Jenkins -> Plugins.

Then go to Global Tool Configuration and do the steps below:
*OBS: At the moment of we were writing this documentation, we were using node 16.17.1.*

## Add jenkins to sudoers
This is necessary to run sudo in the pipeline without ask password.
```
sudo visudo
```
And add the follow:
```
jenkins ALL=(ALL) NOPASSWD: ALL
```

## Setup jenkins user to use AWS
For this part we're predicting that you already have the aws cli installed in your machine. We not cover that on this documentation.
**First create a .aws with jenkins user.**
```
sudo -su jenkins
cd /var/lib/jenkins/
mkdir .aws
```
**Then run aws configure and add the corrects:**
```
- AccessKeyId
- SecretAccressKey
- Region
```
# Jenkinsfile Pipeline
*Take a look in Jenkinsfile inside the root of this project*
## What this pipeline should do?
1. Set the aws credentials to some env var.
2. Do a checkout of the SCM. (Stage 1)
3. Set the env vars to run the client container. (Stage 2)
4. Run the script start-client.sh that will download the required files from S3 and execute the rabbitmq, worker and client containers. (Stage 3)
5. Do a checkout of nightfall_3_private repo. (Stage 4 - parallel 1)
- Go to the certificates folder of this project.
- Open the VPN with the openvpn comand.
- For that is necessary add jenkins user to sudoers*
6. Go to the nightfall-smoke-tests folder and run npm ci and test. (Stage 4 - parallel 2)
7. Close VPN and stop containers. (Stage 5)
8. We'll have also a post step that will send a message to slack if some error was caught. (Post - Unstable)
## 1 - Set the aws credentials to some env var.
```
environment {
My_User_Credentials = credentials('aws-user-pwd')
}
```
### Credentials
To use this. First we need to create a UserWithPassoword credentials using the AWS AccessKeyId and SecretAccessKey:
#### A simple Username with password credential
To set the variable direct in the pipeline code lets create a simple Username with password credential type for that.
The Username will be our AWS Access key ID and the password our Secret access key. Talk with the administrator to get these data.
The id of the credential gonna be used to get this data in the pipeline and pass to the tests.

With it done, when you run the pipeline Jenkins will generate two env vars that can be used within javascript test (My_User_Credentials_USR and My_User_Credentials_PSW). To get this env vars you just need to use process.env.
```
AWS.config.update({
accessKeyId: process.env.My_User_Credentials_USR,
secretAccessKey: process.env.My_User_Credentials_PSW,
region: "us-east-2", // SSM Region
});
```
*Take a look on smoke-tests.spec.js to see the complete code.*
#### Now lets create a real aws credential. For that verify first if you have the follow plugin:

If not install this.
After that go to manage credentials - globals - and create an AWS credential.
