Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
ci: build, start, test
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Jun 19, 2024
1 parent c93a113 commit e9a8d36
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.git
.gitignore
Dockerfile
.dockerignore
dist
node_modules
.env
README.md
20 changes: 14 additions & 6 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ concurrency:
cancel-in-progress: true

jobs:
test:
name: Test
api:
name: Test API
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: setup
run: docker compose up --quiet-pull --wait --wait-timeout 300
- name: hurl
run: docker compose run --quiet-pull integration-tests
- name: dump logs
run: docker compose logs
if: failure()
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: always()
with:
node-version: 21
- name: build
run: npm ci
check_name: Results
report_paths: tests/api/report.xml
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM node:lts-alpine AS builder
USER node
WORKDIR /opt/app
COPY package*.json .
RUN npm ci
COPY --chown=node:node . .
RUN npm run build && npm prune --omit=dev

FROM node:lts-alpine

ENV NODE_ENV production
USER node
WORKDIR /home/node

COPY --from=builder --chown=node:node /opt/app/package*.json .
COPY --from=builder --chown=node:node /opt/app/node_modules ./node_modules
COPY --from=builder --chown=node:node /opt/app/dist ./dist

# COPY package*.json .
# COPY node_modules ./node_modules
# COPY dist ./dist
ARG PORT
EXPOSE ${PORT:-3000}

CMD ["node", "dist/main.js"]
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
services:
server:
build: .
environment:
- OPA_URL=http://opa:8181
depends_on:
- opa
expose:
- 3000

opa:
image: openpolicyagent/opa:latest
ports:
- '8181:8181'
command:
- run
- --server
- --addr=:8181
- --log-level=debug
- --bundle
- --disable-telemetry
- /policies
volumes:
- ./policies:/policies

integration-tests:
image: ghcr.io/orange-opensource/hurl:latest
volumes:
- ./tests:/tests:rw
entrypoint: sh
command:
- -c
- 'hurl --test --retry=10 --verbose --report-junit=tests/api/report.xml /tests/api/*.hurl'
environment:
- HURL_host=server:3000
depends_on:
- server
profiles:
- tools
18 changes: 9 additions & 9 deletions basic.hurl → tests/api/basic.hurl
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
GET http://127.0.0.1:3000/hello
GET http://{{host}}/hello
Content-Type: application/json
HTTP 200
{"hello":"world"}

POST http://127.0.0.1:3000/auth/login
POST http://{{host}}/auth/login
Content-Type: application/json
{"username": "john", "password": "changeme"}
HTTP 201
[Captures]
access_token: jsonpath "$['access_token']"

GET http://127.0.0.1:3000/profile
GET http://{{host}}/profile
Content-Type: application/json
Authorization: Bearer {{access_token}}
HTTP 200
{"id":1,"username":"john"}

GET http://127.0.0.1:3000/cats
GET http://{{host}}/cats
Content-Type: application/json
Authorization: Bearer {{access_token}}
HTTP 200
[]

POST http://127.0.0.1:3000/cats
POST http://{{host}}/cats
Content-Type: application/json
Authorization: Bearer {{access_token}}
{"name": "garfield", "age": 5, "breed": "unknown"}
HTTP 403

# Try as admin (maria)
POST http://127.0.0.1:3000/auth/login
POST http://{{host}}/auth/login
Content-Type: application/json
{"username": "maria", "password": "guess"}
HTTP 201
[Captures]
access_token: jsonpath "$['access_token']"

POST http://127.0.0.1:3000/cats
POST http://{{host}}/cats
Content-Type: application/json
Authorization: Bearer {{access_token}}
{"name": "garfield", "age": 5, "breed": "unknown"}
HTTP 201

GET http://127.0.0.1:3000/cats
GET http://{{host}}/cats
Content-Type: application/json
Authorization: Bearer {{access_token}}
HTTP 200
[{"name":"garfield","age":5,"breed":"unknown"}]

GET http://127.0.0.1:3000/cats/garfield
GET http://{{host}}/cats/garfield
Content-Type: application/json
Authorization: Bearer {{access_token}}
HTTP 200
Expand Down
19 changes: 19 additions & 0 deletions tests/api/cats.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
POST http://{{host}}/auth/login
Content-Type: application/json
{"username": "maria", "password": "guess"}
HTTP 201
[Captures]
access_token: jsonpath "$['access_token']"

POST http://{{host}}/cats
Content-Type: application/json
Authorization: Bearer {{access_token}}
{"name": "garfield", "age": 5, "breed": "unknown"}
HTTP 201

GET http://{{host}}/cats/garfield
Content-Type: application/json
Authorization: Bearer {{access_token}}
x-user: stephan
HTTP 200
{"name":"garfield","age":5,"breed":"unknown"}
1 change: 1 addition & 0 deletions tests/api/report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite tests="2" errors="0" failures="0"><testcase id="/tests/api/basic.hurl" name="/tests/api/basic.hurl" time="0.135" /><testcase id="/tests/api/cats.hurl" name="/tests/api/cats.hurl" time="0.018" /></testsuite></testsuites>

0 comments on commit e9a8d36

Please sign in to comment.