Skip to content

Commit

Permalink
Merge pull request #1 from amplication/feat/dsg-e2e-tests-automation
Browse files Browse the repository at this point in the history
Feat/DSG e2e tests automation
  • Loading branch information
abrl91 authored Apr 1, 2024
2 parents f7f1f94 + 66e83b1 commit cd54c38
Show file tree
Hide file tree
Showing 35 changed files with 18,626 additions and 0 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/dsg-e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Data Service Generator E2E Test

on:
# schedule:
# - cron: '0 1 * * *'
workflow_dispatch:
inputs:
ecr_tag:
description: 'ECR container tag'
required: true
default: 'next'

jobs:
prepare-test-cases:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-test-cases.outputs.test-cases }}
steps:
- name: Checkout
uses: actions/checkout@v4

- id: set-test-cases
name: Generate matrix for test cases
run: |
echo "test-cases=$(find test-cases -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
build-and-run-generated-code:
runs-on: ubuntu-latest
needs: prepare-test-cases
strategy:
fail-fast: false
matrix:
test-case: ${{fromJson(needs.prepare-test-cases.outputs.test-cases)}}
env:
AWS_ACCOUNT_OPS_ID: ${{ secrets.AWS_ACCOUNT_OPS_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "package.json"
cache: "npm"
cache-dependency-path: "package.lock.json"

- name: Install dependencies
run: npm ci

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v1

- name: Run container
run: |
docker run -v ${{ github.workspace }}:/data --env-file ${{ github.workspace }}/test-cases/${{ matrix.test_case }}/dsg.env \
-e BUILD_SPEC_PATH=${{ github.workspace }}/test-cases/${{ matrix.test_case }}/input.json \
-e BUILD_OUTPUT_PATH=${{ github.workspace }}/test-cases/${{ matrix.test_case }}/generated \
-w /workspace/packages/data-service-generator \
${env.AWS_ACCOUNT_OPS_ID}.dkr.ecr.us-east-1.amazonaws.com/data-service-generator:${{ github.event.inputs.ecr_tag }} \
sh -c "node ./src/main.js"
- name: Check if directory exists
run: |
if [ ! -d ${{ github.workspace }}/test-cases/${{ matrix.test_case }}/generated/ ]; then
echo "Directory ${{ github.workspace }}/test-cases/${{ matrix.test_case }}/generated/ does not exist."
exit 1
fi
- name: Run generated code
run: |
cd ${{ github.workspace }}/test-cases/${{ matrix.test_case }}/generated/server
npm install
npm run prisma:generate
npm run db:init
npm run compose:up
- name: Run e2e tests
run: npm run e2e:test
working-directory: ${{ github.workspace }}

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
test-cases/**/generated/
.DS_Store
4 changes: 4 additions & 0 deletions dsg.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BUILD_ID=1
RESOURCE_ID=1
BUILD_MANAGER_URL=
REMOTE_ENV=false
4 changes: 4 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
103 changes: 103 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import fs from "fs";
import path from "path";
import {
DSGResourceData,
PluginInstallation,
} from "@amplication/code-gen-types";
import { PluginCombinationName, TestConfig } from "./types";
import {
authCore,
authBasic,
postgres,
authJWT,
auth0,
authSAML,
mysql,
} from "./test-data/plugins";
import {
entities,
resourceInfo,
roles,
moduleContainers,
moduleDtos,
customActions,
} from "./test-data";
import {
EnumDataType,
EnumResourceType,
} from "@amplication/code-gen-types/src/models";

const pluginCombinations: Record<PluginCombinationName, PluginInstallation[]> =
{
[PluginCombinationName.POSTGRES_NO_AUTH]: [postgres],
[PluginCombinationName.POSTGRES_BASIC]: [postgres, authCore, authBasic],
[PluginCombinationName.POSTGRES_JWT]: [postgres, authCore, authJWT],
[PluginCombinationName.POSTGRES_AUTH0]: [postgres, authCore, auth0],
[PluginCombinationName.POSTGRES_SAML]: [postgres, authSAML],
[PluginCombinationName.MYSQL_NO_AUTH]: [mysql],
[PluginCombinationName.MYSQL_BASIC]: [mysql, authCore, authBasic],
[PluginCombinationName.MYSQL_JWT]: [mysql, authCore, authJWT],
[PluginCombinationName.MYSQL_AUTH0]: [mysql, authCore, auth0],
};

/**
* Generates test config for each test case from the plugin combinations
* Taking care of case such as when MySQL is installed, creating entities without multi-select option data type
* @returns the test config for each test case
*/
function createTestConfig(): TestConfig[] {
const results = Object.entries(pluginCombinations).map(([name, plugins]) => {
let mockEntities = entities;

// remove entity field types that are not supported by mysql
if (plugins.find((plugin) => plugin.npm.indexOf("plugin-db-mysql"))) {
mockEntities = entities.map((entity) => {
entity.fields = entity.fields.filter(
(field) => field.dataType !== EnumDataType.MultiSelectOptionSet
);
return entity;
});
}

return {
name: name as PluginCombinationName,
data: {
buildId: "1",
entities: mockEntities,
roles,
resourceInfo,
resourceType: EnumResourceType.Service,
pluginInstallations: plugins,
moduleActions: customActions,
moduleContainers,
moduleDtos,
} as DSGResourceData,
dsgEnv: {
BUILD_SPEC_PATH: `/data/test-cases/${name}/input.json`,
BUILD_OUTPUT_PATH: `/data/test-cases/${name}/generated`,
},
};
});

return results;
}

/**
* Creates input.json and dsg.env for each test case from the test config
*/
function generateTestCasesDependencies(): void {
const testConfigs = createTestConfig();
testConfigs.forEach(({ name, data, dsgEnv }) => {
const testCaseDir = path.join(process.cwd(), `test-cases/${name}`);
fs.mkdirSync(testCaseDir, { recursive: true });

const inputJsonPath = path.join(testCaseDir, "input.json");
fs.writeFileSync(inputJsonPath, JSON.stringify(data, null, 2));

const dsgEnvPath = path.join(testCaseDir, "dsg.env");
const dsgEnvContent = `BUILD_SPEC_PATH=${dsgEnv.BUILD_SPEC_PATH}\nBUILD_OUTPUT_PATH=${dsgEnv.BUILD_OUTPUT_PATH}`;
fs.writeFileSync(dsgEnvPath, dsgEnvContent);
});
}

generateTestCasesDependencies();
Loading

0 comments on commit cd54c38

Please sign in to comment.