version: '2.3'

services:
  balena-supervisor-sut:
    build:
      context: ./
      dockerfile: Dockerfile.template
      args:
        ARCH: ${ARCH:-amd64}
    command: ['/wait-for-it.sh', '--', '/usr/src/app/entry.sh']
    stop_grace_period: 3s
    # Use bridge networking for the tests
    environment:
      DOCKER_HOST: tcp://docker:2375
      DBUS_SYSTEM_BUS_ADDRESS: unix:path=/shared/dbus/system_bus_socket
      # Required to skip device mounting in test env
      TEST: 1
    depends_on:
      - docker
      - mock-systemd
    volumes:
      - dbus:/shared/dbus
      - tmp:/mnt/root/tmp/balena-supervisor/services
      - ./test/data/root:/mnt/root
      - ./test/data/root/mnt/boot:/mnt/boot
      - ./test/lib/wait-for-it.sh:/wait-for-it.sh
    tmpfs:
      - /data # sqlite3 database
      - /mnt/root/tmp

  # The service setup
  mock-systemd:
    image: ghcr.io/balena-os/mock-systemd-bus
    # Necessary to run systemd in a container
    privileged: true
    volumes:
      - dbus:/shared/dbus
    environment:
      DBUS_SYSTEM_BUS_ADDRESS: unix:path=/shared/dbus/system_bus_socket
      MOCK_SYSTEMD_UNITS: openvpn.service avahi.socket

  docker:
    image: docker:dind
    stop_grace_period: 3s
    privileged: true
    environment:
      DOCKER_TLS_CERTDIR: ''
    command: --tls=false # --debug

  mdns:
    image: ydkn/avahi:latest
    # This hostname should be resolved
    hostname: my-device

  sut:
    # Build the supervisor code for development and testing
    build:
      context: ./
      dockerfile: Dockerfile.template
      target: test
      args:
        # Change this if testing in another architecture
        ARCH: ${ARCH:-amd64}
    command:
      [
        './test/lib/wait-for-it.sh',
        '--supervisor',
        '--',
        'npm',
        'run',
        'test:integration',
      ]
    depends_on:
      - balena-supervisor-sut
      - docker
      - mock-systemd
      - mdns
    stop_grace_period: 3s
    volumes:
      - dbus:/shared/dbus
      - tmp:/mnt/root/tmp/balena-supervisor/services
    # Set required supervisor configuration variables here
    environment:
      DOCKER_HOST: tcp://docker:2375
      DBUS_SYSTEM_BUS_ADDRESS: unix:path=/shared/dbus/system_bus_socket
      BALENA_SUPERVISOR_ADDRESS: http://balena-supervisor-sut:48484
      # Required by migrations
      CONFIG_MOUNT_POINT: /mnt/boot/config.json
      # Required to set mountpoints normally set in entry.sh
      ROOT_MOUNTPOINT: /mnt/root
      BOOT_MOUNTPOINT: /mnt/boot
      HOST_OS_VERSION_PATH: /mnt/root/etc/os-release
      STATE_MOUNTPOINT: /mnt/state
      DATA_MOUNTPOINT: /mnt/data
      DATABASE_PATH: /data/database.sqlite
    # 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/root
      - /mnt/boot
      - /mnt/state
      - /mnt/data

volumes:
  # Use tmpfs to avoid files remaining between runs
  dbus:
    driver_opts:
      type: tmpfs
      device: tmpfs
  tmp:
    driver_opts:
      type: tmpfs
      device: tmpfs
