1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DeploymentPipelineStepTest extends PipelineTest { |
|
|
|
|
4
|
|
|
|
5
|
|
|
protected static $fixture_file = 'PipelineTest.yml'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Makes the dummy deployment step |
9
|
|
|
* |
10
|
|
|
* @return DeploymentPipelineStep |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
public function getDummyDeployment($newEnv = false) { |
|
|
|
|
13
|
|
|
$deployStep = $this->objFromFixture('DeploymentPipelineStep', 'testdeploy'); |
14
|
|
|
$deployStep->Config = serialize(array('MaxDuration' => '3600')); |
15
|
|
|
$deployStep->write(); |
16
|
|
|
$pipeline = $deployStep->Pipeline(); |
17
|
|
|
|
18
|
|
|
// simulates a new environment that hasn't got any current build |
19
|
|
|
if($newEnv) { |
20
|
|
|
$pipeline->EnvironmentID = $this->idFromFixture('PipelineTest_Environment', 'newenvironment'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$pipeline->Config = serialize(array()); |
24
|
|
|
$pipeline->write(); |
25
|
|
|
return $deployStep; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testSuccessful() { |
29
|
|
|
$step = $this->getDummyDeployment(); |
30
|
|
|
$step->start(); |
31
|
|
|
|
32
|
|
|
// Assert not error at startup |
33
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
34
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
35
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot creating snapshot of database')); |
|
|
|
|
36
|
|
|
|
37
|
|
|
// Mark the snapshot as completed and check result |
38
|
|
|
$snapshot = $step->Pipeline()->PreviousSnapshot(); |
39
|
|
|
$snapshot->markFinished(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// Retry step |
42
|
|
|
PipelineTest_MockLog::clear(); |
43
|
|
|
$step->start(); |
44
|
|
|
|
45
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
46
|
|
|
$this->assertEquals('Deployment', $step->Doing); |
|
|
|
|
47
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Deployment starting deployment')); |
|
|
|
|
48
|
|
|
|
49
|
|
|
// Mark the service as completed and check result |
50
|
|
|
$author = $this->objFromFixture('Member', 'author'); |
51
|
|
|
$deployment = $step->Pipeline()->CurrentDeployment(); |
52
|
|
|
$this->assertEquals($deployment->DeployerID, $author->ID); |
|
|
|
|
53
|
|
|
$deployment->markFinished(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// Retry step |
56
|
|
|
PipelineTest_MockLog::clear(); |
57
|
|
|
$step->start(); |
58
|
|
|
$this->assertEquals('Finished', $step->Status); |
|
|
|
|
59
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Deployment...')); |
|
|
|
|
60
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Step finished successfully!')); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test failure at the snapshot step |
65
|
|
|
*/ |
66
|
|
|
public function testSnapshotFailure() { |
67
|
|
|
$step = $this->getDummyDeployment(); |
68
|
|
|
$step->start(); |
69
|
|
|
|
70
|
|
|
// Assert not error at startup |
71
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
72
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
73
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot creating snapshot of database')); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// Mark the service as completed and check result |
76
|
|
|
$snapshot = $step->Pipeline()->PreviousSnapshot(); |
77
|
|
|
$snapshot->markFailed(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// Retry step |
80
|
|
|
PipelineTest_MockLog::clear(); |
81
|
|
|
$step->start(); |
82
|
|
|
$this->assertEquals('Failed', $step->Status); |
|
|
|
|
83
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Snapshot...')); |
|
|
|
|
84
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot failed with task status Failed')); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test snapshot is skipped when environment has no build |
89
|
|
|
*/ |
90
|
|
|
public function testSnapshotSkipped() { |
91
|
|
|
$step = $this->getDummyDeployment(true); |
92
|
|
|
$step->start(); |
93
|
|
|
|
94
|
|
|
// Assert not error at startup |
95
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
96
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
97
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('[Skipped] No current build, skipping snapshot')); |
|
|
|
|
98
|
|
|
|
99
|
|
|
// Mark the service as completed and check result |
100
|
|
|
$snapshot = $step->Pipeline()->PreviousSnapshot(); |
101
|
|
|
$this->assertFalse($snapshot->exists(), 'No snapshot was created'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test failure at the deployment step |
106
|
|
|
*/ |
107
|
|
|
public function testDeploymentFailure() { |
108
|
|
|
$step = $this->getDummyDeployment(); |
109
|
|
|
$step->start(); |
110
|
|
|
|
111
|
|
|
// Assert not error at startup |
112
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
113
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
114
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot creating snapshot of database')); |
|
|
|
|
115
|
|
|
|
116
|
|
|
// Mark the snapshot as completed and check result |
117
|
|
|
$snapshot = $step->Pipeline()->PreviousSnapshot(); |
118
|
|
|
$snapshot->markFinished(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
// Retry step |
121
|
|
|
PipelineTest_MockLog::clear(); |
122
|
|
|
$step->start(); |
123
|
|
|
|
124
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
125
|
|
|
$this->assertEquals('Deployment', $step->Doing); |
|
|
|
|
126
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Deployment starting deployment')); |
|
|
|
|
127
|
|
|
|
128
|
|
|
// Mark the service as completed and check result |
129
|
|
|
$deployment = $step->Pipeline()->CurrentDeployment(); |
130
|
|
|
$deployment->markFailed(); |
|
|
|
|
131
|
|
|
|
132
|
|
|
// Retry step |
133
|
|
|
PipelineTest_MockLog::clear(); |
134
|
|
|
$step->start(); |
135
|
|
|
$this->assertEquals('Failed', $step->Status); |
|
|
|
|
136
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Deployment...')); |
|
|
|
|
137
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Deployment failed with task status Failed')); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testDelayedSuccess() { |
141
|
|
|
$step = $this->getDummyDeployment(); |
142
|
|
|
$step->start(); |
143
|
|
|
|
144
|
|
|
// Assert not error at startup |
145
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
146
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
147
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot creating snapshot of database')); |
|
|
|
|
148
|
|
|
|
149
|
|
|
// Retry step |
150
|
|
|
PipelineTest_MockLog::clear(); |
151
|
|
|
$step->start(); |
152
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
153
|
|
|
$this->assertEquals('Snapshot', $step->Doing); |
|
|
|
|
154
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Snapshot...')); |
|
|
|
|
155
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot is still in progress')); |
|
|
|
|
156
|
|
|
|
157
|
|
|
// Mark the snapshot as completed and check result |
158
|
|
|
$snapshot = $step->Pipeline()->PreviousSnapshot(); |
159
|
|
|
$snapshot->markFinished(); |
|
|
|
|
160
|
|
|
|
161
|
|
|
// Advance to deployment |
162
|
|
|
PipelineTest_MockLog::clear(); |
163
|
|
|
$step->start(); |
164
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
165
|
|
|
$this->assertEquals('Deployment', $step->Doing); |
|
|
|
|
166
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Deployment starting deployment')); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// Retry step |
169
|
|
|
PipelineTest_MockLog::clear(); |
170
|
|
|
$step->start(); |
171
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
172
|
|
|
$this->assertEquals('Deployment', $step->Doing); |
|
|
|
|
173
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Deployment...')); |
|
|
|
|
174
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Deployment is still in progress')); |
|
|
|
|
175
|
|
|
|
176
|
|
|
// Mark the service as completed and check result |
177
|
|
|
$deployment = $step->Pipeline()->CurrentDeployment(); |
178
|
|
|
$deployment->markFinished(); |
|
|
|
|
179
|
|
|
|
180
|
|
|
// Retry step |
181
|
|
|
PipelineTest_MockLog::clear(); |
182
|
|
|
$step->start(); |
183
|
|
|
$this->assertEquals('Finished', $step->Status); |
|
|
|
|
184
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Deployment...')); |
|
|
|
|
185
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Step finished successfully!')); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testTimeout() { |
189
|
|
|
$step = $this->getDummyDeployment(); |
190
|
|
|
$step->start(); |
191
|
|
|
|
192
|
|
|
// Assert not error at startup |
193
|
|
|
$this->assertEquals('Started', $step->Status); |
|
|
|
|
194
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('TestDeployStep:Snapshot creating snapshot of database')); |
|
|
|
|
195
|
|
|
|
196
|
|
|
// Go to two hours into the future |
197
|
|
|
SS_Datetime::set_mock_now(date('Y-m-d H:i:s', strtotime('+2 hours'))); |
198
|
|
|
|
199
|
|
|
// Retry step |
200
|
|
|
PipelineTest_MockLog::clear(); |
201
|
|
|
$step->start(); |
202
|
|
|
$this->assertEquals('Failed', $step->Status); |
|
|
|
|
203
|
|
|
$this->assertTrue($step->isTimedOut()); |
|
|
|
|
204
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message('Checking status of TestDeployStep:Snapshot...')); |
|
|
|
|
205
|
|
|
$this->assertTrue(PipelineTest_MockLog::has_message( |
|
|
|
|
206
|
|
|
'TestDeployStep:Snapshot took longer than 3600 seconds to run and has timed out' |
207
|
|
|
)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
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.