1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class PipelineTest extends DeploynautTest { |
|
|
|
|
4
|
|
|
|
5
|
|
|
public function setUp() { |
6
|
|
|
parent::setUp(); |
7
|
|
|
Injector::inst()->load(array( |
8
|
|
|
'DeploynautLogFile' => 'PipelineTest_MockLog', |
9
|
|
|
'ConfirmationMessagingService' => 'PipelineTest_RecordingMessageSender', |
10
|
|
|
'DNDeployment' => 'PipelineTest_DNDeployment', |
11
|
|
|
'DNDataTransfer' => 'PipelineTest_DNDataTransfer' |
12
|
|
|
)); |
13
|
|
|
$this->clearLog(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Gets the config data from the test pipeline config file |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
* @throws Exception |
21
|
|
|
*/ |
22
|
|
|
protected function getPipelineConfig() { |
23
|
|
|
require_once 'thirdparty/spyc/spyc.php'; |
24
|
|
|
$path = __DIR__ . '/PipelineTest_Config.yml'; |
25
|
|
|
if(!file_exists($path)) { |
26
|
|
|
throw new Exception(sprintf('YAML configuration for pipeline not found at path "%s"', $path)); |
27
|
|
|
} |
28
|
|
|
return Spyc::YAMLLoad($path); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Reset logs of scaffolded services |
33
|
|
|
*/ |
34
|
|
|
protected function clearLog() { |
35
|
|
|
PipelineTest_MockLog::clear(); |
36
|
|
|
PipelineTest_RecordingMessageSender::clear(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function assertHasLog($message) { |
40
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message($message), "Assert log \"$message\""); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function assertNotLogged($message) { |
44
|
|
|
$this->assertFalse(PipelineTest_MockLog::has_message($message), "Assert not logged \"$message\""); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function assertSentMessage($message, $recipient) { |
48
|
|
|
$this->assertTrue( |
|
|
|
|
49
|
|
|
PipelineTest_RecordingMessageSender::has_message($message, $recipient), |
50
|
|
|
"Assert message \"$message\" sent to \"$recipient\"" |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function assertNotSentMessage($message, $recipient) { |
55
|
|
|
$this->assertFalse( |
|
|
|
|
56
|
|
|
PipelineTest_RecordingMessageSender::has_message($message, $recipient), |
57
|
|
|
"Assert message \"$message\" not sent to \"$recipient\"" |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Dummy logging service |
65
|
|
|
*/ |
66
|
|
|
class PipelineTest_MockLog extends DeploynautLogFile implements TestOnly { |
|
|
|
|
67
|
|
|
|
68
|
|
|
protected static $messages = array(); |
69
|
|
|
|
70
|
|
|
public function __construct($logFile) {} |
71
|
|
|
|
72
|
|
|
public static function clear() { |
73
|
|
|
self::$messages = array(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function get_messages() { |
77
|
|
|
return self::$messages; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function write($message) { |
81
|
|
|
self::log($message); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function log($message) { |
85
|
|
|
self::$messages[] = $message; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function exists() { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function content() { |
94
|
|
|
return implode(PHP_EOL, self::$messages); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if any message contains the given string |
99
|
|
|
* |
100
|
|
|
* @param string $test |
101
|
|
|
* @return boolean True if this message is contained in any of the given messages |
102
|
|
|
*/ |
103
|
|
|
public static function has_message($test) { |
104
|
|
|
foreach(self::$messages as $message) { |
105
|
|
|
if(strpos($message, $test) !== false) return true; |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
class PipelineTest_RecordingMessageSender extends EmailMessagingService implements TestOnly { |
|
|
|
|
112
|
|
|
|
113
|
|
|
protected static $messages = array(); |
114
|
|
|
|
115
|
|
|
public static function clear() { |
116
|
|
|
self::$messages = array(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function get_messages() { |
120
|
|
|
return self::$messages; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function has_message($test, $recipient = null) { |
124
|
|
|
foreach(self::$messages as $message) { |
125
|
|
|
if($message[1] !== $test) continue; |
126
|
|
|
if($recipient && $message[0] === $recipient) return true; |
127
|
|
|
} |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function sendViaEmail($source, $from, $to, $subject, $body) { |
132
|
|
|
self::$messages[] = array($to, $body); |
133
|
|
|
$source->log("Sent message to $to (subject: $subject)"); |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Dummy deployment service |
142
|
|
|
*/ |
143
|
|
|
class PipelineTest_DNDeployment extends DNDeployment implements TestOnly { |
|
|
|
|
144
|
|
|
|
145
|
|
View Code Duplication |
public function __construct($record = null, $isSingleton = false, $model = null) { |
|
|
|
|
146
|
|
|
// Set the fields data. |
147
|
|
|
if(!$record) { |
148
|
|
|
$record = array( |
149
|
|
|
'ID' => 0, |
150
|
|
|
'ClassName' => 'DNDeployment', |
151
|
|
|
'RecordClassName' => 'DNDeployment' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
parent::__construct($record, $isSingleton, $model); |
156
|
|
|
|
157
|
|
|
$this->class = 'DNDeployment'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function enqueueDeployment() { |
161
|
|
|
// Mock behaviour of enqueue without actually enqueuing anything |
162
|
|
|
$environment = $this->Environment(); |
163
|
|
|
$project = $environment->Project(); |
164
|
|
|
|
165
|
|
|
$log = $this->log(); |
166
|
|
|
$log->write(sprintf('Deploying "%s" to "%s"', $this->SHA, $environment->getFullName())); |
167
|
|
|
|
168
|
|
|
if(!$this->DeployerID) { |
169
|
|
|
$this->DeployerID = Member::currentUserID(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
if($this->DeployerID) { |
|
|
|
|
173
|
|
|
$deployer = $this->Deployer(); |
174
|
|
|
$message = sprintf( |
175
|
|
|
'Deploy to %s:%s initiated by %s (%s)', |
176
|
|
|
$project->Name, |
177
|
|
|
$environment->Name, |
178
|
|
|
$deployer->getName(), |
179
|
|
|
$deployer->Email |
180
|
|
|
); |
181
|
|
|
$log->write($message); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return 'dummytoken'; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getSHA() { |
188
|
|
|
$sha = parent::getField('SHA'); |
|
|
|
|
189
|
|
|
return $sha ?: '9ae502821345ab39b04d46ce6bb822ccdd7f7414'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function start() { |
193
|
|
|
$this->Status = 'Queued'; |
194
|
|
|
$this->write(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function markFinished() { |
198
|
|
|
$this->Status = 'Finished'; |
199
|
|
|
$this->write(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function markFailed() { |
203
|
|
|
$this->Status = 'Failed'; |
204
|
|
|
$this->write(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function ResqueStatus() { |
208
|
|
|
// Just lie, remembering to map 'Finished' to 'Complete' |
209
|
|
|
return $this->Status === 'Finished' ? 'Complete' : $this->Status; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
class PipelineTest_DNDataTransfer extends DNDataTransfer implements TestOnly { |
|
|
|
|
214
|
|
|
|
215
|
|
View Code Duplication |
public function __construct($record = null, $isSingleton = false, $model = null) { |
|
|
|
|
216
|
|
|
// Set the fields data. |
217
|
|
|
if(!$record) { |
218
|
|
|
$record = array( |
219
|
|
|
'ID' => 0, |
220
|
|
|
'ClassName' => 'DNDataTransfer', |
221
|
|
|
'RecordClassName' => 'DNDataTransfer' |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
parent::__construct($record, $isSingleton, $model); |
226
|
|
|
|
227
|
|
|
$this->class = 'DNDataTransfer'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function start() { |
231
|
|
|
$this->Status = 'Queued'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function markFinished() { |
235
|
|
|
$this->Status = 'Finished'; |
236
|
|
|
$this->write(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function markFailed() { |
240
|
|
|
$this->Status = 'Failed'; |
241
|
|
|
$this->write(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function ResqueStatus() { |
245
|
|
|
// Just lie, remembering to map 'Finished' to 'Complete' |
246
|
|
|
return $this->Status === 'Finished' ? 'Complete' : $this->Status; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
class PipelineTest_Project extends DNProject implements TestOnly { |
|
|
|
|
251
|
|
|
|
252
|
|
|
public function repoExists() { |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
class PipelineTest_Environment extends DNEnvironment implements TestOnly { |
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Use the demo backend |
262
|
|
|
* |
263
|
|
|
* @var string |
264
|
|
|
*/ |
265
|
|
|
public function Backend() { |
266
|
|
|
return new DemoDeploymentBackend; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.