Skip to content

Commit

Permalink
[deployphp#2197] Moved docker to test dir & separated e2e tests from …
Browse files Browse the repository at this point in the history
…unit-tests

The docker directory has been moved into test/ directory.
Additionally the e2e tests have been separated from the unit-test
files:
  * AbstractE2ETest class is directly inheriting from TestCase,
    dropping relation to the AbstractTest
  * Separate bootstrap.php file for e2e test was created
  * Separate phpunit-e2e.xml.dist file was created to configure
    PHPUnit for just the e2e tests
  * deployer docker service now runs only e2e tests by default
  • Loading branch information
Bartlomiej Sacharski committed Oct 15, 2020
1 parent ef78d77 commit ba40142
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 20 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"scripts": {
"test": "pest",
"test-e2e": "pest --config test/e2e/phpunit-e2e.xml.dist",
"lint": "phpcs"
},
"bin": [
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@
<testsuite name="Joy">
<directory>test/joy/</directory>
</testsuite>
<testsuite name="e2e">
<directory>test/e2e/</directory>
</testsuite>
</testsuites>
</phpunit>
23 changes: 19 additions & 4 deletions docker/Dockerfile → test/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ RUN ssh-keygen \
-t rsa \
-f ~/.ssh/id_rsa

RUN git config --global user.email "[email protected]" \
&& git config --global user.name "E2E Deployer"

ARG XDEBUG_VERSION=2.9.8
RUN set -eux; \
apk add --no-cache --virtual .build-deps $PHPIZE_DEPS; \
Expand All @@ -27,7 +30,6 @@ RUN set -eux; \
apk del .build-deps

COPY --from=composer /tmp/composer /bin/composer
ENV E2E_ENV=1
VOLUME [ "/project" ]
WORKDIR /project

Expand All @@ -47,8 +49,8 @@ RUN mkdir /run/sshd \
&& sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd

# Configure Apache to expose healthcheck & configure site to use /var/www/html/current ad document root
COPY ./conf/healthcheck.conf /etc/apache2/sites-available/healthcheck.conf
COPY ./initial-site/index.html /var/www/html/initial-site/index.html
COPY conf/healthcheck.conf /etc/apache2/sites-available/healthcheck.conf
COPY initial-site/index.html /var/www/html/initial-site/index.html

ENV APACHE_DOCUMENT_ROOT /var/www/html/current
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/000-default.conf \
Expand All @@ -70,12 +72,25 @@ RUN useradd \
&& chmod 700 ~deployer/.ssh \
&& chmod 600 ~deployer/.ssh/authorized_keys

COPY ./scripts/start-servers.sh /usr/local/bin/start-servers
RUN useradd \
--create-home \
git \
&& mkdir ~git/.ssh \
&& touch ~git/.ssh/authorized_keys \
&& chown -R git:git ~git/.ssh \
&& chmod 700 ~git/.ssh \
&& chmod 700 ~git/.ssh/authorized_keys \
&& mkdir ~git/repository \
&& git init --bare ~git/repository \
&& chown -R git:git ~git/repository

COPY scripts/start-servers.sh /usr/local/bin/start-servers
COPY --from=composer /tmp/composer /usr/local/bin/composer
COPY --from=deployer /root/.ssh/id_rsa.pub /tmp/root_rsa.pub

RUN chmod a+x /usr/local/bin/start-servers \
&& cat /tmp/root_rsa.pub >> ~deployer/.ssh/authorized_keys \
&& cat /tmp/root_rsa.pub >> ~git/.ssh/authorized_keys \
&& rm -rf /tmp/root_rsa.pub

EXPOSE 22 80 81
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions docker/docker-compose.yml → test/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ version: '2.4'
services:
deployer:
build:
context: .
context: ""
target: deployer
depends_on:
server:
condition: service_healthy
volumes:
- ./../:/project
command: "php vendor/bin/pest"
- ./../../:/project
command: "php vendor/bin/pest --config test/e2e/phpunit-e2e.xml.dist"
networks:
- e2e
# environment:
Expand All @@ -29,7 +29,7 @@ services:

server:
build:
context: .
context: ""
target: server
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:81/health_check"]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 40 additions & 7 deletions test/e2e/AbstractE2ETest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
<?php declare(strict_types=1);
namespace e2e;

use Deployer\AbstractTest;
use Deployer\Deployer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\ApplicationTester;

abstract class AbstractE2ETest extends AbstractTest
abstract class AbstractE2ETest extends TestCase
{
protected function setUp(): void
/**
* @var ApplicationTester
*/
protected $tester;

/**
* @var Deployer
*/
protected $deployer;

public static function setUpBeforeClass(): void
{
self::cleanUp();
mkdir(__TEMP_DIR__);
}

public static function tearDownAfterClass(): void
{
parent::setUp();
$isE2EEnvironment = filter_var(getenv('E2E_ENV'), FILTER_VALIDATE_BOOLEAN);
self::cleanUp();
}

if ($isE2EEnvironment !== true) {
$this->markTestSkipped('Cannot execute in non-E2E environment');
protected static function cleanUp(): void
{
if (is_dir(__TEMP_DIR__)) {
exec('rm -rf ' . __TEMP_DIR__);
}
}

protected function init(string $recipe): void
{
$console = new Application();
$console->setAutoExit(false);
$this->tester = new ApplicationTester($console);

$this->deployer = new Deployer($console);
Deployer::load($recipe);
$this->deployer->init();
$this->deployer->config->set('deploy_path', __TEMP_DIR__ . '/{{hostname}}');
}
}
3 changes: 1 addition & 2 deletions test/e2e/FunctionsE2ETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public function testRunWithPlaceholders(): void
]);

$display = $this->tester->getDisplay();
$display = trim($display); // Output may contain newlines, so we should trim them in advance

self::assertEquals(0, $this->tester->getStatusCode(), $display);
self::assertEquals('placeholder {{bar}} xyz%', $display);
self::assertStringContainsString('placeholder {{bar}} xyz%', $display);
}
}
22 changes: 22 additions & 0 deletions test/e2e/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
$loaded = false;
$file = __DIR__ . '/../../vendor/autoload.php';

if (file_exists($file)) {
require $file;
} else {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
'composer install' . PHP_EOL
);
}

// For loading recipes
set_include_path(__DIR__ . '/../..' . PATH_SEPARATOR . get_include_path());

define('DEPLOYER_BIN', __DIR__ . '/../../bin/dep');
define('__FIXTURES__', __DIR__ . '/../fixtures');
define('__REPOSITORY__', __DIR__ . '/../fixtures/repository');
define('__TEMP_DIR__', sys_get_temp_dir() . '/deployer');

require_once __DIR__ . '/AbstractE2ETest.php';
8 changes: 8 additions & 0 deletions test/e2e/phpunit-e2e.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="e2e">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 3 additions & 0 deletions test/e2e/recipe/laravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../../../recipe/laravel.php';

0 comments on commit ba40142

Please sign in to comment.