Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
  • Loading branch information
PromoFaux committed Jul 23, 2023
1 parent 3b5cf34 commit 2974f1f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 449 deletions.
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
3 changes: 2 additions & 1 deletion build-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ 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
14 changes: 4 additions & 10 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
FROM python:3.10-slim-bullseye
FROM python:3.10-alpine3.18

# Only works for docker CLIENT (bind mounted socket)
COPY --from=docker:20.10.17 /usr/local/bin/docker /usr/local/bin/
COPY --from=docker:24-cli /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/* \
RUN apk add --no-cache \
curl \
&& 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 Pipfile* /root/
WORKDIR /root
Expand Down
4 changes: 2 additions & 2 deletions test/cmd.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
set -eux

docker build ./src --tag pihole:${GIT_TAG} --no-cache
Expand All @@ -9,4 +9,4 @@ 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/
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

0 comments on commit 2974f1f

Please sign in to comment.