Skip to content

Add behat testing to Cron command #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
27a8583
Add behat test for a clean installation
L0RD-ZER0 Jun 5, 2024
546629b
Add other behat sites for single and multiple site entries
L0RD-ZER0 Jun 5, 2024
03df923
Reset `sites_created` to empty
L0RD-ZER0 Jun 5, 2024
f4c9c25
Fix issue with resetting internal state strings
L0RD-ZER0 Jun 5, 2024
cd58264
Fix typo in @When
L0RD-ZER0 Jun 5, 2024
8750148
Swap site types to appropriate locations
L0RD-ZER0 Jun 5, 2024
b15fc1e
Update behat configuration for sepration of behat features into indiv…
L0RD-ZER0 Jun 7, 2024
05cf80e
Add a shared context to avoid having to repeat common stuff for scena…
L0RD-ZER0 Jun 7, 2024
1f29ba6
Resolve function redeclaraion error by removing ``unexpectedOutput``
L0RD-ZER0 Jun 7, 2024
a6a2dd1
Add behat tests for ee cron create
L0RD-ZER0 Jun 10, 2024
e2098eb
Add behat tests for ee cron delete
L0RD-ZER0 Jun 10, 2024
868fea1
Add behat tests for ``ee cron run-now``
L0RD-ZER0 Jun 10, 2024
7b12b39
Add behat test workflow
L0RD-ZER0 Jun 12, 2024
12df95b
Fix path not found problems
L0RD-ZER0 Jun 12, 2024
159f1e3
Build and install EasyEngine phar to avoid attempting altering path
L0RD-ZER0 Jun 12, 2024
8741ea5
Update the PATH because of some issues in requiring dependencies
L0RD-ZER0 Jun 12, 2024
ba6ccc8
Add some extra debugigng info to the action
L0RD-ZER0 Jun 12, 2024
5e830e5
Retry using phar for action
L0RD-ZER0 Jun 12, 2024
3fc12e1
Rebuild autoload to include dev after phar creation
L0RD-ZER0 Jun 12, 2024
05ba2a8
Run behat using sudo
L0RD-ZER0 Jun 12, 2024
a021d1d
Do some final cleanups on workflow execution
L0RD-ZER0 Jun 12, 2024
f578ee6
Add behat tests for ``ee cron update``
L0RD-ZER0 Jun 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions .github/workflows/behat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
on:
push:
branches:
- master
- develop
- tests/behat
pull_request:
branches:
- master
- develop
workflow_dispatch:
name: Run behat tests to verify changes
jobs:
behat:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
#php: [ 8.1, 8.2, 8.3 ]
php: [ 8.3 ]
steps:
- name: Split repository name to get owner and repository name
id: split
run: |
IFS='/' read -r owner repo <<< "${{ github.repository }}"
echo "OWNER=$owner" >> $GITHUB_ENV
echo "REPO=$repo" >> $GITHUB_ENV
echo "OWNER_REPO=$owner\_$repo" >> $GITHUB_ENV
echo "REPO=$repo" >> $GITHUB_OUTPUT
echo "OWNER=$owner" >> $GITHUB_OUTPUT
- name: Checkout EasyEngine repository
uses: actions/checkout@v4
with:
repository: EasyEngine/easyengine
path: easyengine
lfs: true
submodules: true
- name: Checkout This repository
uses: actions/checkout@v4
with:
repository: ${{ github.repository }}
path: ${{ steps.split.outputs.REPO }}
lfs: true
submodules: true

- uses: shivammathur/setup-php@v2
with:
php-version: '${{ matrix.php }}'
coverage: none
tools: composer
extensions: pcntl, curl, sqlite3, zip, dom, mbstring, json
- name: Print PHP version and Modules
run: |
php -v
php -m
composer -V

- name: Install composer dependencies
run: |
cd easyengine
composer install --no-interaction --no-progress --no-suggest --prefer-dist
cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }}
composer install --no-interaction --no-progress --no-suggest --prefer-dist
cd ${{ github.workspace }}

- name: Replace easyengine dependency with local path
run: |
cd ${{ github.workspace }}
rm -rf easyengine/vendor/${{ steps.split.outputs.OWNER }}/${{ steps.split.outputs.REPO }}
mkdir -p easyengine/vendor/${{ steps.split.outputs.OWNER }}
ln -s $(pwd)/${{ steps.split.outputs.REPO }} easyengine/vendor/${{ steps.split.outputs.OWNER }}/${{ steps.split.outputs.REPO }}
cd easyengine
composer dump-autoload
cd ${{ github.workspace }}

- name: Update docker
run: |
sudo apt purge nginx nginx-common docker docker-engine docker.io docker-ce containerd runc
curl -fsSL https://get.docker.com/ | sudo bash
sudo systemctl restart docker.service

- name: Install docker-compose
run: |
sudo curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

- name: build easyengine phar
run: |
cd ${{ github.workspace }}/easyengine
composer dump-autoload --optimize --no-dev --no-interaction
php -dphar.readonly=0 utils/make-phar.php easyengine.phar
mv easyengine.phar /usr/local/bin/ee
chmod +x /usr/local/bin/ee
cd ${{ github.workspace }}

- name: Add easyengine and behat to PATH
run: |
echo "${{ github.workspace }}/easyengine/bin" >> $GITHUB_PATH
echo "${{ github.workspace }}/easyengine/vendor/bin" >> $GITHUB_PATH
- name: Run behat tests
run: |
cd ${{ github.workspace }}/easyengine
composer dump-autoload --no-interaction
cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }}
sudo ${{ github.workspace }}/easyengine/vendor/bin/behat --strict --no-interaction --no-colors
cd -
32 changes: 32 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
default:
suites:
list:
paths:
- "%paths.base%/features/list.feature"
contexts:
- "SharedContext"
- "ListContext"
create:
paths:
- "%paths.base%/features/create.feature"
contexts:
- "SharedContext"
- "CreateContext"
delete:
paths:
- "%paths.base%/features/delete.feature"
contexts:
- "SharedContext"
- "DeleteContext"
run-now:
paths:
- "%paths.base%/features/run-now.feature"
contexts:
- "SharedContext"
- "RunNowContext"
update:
paths:
- "%paths.base%/features/update.feature"
contexts:
- "SharedContext"
- "UpdateContext"
81 changes: 81 additions & 0 deletions features/bootstrap/CreateContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;


/**
* Defines behat context for ``ee cron create``.
*/
class CreateContext implements Context
{

private SharedContext $shared_context;
private string $cronCommand;
private string $site;
private string $schedule;

/**
* Initializes context.
*/
public function __construct()
{
}

/**
* @BeforeScenario
*/
public function gatherSharedContext(BeforeScenarioScope $scope)
{
$this->shared_context = $scope->getEnvironment()->getContext(SharedContext::class);
}

/**
* @When I create a cron for site `:site` with schedule `:schedule` and command `:command`
*/
function create_cron(string $site, string $schedule, string $command)
{
$this->site = $site;
$this->schedule = $schedule;
$this->cronCommand = $command;
$this->shared_context->command = "ee cron create $site --schedule=\"$schedule\" --command=\"$command\"";
exec($this->shared_context->command, $output, $return_status);
$this->shared_context->output = implode($output);
$this->shared_context->return_status = $return_status;
}

/**
* @Then I should see the relavant cron job in list of crons for site `:site`
* @throws Exception: If the cron job is not found
*/
function cron_job_exists(string $site)
{
exec("ee cron list $site", $output, $return_status);
if ($return_status !== 0) {
throw new Exception("Could not list crons for site $site. Got a return status code of $return_status with the following output:\n\n" . implode($output));
}
if (
// All three of these should be present in the output, otherwise the cron job was not created properly
strpos(implode($output), $this->cronCommand) === false ||
strpos(implode($output), $this->schedule) === false ||
strpos(implode($output), $this->site) === false
) {
throw new Exception("Could not find the cron job in the list of crons for site $site. The cron job was supposed to be:\n\n" . $this->cronCommand . "\n\nBut the list of crons for site $site was:\n\n" . implode($output));
}
}

/**
* @Then Cron creation should show an error message
* @throws Exception: If the cron creation does not fail
*/
function cron_creation_errors()
{
if ( trim($this->shared_context->output) === "" ) {
# Error message is thrown directly to stderr, so there shouldn't be any output
return;
}
throw new Exception("Cron creation did not fail as expected. The output was:\n\n" . $this->shared_context->output);
}
}
96 changes: 96 additions & 0 deletions features/bootstrap/DeleteContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;


/**
* Defines behat context for ``ee cron delete``.
*/
class DeleteContext implements Context
{

private SharedContext $shared_context;

/**
* Initializes context.
*/
public function __construct()
{
}

/**
* @BeforeScenario
*/
public function gatherSharedContext(BeforeScenarioScope $scope)
{
$this->shared_context = $scope->getEnvironment()->getContext(SharedContext::class);
}

/**
* @When I delete that cron job
*/
function delete_created_cron()
{
$this->shared_context->command = "ee cron delete {$this->shared_context->cron_created}";
exec($this->shared_context->command, $output, $return_status);
$this->shared_context->output = implode($output);
$this->shared_context->return_status = $return_status;
}

/**
* @Then I should see success message for deleting cron job
* @throws Exception: If the output is not as expected
*/
function success_message_for_deleting_cron()
{
$id_to_delete = $this->shared_context->cron_created;
if (false === strpos($this->shared_context->output, "Success: Deleted cron with id $id_to_delete")) {
throw new Exception("Expected output to contain `Deleted cron job` but got:\n\n" . $this->shared_context->output);
}
}

/**
* @Then I should not see the cron job in the list of crons
* @throws Exception: If the output is not as expected
*/
function cron_deleted()
{
exec("ee cron list --all | awk '{print $1}'", $output, $return_status);
$output = array_map(
function ($line) {
return trim($line);
},
$output
);
if (in_array((string) $this->shared_context->cron_created, $output)) {
throw new Exception("Cron job was not deleted. Expected output to not contain `{$this->shared_context->sites_created[0]}` but got:\n\n" . $output);
}
}

/**
* @When I delete a cron job that does not exist
* @throws Exception: If the randomness is not random?
*/
function delete_non_existent_cron() {
$this->shared_context->cron_created = random_int(1, 65535);
$this->shared_context->command = "ee cron delete {$this->shared_context->cron_created}";
exec($this->shared_context->command, $output, $return_status);
$this->shared_context->output = implode($output);
$this->shared_context->return_status = $return_status;
}

/**
* @Then I should see an error message for deleting cron job
* @throws Exception: If the output is not empty
*/
function error_message_for_deleting_non_existent_cron() {
if ("" === trim($this->shared_context->output)) {
// Error is directly thrown to stderr
return;
}
throw new Exception("Expected an error message for deleting a non-existent cron job but got an empty output.");
}
}
Loading