From 18c2f8cec90c6cb6d77be78cfc1176b05b0cd33b Mon Sep 17 00:00:00 2001 From: Felipe Lalanne Date: Thu, 25 Aug 2022 13:12:53 -0400 Subject: [PATCH] Migrate lib/legacy tests to integration tests This also sets up a dbus container as part of the test environment. This container exposes a socket that the sut container can use --- .mochapodrc.yml | 13 ++++++----- docker-compose.test.yml | 11 +++++++-- .../src => integration}/lib/legacy.spec.ts | 23 ++++--------------- test/lib/dbus/Dockerfile | 10 ++++++++ test/lib/dbus/dbus.conf | 9 ++++++++ 5 files changed, 40 insertions(+), 26 deletions(-) rename test/{legacy/src => integration}/lib/legacy.spec.ts (94%) create mode 100644 test/lib/dbus/Dockerfile create mode 100644 test/lib/dbus/dbus.conf diff --git a/.mochapodrc.yml b/.mochapodrc.yml index f6d7d0a0..f723f2c6 100644 --- a/.mochapodrc.yml +++ b/.mochapodrc.yml @@ -6,12 +6,13 @@ testfs: # them in the local source. These can be overriden # in the `testfs` configuration. filesystem: - /mnt/boot/config.json: - from: test/data/testconfig.json - /mnt/boot/config.txt: - from: test/data/mnt/boot/config.txt - /mnt/boot/device-type.json: - from: test/data/mnt/boot/device-type.json + /mnt/root: + /mnt/boot/config.json: + from: test/data/testconfig.json + /mnt/boot/config.txt: + from: test/data/mnt/boot/config.txt + /mnt/boot/device-type.json: + from: test/data/mnt/boot/device-type.json /mnt/root/etc/os-release: from: test/data/etc/os-release # The `keep` list defines files that already exist in the diff --git a/docker-compose.test.yml b/docker-compose.test.yml index aa32b952..b6a52eea 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -9,6 +9,10 @@ services: context: ./ command: sleep infinity + dbus: + build: + context: ./test/lib/dbus/ + docker: image: docker:dind privileged: true @@ -28,16 +32,19 @@ services: depends_on: - balena-supervisor - docker + - dbus # Set required supervisor configuration variables here environment: DOCKER_HOST: tcp://docker:2375 + DBUS_SYSTEM_BUS_ADDRESS: tcp:host=dbus,port=6667,family=ipv4 # Required by migrations - CONFIG_MOUNT_POINT: /mnt/boot/config.json + CONFIG_MOUNT_POINT: /mnt/root/mnt/boot/config.json + # Read by constants to setup `bootMountpoint` + BOOT_MOUNTPOINT: /mnt/boot # Set required mounts as tmpfs or volumes here # if specific files need to be backed up between tests, # make sure to add them to the `testfs` configuration under # .mochapodrc.yml tmpfs: - /data - - /mnt/boot - /mnt/root diff --git a/test/legacy/src/lib/legacy.spec.ts b/test/integration/lib/legacy.spec.ts similarity index 94% rename from test/legacy/src/lib/legacy.spec.ts rename to test/integration/lib/legacy.spec.ts index 7e5ecabc..be3e36de 100644 --- a/test/legacy/src/lib/legacy.spec.ts +++ b/test/integration/lib/legacy.spec.ts @@ -1,40 +1,29 @@ import { expect } from 'chai'; import { isRight } from 'fp-ts/lib/Either'; -import * as sinon from 'sinon'; import * as nock from 'nock'; import { TargetState } from '~/src/types'; import * as config from '~/src/config'; import * as legacy from '~/lib/legacy'; -import log from '~/lib/supervisor-console'; describe('lib/legacy', () => { before(async () => { - // disable log output during testing - sinon.stub(log, 'debug'); - sinon.stub(log, 'warn'); - sinon.stub(log, 'info'); - sinon.stub(log, 'event'); - sinon.stub(log, 'success'); - - await config.initialized; - // Set the device uuid and name + // these migration methods read some data from the database + // (and other data from the API) + // which is also why they need to be defined as integration tests + // TODO: when the supervisor is a full app, we'll be able to control updates + // using contracts, meaning this legacy code can dissapear await config.set({ uuid: 'local' }); await config.set({ name: 'my-device' }); }); - after(() => { - sinon.restore(); - }); - describe('Converting target state v2 to v3', () => { it('accepts a local target state with empty configuration', async () => { const target = await legacy.fromV2TargetState({} as any, true); const decoded = TargetState.decode(target); if (!isRight(decoded)) { - console.log(decoded.left); // We do it this way let the type guard be triggered expect.fail('Resulting target state is a valid v3 target state'); } @@ -152,7 +141,6 @@ describe('lib/legacy', () => { const decoded = TargetState.decode(target); if (!isRight(decoded)) { - console.log(decoded.left); // We do it this way let the type guard be triggered expect.fail('Resulting target state is a valid v3 target state'); } @@ -228,7 +216,6 @@ describe('lib/legacy', () => { const decoded = TargetState.decode(target); if (!isRight(decoded)) { - console.log(decoded.left); // We do it this way let the type guard be triggered expect.fail('Resulting target state is a valid v3 target state'); } diff --git a/test/lib/dbus/Dockerfile b/test/lib/dbus/Dockerfile new file mode 100644 index 00000000..9dcfc952 --- /dev/null +++ b/test/lib/dbus/Dockerfile @@ -0,0 +1,10 @@ +FROM ubuntu:20.04 + +# Install Systemd +RUN apt-get update && apt-get install -y --no-install-recommends \ + dbus \ + && rm -rf /var/lib/apt/lists/* + +COPY dbus.conf /etc/dbus-1/session.d/ + +ENTRYPOINT ["dbus-run-session", "sleep", "infinity"] diff --git a/test/lib/dbus/dbus.conf b/test/lib/dbus/dbus.conf new file mode 100644 index 00000000..b061e1f8 --- /dev/null +++ b/test/lib/dbus/dbus.conf @@ -0,0 +1,9 @@ + + + + tcp:host=localhost,bind=*,port=6667,family=ipv4 + unix:tmpdir=/tmp + ANONYMOUS + +