Skip to content

feat(tests): add Localstack testing workflow #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions .github/workflows/localstack-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Localstack Tests

on:
workflow_call:
inputs:
alchemy-api-key:
description: 'API key for Alchemy'
required: true
type: string
infura-project-id:
description: 'Project ID for Infura'
required: true
type: string
etherscan-api-key:
description: 'API key for Etherscan'
required: true
type: string
sonar-token:
description: 'Token for SonarCloud/ SonarQube'
required: true
type: string
sonar-host-url:
description: 'URL of Sonar server'
required: true
type: string
node-version:
description: 'Node.js version to use'
required: false
type: string
default: '18'
run-check-types:
description: 'Enable check types'
required: false
type: boolean
default: true

jobs:
test:
runs-on: ubuntu-latest
env:
ALCHEMY_API_KEY: ${{ inputs.alchemy-api-key }}
INFURA_PROJECT_ID: ${{ inputs.infura-project-id }}
ETHERSCAN_API_KEY: ${{ inputs.etherscan-api-key }}
SONAR_TOKEN: ${{ inputs.sonar-token }}
SONAR_HOST_URL: ${{ inputs.sonar-host-url }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}

- name: Install dependencies
run: npm ci

- name: Prepare local stack
id: prepare
run: npm run start-test-stack

- name: Build
run: npm run build

- name: Check format
run: npm run check-format

- name: Lint
run: npm run lint

- name: Check Types
if: ${{ inputs.run-check-types }}
run: npm run check-types

- name: Install global & Test
run: |
sudo apt-get update
sudo apt-get install -y xxd
npm install -g .
Comment on lines +78 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the goal of those lines ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but it's not always needed if you check dataprotector-sdk for instance
https://github.com/iExecBlockchainComputing/dataprotector-sdk/blob/develop/packages/sdk/.drone.yml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If is in var it's good for you ?

npm test

- name: SonarScanner
uses: SonarSource/[email protected]
env:
SONAR_TOKEN: ${{ inputs.sonar-token }}
SONAR_HOST_URL: ${{ inputs.sonar-host-url }}
Comment on lines +83 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what SonarScanner is ? What it do ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Le-Caignec Good for the explain ?

131 changes: 131 additions & 0 deletions localstack-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Localstack Tests - Reusable Workflow Documentation 🚀

## Overview 🌟

This reusable GitHub Actions workflow automates the process of running tests against a local stack environment for Node.js applications. It is configurable via inputs for API keys, Sonar configuration, Node.js version, and other options. The workflow performs the following actions:

- **Sets Up Environment**: Configures the environment with necessary API keys and tokens. 🔑
- **Prepares Node.js**: Installs the specified Node.js version. ⚙️
- **Installs Dependencies**: Uses `npm ci` to install the dependencies. 📦
- **Prepares Local Stack**: Sets up the test environment using `npm run start-test-stack`. 🏗️
- **Builds the Project**: Builds the project using `npm run build`. 🔨
- **Runs Quality Checks**: Performs format checking, linting, and optional type checking. 🧹
- **Installs Globally & Runs Tests**: Installs the package globally and runs tests. ✅
- **Runs Sonar Analysis**: Performs code analysis using SonarQube/SonarCloud. 📊

## Workflow Inputs 🛠️

| **Input** | **Description** | **Required** | **Default** |
|----------------------|--------------------------------------|--------------|-------------|
| **alchemy-api-key** | API key for Alchemy. | Yes | - |
| **infura-project-id**| Project ID for Infura. | Yes | - |
| **etherscan-api-key**| API key for Etherscan. | Yes | - |
| **sonar-token** | Token for SonarCloud/SonarQube. | Yes | - |
| **sonar-host-url** | URL of Sonar server. | Yes | - |
| **node-version** | Node.js version to use. | No | `18` |
| **run-check-types** | Enable check types. | No | `true` |

### Environment Variables 🌍

The workflow sets up the following environment variables for the test job:

- `ALCHEMY_API_KEY`: API key for Alchemy services
- `INFURA_PROJECT_ID`: Project ID for Infura services
- `ETHERSCAN_API_KEY`: API key for Etherscan services
- `SONAR_TOKEN`: Authentication token for SonarQube/SonarCloud
- `SONAR_HOST_URL`: URL of the Sonar server

## Job and Steps ⚙️

### Job Name: `test`

- **Runs On**: `ubuntu-latest`
- **Environment Variables**: Sets up API keys and tokens from inputs

#### Steps:

1. **Checkout Repository**: Uses `actions/checkout@v4` to fetch your code. 📥
2. **Setup Node.js**: Configures Node.js with `actions/setup-node@v4` using the specified version. ⚙️
3. **Install Dependencies**: Runs `npm ci` to install dependencies. 📦
4. **Prepare Local Stack**: Executes `npm run start-test-stack` to set up the test environment. 🏗️
5. **Build**: Builds the project using `npm run build`. 🔨
6. **Check Format**: Verifies code formatting using `npm run check-format`. 🧹
7. **Lint**: Performs code linting using `npm run lint`. 🧹
8. **Check Types**: Conditionally runs type checking using `npm run check-types` if enabled. 🔍
9. **Install Global & Test**: Installs the package globally and runs tests. ✅
- Updates apt packages
- Installs xxd utility
- Installs the package globally
- Runs tests using `npm test`
10. **SonarScanner**: Runs SonarQube/SonarCloud analysis using `SonarSource/[email protected]`. 📊

## How to Use This Reusable Workflow 🔄

1. **Save the Workflow File**
This workflow is already saved as `.github/workflows/localstack-tests.yml` in the repository. 💾

2. **Call the Reusable Workflow**
In another workflow file (e.g., triggered by a pull request), invoke this reusable workflow like so:

```yaml
name: Run Localstack Tests
on:
pull_request:
branches: [main, develop]

jobs:
test:
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/localstack-tests.yml@main
with:
alchemy-api-key: ${{ secrets.ALCHEMY_API_KEY }}
infura-project-id: ${{ secrets.INFURA_PROJECT_ID }}
etherscan-api-key: ${{ secrets.ETHERSCAN_API_KEY }}
sonar-token: ${{ secrets.SONAR_TOKEN }}
sonar-host-url: ${{ secrets.SONAR_HOST_URL }}
node-version: '20'
run-check-types: true
```

3. **Configure Secrets**
Ensure that the following secrets are added to your repository's settings:
- `ALCHEMY_API_KEY`: Your Alchemy API key
- `INFURA_PROJECT_ID`: Your Infura project ID
- `ETHERSCAN_API_KEY`: Your Etherscan API key
- `SONAR_TOKEN`: Your SonarQube/SonarCloud authentication token

## Prerequisites 📋

1. **NPM Scripts**:
- Your project must have the following npm scripts defined in package.json:
- `start-test-stack`: Script to set up the local test environment
- `build`: Script to build the project
- `check-format`: Script to verify code formatting
- `lint`: Script to perform code linting
- `check-types`: Script for type checking (optional, can be disabled)
- `test`: Script to run tests

2. **Sonar Configuration**:
- You should have a sonar-project.properties file or equivalent configuration for SonarQube/SonarCloud analysis.

## Workflow Steps in Detail 🔍

1. **Environment Setup**:
- Sets up environment variables for API keys and tokens.
- Checks out the repository code.

2. **Node.js Configuration**:
- Installs the specified Node.js version (default: 18).
- Prepares the Node.js environment for testing.

3. **Build and Quality Checks**:
- Installs dependencies using `npm ci`.
- Prepares the local stack environment.
- Builds the project.
- Performs code quality checks (formatting, linting, and optional type checking).

4. **Testing**:
- Installs the package globally.
- Runs tests using `npm test`.

5. **Code Analysis**:
- Runs SonarQube/SonarCloud analysis to evaluate code quality and identify issues.