Skip to content

Commit

Permalink
Improve testing setup, tidy up cert generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Keoghan committed Sep 13, 2018
1 parent 76cd7b5 commit 8cdb924
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 119 deletions.
38 changes: 28 additions & 10 deletions app/Porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Support\Contracts\Cli;
use App\Support\Contracts\ImageRepository;
use App\Support\Contracts\ImageSetRepository;
use App\Support\Images\Image;

class Porter
{
Expand Down Expand Up @@ -195,16 +196,6 @@ public function build()
$this->dockerCompose->command('build')->perform();
}

/**
* Pull our docker images
*/
public function pullImages()
{
foreach ($this->getDockerImageSet()->all() as $image) {
$this->cli->passthru("docker pull {$image->name}");
}
}

/**
* Build the current images
*/
Expand All @@ -225,6 +216,33 @@ public function pushImages()
}
}

/**
* Pull our docker images
*/
public function pullImages()
{
foreach ($this->getDockerImageSet()->all() as $image) {
if (running_tests() && $this->hasImage($image)) {
continue;
}

$this->cli->passthru("docker pull {$image->name}");
}
}

/**
* Check if we already have the image
*
* @param Image $image
* @return bool
*/
public function hasImage(Image $image)
{
$output = $this->cli->exec("docker image inspect {$image->name}");

return strpos($output, "Error: No such image: {$image->name}") === false;
}

/**
* Get the current image set to use.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Support/Mechanics/Untrained.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getUserHomePath()
*/
public function isTesting()
{
return config('app.env') == 'testing';
return running_tests();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Support/Ssl/CertificateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function createCa()
}

exec(sprintf(
'openssl req -new -newkey rsa:2048 -days 730 -nodes -x509 -subj "/C=/ST=/O=%s/localityName=/commonName=%s/organizationalUnitName=Developers/emailAddress=%s/" -keyout %s -out %s',
'openssl req -new -newkey rsa:2048 -days 730 -nodes -x509 -subj "/C=GB/ST=Berks/O=%s/localityName=Reading/commonName=%s/organizationalUnitName=Developers/emailAddress=%s/" -keyout %s -out %s',
$this->oName, $this->cName, $this->email, $paths->key, $paths->pem
));

Expand Down Expand Up @@ -158,8 +158,8 @@ public function createPrivateKey($keyPath)
public function createSigningRequest($url, $keyPath, $csrPath, $confPath)
{
exec(sprintf(
'openssl req -new -key %s -out %s -subj "/C=/ST=/O=/localityName=/commonName=%s/organizationalUnitName=/emailAddress=%s%s/" -config %s',
$keyPath, $csrPath, $url, $url, '@'.$this->domain, $confPath
'openssl req -new -key %s -out %s -subj "/C=GB/ST=Berks/O=%s/localityName=Reading/commonName=%s/organizationalUnitName=Developers/emailAddress=%s%s/" -config %s',
$keyPath, $csrPath, $this->domain, $url, $url, '@'.$this->domain, $confPath
));
}

Expand Down
8 changes: 8 additions & 0 deletions app/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ function setting($key = null, $default = null)

return Setting::where('name', $key)->value('value') ?? $default;
}

/**
* Check if we're running tests since environment is limited to production/development
*/
function running_tests()
{
return config('app.running_tests');
}
5 changes: 4 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
\Illuminate\View\ViewServiceProvider::class,
],

'env' => env('APP_ENV', 'local'),
/**
* Since environment is limited to production/development, add a flag for tests.
*/
'running_tests' => env('RUNNING_TESTS', false),
];
12 changes: 5 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
defaultTestSuite="Unit"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Live">
<directory suffix="Test.php">./tests/Live</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>docker</group>
<group>certificates</group>
</exclude>
</groups>
<listeners>
<listener class="NunoMaduro\Collision\Adapters\Phpunit\Listener"/>
</listeners>
Expand All @@ -31,7 +29,7 @@
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="RUNNING_TESTS" value="true"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
Expand Down
14 changes: 7 additions & 7 deletions resources/views/ssl/conf.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[req]
[ req]
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
countryName = Country Name (2 letter code)
countryName_default = US
countryName_default = GB
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = MN
stateOrProvinceName_default = Berks
localityName = Locality Name (eg, city)
localityName_default = Minneapolis
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = Domain Control Validated
commonName = Internet Widgits Ltd
localityName_default = Reading
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = Developers
commonName = {{ $url }}
commonName_max = 64

[ v3_req ]
Expand Down
20 changes: 11 additions & 9 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Tests;

use App\Providers\AppServiceProvider;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as IlluminateTestCase;
use Illuminate\Support\Facades\Artisan;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

abstract class BaseTestCase extends IlluminateTestCase
{
use CreatesApplication, MockeryPHPUnitIntegration, DatabaseMigrations;
use CreatesApplication, MockeryPHPUnitIntegration, DatabaseTransactions;

/**
* Holds an application instance.
Expand All @@ -27,16 +27,18 @@ protected function setUp(): void
{
parent::setUp();

if (static::$migrated) {
return;
}
$this->afterApplicationCreated(function () {
if (static::$migrated) {
return;
}

$this->performMigrations();
Artisan::call('migrate:fresh');

static::$migrated = true;
$this->preparePorter();
});
}

protected function performMigrations()
protected function preparePorter()
{

}
Expand Down
36 changes: 0 additions & 36 deletions tests/Feature/PorterStartStopTest.php

This file was deleted.

3 changes: 2 additions & 1 deletion tests/Feature/ProviderBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Tests\Feature;

use App\Support\Ssl\CertificateBuilder;
use Tests\BaseTestCase;

class ProviderBaseTest extends \Tests\BaseTestCase
class ProviderBaseTest extends BaseTestCase
{
/** @test */
public function it_passes_the_correct_ssl_directory_to_the_certificate_builder()
Expand Down
31 changes: 31 additions & 0 deletions tests/Live/PorterServesSitesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Live;

use Illuminate\Support\Facades\Artisan;
use Tests\LiveTestCase;

class PorterServesSitesTest extends LiveTestCase
{
/** @test */
public function porter_makes_the_sample_site_available()
{
Artisan::call('site:unsecure', ['site' => 'sample']);

$phpinfo = $this->get('http://sample.test');

$this->assertContains('php', $phpinfo);
}

/**
* @test
*/
public function porter_makes_the_sample_site_available_securely()
{
Artisan::call('site:secure', ['site' => 'sample']);

$phpinfo = $this->get('https://sample.test');

$this->assertContains('php', $phpinfo);
}
}
42 changes: 17 additions & 25 deletions tests/Feature/PorterServesSitesTest.php → tests/LiveTestCase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Tests\Feature;
namespace Tests;

use App\Porter;
use Illuminate\Support\Facades\Artisan;
use Tests\BaseTestCase;

class PorterServesSitesTest extends BaseTestCase
class LiveTestCase extends BaseTestCase
{
/** @var Porter */
protected $porter;
Expand All @@ -17,8 +16,6 @@ public function setUp() :void

$this->porter = app(Porter::class);

Artisan::call('begin', ['home' => __DIR__.'/../TestWebRoot', '--force' => true]);

Artisan::call('start');

$this->assertTrue($this->porter->isUp(), 'Porter could not start.');
Expand All @@ -33,27 +30,9 @@ public function tearDown()
parent::tearDown();
}

/**
* @test
* @group docker
*/
public function porter_makes_the_sample_site_available()
protected function preparePorter()
{
Artisan::call('site:unsecure', ['site' => 'sample']);

$phpinfo = $this->get('http://sample.test');

$this->assertContains('php', $phpinfo);
}

protected function buildResolveOption($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);

$port = $scheme == 'http' ? 80 : 443;

return ["{$host}:{$port}:127.0.0.1"];
Artisan::call('begin', ['home' => __DIR__.'/TestWebRoot', '--force' => true]);
}

/**
Expand All @@ -71,6 +50,9 @@ public function get($url, array $headers = [])
// force it to resolve to 127.0.0.1
curl_setopt($ch, CURLOPT_RESOLVE, $this->buildResolveOption($url));

// Since the cert won't be trusted, ignore it
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$phpinfo = curl_exec($ch);

if (curl_errno($ch)) {
Expand All @@ -80,4 +62,14 @@ public function get($url, array $headers = [])

return $phpinfo;
}

protected function buildResolveOption($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);

$port = $scheme == 'http' ? 80 : 443;

return ["{$host}:{$port}:127.0.0.1"];
}
}
15 changes: 0 additions & 15 deletions tests/PorterReadyTestCase.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Models\Site;
use Tests\BaseTestCase;

class PhpVersionBaseTest extends BaseTestCase
class PhpVersionTest extends BaseTestCase
{
/** @test */
public function it_returns_a_safe_version_number()
Expand Down
Loading

0 comments on commit 8cdb924

Please sign in to comment.