/
ExecutionsImportControllerSpec.groovy
107 lines (88 loc) · 4.02 KB
/
ExecutionsImportControllerSpec.groovy
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
/*
* Copyright 2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.orca.controllers
import com.netflix.spinnaker.kork.web.exceptions.InvalidRequestException
import com.netflix.spinnaker.orca.ExecutionStatus
import com.netflix.spinnaker.orca.front50.Front50Service
import com.netflix.spinnaker.orca.front50.model.Application
import com.netflix.spinnaker.orca.model.ExecutionImportResponse
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionNotFoundException
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository
import de.huxhorn.sulky.ulid.ULID
import retrofit.RetrofitError
import spock.lang.Specification
import spock.lang.Unroll
class ExecutionsImportControllerSpec extends Specification {
ExecutionRepository executionRepository = Mock(ExecutionRepository)
Front50Service front50Service = Mock(Front50Service)
ExecutionsImportController controller
def setup() {
executionRepository = Mock(ExecutionRepository)
controller = new ExecutionsImportController(executionRepository, front50Service)
}
@Unroll
def 'should throw error while saving execution with status: #invalidStatus '() {
given:
String executionId = new ULID().nextULID()
Execution execution = new Execution(Execution.ExecutionType.PIPELINE, executionId, 'testapp')
when:
execution.status = invalidStatus
controller.createExecution(execution)
then:
thrown(InvalidRequestException)
1 * front50Service.get('testapp') >> { new Application(name: 'testapp') }
1 * executionRepository.retrieve(Execution.ExecutionType.PIPELINE, executionId) >> { throw new ExecutionNotFoundException('No execution')}
0 * _
where:
invalidStatus << [ExecutionStatus.RUNNING, ExecutionStatus.PAUSED, ExecutionStatus.NOT_STARTED]
}
@Unroll
def 'should succeed while saving execution with status: #validStatus '() {
given:
String executionId = new ULID().nextULID()
Execution execution = new Execution(Execution.ExecutionType.PIPELINE, executionId, 'testapp')
when:
execution.status = validStatus
ExecutionImportResponse result = controller.createExecution(execution)
then:
noExceptionThrown()
1 * front50Service.get('testapp') >> { new Application(name: 'testapp') }
1 * executionRepository.retrieve(Execution.ExecutionType.PIPELINE, executionId) >> { throw new ExecutionNotFoundException('No execution')}
1 * executionRepository.store(execution)
0 * _
result.executionId == executionId
result.executionStatus == validStatus
where:
validStatus << [ExecutionStatus.SUCCEEDED, ExecutionStatus.CANCELED, ExecutionStatus.TERMINAL]
}
def 'should succeed to import if unable to retrieve app from front50'() {
given:
String executionId = new ULID().nextULID()
Execution execution = new Execution(Execution.ExecutionType.PIPELINE, executionId, 'testapp')
execution.status = ExecutionStatus.SUCCEEDED
when:
ExecutionImportResponse result = controller.createExecution(execution)
then:
noExceptionThrown()
1 * front50Service.get('testapp') >> { throw RetrofitError.unexpectedError('http://test.front50.com', new RuntimeException())}
1 * executionRepository.retrieve(Execution.ExecutionType.PIPELINE, executionId) >> { throw new ExecutionNotFoundException('No execution')}
1 * executionRepository.store(execution)
0 * _
result.executionId == executionId
result.executionStatus == ExecutionStatus.SUCCEEDED
}
}