Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests back to the development-v6 container #1396

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ on:
push:
branches:
- development-v6
pull_request:

env:
dockerhub: ${{ secrets.DOCKERHUB_NAMESPACE }}/pihole
ghcr: ghcr.io/${{ github.repository_owner }}/pihole

jobs:
build-and-test:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -30,8 +29,6 @@ jobs:
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
ref: development-v6

- name: Docker meta (Docker Hub and GitHub Container Registry)
id: meta
Expand All @@ -46,7 +43,6 @@ jobs:
development-v6

- name: Login to DockerHub and GitHub Container Registry
if: github.event_name != 'pull_request'
uses: ./.github/actions/login-repo
with:
docker_username: ${{ secrets.DOCKERHUB_USER }}
Expand All @@ -57,7 +53,7 @@ jobs:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all
platforms: ${{ matrix.platform}}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

Expand All @@ -72,7 +68,7 @@ jobs:
CONTAINER=${{ matrix.container }}
labels: ${{ steps.meta.outputs.labels }}
outputs: |
type=image,name=${{ env[matrix.registry] }},push-by-digest=${{ github.event_name != 'pull_request' }},name-canonical=true,push=${{ github.event_name != 'pull_request' }}
type=image,name=${{ env[matrix.registry] }},push-by-digest=true,name-canonical=true,push=true

- name: Export digests
run: |
Expand All @@ -92,14 +88,13 @@ jobs:
# If we would push immediately above, the individual runners would overwrite each other's images
# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
merge-and-deploy:
if: github.event_name != 'pull_request'
strategy:
fail-fast: false
matrix:
registry: [dockerhub, ghcr]
runs-on: ubuntu-latest
needs:
- build-and-test
- build
steps:
- name: Checkout Repo
uses: actions/checkout@v3
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build and Test (development-v6)
on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# Official docker images for docker are only available for amd64 and arm64
# TODO: Look at: https://github.com/docker-library/official-images#architectures-other-than-amd64
# Is testing on all platforms really necessary?
platform: [linux/amd64, linux/arm64/v8]
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: ${{ matrix.platform}}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Run Tests
run: |
echo "Building image to test"
PLATFORM=${{ matrix.platform}} ./build-and-test.sh
5 changes: 3 additions & 2 deletions build-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
set -ex

if [[ "$1" == "enter" ]]; then
enter="-it --entrypoint=bash"
enter="-it --entrypoint=sh"
fi

GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD | sed "s/\//-/g")
GIT_TAG=$(git describe --tags --exact-match 2> /dev/null || true)
GIT_TAG="${GIT_TAG:-$GIT_BRANCH}"
PLATFORM="${PLATFORM:-linux/amd64}"

# generate and build dockerfile
docker build --tag image_pipenv --file test/Dockerfile test/
docker buildx build --load --platform=${PLATFORM} --tag image_pipenv --file test/Dockerfile test/
docker run --rm \
--volume /var/run/docker.sock:/var/run/docker.sock \
--volume "$(pwd):/$(pwd)" \
Expand Down
31 changes: 12 additions & 19 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
FROM python:3.10-slim-bullseye
FROM docker:24.0.4-cli-alpine3.18

# Only works for docker CLIENT (bind mounted socket)
COPY --from=docker:20.10.17 /usr/local/bin/docker /usr/local/bin/

ARG packages
RUN apt-get update && \
apt-get install -y python3-dev curl gcc make \
libffi-dev libssl-dev ${packages} \
&& rm -rf /var/lib/apt/lists/* \
&& pip3 install --no-cache-dir -U pip pipenv

RUN curl -L https://github.com/docker/compose/releases/download/2.10.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose

COPY ./cmd.sh /usr/local/bin/
COPY --chmod=0755 ./cmd.sh /usr/local/bin/cmd.sh
COPY --chmod=0755 ./entrypoint.sh /usr/local/bin/entrypoint.sh
COPY Pipfile* /root/
WORKDIR /root

RUN pipenv install --system \
&& sed -i 's|/bin/sh|/bin/bash|g' /usr/local/lib/python3.10/site-packages/testinfra/backend/docker.py
RUN apk add --no-cache \
python3-dev \
py3-pip \
curl \
&& pip3 install --no-cache-dir -U pip pipenv \
&& pipenv install --system \
# Not 100% sure what this line does, but it's always been in the Dockerfile (aside from pointing at a different file)
# Tests fall over without it. Investigate later.
&& sed -i 's|/bin/sh|/bin/bash|g' /usr/lib/python3.11/site-packages/testinfra/backend/docker.py

RUN echo "set -ex && cmd.sh && \$@" > /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT entrypoint.sh
CMD cmd.sh
6 changes: 3 additions & 3 deletions test/cmd.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
set -eux

docker build ./src --tag pihole:${GIT_TAG} --no-cache
docker buildx build ./src --tag pihole:${GIT_TAG} --no-cache
docker images pihole:${GIT_TAG}

# auto-format the pytest code
python -m black ./test/tests/

# TODO: Add junitxml output and have something consume it
# 2 parallel max b/c race condition with docker fixture (I think?)
py.test -vv -n 2 ./test/tests/
py.test -vv -n auto ./test/tests/
1 change: 1 addition & 0 deletions test/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set -ex && cmd.sh
116 changes: 1 addition & 115 deletions test/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def args_volumes():

@pytest.fixture()
def args_env():
return '-e FTLCONF_LOCAL_IPV4="127.0.0.1"'
return '-e TZ="Europe/London"'


@pytest.fixture()
Expand Down Expand Up @@ -87,33 +87,6 @@ def docker(request, test_args, args, image, cmd, entrypoint):
return docker_generic(request, test_args, args, image, cmd, entrypoint)


@pytest.fixture(scope="module")
def docker_persist(
request,
persist_test_args,
persist_args,
persist_image,
persist_cmd,
persist_entrypoint,
dig,
):
"""
Persistent Docker container for multiple tests, instead of stopping container after one test
Uses DUP'd module scoped fixtures because smaller scoped fixtures won't mix with module scope
"""
persistent_container = docker_generic(
request,
persist_test_args,
persist_args,
persist_image,
persist_cmd,
persist_entrypoint,
)
""" attach a dig container for lookups """
persistent_container.dig = dig(persistent_container.id)
return persistent_container


@pytest.fixture
def entrypoint():
return ""
Expand All @@ -129,12 +102,6 @@ def tag(version):
return "{}".format(version)


@pytest.fixture
def webserver(tag):
"""TODO: this is obvious without alpine+nginx as the alternative, remove fixture, hard code lighttpd in tests?"""
return "lighttpd"


@pytest.fixture()
def image(tag):
image = "pihole"
Expand All @@ -146,64 +113,6 @@ def cmd():
return TAIL_DEV_NULL


@pytest.fixture(scope="module")
def persist_version():
return version


@pytest.fixture(scope="module")
def persist_args_dns():
return "--dns 127.0.0.1 --dns 1.1.1.1"


@pytest.fixture(scope="module")
def persist_args_volumes():
return "-v /dev/null:/etc/pihole/adlists.list"


@pytest.fixture(scope="module")
def persist_args_env():
return '-e ServerIP="127.0.0.1"'


@pytest.fixture(scope="module")
def persist_args(persist_args_volumes, persist_args_env):
return "{} {}".format(persist_args_volumes, persist_args_env)


@pytest.fixture(scope="module")
def persist_test_args():
"""test override fixture to provide arguments separate from our core args"""
return ""


@pytest.fixture(scope="module")
def persist_tag(persist_version):
return "{}".format(persist_version)


@pytest.fixture(scope="module")
def persist_webserver(persist_tag):
"""TODO: this is obvious without alpine+nginx as the alternative, remove fixture, hard code lighttpd in tests?"""
return "lighttpd"


@pytest.fixture(scope="module")
def persist_image(persist_tag):
image = "pihole"
return "{}:{}".format(image, persist_tag)


@pytest.fixture(scope="module")
def persist_cmd():
return TAIL_DEV_NULL


@pytest.fixture(scope="module")
def persist_entrypoint():
return ""


@pytest.fixture
def slow():
"""
Expand All @@ -225,26 +134,3 @@ def _slow(check, timeout=20):
return

return _slow


@pytest.fixture(scope="module")
def dig():
"""separate container to link to pi-hole and perform lookups"""
""" a docker pull is faster than running an install of dnsutils """

def _dig(docker_id):
args = "--link {}:test_pihole".format(docker_id)
image = "azukiapp/dig"
cmd = TAIL_DEV_NULL
dig_container = docker_generic(request, "", args, image, cmd, "")
return dig_container

return _dig


@pytest.fixture
def running_pihole(docker_persist, slow, persist_webserver):
"""Persist a fully started docker-pi-hole to help speed up subsequent tests"""
slow(lambda: docker_persist.run("pgrep pihole-FTL").rc == 0)
slow(lambda: docker_persist.run("pgrep lighttpd").rc == 0)
return docker_persist