/
RuntimeTest.php
207 lines (163 loc) · 6.43 KB
/
RuntimeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Runtime;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Exception;
use Monolog\Logger;
use Monolog\Handler\TestHandler;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Log\LogLevel;
use Symfony\Component\Process\Process;
use DateTime;
class RuntimeTest extends TestCase
{
public function testReleaseIdGeneration()
{
// Given that this is a time based operation, lets conform that at least the format is right
// and the time diff is less than 2 seconds
$now = new DateTime();
$runtime = new Runtime();
$runtime->generateReleaseId();
$releaseId = $runtime->getReleaseId();
$releaseDate = DateTime::createFromFormat('YmdHis', $releaseId);
$this->assertTrue($releaseDate instanceof DateTime);
$dateDiff = $releaseDate->diff($now);
$this->assertLessThanOrEqual(2, $dateDiff->s);
}
public function testEnvOptionWithInvalidEnvironment()
{
$runtime = new Runtime();
$runtime->setConfiguration(['environments' => ['invalid' => []]]);
$runtime->setEnvironment('invalid');
$runtime->setConfiguration(['environments' => []]);
$controlValue = time();
$returnedValue = $runtime->getEnvOption('test', $controlValue);
$this->assertEquals($controlValue, $returnedValue);
}
public function testNoConfigWithEmptyTasks()
{
$runtime = new Runtime();
$tasks = $runtime->getTasks();
$this->assertTrue(is_array($tasks));
$this->assertEquals(0, count($tasks));
}
public function testInvalidEnvironmentEmptyTasks()
{
$runtime = new Runtime();
$runtime->setConfiguration(['environments' => ['invalid' => []]]);
$runtime->setEnvironment('invalid');
$runtime->setConfiguration(['environments' => []]);
$tasks = $runtime->getTasks();
$this->assertTrue(is_array($tasks));
$this->assertEquals(0, count($tasks));
}
public function testInvalidEnvironments()
{
try {
$runtime = new Runtime();
$runtime->setEnvironment('invalid');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof RuntimeException);
}
try {
$runtime = new Runtime();
$runtime->setConfiguration(['environments' => ['valid' => []]]);
$runtime->setEnvironment('invalid');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof RuntimeException);
}
}
public function testTempFile()
{
$runtime = new Runtime();
$tempFile = $runtime->getTempFile();
$this->assertNotEquals('', $tempFile);
$this->assertTrue(file_exists($tempFile));
$this->assertTrue(is_readable($tempFile));
$this->assertTrue(is_writable($tempFile));
$this->assertEquals(0, filesize($tempFile));
}
public function testSSHConfigUndefinedOptions()
{
$runtime = new Runtime();
$sshConfig = $runtime->getSSHConfig();
$this->assertTrue(is_array($sshConfig));
$this->assertTrue(array_key_exists('port', $sshConfig));
$this->assertEquals('22', $sshConfig['port']);
$this->assertTrue(array_key_exists('flags', $sshConfig));
$this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
}
public function testSSHConfigPortDefinedInHostNotation()
{
$runtime = new Runtime();
$runtime->setWorkingHost('223.12.24.64:1056');
$sshConfig = $runtime->getSSHConfig();
$this->assertEquals('1056', $sshConfig['port']);
$runtime->setWorkingHost('223.12.24.64');
$sshConfig = $runtime->getSSHConfig();
$this->assertEquals('22', $sshConfig['port']);
}
public function testSSHConfigEmptyOptions()
{
$runtime = new Runtime();
$runtime->setConfiguration(['environments' => ['test' => ['ssh' => []]]]);
$runtime->setEnvironment('test');
$sshConfig = $runtime->getSSHConfig();
$this->assertTrue(is_array($sshConfig));
$this->assertTrue(array_key_exists('port', $sshConfig));
$this->assertEquals('22', $sshConfig['port']);
$this->assertTrue(array_key_exists('flags', $sshConfig));
$this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
}
public function testSSHConfigCustomOptions()
{
$runtime = new Runtime();
$runtime->setConfiguration(['environments' => ['test' => ['ssh' => ['port' => '2222', 'flags' => '-q']]]]);
$runtime->setEnvironment('test');
$sshConfig = $runtime->getSSHConfig();
$this->assertTrue(is_array($sshConfig));
$this->assertTrue(array_key_exists('port', $sshConfig));
$this->assertEquals('2222', $sshConfig['port']);
$this->assertTrue(array_key_exists('flags', $sshConfig));
$this->assertEquals('-q', $sshConfig['flags']);
}
public function testLogger()
{
$logger = new Logger('test');
$handler = new TestHandler();
$logger->pushHandler($handler);
$runtime = new Runtime();
$runtime->setLogger($logger);
$runtime->log('Test Message', LogLevel::INFO);
$this->assertTrue($handler->hasInfoRecords());
$this->assertTrue($handler->hasInfo('Test Message'));
}
public function testLocalCommand()
{
$runtime = new Runtime();
/** @var Process $process */
$process = $runtime->runLocalCommand('date +%s');
$timestamp = time();
$this->assertTrue($process->isSuccessful());
$this->assertEquals($timestamp, trim($process->getOutput()));
/** @var Process $process */
$process = $runtime->runLocalCommand('false');
$this->assertFalse($process->isSuccessful());
}
public function testCurrentUser()
{
$runtime = new Runtime();
$userData = posix_getpwuid(posix_geteuid());
$this->assertTrue(is_array($userData));
$this->assertArrayHasKey('name', $userData);
$this->assertEquals($userData['name'], $runtime->getCurrentUser());
}
}