Skip to content

Commit

Permalink
Add packer testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dlundgren committed Oct 30, 2024
1 parent 687113a commit a8ae064
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 13 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ jobs:
name: Tests PHP ${{ matrix.php }}
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
services:
phagrancy-app:
image: serversideup/php-8.0-fpm-nginx
options: --name phagrancy-app
ports:
- "8080:8080"
volumes:
- "${{ github_workspace }}:/var/www/html"
env:
# serversideup/php
NGINX_WEBROOT: "/var/www/html/web"
# PHAGRANCY
PHAGRANCY_API_TOKEN: "token"
PHAGRANCY_STORAGE_PATH: "/tmp"
strategy:
fail-fast: false
matrix:
Expand All @@ -22,6 +36,16 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Restart app container
uses: docker://docker
with:
args: docker restart phagrancy-app

- name: Packer Setup
uses: hashicorp/setup-packer@main
with:
version: "latest"

- name: Set up PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
Expand All @@ -32,11 +56,11 @@ jobs:
uses: ramsey/composer-install@v1

- name: Tests
run: composer test
run: composer ci-test

- name: Code Climate Coverage Action Test
uses: MartinNuc/codeclimate-action@1
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TEST_REPORTER_ID }}
with:
coverageCommand: composer test-coverage
coverageCommand: composer test-coverage
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
"mikey179/vfsstream": "~1.6",
"helmich/phpunit-psr7-assert": "~4.1",
"syberisle/mock-php-stream": "~1.1",
"phpstan/phpstan": "^1.12"
"phpstan/phpstan": "^1.12",
"symfony/process": "^5.4"
},
"scripts" : {
"test": "vendor/bin/phpunit",
"test-packer": [
"@putenv PHAGRANCY_TEST_USE_DOCKER_COMPOSE=1",
"vendor/bin/phpunit --testsuite Packer"
],
"test": "vendor/bin/phpunit --testsuite Phagrancy",
"ci-test": "vendor/bin/phpunit --testsuite Phagrancy,Packer",
"stan": "vendor/bin/phpstan analyze src",
"test-coverage": "vendor/bin/phpunit --coverage-clover build/logs/clover.xml"
"test-coverage": "vendor/bin/phpunit --testsuite Phagrancy --coverage-clover build/logs/clover.xml"
}
}
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<testsuites>
<testsuite name="Phagrancy">
<directory>./tests/</directory>
<exclude>./tests/packer/PackerTest.php</exclude>
</testsuite>
<testsuite name="Packer">
<file>./tests/packer/PackerTest.php</file>
</testsuite>
</testsuites>
</phpunit>
22 changes: 22 additions & 0 deletions src/Http/Response/InternalServerError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @file
* Contains Phagrancy\Http\Response\InternalServerError
*/

namespace Phagrancy\Http\Response;

/**
* HTTP 500/Internal Server Error
*
* @package Phagrancy\Http\Response
*/
class InternalServerError
extends Json
{
public function __construct(string $message)
{
parent::__construct(['error' => 500, 'message' => $message], 500);
}
}
9 changes: 6 additions & 3 deletions src/Http/Response/SendBoxFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Phagrancy\Http\Response;

use Phagrancy\Model\Entity\Box;
use Slim\Http\Headers;
use Slim\Http\Response;
use Slim\Http\Stream;
Expand All @@ -19,17 +20,19 @@
class SendBoxFile
extends Response
{
public function __construct($box, $version, $provider, $file)
public function __construct(Box $box, string $version, string $provider, string $file)
{
$filename = "{$box->name()}-{$provider}-{$version}.box";
parent::__construct(
200,
new Headers(
[
'Cache-Control' => "must-revalidate",
'Expires' => 0,
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . "{$box->name()}-{$provider}-{$version}.box" . '"'
]),
'Content-Disposition' => 'attachment; filename="' . $filename . '"'
]
),
new Stream(fopen($file, 'rb'))
);
}
Expand Down
30 changes: 25 additions & 5 deletions src/ServiceProvider/Pimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function __construct($rootPath)
$this->rootPath = $rootPath;
}

public function register(Container $di)
/**
* @return mixed
*/
private function loadEnv()
{
$envFile = "{$this->rootPath}/.env";
if (file_exists($envFile)) {
Expand All @@ -47,8 +50,25 @@ public function register(Container $di)

$this->env = $envLoader->toArray();
}
foreach (['api_token','storage_path','access_token','access_password'] as $var) {
$value = getenv("PHAGRANCY_" . strtoupper($var));
$this->env[$var] = empty($value) ? ($this->env[$var] ?? null) : $value;
}

return $this->env;
}

$di['env'] = $this->env;
public function register(Container $di): void
{
$envFile = "{$this->rootPath}/.env";
if (file_exists($envFile)) {
$envLoader = new Loader("{$this->rootPath}/.env");
$envLoader->parse();

$this->env = $envLoader->toArray();
}

$di['env'] = $this->env = $this->loadEnv();
$di['path.storage'] = $this->resolveStoragePath();

// Authorization middleware
Expand All @@ -73,9 +93,9 @@ public function register(Container $di)
};

// action handlers
$di[Action\Scopes::class] = function ($c) {
$di[Action\Scopes::class] = function ($c) {
return new Action\Scopes($c[Repository\Scope::class], new Input\Scope());
};
};

$di[Action\Scope\Index::class] = function ($c) {
return new Action\Scope\Index($c[Repository\Scope::class], new Input\Scope());
Expand Down Expand Up @@ -152,7 +172,7 @@ public function register(Container $di)
};
}

private function resolveStoragePath()
private function resolveStoragePath(): string
{
$path = "data/storage";
if (isset($this->env['storage_path'])) {
Expand Down
86 changes: 86 additions & 0 deletions tests/packer/PackerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* @file
* Contains Phagrancy\PackerTest
*/

namespace packer;

use Phagrancy\TestCase\Integration;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

/**
* Tests against packer itself
*/
class PackerTest
extends Integration
{
protected static $bin;
protected static $path;
protected static $process;
protected static $useDockerCompose;

public static function setUpBeforeClass(): void
{
self::$bin = (new ExecutableFinder())->find('packer');
if (empty(self::$bin)) {
self::fail("Missing packer binary");
}

self::$useDockerCompose = getenv('PHAGRANCY_TEST_USE_DOCKER_COMPOSE');
if (self::$useDockerCompose) {
self::$process = Process::fromShellCommandline("docker compose up -d");
self::$process->setWorkingDirectory(__DIR__);
self::$process->mustRun();
sleep(5);
}
}

public static function tearDownAfterClass(): void
{
if (self::$useDockerCompose) {
$down = Process::fromShellCommandline('docker compose down');
$down->setWorkingDirectory(__DIR__);
$down->mustRun();
}
}

private function runPacker($file)
{
$build = Process::fromShellCommandline("packer build {$file}.json");
$build->setWorkingDirectory(__DIR__);
$build->mustRun();

$curl = Process::fromShellCommandline('curl -s http://127.0.0.1:8080/test/test');
$curl->mustRun();

$response = json_decode($curl->getOutput());

$versions = [];
foreach ($response->versions as $version) {
$versions[] = $version->version;
}

return $versions;
}

public function provideVersions()
{
return [
['v1', '1.2.3-alpha'],
['v1d', '1.2.3-alpha-direct'],
['v2', '2.0-beta'],
['v2d', '2.0-beta-direct'],
];
}

/**
* @dataProvider provideVersions
*/
public function testPacker($version, $expectedVersion)
{
self::assertContains($expectedVersion, $this->runPacker($version));
}
}
17 changes: 17 additions & 0 deletions tests/packer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
services:
app:
image: serversideup/php:8.0-fpm-nginx
container_name: phagrancy-app
ports:
- "8080:8080"
- "8443:8443"
volumes:
- "${PWD}/../../:/var/www/html"
environment:
# serversideup/php
PHP_OPCACHE_ENABLE: 0
NGINX_WEBROOT: "/var/www/html/web"
# PHAGRANCY
PHAGRANCY_API_TOKEN: "token"
PHAGRANCY_STORAGE_PATH: "/tmp"
Loading

0 comments on commit a8ae064

Please sign in to comment.