Skip to content

Commit

Permalink
WIP - Unit Testing w/ Docker Containers
Browse files Browse the repository at this point in the history
  • Loading branch information
timnolte committed Nov 13, 2023
1 parent af66d4e commit ebda00a
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 80 deletions.
25 changes: 20 additions & 5 deletions .devcontainer/devcontainer.json
Expand Up @@ -4,7 +4,7 @@
"dockerComposeFile": "../docker-compose.yml",
"service": "app",
"mounts": ["source=dind-var-lib-docker,target=/var/lib/docker,type=volume"],
"workspaceMount": "source=${localWorkspaceFolder}/openid-connect-generic,target=/workspaces,type=bind,consistency=delegated",
//"workspaceMount": "source=${localWorkspaceFolder}/openid-connect-generic,target=/workspaces,type=bind,consistency=delegated",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

"customizations": {
Expand All @@ -29,9 +29,21 @@
"dockerDashComposeVersion": "v2"
},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
"ghcr.io/devcontainers/features/github-cli:1": {},
"./local-features/wp-cli": "latest",
"./local-features/welcome-message": "latest"
},

"overrideFeatureInstallOrder": [
"ghcr.io/devcontainers/features/common-utils",
"ghcr.io/devcontainers/features/node",
"ghcr.io/devcontainers/features/docker-in-docker",
"ghcr.io/devcontainers/features/git",
"ghcr.io/devcontainers/features/github-cli",
"./local-features/wp-cli",
"./local-features/welcome-message"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8080, 8081, 8026, 3306],

Expand All @@ -52,14 +64,17 @@
},

// Use `onCreateCommand` to run commands as part of the container creation.
"onCreateCommand": "sudo chmod +x .devcontainer/install.sh && .devcontainer/install.sh",
//"onCreateCommand": "chmod +x .devcontainer/install.sh && .devcontainer/install.sh",

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "sudo chmod +x .devcontainer/setup.sh && .devcontainer/setup.sh",
"postCreateCommand": "chmod +x .devcontainer/setup.sh && .devcontainer/setup.sh",

// Use 'postStartCommand' to run commands after the container has started.
"postStartCommand": "cd /app && wp plugin activate daggerhart-openid-connect-generic",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "wp_php"
"remoteUser": "wp_php",

// A set of name-value pairs that sets or overrides environment variables for the devcontainer.json supporting service / tool (or sub-processes like terminals) but not the container as a whole.
"remoteEnv": { "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}" }
}
22 changes: 0 additions & 22 deletions .devcontainer/install.sh

This file was deleted.

@@ -0,0 +1,8 @@
{
"id": "welcome-message",
"name": "Install the First Start Welcome Message",
"install": {
"app": "",
"file": "install.sh"
}
}
13 changes: 13 additions & 0 deletions .devcontainer/local-features/welcome-message/install.sh
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -eux

export DEBIAN_FRONTEND=noninteractive

# Copy the welcome message
if [ ! -f /usr/local/etc/vscode-dev-containers/first-run-notice.txt ]; then
echo "Installing First Run Notice..."
echo -e "👋 Welcome to \"OpenID Connect for WP Development\" in Dev Containers!\n\n🛠️ Your environment is fully setup with all the required software.\n\n🚀 To get started, wait for the \"postCreateCommand\" to finish setting things up, then open the portforwarded URL and append '/wp/wp-admin'. Login to the WordPress Dashboard using \`admin/password\` for the credentials.\n" | sudo tee /usr/local/etc/vscode-dev-containers/first-run-notice.txt
fi

echo "Done!"
8 changes: 8 additions & 0 deletions .devcontainer/local-features/wp-cli/devcontainer-feature.json
@@ -0,0 +1,8 @@
{
"id": "wp-cli",
"name": "Install the WP-CLI",
"install": {
"app": "",
"file": "install.sh"
}
}
14 changes: 14 additions & 0 deletions .devcontainer/local-features/wp-cli/install.sh
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -eux

export DEBIAN_FRONTEND=noninteractive

# Install the WP-CLI.
if [ ! -f /usr/local/bin/wp ]; then
echo "Installing WP-CLI..."
sudo curl -sS -o /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
sudo chmod +x /usr/local/bin/wp
fi

echo "Done!"
9 changes: 7 additions & 2 deletions .devcontainer/setup.sh
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

set -eux

Expand All @@ -12,9 +12,12 @@ fi

PLUGIN_DIR=/workspaces/openid-connect-generic

# Attempt to make ipv4 traffic have a higher priority than ipv6.
sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"

# Install Composer dependencies.
cd "${PLUGIN_DIR}"
composer install
COMPOSER_ALLOW_XDEBUG=0 COMPOSER_MEMORY_LIMIT=-1 composer install

# Install NPM dependencies.
cd "${PLUGIN_DIR}"
Expand All @@ -24,3 +27,5 @@ npm ci
cd "/app"
echo "Setting up WordPress at $SITE_HOST"
wp core install --url="$SITE_HOST" --title="OpenID Connect Development" --admin_user="admin" --admin_email="admin@example.com" --admin_password="password" --skip-email

echo "Done!"
6 changes: 0 additions & 6 deletions .devcontainer/welcome-message.txt

This file was deleted.

55 changes: 10 additions & 45 deletions .github/workflows/pr-unit-testing.yml
Expand Up @@ -7,11 +7,7 @@ on:
pull_request:

env:
PHP_VERSION: '7.4'
WP_VERSION: '5.9.*'
WP_MULTISITE: 0
COMPOSER_VERSION: '2.2'
ACTION_VERSION: 2
ACTION_VERSION: 3

jobs:
pr_unit_testing:
Expand All @@ -24,50 +20,19 @@ jobs:
# https://github.com/marketplace/actions/checkout
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
if: ${{ !env.ACT }}
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer dependencies
if: ${{ !env.ACT }}
uses: actions/cache@v2
env:
composer-cache-name: cache-composer
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-build-${{ env.composer-cache-name }}-wp-${{ env.WP_VERSION }}-v${{ env.ACTION_VERSION }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.composer-cache-name }}-wp-${{ env.WP_VERSION }}-v${{ env.ACTION_VERSION }}-
- name: Setup Node Environment
# https://github.com/marketplace/actions/setup-node-js-environment
uses: actions/setup-node@v2
- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
node-version-file: '.nvmrc'
cache: ${{ !env.ACT && 'npm' || '' }}

- name: Setup PHP & Composer Environment
# https://github.com/marketplace/actions/setup-php-action
uses: shivammathur/setup-php@v2
with:
php-version: "${{ env.PHP_VERSION }}"
tools: "composer:${{ env.COMPOSER_VERSION }}"

- name: Environment Check
run: php -v && composer --version

- name: Require Specified WordPress Version
run: composer require johnpbloch/wordpress-core:${{ env.WP_VERSION }} php-stubs/wordpress-stubs:${{ env.WP_VERSION }} wp-phpunit/wp-phpunit:${{ env.WP_VERSION }} --dev --prefer-source --update-with-all-dependencies
swap-size-gb: 10

- name: Install Composer Dependencies
run: composer install --prefer-dist
- name: Start Docker Environment
run: docker compose -f docker-compose.ci.yml up -d

- name: NPM Setup
run: npm ci
- name: Run PHPUnit Tests
run: docker compose -f docker-compose.ci.yml exec app /bin/bash -c 'composer install && composer phpunit'

- name: Unit Tests
run: npm run test
- name: Shutdown Docker Environment
run: docker compose -f docker-compose.ci.yml down

- name: Generate Coverage Report
# https://github.com/marketplace/actions/coverage-report-as-comment-clover
Expand Down
48 changes: 48 additions & 0 deletions docker-compose.ci.yml
@@ -0,0 +1,48 @@
# This is the Compose file for command-line services.
# Anything that doesn't need to be run as part of the main `docker-compose up'
# command should reside in here and be invoked by a helper script.
version: "3.7"

services:
app:
image: wordpressdevelop/php:8.0-fpm
depends_on:
- db
working_dir: /workspaces/openid-connect-generic
environment: &env
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_TEST_DB_NAME: wordpress_test
volumes:
- .:/workspaces/openid-connect-generic:cached
- ./tools/local-env:/app:cached
- ./tools/php/php-cli.ini:/usr/local/etc/php/php-cli.ini:ro,cached
- .:/app/wp-content/plugins/daggerhart-openid-connect-generic:ro,cached
- ~/.composer:/root/.composer:cached
- ~/.npm:/root/.npm:cached
networks:
- ci-oidcwp-net

db:
image: mariadb
ports:
- 3306:3306
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
- db:/var/lib/mysql
- ./tests/db-wordpress_test.sql:/docker-entrypoint-initdb.d/db-wordpress_test.sql
networks:
- ci-oidcwp-net

volumes:
db:

networks:
ci-oidcwp-net:

0 comments on commit ebda00a

Please sign in to comment.