Skip to content

Commit

Permalink
[deployphp#2197] Added testcases for running commands with sudo
Browse files Browse the repository at this point in the history
Added two testcases for e2e tests, that verify:
* that sudo command can be ran with password provided interactively
* that sudo command can be ran with password passed via argument

To allow for these tests to happen, the Dockerfile for server
service had to be modified - the deployer user will require a
password, when running command with sudo.
  • Loading branch information
Bartlomiej Sacharski committed Oct 18, 2020
1 parent 1c610d1 commit ffb9772
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ RUN useradd \
--create-home \
deployer \
&& echo 'deployer:deployer' | chpasswd \
&& echo 'deployer ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& echo 'deployer ALL=(ALL) ALL' >> /etc/sudoers \
&& mkdir ~deployer/.ssh \
&& touch ~deployer/.ssh/authorized_keys \
&& chown -R deployer:deployer ~deployer/.ssh \
Expand Down
2 changes: 1 addition & 1 deletion test/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ The `server` container contains:
* git
* PHP 7.3
* SSH server with
* sudo (user `deployer` can use `sudo` without having to enter passwords)
* sudo (user `deployer` can use `sudo` after providing a password: `deployer`)
6 changes: 3 additions & 3 deletions test/e2e/FunctionsE2ETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function testRunWithPlaceholders(): void
'test:functions:run-with-placeholders',
'-f' => self::RECIPE,
'selector' => 'all',
]);
], [ 'decorated' => false ]);

$display = $this->tester->getDisplay();
$display = trim($this->tester->getDisplay());

self::assertEquals(0, $this->tester->getStatusCode(), $display);
self::assertStringContainsString('placeholder {{bar}} xyz%', $display);
self::assertEquals('placeholder {{bar}} xyz%', $display);
}
}
49 changes: 49 additions & 0 deletions test/e2e/MiscE2ETest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace e2e;

class MiscE2ETest extends AbstractE2ETest
{
private const RECIPE = __DIR__ . '/recipe/misc.php';

/**
* @group e2e
*/
public function testSudoWithPasswordEnteredInteractively(): void
{
$this->init(self::RECIPE);

// We're adding this to inputs, to have it passed with via the STDIN
$this->tester->setInputs(['deployer']);

$this->tester->run([
'test:misc:sudo-write-user',
'-f' => self::RECIPE,
'selector' => 'all',
], [ 'decorated' => false ]);

$display = trim($this->tester->getDisplay());

self::assertEquals(0, $this->tester->getStatusCode(), $display);
self::assertStringContainsString('Current user is: root', $display);
}

/**
* @group e2e
*/
public function testSudoWithPasswordProvidedViaArgument(): void
{
$this->init(self::RECIPE);

$this->tester->run([
'test:misc:sudo-write-user',
'-f' => self::RECIPE,
'-o' => [ 'sudo_pass=deployer' ],
'selector' => 'all',
], [ 'decorated' => false ]);

$display = trim($this->tester->getDisplay());

self::assertEquals(0, $this->tester->getStatusCode(), $display);
self::assertStringContainsString('Current user is: root', $display);
}
}
3 changes: 2 additions & 1 deletion test/e2e/recipe/functions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php declare(strict_types=1);
namespace Deployer;

require_once __DIR__ . '/deploy.php';
// we need to user require instead of require_once, as the hosts HAVE to be loaded multiple times
require __DIR__ . '/deploy.php';

task('test:functions:run-with-placeholders', function (): void {
$cmd = "echo 'placeholder %foo% %baz%'";
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/recipe/laravel.php

This file was deleted.

12 changes: 12 additions & 0 deletions test/e2e/recipe/misc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);
namespace Deployer;

// we need to user require instead of require_once, as the hosts HAVE to be loaded multiple times
require __DIR__ . '/deploy.php';

task('test:misc:sudo-write-user', function (): void {
$cmd = 'sudo bash -c \'echo Current user is: $USER\'';
$output = run($cmd);
writeln($output);
})->shallow();

0 comments on commit ffb9772

Please sign in to comment.