Skip to content

Commit

Permalink
[TASK] Add bats testing for the docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
ochorocho committed Jul 7, 2024
1 parent 4226f91 commit deee46f
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ jobs:
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.2.1

- name: Check out code
uses: actions/checkout@v1

- name: "Test ddev ${{ matrix.version }} image"
shell: 'script -q -e -c "bash {0}"'
run: |
./build.sh -v ${{ matrix.version }} -l
DDEV_VERSION=${{ matrix.version }} bash bats tests
-
name: "ddev ${{ matrix.version }}"
shell: 'script -q -e -c "bash {0}"'
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test

on:
pull_request:
push:
branches:
- '**'
- '!main'
jobs:
build:
name: Build Container
runs-on: ubuntu-latest
strategy:
matrix:
version: [ v1.23, v1.22 ]
steps:
-
name: Checkout code
uses: actions/checkout@v3
-
name: Docker info
run: docker info
-
name: Docker Builder
run: docker buildx ls
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.2.1

- name: Check out code
uses: actions/checkout@v1

- name: "Test ddev ${{ matrix.version }} image"
shell: 'script -q -e -c "bash {0}"'
run: |
./build.sh -v ${{ matrix.version }} -l
DDEV_VERSION=${{ matrix.version }} bash bats tests
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,26 @@ Available options:
| ./build.sh -v v1.23 | v1.23, v1.23.x (latest bugfix) |
| ... | ... |


## TEST

Any good to disable TLS?!

```bash
NETWORK="ddev-docker"
if docker network inspect "$NETWORK" &>/dev/null; then
echo "Network '$NETWORK' already exists."
else
echo "Creating network '$NETWORK'."
docker network create "$NETWORK"
fi

# Get DinD ready - need privileged mode?!?!?
# WORKING::: docker run --privileged -e DOCKER_TLS_CERTDIR="" --name ddev-dind -d --network ddev-docker --network-alias docker docker:dind
docker run --privileged -e DOCKER_TLS_CERTDIR="" --name ddev-dind -d --network ddev-docker --network-alias docker docker:dind-rootless

# Wait till DinD is ready

# Run ddev/docker related commands - - -e DOCKER_HOST="tcp://docker:2375/"
docker run --rm -it --network ddev-docker ghcr.io/ochorocho/ddev-gitlab-ci:v1.23.3 version
```
33 changes: 31 additions & 2 deletions ddev-install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/ash

apk add --no-cache bash sudo jq
apk add --no-cache bash sudo jq curl
adduser -D ddev -g "ddev" -s /bin/bash -D ddev -h /home/ddev
echo "ddev ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ddev && chmod 0440 /etc/sudoers.d/ddev
unamearch=$(uname -m)
Expand All @@ -27,9 +27,38 @@ mkdir ddev
tar xfvz "ddev_linux-${ARCH}.${DDEV_VERSION}.tar.gz" --directory ddev
mv ddev/ddev /usr/local/bin/
mv ddev/mkcert /usr/local/bin/
sudo -i ddev /usr/local/bin/mkcert -install
rm -Rf ddev "ddev_linux-${ARCH}.${DDEV_VERSION}.tar.gz"

# Ensure required folders exist
mkdir -p /home/ddev/.ddev/commands/host
echo "
disable_http2: false
fail_on_hook_fail: false
instrumentation_opt_in: false
internet_detection_timeout_ms: 3000
last_started_version: ${DDEV_VERSION}
letsencrypt_email: ""
mkcert_caroot: /home/ddev/.local/share/mkcert
no_bind_mounts: false
omit_containers: []
performance_mode: none
project_tld: ddev.site
router: traefik
router_bind_all_interfaces: false
router_http_port: \"80\"
router_https_port: \"443\"
mailpit_http_port: \"8025\"
mailpit_https_port: \"8026\"
simple_formatting: false
table_style: default
traefik_monitor_port: \"10999\"
use_hardened_images: false
use_letsencrypt: false
wsl2_no_windows_hosts_mgt: false
web_environment: []
xdebug_ide_location: ""
" > /home/ddev/.ddev/global_config.yaml

mkdir /builds
chown -R ddev:ddev /home/ddev/.ddev/
chown -R ddev:ddev /home/ddev /builds
7 changes: 0 additions & 7 deletions test.sh

This file was deleted.

36 changes: 36 additions & 0 deletions tests/setup_suite.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bats

setup_suite() {
NETWORK="ddev-docker"
if docker network inspect "$NETWORK" &>/dev/null; then
echo "Network '$NETWORK' already exists."
else
echo "Creating network '$NETWORK'."
docker network create "$NETWORK"
fi

docker run --privileged -e DOCKER_TLS_CERTDIR="" --name ddev-dind -d --network ddev-docker --network-alias docker docker:dind
waitForDinD
}

waitForDinD() {
local TEST_COMMAND="
COUNT=0;
while ! docker info > /dev/null 2>&1; do
if [ \"\${COUNT}\" -ge 30 ]; then
echo \"Could not connect to docker after \$COUNT seconds\"
exit 1
fi
sleep 1
COUNT=\$((COUNT + 1));
done
"

docker run --rm --network ddev-docker --name wait-for-docker-dind "docker:latest" /bin/sh -c "${TEST_COMMAND}"
}

teardown_suite() {
docker stop ddev-dind
docker rm ddev-dind
}
80 changes: 80 additions & 0 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

@test "See docker version" {
run docker-run "docker version -f json"

version=$(echo "$output" | yq .Client.Version)
regex='^([0-9]+)\.([0-9]+)\.([0-9]+)$'

[[ $version =~ $regex ]]
[ "$status" -eq 0 ]
}

@test "See ddev version" {
run docker-run "ddev version -j"

version=$(echo "$output" | head -2 | tail -1 | yq '.raw.["DDEV version"]')
regex='^v([0-9]+)\.([0-9]+)\.([0-9]+)$'

[[ $version =~ $regex ]]
[ "$status" -eq 0 ]
}

@test "See mkcert is installed" {
run docker-run "mkcert -help"

[[ "$output" == *"Usage of mkcert:"* ]]
[ "$status" -eq 0 ]
}

@test "Create and run a ddev project" {
local TEST_COMMAND="
mkdir ~/ddev-test
cd ~/ddev-test
echo '<?php echo \"Hello World\";' > index.php
ddev config --project-type php --auto
ddev config global --no-bind-mounts=true
ddev start
curl https://ddev-test.ddev.site/index.php
ddev poweroff
"
run docker-run "${TEST_COMMAND}"

[[ "$output" == *"Configuration complete. You may now run 'ddev start'"* ]]
[[ "$output" == *"Hello World"* ]]
[ "$status" -eq 0 ]
}

@test "Run ddev debug dockercheck" {
run docker-run "ddev debug dockercheck"

[[ "$output" == *"Able to run simple container that mounts a volume."* ]]
[[ "$output" == *"Able to use internet inside container."* ]]
[ "$status" -eq 0 ]
}

# Use "--no-bind-mounts=true" to make "ddev debug test" pass. This is only required in testing environment
@test "Run ddev debug test" {
local TEST_COMMAND="
mkdir ~/ddev-test
cd ~/ddev-test
ddev config --project-type php --auto
ddev config global --no-bind-mounts=true
ddev debug test
"
run docker-run "${TEST_COMMAND}"

[[ "$output" == *"All project containers are now ready."* ]]
[[ "$output" == *"Successfully started tryddevproject-"* ]]
[ "$status" -eq 0 ]
}

yq() {
docker run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@"
}

docker-run() {
local COMMAND=${1}
# @todo: Pass in the current version
docker run --rm -it --network ddev-docker ghcr.io/ochorocho/ddev-gitlab-ci:"${DDEV_VERSION}" /bin/sh -c "${COMMAND}"
}

0 comments on commit deee46f

Please sign in to comment.