Commit 3943d552 authored by Jure's avatar Jure

test: omit tests from CI and add semi-automatic testing

parent bbf84488
Pipeline #13067 passed with stages
in 11 minutes and 28 seconds
......@@ -9,7 +9,6 @@ stages:
- test
- review
- audit
- staging
- production
build:
......@@ -24,9 +23,9 @@ build:
- docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
- echo "Ignore warning! Cannot perform an interactive login from a non TTY device"
- docker push $IMAGE_ORG/$IMAGE_NAME:$CI_COMMIT_SHA
- echo "Building XSweet job runner image"
- docker build -t pubsweet/job-xsweet:$CI_COMMIT_SHA components/server/job-xsweet
- docker push pubsweet/job-xsweet:$CI_COMMIT_SHA
# - echo "Building XSweet job runner image"
# - docker build -t pubsweet/job-xsweet:$CI_COMMIT_SHA components/server/job-xsweet
# - docker push pubsweet/job-xsweet:$CI_COMMIT_SHA
pages:
image: $IMAGE_ORG/$IMAGE_NAME:$CI_COMMIT_SHA
......@@ -133,41 +132,27 @@ test:
# specify host here else it confuses the linked postgres image
- PGHOST=postgres yarn test
test:job-xsweet:
image: $IMAGE_ORG/$IMAGE_NAME:$CI_COMMIT_SHA
stage: test
variables:
# don't clone repo as image already has it
GIT_STRATEGY: none
# setup data for postgres image
POSTGRES_USER: test
POSTGRES_PASSWORD: pw
# connection details for tests
PGUSER: test
PGPASSWORD: pw
NODE_ENV: test
DATABASE_URL: postgres://test:pw@postgres/test
services:
- postgres
- pubsweet/job-xsweet
except:
- tags
script:
- cd ${HOME}
- PGHOST=postgres cd components/server && yarn jest --testRegex test/standaloneXsweetTest.js
# if tests pass we will push latest, labelled with current commit hash
push:latest:
image: docker:latest
stage: staging
script:
- if [ -z "$DOCKERHUB_USERNAME" ] || [ -z "$DOCKERHUB_PASSWORD" ]; then echo "Not pushing" && exit 0; fi
- docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD
- echo "Ignore warning! Cannot perform an interactive login from a non TTY device"
- docker build -t $IMAGE_ORG/$IMAGE_NAME:latest --label COMMIT_SHA=$CI_COMMIT_SHA .
- docker push $IMAGE_ORG/$IMAGE_NAME:latest
only:
- master
except:
- tags
\ No newline at end of file
# Reenable this once GitLab docker networking is sorted:
# https://gitlab.com/gitlab-org/gitlab-runner/merge_requests/1041
# test:job-xsweet:
# image: $IMAGE_ORG/$IMAGE_NAME:$CI_COMMIT_SHA
# stage: test
# variables:
# # don't clone repo as image already has it
# GIT_STRATEGY: none
# # setup data for postgres image
# POSTGRES_USER: test
# POSTGRES_PASSWORD: pw
# # connection details for tests
# PGUSER: test
# PGPASSWORD: pw
# NODE_ENV: test
# DATABASE_URL: postgres://test:pw@postgres/test
# services:
# - postgres
# - pubsweet/job-xsweet
# except:
# - tags
# script:
# - cd ${HOME}
# - PGHOST=postgres cd components/server/job-xsweet && yarn jest --testRegex test/standaloneXsweetTest.js
\ No newline at end of file
......@@ -29,3 +29,20 @@ Run it with: `docker run pubsweet/job-xsweet:latest -e DATABASE_URL=yourdatabase
The DATABASE_URL is needed so that the Docker container knows how to connect to your database.
After the Docker container starts up, jobs from the endpoint will start to be processed.
# Testing
Testing has to be done semi-automatically for now, as GitLab CI does not allow for service-2-service communication yet (https://gitlab.com/gitlab-org/gitlab-runner/merge_requests/1041), by running:
```
cd components/server/job-xsweet && yarn test --testRegex test/standaloneXsweetTest.js
```
And in a separate window, building and starting the `job-xsweet` container like so:
```
docker build -t pubsweet/job-xsweet components/server/job-xsweet
docker run -e DATABASE_URL="postgres://yourUsername@host.docker.internal/test" pubsweet/job-xsweet
```
This will test both the GraphQL subscription and the classic long-running HTTP request path.
......@@ -6,10 +6,8 @@ module.exports = {
database: 'test',
},
pool: { min: 0, max: 10, idleTimeoutMillis: 1000 },
ignoreTerminatedConnectionError: true,
port: 4000,
secret: 'test',
sse: false,
},
pubsweet: {
components: [
......
......@@ -92,7 +92,6 @@ const xsweetHandler = enablePubsub => async job => {
docxToHTMLJob: { status: 'Conversion complete' },
})
}
return { html: processedHtml }
} catch (e) {
if (enablePubsub) {
......
// const log = require('why-is-node-running')
const WebSocket = require('ws')
Object.assign(global, {
......@@ -20,102 +21,117 @@ const wait = require('waait')
jest.setTimeout(60000)
describe('XSweet job', () => {
let token
// let user
let server
let client
let token
beforeAll(async () => {
// await cleanDB()
server = await startServer()
token = authentication.token.create({ id: 1, username: 'test' })
client = new SubscriptionClient(`ws://localhost:4000/subscriptions`, {
connectionParams: {
authToken: token,
},
})
})
afterAll(async done => {
await destroy()
await server.close()
setImmediate(() => server.emit('close'))
await wait(500)
done()
})
afterEach(async () => {
await client.client.close()
describe('Endpoint', () => {
it('returns html', async () => {
const file = fs.createReadStream(path.join(__dirname, '../src/test.docx'))
const { text } = await superagent
.post('http://localhost:4000/convertDocxToHTML')
.attach('docx', file)
expect(text).toMatch(/Testing conversion 123/)
})
})
it('can process it', async () => {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.field(
'operations',
JSON.stringify({
operationName: null,
variables: { file: null },
query: `mutation createDocxToHTMLJob($file: Upload!) {
createDocxToHTMLJob(file: $file) {
status
id
}
}`,
}),
)
.field('map', JSON.stringify({ '0': ['variables.file'] }))
.attach(
'0',
fs.readFileSync(path.join(__dirname, '../src/test.docx')),
'test.docx',
)
.set('Authorization', `Bearer ${token}`)
expect(body.data.createDocxToHTMLJob.status).toBe('Uploading file')
const subscriptionPromise = new Promise((resolve, reject) => {
client
.request({
query: `subscription docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
id
describe('GraphQL subscriptions', () => {
// let user
let client
beforeAll(() => {
client = new SubscriptionClient(`ws://localhost:4000/subscriptions`, {
connectionParams: {
authToken: token,
},
})
})
afterAll(() => {
client.client.close()
})
it('can process it', async () => {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.field(
'operations',
JSON.stringify({
operationName: null,
variables: { file: null },
query: `mutation createDocxToHTMLJob($file: Upload!) {
createDocxToHTMLJob(file: $file) {
status
html
id
}
}`,
}),
)
.field('map', JSON.stringify({ '0': ['variables.file'] }))
.attach(
'0',
fs.readFileSync(path.join(__dirname, '../src/test.docx')),
'test.docx',
)
.set('Authorization', `Bearer ${token}`)
expect(body.data.createDocxToHTMLJob.status).toBe('Uploading file')
const subscriptionPromise = new Promise((resolve, reject) => {
client
.request({
query: `subscription docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
id
status
html
}
}
}
`,
variables: {
jobId: body.data.createDocxToHTMLJob.id,
},
})
.subscribe({
next: async res => {
if (res.data.docxToHTMLJob.status === 'Done') {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${token}`)
.send({
variables: { jobId: res.data.docxToHTMLJob.id },
query: `query docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
status
html
}
}`,
})
expect(body.data.docxToHTMLJob.html).toMatch(/Test/)
resolve(true)
}
return true
},
error: reject,
})
`,
variables: {
jobId: body.data.createDocxToHTMLJob.id,
},
})
.subscribe({
next: async res => {
if (res.data.docxToHTMLJob.status === 'Done') {
const { body } = await superagent
.post('http://localhost:4000/graphql')
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${token}`)
.send({
variables: { jobId: res.data.docxToHTMLJob.id },
query: `query docxToHTMLJob($jobId: String!) {
docxToHTMLJob(jobId: $jobId) {
status
html
}
}`,
})
resolve(body.data.docxToHTMLJob.html)
}
return true
},
error: reject,
})
})
const result = await subscriptionPromise
expect(result).toMatch(/Testing conversion 123/)
})
expect(await subscriptionPromise).toBe(true)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment