Skip to content

Commit

Permalink
Fixes libms windows error and tests (#888)
Browse files Browse the repository at this point in the history
 - Fixes libms error on Windows OS
 - Improves e2e tests and makes them work on github actions
 - Adds docker compose for developers

---------
Co-authored-by: nichlaes <[email protected]>
  • Loading branch information
nichlaes authored Sep 16, 2024
1 parent 228ed5c commit 5be6bb2
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 75 deletions.
63 changes: 42 additions & 21 deletions .github/workflows/lib-ms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ on:
paths:
- 'servers/lib/**'
workflow_dispatch:
paths:
- 'servers/lib/**'

jobs:
test-lib-ms:
name: Test library microservice
runs-on: ubuntu-latest
defaults:
run:
working-directory: servers/lib

steps:
- name: Checkout
Expand All @@ -27,47 +28,67 @@ jobs:
cache: "yarn"
cache-dependency-path: "**/yarn.lock"

# - name: Install pm2
# run: |
# npm install -g pm2
- name: Install pm2 and jest
run: |
npm install -g pm2
npm install -g jest
- name: Run the linting checks
run: |
cd servers/lib
yarn install
yarn syntax
- name: Build the lib microservice
run: |
cd servers/lib
yarn install
yarn build
- name: Run all tests
env:
PORT: 4001
LOCAL_PATH: ${{ github.workspace }}/files
MODE: local
LOG_LEVEL: debug
APOLLO_PATH: /lib
run: |
cd servers/lib
yarn install
yarn build
yarn test:all
- name: Start http mode
env:
PORT: 4001
PORT: 4003
LOCAL_PATH: ${{ github.workspace }}/files
MODE: local
LOG_LEVEL: debug
APOLLO_PATH: /lib
run: |
yarn install
yarn build
yarn start:pm2
- name: Check server is running
env:
PORT: 4003
uses: nick-fields/retry@v3
with:
timeout_seconds: 10
max_attempts: 4
command: "curl -f -LI http://localhost:${{ env.PORT }}/lib/files"

- name: Run http test
env:
PORT: 4003
LOCAL_PATH: ${{ github.workspace }}/files
MODE: local
LOG_LEVEL: debug
APOLLO_PATH: /lib
run: |
yarn test:http-github
# - name: Run http mode tests
# run: |
# cd servers/lib
# yarn install
# yarn build
# yarn test:http-github
# env:
# PORT: 4002
# LOCAL_PATH: ${{ github.workspace }}/files
# MODE: local
# LOG_LEVEL: debug
# APOLLO_PATH: /lib
- name: Stop http mode
run: |
yarn stop:pm2
- name: Upload test coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
62 changes: 62 additions & 0 deletions servers/lib/DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ The URL endpoint for this microservice is located at: `localhost:PORT/lib`

The [API](./API.md) page shows sample queries and responses.

## Manual testing

As of now, some features require manual testing or attention.

### CloudCMD (a npm package dependency)

Due to how CloudCMD works,
it is required to manually run `yarn test:http` with `LOCAL_PATH` both as an
absolute path and local path. For example, the manual tests
need to be done with the following `LOCAL_PATH` values.

| OS | Absolute Path | Relative Path |
| ------- | ----------------------------- | ------------- |
| Windows | C:\DTaaS\files | ..\..\files |
| Linux | /Users/<Username>/DTaaS/files | ../../files |

### PM2

the pm2 might not close spawned processes correctly,
therefore please use the following command to kill a process.
`pm2 del <id>`.

**NOTE:** to see the processes spawned by pm2 use `pm2 list`

## :package: :ship: Packages

### Docker images
Expand Down Expand Up @@ -86,6 +110,44 @@ with publishing **libms npm package** in local computer.
Application of the advice given on that page for **libms** will require
running the following commands.

## Use in Docker Environment

### Adjust Configuration (Optional)

The microservices require configuration and the docker version
of the microservices uses the configuration
file available in `config/.env.default`.

Please add a `.env` file with the environment variables for
the docker compose file to use.
e.g.

```sh
PORT='4001'
MODE='local'
LOCAL_PATH='..\..\files'
```

### Use

The commands to start and stop the appliation are:

**NOTE**: the docker compose file is located in the `servers/lib` directory.

```bash
docker compose -f compose.lib.dev.yml up -d
```

This command brings up the lib docker container and makes
the website available at <http://localhost:4001>.
The `config/.env.default` file is used as the microservice configuration.
If the configuration values are changed, please restart the container.

```bash
docker compose -f compose.lib.dev.yml down
docker compose -f compose.lib.dev.yml up -d
```

### Publish

```bash
Expand Down
31 changes: 31 additions & 0 deletions servers/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ your Github username and the password is your Github
In order for the npm to download the package, your personal access token
needs to have _read:packages_ scope.

## Use in Docker Environment

### Adjust Configuration (Optional)

The microservices require configuration,
see the [Configure](#gear-configure) section for more info.

The docker version of the microservices uses the configuration
file available in `config/.env.default`.
If you would like to adjust the configuration, please change this file.

### Use

The commands to start and stop the appliation are:

```bash
git clone https://github.com/INTO-CPS-Association/DTaaS.git
cd servers/lib
docker compose -f compose.lib.yml up -d
```

This command brings up the lib docker container and makes
the website available at <http://localhost:4001>.
The `config/.env.default` file is used as the microservice configuration.
If the configuration values are changed, please restart the container.

```bash
docker compose -f compose.lib.yml down
docker compose -f compose.lib.yml up -d
```

## :gear: Configure

The microservices requires config specified in INI format.
Expand Down
9 changes: 9 additions & 0 deletions servers/lib/compose.lib.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
libms:
build:
context: ../../
dockerfile: ./docker/libms.dockerfile
volumes:
- ${LOCAL_PATH}:/dtaas/libms/files
ports:
- ${PORT}:4001
3 changes: 2 additions & 1 deletion servers/lib/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"node_modules",
"./dist",
"../src/app.module.ts",
"../src/main.ts"
"../src/main.ts",
"../src/bootstrap.ts"
],
"modulePathIgnorePatterns": ["config"],
"coverageDirectory": "<rootDir>/coverage/",
Expand Down
12 changes: 6 additions & 6 deletions servers/lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@into-cps-association/libms",
"version": "0.4.5",
"version": "0.4.6",
"description": "microservices that handles request by fetching and returning the file-names and folders of given directory",
"author": "[email protected]",
"contributors": [
Expand All @@ -11,21 +11,21 @@
"private": false,
"license": "SEE LICENSE IN <LICENSE.md>",
"scripts": {
"build": "npx tsc",
"build": "tsc",
"clean": "npx rimraf build node_modules coverage dist src.svg test.svg",
"format": "prettier --ignore-path ../.gitignore --write \"**/*.{ts,tsx,css,scss}\"",
"graph": "npx madge --image src.svg src && npx madge --image test.svg test",
"start": "node dist/src/main.js",
"start:pm2": "pm2 start pm2.config.js",
"stop:pm2": "pm2 delete libms",
"syntax": "npx eslint . --fix",
"syntax": "eslint . --fix",
"pretest": "npx shx cp test/data/user2/tools/README.md ../../files/user2/tools/README.md",
"posttest": "npx rimraf ../../files/user2/tools/README.md",
"test:all": "npx cross-env LOCAL_PATH=test/data jest --testPathIgnorePatterns=cloudcmd --coverage",
"test:e2e": "npx cross-env LOCAL_PATH=test/data jest --config ./test/jest-e2e.json --coverage",
"test:http": "yarn build && pm2 start -f --name libms-test dist/src/main.js -- -c .env -H ./config/http.json && jest test/cloudcmd --coverage --coverageThreshold=\"{}\" && pm2 delete libms-test",
"test:http-nocov": "yarn build && pm2 start -f --name libms-test dist/src/main.js -- -c .env -H ./config/http.json && jest test/cloudcmd --coverage=false && pm2 delete libms-test",
"test:http-github": "yarn build && yarn start:pm2 && jest test/cloudcmd --coverage --coverageThreshold=\"{}\" && pm2 delete libms",
"test:http-github": "jest test/cloudcmd --coverage --coverageThreshold=\"{}\"",
"test:int": "npx cross-env LOCAL_PATH=test/data jest ../test/integration --coverage",
"test:nocov": "yarn test:http-nocov && npx cross-env LOCAL_PATH=test/data jest --testPathIgnorePatterns=cloudcmd --coverage=false",
"test:unit": "npx cross-env LOCAL_PATH=test/data jest ../test/unit --coverage"
Expand Down Expand Up @@ -59,8 +59,8 @@
"@types/jest": "^29.5.12",
"@types/node": "20.12.5",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"@typescript-eslint/parser": "^8.4.0",
"cross-fetch": "^4.0.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion servers/lib/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function bootstrap(options?: BootstrapOptions) {
path: options?.config ?? '.env',
override: true,
});
if (configFile.error) {
if (configFile.error && process.env.LOCAL_PATH === undefined) {
// eslint-disable-next-line no-console
console.error(configFile.error);
if (options.runHelp) {
Expand Down
12 changes: 10 additions & 2 deletions servers/lib/src/cloudcmd/cloudcmd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { INestApplication } from '@nestjs/common';
import { Server } from 'socket.io';
import * as cloudcmd from 'cloudcmd';
import { join } from 'path';
import { join, relative } from 'path';

const isWindowsAbsolutePath = (filesPath: string) => filesPath.includes(':');

const runCloudCMD = (
app: INestApplication,
Expand All @@ -13,7 +15,13 @@ const runCloudCMD = (
configPath: join(process.cwd(), optionsPath),
});

configManager('root', filesPath);
if (isWindowsAbsolutePath(filesPath)) {
const workDir = process.cwd();
const relativePath = relative(workDir, filesPath);
configManager('root', relativePath);
} else {
configManager('root', filesPath);
}

const server = app.getHttpServer();

Expand Down
2 changes: 1 addition & 1 deletion servers/lib/src/files/services/local-files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class LocalFilesService implements IFilesService {

return LocalFilesService.formatResponse(name, content);
} catch (error) {
throw new InternalServerErrorException('Error reading file');
throw new InternalServerErrorException('Error reading file', error);
}
}

Expand Down
10 changes: 4 additions & 6 deletions servers/lib/test/cloudcmd/cloudcmd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ describe('cloudcmd test for the application', () => {
responseType: 'json',
},
);
/* eslint-disable no-console */
console.log(response.data);
/* eslint-enable no-console */
expect(response.data.path).toEqual('/');
expect(response.data.files[0].name).toEqual('common');
expect(response.data.files[1].name).toEqual('user1');
expect(response.data.files[2].name).toEqual('user2');
const fileNames = response.data.files.map((file) => file.name);
expect(fileNames).toContain('common');
expect(fileNames).toContain('user1');
expect(fileNames).toContain('user2');
}, 10000);

it('should return the content of a file that is uplaoded to cloudcmd ', async () => {
Expand Down
2 changes: 1 addition & 1 deletion servers/lib/test/unit/files-service.factory.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ describe('FilesServiceFactory', () => {

it('should throw an error when MODE is invalid', () => {
jest.spyOn(configService, 'get').mockReturnValue('invalid');
expect(() => serviceFactory.create()).toThrowError(`Invalid MODE: invalid`);
expect(() => serviceFactory.create()).toThrow(`Invalid MODE: invalid`);
});
});
Loading

0 comments on commit 5be6bb2

Please sign in to comment.