Skip to content

Commit

Permalink
chore(#9169): build couchdb image for api integration tests (#9455)
Browse files Browse the repository at this point in the history
Instead of downloading a pre-released couchdb image, we build the couchdb image to test api integration.
The image gets built when publishing, so downloading all layers was required for a later step anyway.

Removed one unused couchdb compose file.
Added a missing callback in an ancient migration that caused tests to fail. 

#9169
  • Loading branch information
dianabarsan authored and witash committed Sep 26, 2024
1 parent a5dc0e4 commit c06a703
Show file tree
Hide file tree
Showing 26 changed files with 42 additions and 186 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Couch Start
run: ./scripts/ci/couch-start
- name: Create logs directory
run: mkdir tests/logs
- run: npm ci
Expand Down
1 change: 1 addition & 0 deletions api/src/migrations/extract-person-contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const createPerson = function(id, callback) {
.catch(() => {
// we tried our best - log the details and exit
logger.error(`Failed to restore contact on facility "${facilityId}", contact: ${JSON.stringify(oldContact)}`);
callback();
});
};

Expand Down
12 changes: 12 additions & 0 deletions api/tests/integration/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
process.env.COUCH_URL='http://admin:pass@localhost:5984/medic';

module.exports = {
allowUncaught: false,
color: true,
checkLeaks: true,
fullTrace: true,
asyncOnly: false,
spec: './api/tests/integration/**/*.js',
timeout: 20000,
reporter: 'spec',
};
9 changes: 9 additions & 0 deletions api/tests/integration/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
couchdb:
build: ../../../couchdb
ports:
- "5984:5984"
environment:
- "COUCHDB_USER=admin"
- "COUCHDB_PASSWORD=pass"
- "SVC_NAME=couchdb"
10 changes: 10 additions & 0 deletions api/tests/integration/couch-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -eu

set -e

docker compose -f ./api/tests/integration/compose.yml up -d
echo "Starting CouchDB"

until nc -z localhost 5984; do sleep 1; done
sleep 2
echo "CouchDB Started"
3 changes: 3 additions & 0 deletions api/tests/integration/couch-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -eu
set -e
docker compose -f ./api/tests/integration/compose.yml down --remove-orphans
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const getUserDoc = async (id) => db.users.get(id);
describe('add-contact-id-to-user migration', function() {
afterEach(() => {
sinon.restore();
utils.tearDown();
});

it('migrates the contact_id value from user-settings to _users for all users', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const assertUserDb = (name) => {
};

describe('add-meta-validate-doc-update migration', () => {
afterEach(() => utils.tearDown());

it('should work with no user dbs', () => {
return utils
.initDb([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const utils = require('./utils');
const UUID_REGEX = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;

describe('add-uuid-to-scheduled-tasks migration', function() {
afterEach(function() {
return utils.tearDown();
});

const FUTURE = moment().add(1, 'week').toISOString();
const PAST = moment().subtract(1, 'week').toISOString();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const utils = require('./utils');

describe('clean-up-corrupted-users migration', function() {
afterEach(function() {
return utils.tearDown();
});

it('cleans up users with $promise and $resolved fields', function() {
// given
return utils.initDb([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ const utils = require('./utils');
const DDOC_ID = '_design/medic';

describe('convert-translation-messages-fix migration', function() {
afterEach(function() {
return utils.tearDown();
});

it('converts translation messages structure', () => {

// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ const migrate = function() {
};

describe('create-patient-contacts migration', function() {
afterEach(function() {
return utils.tearDown();
});

it('should run cleanly with no registered patients', function() {
return utils.initDb([])
.then(migrate)
Expand Down
7 changes: 0 additions & 7 deletions api/tests/integration/migrations/emit-complete.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
const sinon = require('sinon');

const utils = require('./utils');

describe('emit-complete', function() {
beforeEach(async () => {
await utils.initDb([]);
});

afterEach(() => {
utils.tearDown();
sinon.restore();
});

it('should do nothing when tasks not configured', function() {
// given
return utils.initSettings({ tasks: { rules: '' } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const settings = {
describe('extract-person-contacts migration', () => {
afterEach(() => {
sinon.restore();
return utils.tearDown();
});

it('converts and minifies a 0.4 structure into a 2.x one', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const MIGRATION = 'extract-transition-seq';

describe(`${MIGRATION} migration`, function() {
before(async () => utils.initDb([]));
after(async () => utils.tearDown());

const wipe = (dbRef, docName) => {
return dbRef.get(docName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const utils = require('./utils');

describe('extract-translations', function() {
afterEach(() => {
utils.tearDown();
sinon.restore();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const utils = require('./utils');

describe('namespace-form-fields migration', function() {
afterEach(function() {
return utils.tearDown();
});

it('should put form fields in fields property', function() {
// given
Expand Down
3 changes: 0 additions & 3 deletions api/tests/integration/migrations/remove-empty-parents.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const utils = require('./utils');

describe('remove-empty-parents migration', function() {
afterEach(function() {
return utils.tearDown();
});

it('should not affect well-defined parents', function() {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const utils = require('./utils');

describe('remove-user-language migration', function() {
afterEach(() => utils.tearDown());

it('cleans up users with language field', function() {
const initialUsers = [
Expand Down
4 changes: 0 additions & 4 deletions api/tests/integration/migrations/test-framework.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
const utils = require('./utils');

describe('migrations integration test framework', function() {
afterEach(function() {
return utils.tearDown();
});

describe('no-op', function() {
it('should leave an empty db empty', function() {
// given
Expand Down
57 changes: 5 additions & 52 deletions api/tests/integration/migrations/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const readFileAsync = promisify(fs.readFile);
const logger = require('@medic/logger');
const db = require('../../../src/db');
const { expect } = require('chai');

Expand Down Expand Up @@ -67,7 +66,7 @@ const matches = (expected, actual) => {

const assertDb = expected => {
return db
.get('medic-test').allDocs({ include_docs: true })
.get('medic').allDocs({ include_docs: true })
.then(results => {
let actual = results.rows.map(row => _.omit(row.doc, ['_rev']));
expected.sort(byId);
Expand Down Expand Up @@ -153,31 +152,7 @@ const matchDbs = (expected, actual) => {
}
};

const realMedicDb = db.medic;
const realSentinelDb = db.sentinel;
const realUsersDb = db.users;
const switchToRealDbs = () => {
db.medic = realMedicDb;
db.sentinel = realSentinelDb;
db.users = realUsersDb;
};

const switchToTestDbs = () => {
db.medic = new PouchDB(
realMedicDb.name.replace(/medic$/, 'medic-test')
);
db.sentinel = new PouchDB(
realSentinelDb.name.replace(/medic-sentinel$/, 'medic-sentinel-test')
);
db.users = new PouchDB(
realUsersDb.name.replace(/_users$/, 'users-test')
);
};

const initDb = content => {

switchToTestDbs();

return _resetDb()
.then(() => {
const medicPath = path.join(__dirname, '../../../../build/ddocs/medic.json');
Expand All @@ -196,31 +171,10 @@ const initDb = content => {
});
};

const _resetDb = (attempts = 0) => {
if (attempts === 3) {
return Promise.reject(new Error('Unable to reset medic-test db'));
}

return db.exists('medic-test')
.then(exists => {
if (exists) {
return db.get('medic-test').destroy();
}
})
.then(() => {
return db.get('medic-test');
})
.catch(err => {
logger.error('Could not create "medic-test" directly after deleting, pausing and trying again');
logger.error(err);
return new Promise(resolve => {
setTimeout(() => resolve(_resetDb(attempts + 1)), 3000);
});
});
};

const tearDown = () => {
switchToRealDbs();
const _resetDb = async () => {
const res = await db.medic.allDocs();
const toDelete = res.rows.map(row => ({ _id: row.id, _rev: row.value.rev, _deleted: true }));
await db.medic.bulkDocs(toDelete);
};

const runMigration = migration => {
Expand Down Expand Up @@ -273,7 +227,6 @@ module.exports = {
initSettings: initSettings,
getSettings: getSettings,
runMigration: runMigration,
tearDown: tearDown,
getDdoc: getDdoc,
insertAttachment: insertAttachment,
getDocStub
Expand Down
74 changes: 0 additions & 74 deletions couchdb/prod.couchdb-cluster.yml

This file was deleted.

2 changes: 0 additions & 2 deletions couchdb/tests/compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.7"

services:
couchdb.1:
build:
Expand Down
2 changes: 1 addition & 1 deletion couchdb/tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ setup() {
assert_output --partial '"status":"ok"'
}

@test "data inserted on one couchb can be retrieved from a peer" {
@test "data inserted on one server can be retrieved from a peer" {
local id
id=$(
</dev/urandom tr -dc _A-Z-a-z-0-9 | head -c"${1:-32}"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"-- DEV TEST SCRIPTS": "-----------------------------------------------------------------------------------------------",
"integration-all-local": "export VERSION=$(node ./scripts/build/get-version.js) && ./scripts/build/build-service-images.sh && npm run ci-integration-all",
"integration-sentinel-local": "export VERSION=$(node ./scripts/build/get-version.js) && ./scripts/build/build-service-images.sh && npm run ci-integration-sentinel",
"integration-api": "mocha 'api/tests/integration/**/*.js' -t 10000",
"integration-api": "./api/tests/integration/couch-start.sh && mocha --config ./api/tests/integration/.mocharc.js && ./api/tests/integration/couch-stop.sh",
"integration-all-k3d-local": "export VERSION=$(node ./scripts/build/get-version.js) && ./scripts/build/build-service-images.sh && npm run ci-integration-all-k3d",
"integration-sentinel-k3d-local": "export VERSION=$(node ./scripts/build/get-version.js) && ./scripts/build/build-service-images.sh && npm run ci-integration-sentinel-k3d",
"integration-cht-form": "wdio run ./tests/integration/cht-form/wdio.conf.js",
Expand Down
Loading

0 comments on commit c06a703

Please sign in to comment.