From 27a85839643529bb12b746a25d692489681aecd1 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:38:47 +0530 Subject: [PATCH 01/22] Add behat test for a clean installation --- features/bootstrap/FeatureContext.php | 94 +++++++++++++++++++++++++++ features/cron.feature | 14 ++++ 2 files changed, 108 insertions(+) create mode 100644 features/bootstrap/FeatureContext.php create mode 100644 features/cron.feature diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php new file mode 100644 index 0000000..76576f3 --- /dev/null +++ b/features/bootstrap/FeatureContext.php @@ -0,0 +1,94 @@ +command = "ee cron list --all"; + exec($this->command, $output, $return_status); + $this->output = implode($output); + $this->return_status = $return_status; + } + + /** + * @Then Exit Code must not be 0 + */ + function non_zero_status() + { + if ($this->return_status === 0) { + throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); + } + } + + + /** + * @Then I get an error message for no cron jobs + */ + function no_cron_error() + { + if ("" !== trim($this->output)) { + throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); + } + } +} diff --git a/features/cron.feature b/features/cron.feature new file mode 100644 index 0000000..cf29208 --- /dev/null +++ b/features/cron.feature @@ -0,0 +1,14 @@ +Feature: Cron-Command + We have a command that we make use of in order to manage cron-jobs + run by easyengine using ofelia cron manager. + + Scenario: + If I list all of the cron entries and there are no sites + present, then I should get an error message for no cron jobs + + Given EE is present + And No site has been created + When I list all of the cron entries + Then I get an error message for no cron jobs + And Exit Code must not be 0 + From 546629bc5dc2d1a55dae2b0475d7532387b31da9 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:33:23 +0530 Subject: [PATCH 02/22] Add other behat sites for single and multiple site entries --- features/bootstrap/FeatureContext.php | 70 +++++++++++++++++++++++++++ features/cron.feature | 35 ++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 76576f3..3f9aaf2 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -20,6 +20,7 @@ class FeatureContext implements Context public string $command; public string $output; public int $return_status; + public array $sites_created = []; /** * Initializes context. @@ -91,4 +92,73 @@ function no_cron_error() throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); } } + + /** + * @Given I created site `:site_name` with type `:site_type` + */ + function create_site(string $site_name, string $site_type) { + $this->sites_created[] = $site_name; + exec("ee site create $site_name --type=$site_type", $output, $return_status); + if ($return_status !== 0) { + throw new Exception("Could not create site $site_name with type $site_type. Output: " . implode($output)); + } + } + + /** + * @Then I should see a list of cron jobs for those sites + */ + function crons_for_all_sites() { + $this->command = "ee cron list --all"; + exec($this->command, $output, $return_status); + $this->output = implode($output); + $this->return_status = $return_status; + + foreach ($this->sites_created as $site) { + if (strpos($this->output, $site) === false) { + throw new Exception("Could not find cron job for site $site in the output of the command: " . $this->output); + } + } + } + + /** + * @Then exit code must be 0 + */ + function zero_status() { + if ($this->return_status !== 0) { + throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); + } + } + + /** + * @When I list cron entries for the site `:site_name` + */ + function list_cron_for_site(string $site_name) { + $this->command = "ee cron list $site_name"; + exec($this->command, $output, $return_status); + $this->output = implode($output); + $this->return_status = $return_status; + } + + /** + * @Then I should see a list of cron jobs for the site `:site_name` + */ + function crons_for_site(string $site_name) { + if (strpos($this->output, $site_name) === false) { + throw new Exception("Could not find cron job for site $site_name in the output of the command: " . $this->output); + } + } + + /** + * After Scenario Cleanup Hook + * + * @AfterScenario + */ + function after_scenario_cleanup() { + $this->command = null; + $this->output = null; + $this->return_status = 0; + foreach ($this->sites_created as $site) { + exec("ee site delete $site --yes"); + } + } } diff --git a/features/cron.feature b/features/cron.feature index cf29208..c001294 100644 --- a/features/cron.feature +++ b/features/cron.feature @@ -12,3 +12,38 @@ Feature: Cron-Command Then I get an error message for no cron jobs And Exit Code must not be 0 + Scenario: + If I list all of the cron entries and there are sites + which require a cron service (type wp), then I should + get a list of cron jobs for those sites + + Given EE is present + And I created site `test.site` with type `wp` + And I created site `test2.site` with type `wp` + And I created site `test3.site` with type `wp` + When I list all of the cron entries + Then I should see a list of cron jobs for those sites + And Exit Code must be 0 + + Scenario: + If I list all of the cron entries for a site which does not + requires a cron service, then I should get an error message + for no cron jobs + + Given EE is present + And I created site `test.site` with type `wp` + When I list cron jobs for the site `test.site` + Then I get an error message for no cron jobs + And Exit Code must not be 0 + + + Scenario: + If I list all of the cron entries for a site which requires + a cron service, then I should get a list of cron jobs for + that site + + Given EE is present + And I created site `test.site` with type `html` + When I list cron jobs for the site `test.site` + Then I should see a list of cron jobs for the site `test.site` + And Exit Code must be 0 From 03df923e41c0eac713a89d3b3d1f6ae0a1ff9399 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:35:01 +0530 Subject: [PATCH 03/22] Reset `sites_created` to empty --- features/bootstrap/FeatureContext.php | 1 + 1 file changed, 1 insertion(+) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 3f9aaf2..7ef319c 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -160,5 +160,6 @@ function after_scenario_cleanup() { foreach ($this->sites_created as $site) { exec("ee site delete $site --yes"); } + $this->sites_created = []; } } From f4c9c257670a8827c2e13b3d5c5ad193c20e3269 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:57:18 +0530 Subject: [PATCH 04/22] Fix issue with resetting internal state strings --- features/bootstrap/FeatureContext.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 7ef319c..fc56fcb 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -154,8 +154,8 @@ function crons_for_site(string $site_name) { * @AfterScenario */ function after_scenario_cleanup() { - $this->command = null; - $this->output = null; + $this->command = ""; + $this->output = ""; $this->return_status = 0; foreach ($this->sites_created as $site) { exec("ee site delete $site --yes"); From cd58264795bf1c23b729557251cb0bd9e9731a75 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:59:42 +0530 Subject: [PATCH 05/22] Fix typo in @When --- features/bootstrap/FeatureContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index fc56fcb..e084ec2 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -130,7 +130,7 @@ function zero_status() { } /** - * @When I list cron entries for the site `:site_name` + * @When I list cron jobs for the site `:site_name` */ function list_cron_for_site(string $site_name) { $this->command = "ee cron list $site_name"; From 875014808bde83400bc9071ee121def6be7d2bf8 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 5 Jun 2024 17:12:18 +0530 Subject: [PATCH 06/22] Swap site types to appropriate locations --- features/cron.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/cron.feature b/features/cron.feature index c001294..ea68de7 100644 --- a/features/cron.feature +++ b/features/cron.feature @@ -31,7 +31,7 @@ Feature: Cron-Command for no cron jobs Given EE is present - And I created site `test.site` with type `wp` + And I created site `test.site` with type `html` When I list cron jobs for the site `test.site` Then I get an error message for no cron jobs And Exit Code must not be 0 @@ -43,7 +43,7 @@ Feature: Cron-Command that site Given EE is present - And I created site `test.site` with type `html` + And I created site `test.site` with type `wp` When I list cron jobs for the site `test.site` Then I should see a list of cron jobs for the site `test.site` And Exit Code must be 0 From b15fc1e9633b8986e91327862d8f2b3ef04b2a67 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:11:54 +0530 Subject: [PATCH 07/22] Update behat configuration for sepration of behat features into individual feature, and context files. --- behat.yml | 7 +++++++ features/bootstrap/{FeatureContext.php => ListContext.php} | 4 ++-- features/{cron.feature => list.feature} | 0 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 behat.yml rename features/bootstrap/{FeatureContext.php => ListContext.php} (97%) rename features/{cron.feature => list.feature} (100%) diff --git a/behat.yml b/behat.yml new file mode 100644 index 0000000..628faf2 --- /dev/null +++ b/behat.yml @@ -0,0 +1,7 @@ +default: + suites: + list: + paths: + - "%paths.base%/features/list.feature" + contexts: + - "ListContext" diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/ListContext.php similarity index 97% rename from features/bootstrap/FeatureContext.php rename to features/bootstrap/ListContext.php index e084ec2..9ab65d1 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/ListContext.php @@ -12,9 +12,9 @@ function unexpectedOutput(string $command, string $output, int $return_status): /** - * Defines application features from the specific context. + * Defines behat context for ``ee cron list``. */ -class FeatureContext implements Context +class ListContext implements Context { public string $command; diff --git a/features/cron.feature b/features/list.feature similarity index 100% rename from features/cron.feature rename to features/list.feature From 05cf80e6c5ad8e0b2b67bd3f43dfb179218b7ae9 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:57:17 +0530 Subject: [PATCH 08/22] Add a shared context to avoid having to repeat common stuff for scenarios --- behat.yml | 1 + features/bootstrap/ListContext.php | 118 +++++++-------------------- features/bootstrap/SharedContext.php | 118 +++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 90 deletions(-) create mode 100644 features/bootstrap/SharedContext.php diff --git a/behat.yml b/behat.yml index 628faf2..1f7c1dd 100644 --- a/behat.yml +++ b/behat.yml @@ -4,4 +4,5 @@ default: paths: - "%paths.base%/features/list.feature" contexts: + - "SharedContext" - "ListContext" diff --git a/features/bootstrap/ListContext.php b/features/bootstrap/ListContext.php index 9ab65d1..f0253f4 100644 --- a/features/bootstrap/ListContext.php +++ b/features/bootstrap/ListContext.php @@ -1,6 +1,7 @@ shared_context = $scope->getEnvironment()->getContext(SharedContext::class); } /** @@ -66,100 +44,60 @@ function no_site_created() */ function list_cron() { - $this->command = "ee cron list --all"; - exec($this->command, $output, $return_status); - $this->output = implode($output); - $this->return_status = $return_status; - } - - /** - * @Then Exit Code must not be 0 - */ - function non_zero_status() - { - if ($this->return_status === 0) { - throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); - } + $this->shared_context->command = "ee cron list --all"; + exec($this->shared_context->command, $output, $return_status); + $this->shared_context->output = implode($output); + $this->shared_context->return_status = $return_status; } /** * @Then I get an error message for no cron jobs + * @throws Exception: If the output is not empty */ function no_cron_error() { - if ("" !== trim($this->output)) { - throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); - } - } - - /** - * @Given I created site `:site_name` with type `:site_type` - */ - function create_site(string $site_name, string $site_type) { - $this->sites_created[] = $site_name; - exec("ee site create $site_name --type=$site_type", $output, $return_status); - if ($return_status !== 0) { - throw new Exception("Could not create site $site_name with type $site_type. Output: " . implode($output)); + if ("" !== trim($this->shared_context->output)) { + throw new Exception(unexpectedOutput($this->shared_context->command, $this->shared_context->output, $this->shared_context->return_status)); } } /** * @Then I should see a list of cron jobs for those sites + * @throws Exception: If the site name is not found in the output */ function crons_for_all_sites() { - $this->command = "ee cron list --all"; - exec($this->command, $output, $return_status); - $this->output = implode($output); - $this->return_status = $return_status; - - foreach ($this->sites_created as $site) { - if (strpos($this->output, $site) === false) { - throw new Exception("Could not find cron job for site $site in the output of the command: " . $this->output); + $this->shared_context->command = "ee cron list --all"; + exec($this->shared_context->command, $output, $return_status); + $this->shared_context->output = implode($output); + $this->shared_context->return_status = $return_status; + + foreach ($this->shared_context->sites_created as $site) { + if (strpos($this->shared_context->output, $site) === false) { + throw new Exception("Could not find cron job for site $site in the output of the command: " . $this->shared_context->output); } } } - /** - * @Then exit code must be 0 - */ - function zero_status() { - if ($this->return_status !== 0) { - throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); - } - } /** * @When I list cron jobs for the site `:site_name` */ function list_cron_for_site(string $site_name) { - $this->command = "ee cron list $site_name"; - exec($this->command, $output, $return_status); - $this->output = implode($output); - $this->return_status = $return_status; + $this->shared_context->command = "ee cron list $site_name"; + 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 a list of cron jobs for the site `:site_name` + * @throws Exception: If the site name is not found in the output */ function crons_for_site(string $site_name) { - if (strpos($this->output, $site_name) === false) { - throw new Exception("Could not find cron job for site $site_name in the output of the command: " . $this->output); + if (strpos($this->shared_context->output, $site_name) === false) { + throw new Exception("Could not find cron job for site $site_name in the output of the command: " . $this->shared_context->output); } } - /** - * After Scenario Cleanup Hook - * - * @AfterScenario - */ - function after_scenario_cleanup() { - $this->command = ""; - $this->output = ""; - $this->return_status = 0; - foreach ($this->sites_created as $site) { - exec("ee site delete $site --yes"); - } - $this->sites_created = []; - } } diff --git a/features/bootstrap/SharedContext.php b/features/bootstrap/SharedContext.php new file mode 100644 index 0000000..f47e0ec --- /dev/null +++ b/features/bootstrap/SharedContext.php @@ -0,0 +1,118 @@ +sites_created[] = $site_name; + exec("ee site create $site_name --type=$site_type", $output, $return_status); + if ($return_status !== 0) { + throw new Exception("Could not create site $site_name with type $site_type. Output: " . implode($output)); + } + } + + /** + * @Then exit code must be 0 + * @throws Exception: If the return status is not 0 + */ + function zero_status() { + if ($this->return_status !== 0) { + throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); + } + } + + /** + * @Then Exit Code must not be 0 + * @throws Exception: If the return status is 0 + */ + function non_zero_status() + { + if ($this->return_status === 0) { + throw new Exception(unexpectedOutput($this->command, $this->output, $this->return_status)); + } + } + + + /** + * After Scenario Cleanup Hook + * + * @AfterScenario + */ + function after_scenario_cleanup() { + $this->command = ""; + $this->output = ""; + $this->return_status = 0; + + foreach ($this->sites_created as $site) { + exec("ee site delete $site --yes"); + } + $this->sites_created = []; + } + +} From 1f29ba6eb69b16d339baccf63c38015d4c47e70d Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:59:19 +0530 Subject: [PATCH 09/22] Resolve function redeclaraion error by removing ``unexpectedOutput`` --- features/bootstrap/ListContext.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/features/bootstrap/ListContext.php b/features/bootstrap/ListContext.php index f0253f4..45fa37b 100644 --- a/features/bootstrap/ListContext.php +++ b/features/bootstrap/ListContext.php @@ -6,12 +6,6 @@ use Behat\Gherkin\Node\TableNode; -function unexpectedOutput(string $command, string $output, int $return_status): string -{ - return "Command did not exit as expected. Ran `$command` and got a return status code of `$return_status` with the following output:\n\n" . $output; -} - - /** * Defines behat context for ``ee cron list``. */ From a6a2dd1b32fb1c51870099c4fb908cb0470e8495 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:50:10 +0530 Subject: [PATCH 10/22] Add behat tests for ee cron create --- behat.yml | 6 +++ features/bootstrap/CreateContext.php | 81 ++++++++++++++++++++++++++++ features/bootstrap/SharedContext.php | 11 ++++ features/create.feature | 24 +++++++++ features/list.feature | 6 ++- 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 features/bootstrap/CreateContext.php create mode 100644 features/create.feature diff --git a/behat.yml b/behat.yml index 1f7c1dd..62ad246 100644 --- a/behat.yml +++ b/behat.yml @@ -6,3 +6,9 @@ default: contexts: - "SharedContext" - "ListContext" + create: + paths: + - "%paths.base%/features/create.feature" + contexts: + - "SharedContext" + - "CreateContext" diff --git a/features/bootstrap/CreateContext.php b/features/bootstrap/CreateContext.php new file mode 100644 index 0000000..da17831 --- /dev/null +++ b/features/bootstrap/CreateContext.php @@ -0,0 +1,81 @@ +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); + } +} diff --git a/features/bootstrap/SharedContext.php b/features/bootstrap/SharedContext.php index f47e0ec..40a5cfa 100644 --- a/features/bootstrap/SharedContext.php +++ b/features/bootstrap/SharedContext.php @@ -98,6 +98,17 @@ function non_zero_status() } } + /** + * @Then I should see `:message` + * @throws Exception: If the output does not contain the success message + */ + function see_message(string $message) + { + if (strpos(trim($this->output), $message) === false) { + throw new Exception("Could not find the message '$message' in the output of the command: " . $this->output); + } + } + /** * After Scenario Cleanup Hook diff --git a/features/create.feature b/features/create.feature new file mode 100644 index 0000000..f1dd644 --- /dev/null +++ b/features/create.feature @@ -0,0 +1,24 @@ +Feature: Cron-Command -> Create + We have a command that we make use of in order to manage cron-jobs + run by easyengine using ofelia cron manager. This is a subcommand + used to create any cron jobs which are to be run on a particular + site's containers. + + Scenario: + I can use the create command to create a cron job for an existing site + + Given EE is present + And I created site `test.site` with type `wp` + When I create a cron for site `test.site` with schedule `"* 1 * * *"` and command `"echo Hello World"` + Then I should see `"Success: Cron created successfully"` + And I should see the relavant cron job in list of crons for site `test.site` + And Exit Code must be 0 + + Scenario: + I can not create a cron job for a non-existing site, the command should error out + + Given EE is present + # Notice that the site `test.site` has not been created + When I create a cron for site `test.site` with schedule `"* 1 * * *"` and command `"echo Hello World"` + Then Cron creation should show an error message + And Exit Code must not be 0 diff --git a/features/list.feature b/features/list.feature index ea68de7..de1776d 100644 --- a/features/list.feature +++ b/features/list.feature @@ -1,6 +1,8 @@ -Feature: Cron-Command +Feature: Cron-Command -> List We have a command that we make use of in order to manage cron-jobs - run by easyengine using ofelia cron manager. + run by easyengine using ofelia cron manager. This is a subcommand + used to list all of the cron jobs that are currently configured to + be managed and run by EasyEngine. Scenario: If I list all of the cron entries and there are no sites From e2098eb7fead468a3456741d58b736d347b30db0 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:50:05 +0530 Subject: [PATCH 11/22] Add behat tests for ee cron delete --- behat.yml | 6 ++ features/bootstrap/DeleteContext.php | 96 ++++++++++++++++++++++++++++ features/bootstrap/SharedContext.php | 22 +++++++ features/delete.feature | 25 ++++++++ 4 files changed, 149 insertions(+) create mode 100644 features/bootstrap/DeleteContext.php create mode 100644 features/delete.feature diff --git a/behat.yml b/behat.yml index 62ad246..67ef661 100644 --- a/behat.yml +++ b/behat.yml @@ -12,3 +12,9 @@ default: contexts: - "SharedContext" - "CreateContext" + delete: + paths: + - "%paths.base%/features/delete.feature" + contexts: + - "SharedContext" + - "DeleteContext" diff --git a/features/bootstrap/DeleteContext.php b/features/bootstrap/DeleteContext.php new file mode 100644 index 0000000..288aca6 --- /dev/null +++ b/features/bootstrap/DeleteContext.php @@ -0,0 +1,96 @@ +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."); + } +} diff --git a/features/bootstrap/SharedContext.php b/features/bootstrap/SharedContext.php index 40a5cfa..90986f4 100644 --- a/features/bootstrap/SharedContext.php +++ b/features/bootstrap/SharedContext.php @@ -23,6 +23,7 @@ class SharedContext implements Context public int $return_status; public array $sites_created = []; + public int $cron_created = -1; /** * Initializes context. @@ -109,6 +110,26 @@ function see_message(string $message) } } + /** + * @Given I created cron for site `:site_name` with schedule `:schedule` and command `:command` + */ + function create_cron(string $site_name, string $schedule, string $command) { + $to_exec = "ee cron create $site_name --schedule=\"$schedule\" --command=\"$command\""; + exec($to_exec, $_, $return_status); + if ($return_status !== 0) { + throw new Exception("Could not create cron job for site $site_name with schedule $schedule and command $command. Output:\n" . $output); + } + // Cron is created, now we need to find the cron id + exec( + "ee cron list $site_name | grep \"$site_name\" | grep \"$schedule\" | grep \"$command\" | awk '{print $1}'", + $_output, $return_status + ); + if ($return_status !== 0) { + throw new Exception("Could not find the cron job in the list of crons. Output:\n" . $output); + } + $this->cron_created = (int) $_output[0]; // The cron id + } + /** * After Scenario Cleanup Hook @@ -119,6 +140,7 @@ function after_scenario_cleanup() { $this->command = ""; $this->output = ""; $this->return_status = 0; + $this->cron_created = -1; foreach ($this->sites_created as $site) { exec("ee site delete $site --yes"); diff --git a/features/delete.feature b/features/delete.feature new file mode 100644 index 0000000..41d2064 --- /dev/null +++ b/features/delete.feature @@ -0,0 +1,25 @@ +Feature: Cron-Command -> Delete + We have a command that we make use of in order to manage cron-jobs + run by easyengine using ofelia cron manager. This is a subcommand + used to remove a cron-job from the list of cron-jobs managed by + ofelia, based on the said job's ID. + + Scenario: + I can use the delete command to remove a cron-job from the list + of cron-jobs managed by ofelia, based on the said job's ID. + + Given EE is present + And I created site `test.site` with type `wp` + And I created cron for site `test.site` with schedule `"* * * * *"` and command `"echo Hello World"` + When I delete that cron job + Then I should see success message for deleting cron job + And I should not see the cron job in the list of crons + And Exit Code must be 0 + +Scenario: + If I try to delete a cron-job that does not exist, I should see an error message + + Given EE is present + When I delete a cron job that does not exist + Then I should see an error message for deleting cron job + And Exit Code must not be 0 From 868fea1d6783c5519d0d89c948f3b73dee1e7dd5 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:26:52 +0530 Subject: [PATCH 12/22] Add behat tests for ``ee cron run-now`` --- behat.yml | 6 +++ features/bootstrap/RunNowContext.php | 67 ++++++++++++++++++++++++++++ features/run-now.feature | 25 +++++++++++ 3 files changed, 98 insertions(+) create mode 100644 features/bootstrap/RunNowContext.php create mode 100644 features/run-now.feature diff --git a/behat.yml b/behat.yml index 67ef661..653404c 100644 --- a/behat.yml +++ b/behat.yml @@ -18,3 +18,9 @@ default: contexts: - "SharedContext" - "DeleteContext" + run-now: + paths: + - "%paths.base%/features/run-now.feature" + contexts: + - "SharedContext" + - "RunNowContext" diff --git a/features/bootstrap/RunNowContext.php b/features/bootstrap/RunNowContext.php new file mode 100644 index 0000000..d7754cc --- /dev/null +++ b/features/bootstrap/RunNowContext.php @@ -0,0 +1,67 @@ +shared_context = $scope->getEnvironment()->getContext(SharedContext::class); + } + + /** + * @When I run that cron job immediately + */ + function run_cron_immediately() + { + $this->shared_context->command = "ee cron run-now {$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; + } + + /** + + * @When I run 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 run-now {$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 cron job not found + * @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(unexpectedOutput($this->shared_context->command, $this->shared_context->output, $this->shared_context->return_status)); + } +} diff --git a/features/run-now.feature b/features/run-now.feature new file mode 100644 index 0000000..2525b5b --- /dev/null +++ b/features/run-now.feature @@ -0,0 +1,25 @@ +Feature: Cron-Command -> Run-Now + We have a command that we make use of in order to manage cron-jobs + run by easyengine using ofelia cron manager. This is a subcommand + used to execute a given cron-job immediately based on its id. + + Scenario: + If I have a cron-job that is scheduled to run at a later time + and I want to run it immediately, I can make use of the + `ee cron run-now` command to execute the cron-job immediately, + and get its output in the console. + + Given EE is present + And I created site `test.site` with type `wp` + And I created cron for site `test.site` with schedule `"* * * * *"` and command `"echo Hello World"` + When I run that cron job immediately + Then I should see `"Hello World"` + And Exit Code must be 0 + +Scenario: + If I try to run a cron job that does not exist, I should see an error message + + Given EE is present + When I run a cron job that does not exist + Then I should see an error message for cron job not found + And Exit Code must not be 0 From 7b12b39f907aea67d60d53c8e96481573424e61f Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:51:48 +0530 Subject: [PATCH 13/22] Add behat test workflow --- .github/workflows/behat.yaml | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .github/workflows/behat.yaml diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml new file mode 100644 index 0000000..aacc376 --- /dev/null +++ b/.github/workflows/behat.yaml @@ -0,0 +1,83 @@ +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 + recursive: true + - name: Checkout This repository + uses: actions/checkout@v4 + with: + repository: ${{ github.repository }} + path: ${{ steps.split.outputs.REPO }} + lfs: true + recursive: true + + - uses: shivammathur/setup-php@v2 + with: + php-version: '${{ matrix.php }}' + coverage: none + tools: composer + extensions: pcntl, curl, sqlite3, zip, dom, mbstring, json + + - name: Install composer dependencies + run: | + cd easyengine + composer install --no-interaction --no-progress --no-suggest --prefer-source + cd - + + cd ${{ steps.split.outputs.REPO }} + composer install --no-interaction --no-progress --no-suggest --prefer-source + cd - + + - name: Replace easyengine dependency with local path + run: | + 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 - + + - name: Add behat and easyengine to PATH + run: | + # Add behat to PATH + echo "::add-path::$(pwd)/easyengine/vendor/bin" + # Add easyengine's /bin/ee to PATH + echo "::add-path::$(pwd)/easyengine/bin" + + - name: Run behat tests + run: | + cd ${{ steps.split.outputs.REPO }} + ../easyengine/vendor/bin/behat --strict --no-interaction --no-colors --format=progress + cd - From 12df95bfb51c26d1f2ef16e747362f639302afb7 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:04:33 +0530 Subject: [PATCH 14/22] Fix path not found problems --- .github/workflows/behat.yaml | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index aacc376..4fc02e3 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -23,11 +23,11 @@ jobs: 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 + 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: @@ -54,30 +54,22 @@ jobs: run: | cd easyengine composer install --no-interaction --no-progress --no-suggest --prefer-source - cd - - - cd ${{ steps.split.outputs.REPO }} + cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} composer install --no-interaction --no-progress --no-suggest --prefer-source - cd - + 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 - - - - name: Add behat and easyengine to PATH - run: | - # Add behat to PATH - echo "::add-path::$(pwd)/easyengine/vendor/bin" - # Add easyengine's /bin/ee to PATH - echo "::add-path::$(pwd)/easyengine/bin" + cd ${{ github.workspace }} - name: Run behat tests run: | - cd ${{ steps.split.outputs.REPO }} - ../easyengine/vendor/bin/behat --strict --no-interaction --no-colors --format=progress + cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} + ${{ github.workspace }}/easyengine/vendor/bin/behat --strict --no-interaction --no-colors --format=progress cd - From 159f1e385354d12f5deca028f306d252eb907cff Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:23:50 +0530 Subject: [PATCH 15/22] Build and install EasyEngine phar to avoid attempting altering path --- .github/workflows/behat.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 4fc02e3..83aee63 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -68,6 +68,14 @@ jobs: composer dump-autoload cd ${{ github.workspace }} + - name: Make and install EasyEngine phar + run: | + cd ${{ github.workspace }}/easyengine + php -dphar.readonly=0 utils/make-phar.php easyengine.phar + mv easyengine.phar /usr/local/bin/ee + chmod +x /usr/local/bin/ee + echo "EasyEngine phar is now installed" + cd ${{ github.workspace }} - name: Run behat tests run: | cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} From 8741ea5ee0961dfaa96f97c2ad8a3ca5d1135502 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:33:42 +0530 Subject: [PATCH 16/22] Update the PATH because of some issues in requiring dependencies --- .github/workflows/behat.yaml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 83aee63..940adea 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -68,16 +68,12 @@ jobs: composer dump-autoload cd ${{ github.workspace }} - - name: Make and install EasyEngine phar + - name: Add easyengine and behat to PATH run: | - cd ${{ github.workspace }}/easyengine - php -dphar.readonly=0 utils/make-phar.php easyengine.phar - mv easyengine.phar /usr/local/bin/ee - chmod +x /usr/local/bin/ee - echo "EasyEngine phar is now installed" - cd ${{ github.workspace }} + echo "PATH=\$PATH:${{ github.workspace }}/easyengine/bin" >> $GITHUB_ENV + echo "PATH=\$PATH:${{ github.workspace }}/easyengine/vendor/bin" >> $GITHUB_ENV - name: Run behat tests run: | cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} - ${{ github.workspace }}/easyengine/vendor/bin/behat --strict --no-interaction --no-colors --format=progress + behat --strict --no-interaction --no-colors --format=progress cd - From ba6ccc88a5a02d7262862ed5805b2d21aded9c76 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:45:09 +0530 Subject: [PATCH 17/22] Add some extra debugigng info to the action --- .github/workflows/behat.yaml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 940adea..ceccfd7 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -49,6 +49,11 @@ jobs: 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: | @@ -70,8 +75,8 @@ jobs: - name: Add easyengine and behat to PATH run: | - echo "PATH=\$PATH:${{ github.workspace }}/easyengine/bin" >> $GITHUB_ENV - echo "PATH=\$PATH:${{ github.workspace }}/easyengine/vendor/bin" >> $GITHUB_ENV + echo "${{ github.workspace }}/easyengine/bin" >> $GITHUB_PATH + echo "${{ github.workspace }}/easyengine/vendor/bin" >> $GITHUB_PATH - name: Run behat tests run: | cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} From 5e830e5e1a83a5c469dca396d873137e725633e3 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:37:39 +0530 Subject: [PATCH 18/22] Retry using phar for action --- .github/workflows/behat.yaml | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index ceccfd7..836b578 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -58,9 +58,9 @@ jobs: - name: Install composer dependencies run: | cd easyengine - composer install --no-interaction --no-progress --no-suggest --prefer-source + 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-source + composer install --no-interaction --no-progress --no-suggest --prefer-dist cd ${{ github.workspace }} - name: Replace easyengine dependency with local path @@ -73,6 +73,26 @@ jobs: 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 From 3fc12e10ee028660f91c9b13e7e778cc69ec9dc4 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:42:02 +0530 Subject: [PATCH 19/22] Rebuild autoload to include dev after phar creation --- .github/workflows/behat.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 836b578..4cc2007 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -99,6 +99,8 @@ jobs: 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 }} behat --strict --no-interaction --no-colors --format=progress cd - From 05ba2a8bb8a1f83eeab1b42eb3d286d2f1b6ef98 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:02:13 +0530 Subject: [PATCH 20/22] Run behat using sudo --- .github/workflows/behat.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 4cc2007..0843b26 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -102,5 +102,5 @@ jobs: cd ${{ github.workspace }}/easyengine composer dump-autoload --no-interaction cd ${{ github.workspace }}/${{ steps.split.outputs.REPO }} - behat --strict --no-interaction --no-colors --format=progress + sudo ${{ github.workspace }}/easyengine/vendor/bin/behat --strict --no-interaction --no-colors --format=progress cd - From a021d1dfb7e7e0484d9515be1dc6e4582823cc93 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:17:13 +0530 Subject: [PATCH 21/22] Do some final cleanups on workflow execution --- .github/workflows/behat.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/behat.yaml b/.github/workflows/behat.yaml index 0843b26..2066feb 100644 --- a/.github/workflows/behat.yaml +++ b/.github/workflows/behat.yaml @@ -34,14 +34,14 @@ jobs: repository: EasyEngine/easyengine path: easyengine lfs: true - recursive: true + submodules: true - name: Checkout This repository uses: actions/checkout@v4 with: repository: ${{ github.repository }} path: ${{ steps.split.outputs.REPO }} lfs: true - recursive: true + submodules: true - uses: shivammathur/setup-php@v2 with: @@ -102,5 +102,5 @@ jobs: 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 --format=progress + sudo ${{ github.workspace }}/easyengine/vendor/bin/behat --strict --no-interaction --no-colors cd - From f578ee6c4a75449295052e4bc534c4a6555f2398 Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:23:53 +0530 Subject: [PATCH 22/22] Add behat tests for ``ee cron update`` --- behat.yml | 6 +++ features/bootstrap/RunNowContext.php | 2 +- features/bootstrap/UpdateContext.php | 66 ++++++++++++++++++++++++++++ features/update.feature | 17 +++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 features/bootstrap/UpdateContext.php create mode 100644 features/update.feature diff --git a/behat.yml b/behat.yml index 653404c..247bafc 100644 --- a/behat.yml +++ b/behat.yml @@ -24,3 +24,9 @@ default: contexts: - "SharedContext" - "RunNowContext" + update: + paths: + - "%paths.base%/features/update.feature" + contexts: + - "SharedContext" + - "UpdateContext" diff --git a/features/bootstrap/RunNowContext.php b/features/bootstrap/RunNowContext.php index d7754cc..7ac3cca 100644 --- a/features/bootstrap/RunNowContext.php +++ b/features/bootstrap/RunNowContext.php @@ -7,7 +7,7 @@ /** - * Defines behat context for ``ee cron delete``. + * Defines behat context for ``ee cron run-now``. */ class RunNowContext implements Context { diff --git a/features/bootstrap/UpdateContext.php b/features/bootstrap/UpdateContext.php new file mode 100644 index 0000000..5844df0 --- /dev/null +++ b/features/bootstrap/UpdateContext.php @@ -0,0 +1,66 @@ +shared_context = $scope->getEnvironment()->getContext(SharedContext::class); + } + + /** + * @When I update that cron to site `:site_name` with schedule `:schedule` and command `:command` + */ + function update_created_cron(string $site_name, string $schedule, string $command) { + $this->updated_site_name = $site_name; + $this->updated_schedule = $schedule; + $this->updated_command = $command; + $this->shared_context->command = "ee cron update {$this->shared_context->cron_created} --site=\"$site_name\" --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 The cron job listing should reflect the changes + * @throws Exception when the changes are not reflected properly + */ + function check_updated_cron() { + $cron_id = $this->shared_context->cron_created; + // Filter out the output to only include our specific cron job + exec("ee cron list $this->updated_site_name | grep '^$cron_id\s'", $output, $return_status); + $output = implode($output); + if ( + false === strpos($output, $this->shared_context->cron_created) || + false === strpos($output, $this->updated_site_name) || + false === strpos($output, $this->updated_schedule) || + false === strpos($output, $this->updated_command) + ) { + throw new Exception("Expected output to contain the updated cron job details but got:\n\n" . $output); + } + } +} diff --git a/features/update.feature b/features/update.feature new file mode 100644 index 0000000..4e0ea10 --- /dev/null +++ b/features/update.feature @@ -0,0 +1,17 @@ +Feature: Cron-Command -> Update + We have a command that we make use of in order to manage cron-jobs + run by easyengine using ofelia cron manager. This is a subcommand + used to update any cron jobs which are to be run by this manager. + + Scenario: + I can use the update command to update various aspects of a cron + job that is to be run by ofelia cron manager. + + Given EE is present + And I created site `test.site` with type `wp` + And I created site `test2.site` with type `wp` + And I created cron for site `test.site` with schedule `"* * * 1 *"` and command `"echo 'Hello World'"` + When I update that cron to site `test2.site` with schedule `"* 1 * * *"` and command `"echo 'It Works!'"` + Then The cron job listing should reflect the changes + And I should see `"Success: Cron update Successfully"` + And Exit Code must be 0