Skip to content

Commit

Permalink
[WIP] tests: Add end-to-end test script and docker compose app
Browse files Browse the repository at this point in the history
  • Loading branch information
detsch committed Oct 7, 2024
1 parent 0ae576c commit 8576a33
Show file tree
Hide file tree
Showing 8 changed files with 815 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dev-shell-e2e-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

docker_dir=docker-e2e-test
docker_path=${PWD}/${docker_dir}

# Function to execute custom commands before exiting
down() {
docker compose --env-file=${docker_path}/.env.dev -f ${docker_path}/docker-compose.yml down --remove-orphans
# remove the docker runtime part
docker volume rm ${docker_dir}_docker-runtime
}

# Register the cleanup function to be called on EXIT
trap down EXIT

mkdir -p $PWD/.device/sysroot
docker compose --env-file=${docker_path}/.env.dev -f ${docker_path}/docker-compose.yml run -e DEV_USER=$(id -u) -e DEV_GROUP=$(id -g) -e OSF_TOKEN=${OSF_TOKEN} -e USER_TOKEN=${USER_TOKEN} aklite-e2e-test $@
19 changes: 19 additions & 0 deletions docker-e2e-test/.env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FACTORY=$FACTORY
AUTH_TOKEN=$USER_TOKEN
DEVICE_TAG=main

DEV_DIR=$PWD/.device
SOTA_DIR=$DEV_DIR/sota
USR_SOTA_DIR=$DEV_DIR/usr.sota
ETC_SOTA_DIR=$DEV_DIR/etc.sota
DOCKER_DIR=$DEV_DIR/docker

# /sysroot dir containing ostree repo in /sysroot/ostree/repo
SYSROOT=$DEV_DIR/sysroot
BOOTDIR=$DEV_DIR/boot

# /var/lib/docker
DOCKER_DATA_ROOT=$DOCKER_DIR/data

# Dir containing Dockerfile and build context to build the "aklite-test" image
AKLITE_E2E_TEST_DOCKER_DIR=$PWD/docker-e2e-test
73 changes: 73 additions & 0 deletions docker-e2e-test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
FROM golang:1.22.2-bookworm AS composeapp
# Build composeapp
WORKDIR /build
RUN git clone https://github.com/foundriesio/composeapp.git && cd composeapp \
&& STOREROOT=/var/sota/reset-apps COMPOSEROOT=/var/sota/compose-apps BASESYSTEMCONFIG=/usr/lib/docker make \
&& cp ./bin/composectl /usr/bin/

# We may add fioctl and fioconfig to the test sequence. For now, we don't use them
# WORKDIR /build
# RUN git clone https://github.com/foundriesio/fioconfig.git && cd fioconfig \
# && make bin/fioconfig-linux-amd64 \
# && cp ./bin/fioconfig-linux-amd64 /usr/bin/fioconfig

# WORKDIR /build
# RUN git clone https://github.com/foundriesio/fioctl.git && cd fioctl \
# && make fioctl-linux-amd64 \
# && cp ./bin/fioctl-linux-amd64 /usr/bin/fioctl


FROM foundries/aklite-dev AS aklite

# Install composectl
COPY --from=composeapp /build/composeapp/bin/composectl /usr/bin/

# # Install fioconfig
# COPY --from=composeapp /build/fioconfig/bin/fioconfig-linux-amd64 /usr/bin/fioconfig

# # Install fioctl
# COPY --from=composeapp /build/fioctl/bin/fioctl-linux-amd64 /usr/bin/fioctl


# Install lmp-device-register
RUN apt-get install -y libboost-iostreams-dev

RUN git clone https://github.com/foundriesio/lmp-device-register \
&& cd lmp-device-register && git checkout mp-90 \
&& cmake -S . -B ./build -DDOCKER_COMPOSE_APP=ON -DHARDWARE_ID=intel-corei7-64 && cmake --build ./build --target install


# Add Docker's official GPG key:
RUN apt-get update && apt-get install -y ca-certificates curl
RUN install -m 0755 -d /etc/apt/keyrings
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
RUN chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
RUN echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null

RUN apt-get update && apt-get install -y docker-ce-cli docker-compose-plugin

# Install docker credential helper and auth configuration
COPY config.json /usr/lib/docker/config.json
COPY docker-credential-fio-helper /usr/bin/docker-credential-fio-helper

# Install gosu required for the entry/startup script to add a user and group in the container
RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.14/gosu-amd64" && \
chmod +x /usr/local/bin/gosu && \
gosu nobody true

# Install pytest
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir pytest

# Copy the entrypoint script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Set entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["bash"]
6 changes: 6 additions & 0 deletions docker-e2e-test/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"credHelpers": {
"hub.foundries.io": "fio-helper"
}
}

45 changes: 45 additions & 0 deletions docker-e2e-test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.8'

services:
dockerd:
image: docker:25.0-dind
command: ["dockerd", "-H", "unix:///var/run/docker/docker.sock"]
volumes:
- ${DOCKER_DATA_ROOT}:/var/lib/docker
- docker-runtime:/var/run/docker
privileged: true

aklite-e2e-test:
build:
context: ${AKLITE_E2E_TEST_DOCKER_DIR}
args:
AKLITE_VER: master
dockerfile: Dockerfile

image: aklite-e2e-test
volumes:
- "${PWD}:${PWD}"
- ${SYSROOT}:/sysroot
- ${SYSROOT}/ostree:/ostree
- ${BOOTDIR}:/boot
- ${SOTA_DIR}:/var/sota
- ${USR_SOTA_DIR}:/usr/lib/sota/conf.d
- ${ETC_SOTA_DIR}:/etc/sota/conf.d
- ${DOCKER_DATA_ROOT}:/var/lib/docker
- docker-runtime:/var/run/docker
working_dir: "${PWD}"
hostname: device
user: "root"
environment:
- FACTORY=${FACTORY}
- AUTH_TOKEN=${AUTH_TOKEN}
- DEVICE_TAG=${DEVICE_TAG}
- DOCKER_HOST=unix:///var/run/docker/docker.sock
- DOCKER_CONFIG=/usr/lib/docker
- CXX=clang++
- CC=clang
depends_on:
- dockerd

volumes:
docker-runtime:
19 changes: 19 additions & 0 deletions docker-e2e-test/docker-credential-fio-helper
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh -e

# Use stderr for logging err output in libaktualizr
export LOG_STDERR=1
SOTA_DIR="${SOTA_DIR-/var/sota}"

LOGLEVEL="${CREDS_LOGLEVEL-4}"

if [ "$1" = "get" ] ; then
if [ ! -f ${SOTA_DIR}/sota.toml ] ; then
echo "ERROR: Device does not appear to be registered under $SOTA_DIR"
exit 1
fi
server=$(grep -m1 '^[[:space:]]*server' ${SOTA_DIR}/sota.toml | cut -d\" -f2)
if [ -z $server ] ; then
server="https://ota-lite.foundries.io:8443"
fi
exec /usr/local/bin/aktualizr-get --loglevel $LOGLEVEL -u ${server}/hub-creds/
fi
68 changes: 68 additions & 0 deletions docker-e2e-test/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh -e

if [ -z $DEV_USER ] || [ -z $DEV_GROUP ]; then
echo "DEV_USER and DEV_GROUP environment variables must be set."
exit 1
fi

# Create a group with the specified GID if it doesn't already exist
if ! getent group $DEV_GROUP >/dev/null; then
groupadd -g $DEV_GROUP devgrp
fi

# Create a user with the specified UID and GID if it doesn't already exist
if ! getent passwd $DEV_USER >/dev/null; then
useradd -u $DEV_USER -g $DEV_GROUP -m dev
fi

# Change ownership of the home directory to the appuser
chown -R dev:devgrp /home/dev

chown dev:devgrp /var/run/docker/docker.sock

chown -R dev:devgrp /var/sota
chown -R dev:devgrp /usr/lib/sota/conf.d
chown -R dev:devgrp /etc/sota/conf.d
chown -R dev:devgrp /var/lib/docker

# Initialize ostree
if [ ! -d /sysroot/ostree/repo ]; then
echo "Initializing sysroot ostree..."
ostree admin init-fs /sysroot
ostree admin os-init lmp
ostree config set core.mode bare-user
${PWD}/tests/make_sys_rootfs.sh initfs lmp intel-corei7-64 lmp
commit=$(ostree commit initfs --branch lmp)
ostree admin deploy --os=lmp $commit
rm -rf initfs
chown -R dev:devgrp /ostree
ostree config set core.mode bare-user-only
fi
if [ ! -d /etc/ostree ]; then
mkdir /etc/ostree
chown -R dev:devgrp /etc/ostree
chown -R dev:devgrp /boot
fi

ln -sfn ${PWD}/aktualizr/build/src/aktualizr_get/aktualizr-get /usr/local/bin/aktualizr-get

# Initialize default toml config
sysroot_cfg=/usr/lib/sota/conf.d/z-90-sysroot.toml
if [ ! -f $sysroot_cfg ]; then
echo "[pacman]\nbooted = 0\nos = lmp" > $sysroot_cfg
fi

bootloader_cfg=/usr/lib/sota/conf.d/z-91-bootloader.toml
if [ ! -f $bootloader_cfg ]; then
echo "[bootloader]\nreboot_command = /usr/bin/true" > $bootloader_cfg
fi

# Set directory used for reboot indication, the `need_reboot` file is created under this dir
if [ ! -d /var/run/aktualizr-session ]; then
mkdir -p /var/run/aktualizr-session
chown dev:devgrp /var/run/aktualizr-session
chmod 700 /var/run/aktualizr-session
fi

# Run the command as the created user
exec gosu $DEV_USER:$DEV_GROUP "$@"
Loading

0 comments on commit 8576a33

Please sign in to comment.