|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class AbstractPipelineTest extends PipelineTest { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
// Fixture file containing test pipeline data |
|
6
|
|
|
protected static $fixture_file = 'AbstractPipelineTest.yml'; |
|
7
|
|
|
|
|
8
|
|
|
public function testCheckPipelineStatus() { |
|
9
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'Pipe2'); |
|
10
|
|
|
$pipeline->Config = serialize(array()); |
|
11
|
|
|
$pipeline->write(); |
|
12
|
|
|
|
|
13
|
|
|
// First step should start. |
|
14
|
|
|
$pipeline->checkPipelineStatus(); |
|
15
|
|
|
|
|
16
|
|
|
$this->assertEquals('Running', $pipeline->Status); |
|
|
|
|
|
|
17
|
|
|
$this->assertEquals(true, $pipeline->isRunning()); |
|
|
|
|
|
|
18
|
|
|
$this->assertDOSEquals(array( |
|
19
|
|
|
array('Name'=>'step1', 'Status'=>'Started'), |
|
20
|
|
|
array('Name'=>'step2', 'Status'=>'Queued'), |
|
21
|
|
|
), $pipeline->Steps()); |
|
22
|
|
|
|
|
23
|
|
|
// Try to progress the pipeline second time. Shouldn't move until step is finished. |
|
24
|
|
|
$pipeline->checkPipelineStatus(); |
|
25
|
|
|
$this->assertEquals('Running', $pipeline->Status); |
|
|
|
|
|
|
26
|
|
|
$this->assertEquals(true, $pipeline->isRunning()); |
|
|
|
|
|
|
27
|
|
|
$this->assertDOSEquals(array( |
|
28
|
|
|
array('Name'=>'step1', 'Status'=>'Started'), |
|
29
|
|
|
array('Name'=>'step2', 'Status'=>'Queued'), |
|
30
|
|
|
), $pipeline->Steps()); |
|
31
|
|
|
|
|
32
|
|
|
// First step finished, pipeline should move now. |
|
33
|
|
|
$firstStep = $pipeline->CurrentStep(); |
|
34
|
|
|
$firstStep->Status = 'Finished'; |
|
35
|
|
|
$firstStep->write(); |
|
36
|
|
|
$pipeline->checkPipelineStatus(); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals('Running', $pipeline->Status); |
|
|
|
|
|
|
39
|
|
|
$this->assertEquals(true, $pipeline->isRunning()); |
|
|
|
|
|
|
40
|
|
|
$this->assertDOSEquals(array( |
|
41
|
|
|
array('Name'=>'step1', 'Status'=>'Finished'), |
|
42
|
|
|
array('Name'=>'step2', 'Status'=>'Started'), |
|
43
|
|
|
), $pipeline->Steps()); |
|
44
|
|
|
|
|
45
|
|
|
// Last step finished. Pipeline should fold now. |
|
46
|
|
|
$lastStep = $pipeline->CurrentStep(); |
|
47
|
|
|
$lastStep->Status = 'Finished'; |
|
48
|
|
|
$lastStep->write(); |
|
49
|
|
|
$pipeline->checkPipelineStatus(); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals('Complete', $pipeline->Status); |
|
|
|
|
|
|
52
|
|
|
$this->assertEquals(false, $pipeline->isRunning()); |
|
|
|
|
|
|
53
|
|
|
$this->assertDOSEquals(array( |
|
54
|
|
|
array('Name'=>'step1', 'Status'=>'Finished'), |
|
55
|
|
|
array('Name'=>'step2', 'Status'=>'Finished'), |
|
56
|
|
|
), $pipeline->Steps()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testAborted() { |
|
60
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'Pipe1'); |
|
61
|
|
|
$pipeline->Config = serialize(array()); |
|
62
|
|
|
$pipeline->write(); |
|
63
|
|
|
|
|
64
|
|
|
$pipeline->markAborted(); |
|
65
|
|
|
|
|
66
|
|
|
// confirm Status is now aborted |
|
67
|
|
|
$this->assertEquals('Aborted', $pipeline->Status); |
|
|
|
|
|
|
68
|
|
|
$this->assertEquals(true, $pipeline->isAborted()); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// Confirm steps have correct status. |
|
71
|
|
|
$this->assertDOSEquals(array( |
|
72
|
|
|
array('Name'=>'step1', 'Status'=>'Finished'), |
|
73
|
|
|
array('Name'=>'step2', 'Status'=>'Aborted'), |
|
74
|
|
|
array('Name'=>'step3', 'Status'=>'Aborted') |
|
75
|
|
|
), $pipeline->Steps()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testAbortPipelineLink() { |
|
79
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'Pipe1'); |
|
80
|
|
|
|
|
81
|
|
|
// Confirm the correct abort link is generated |
|
82
|
|
|
$this->assertEquals($pipeline->Link('abort'), |
|
|
|
|
|
|
83
|
|
|
'naut/project/Project 1/environment/env1/pipeline/'.$pipeline->ID.'/abort'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testCanCancelPipeline() { |
|
87
|
|
|
$member = $this->objFromFixture('Member', 'Deployer2'); |
|
88
|
|
|
$member->logIn(); |
|
89
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'Pipe1'); |
|
90
|
|
|
|
|
91
|
|
|
$environment = $pipeline->Environment(); |
|
92
|
|
|
|
|
93
|
|
|
// confirm Status is now running |
|
94
|
|
|
$this->assertEquals('Running', $pipeline->Status); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
// confirm non-admin member who did not start the pipeline cannot stop it |
|
97
|
|
|
$this->assertEquals(false, $environment->CanCancelPipeline()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
// confirm member who started pipeline can stop it |
|
100
|
|
|
$member = $this->objFromFixture('Member', 'Deployer1'); |
|
101
|
|
|
$member->logIn(); |
|
102
|
|
|
$this->assertEquals(true, $environment->CanCancelPipeline()); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
// confirm admin member can stop pipeline |
|
105
|
|
|
$member = $this->objFromFixture('Member', 'Deployer3'); |
|
106
|
|
|
$member->logIn(); |
|
107
|
|
|
$this->assertEquals(true, $environment->CanCancelPipeline()); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testMarkFailed() { |
|
111
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'Pipe1'); |
|
112
|
|
|
$pipeline->Config = serialize(array()); |
|
113
|
|
|
$pipeline->write(); |
|
114
|
|
|
|
|
115
|
|
|
// Pretend second step fails. |
|
116
|
|
|
$step = $this->objFromFixture('PipelineStep', 'Step2'); |
|
117
|
|
|
$step->Status = 'Failed'; |
|
118
|
|
|
$step->write(); |
|
119
|
|
|
$pipeline->markFailed(); |
|
120
|
|
|
|
|
121
|
|
|
// confirm Status is now Failed. |
|
122
|
|
|
$this->assertEquals('Failed', $pipeline->Status); |
|
|
|
|
|
|
123
|
|
|
$this->assertEquals(true, $pipeline->isFailed()); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
// Confirm steps have correct status. |
|
126
|
|
|
$this->assertDOSEquals(array( |
|
127
|
|
|
array('Name'=>'step1', 'Status'=>'Finished'), |
|
128
|
|
|
array('Name'=>'step2', 'Status'=>'Failed'), |
|
129
|
|
|
array('Name'=>'step3', 'Status'=>'Aborted') |
|
130
|
|
|
), $pipeline->Steps()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Provide rollback-able pipeline on the verge of failing. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getFailingPipeline() { |
|
137
|
|
|
// Get default backups |
|
138
|
|
|
$previous = DNDeployment::create(); |
|
139
|
|
|
$previous->SHA = '9f0a012e97715b1871n41gk30f34268u12a0029q'; |
|
140
|
|
|
$previous->write(); |
|
141
|
|
|
$current = DNDeployment::create(); |
|
142
|
|
|
$current->write(); |
|
143
|
|
|
$snapshot = DNDataTransfer::create(); |
|
144
|
|
|
$snapshot->write(); |
|
145
|
|
|
|
|
146
|
|
|
$pipeline = $this->objFromFixture('Pipeline', 'FailingPipe'); |
|
147
|
|
|
$pipeline->Config = serialize(array( |
|
148
|
|
|
'RollbackStep1' => array( |
|
149
|
|
|
'Class' => 'RollbackStep', |
|
150
|
|
|
'RestoreDB' => false, |
|
151
|
|
|
'MaxDuration' => '3600' |
|
152
|
|
|
), |
|
153
|
|
|
'RollbackStep2' => array( |
|
154
|
|
|
'Class' => 'SmokeTestPipelineStep', |
|
155
|
|
|
'MaxDuration' => '3600' |
|
156
|
|
|
) |
|
157
|
|
|
)); |
|
158
|
|
|
$pipeline->PreviousDeploymentID = $previous->ID; |
|
159
|
|
|
$pipeline->CurrentDeploymentID = $current->ID; |
|
160
|
|
|
$pipeline->PreviousSnapshotID = $snapshot->ID; |
|
161
|
|
|
$pipeline->write(); |
|
162
|
|
|
|
|
163
|
|
|
return $pipeline; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testMarkFailedRollbackStart() { |
|
167
|
|
|
|
|
168
|
|
|
$pipeline = $this->getFailingPipeline(); |
|
169
|
|
|
$this->assertEquals($pipeline->RollbackStep1ID, 0); |
|
|
|
|
|
|
170
|
|
|
$this->assertEquals($pipeline->RollbackStep2ID, 0); |
|
|
|
|
|
|
171
|
|
|
$this->assertFalse($pipeline->isFailed()); |
|
|
|
|
|
|
172
|
|
|
$this->assertFalse($pipeline->isRollback()); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
$pipeline->markFailed(); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertNotEmpty($pipeline->RollbackStep1()); |
|
|
|
|
|
|
177
|
|
|
$this->assertNotEmpty($pipeline->RollbackStep2()); |
|
|
|
|
|
|
178
|
|
|
$this->assertFalse($pipeline->isFailed()); |
|
|
|
|
|
|
179
|
|
|
$this->assertTrue($pipeline->isRollback()); |
|
|
|
|
|
|
180
|
|
|
$this->assertHasLog('Beginning rollback...'); |
|
181
|
|
|
$this->assertEquals($pipeline->RollbackStep1()->Status, 'Started'); |
|
|
|
|
|
|
182
|
|
|
$this->assertEquals($pipeline->RollbackStep2()->Status, 'Queued'); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testMarkFailedRollbackFailing() { |
|
187
|
|
|
|
|
188
|
|
|
$pipeline = $this->getFailingPipeline(); |
|
189
|
|
|
|
|
190
|
|
|
// Trigger rollback. |
|
191
|
|
|
$pipeline->markFailed(); |
|
192
|
|
|
|
|
193
|
|
|
// Fail first rollback step. |
|
194
|
|
|
$pipeline->RollbackStep1()->markFailed(); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertHasLog('Pipeline failed, rollback failed.'); |
|
197
|
|
|
|
|
198
|
|
|
$pipeline = Pipeline::get()->byID($pipeline->ID); |
|
199
|
|
|
$this->assertTrue($pipeline->isFailed()); |
|
|
|
|
|
|
200
|
|
|
$this->assertFalse($pipeline->isRollback()); |
|
|
|
|
|
|
201
|
|
|
$this->assertEquals($pipeline->RollbackStep1()->Status, 'Failed'); |
|
|
|
|
|
|
202
|
|
|
$this->assertEquals($pipeline->RollbackStep2()->Status, 'Aborted'); |
|
|
|
|
|
|
203
|
|
|
$this->assertEquals($pipeline->Status, 'Failed'); |
|
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function testMarkCompleteRollback() { |
|
208
|
|
|
|
|
209
|
|
|
$pipeline = $this->getFailingPipeline(); |
|
210
|
|
|
|
|
211
|
|
|
// Trigger rollback. |
|
212
|
|
|
$pipeline->markFailed(); |
|
213
|
|
|
|
|
214
|
|
|
// Succeed the rollback. |
|
215
|
|
|
$pipeline->RollbackStep1()->finish(); |
|
216
|
|
|
$pipeline->RollbackStep2()->finish(); |
|
217
|
|
|
|
|
218
|
|
|
// Progress the pipeline. Will call markFailed. |
|
219
|
|
|
$pipeline->checkPipelineStatus(); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertHasLog('Pipeline failed, but rollback completed successfully.'); |
|
222
|
|
|
|
|
223
|
|
|
$pipeline = Pipeline::get()->byID($pipeline->ID); |
|
224
|
|
|
$this->assertTrue($pipeline->isFailed()); |
|
|
|
|
|
|
225
|
|
|
$this->assertFalse($pipeline->isRollback()); |
|
|
|
|
|
|
226
|
|
|
$this->assertEquals($pipeline->RollbackStep1()->Status, 'Finished'); |
|
|
|
|
|
|
227
|
|
|
$this->assertEquals($pipeline->RollbackStep2()->Status, 'Finished'); |
|
|
|
|
|
|
228
|
|
|
$this->assertEquals($pipeline->Status, 'Failed'); |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
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.