PageRenderTime 27ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/com/netflix/asgard/DeploymentServiceUnitSpec.groovy

https://gitlab.com/kps007/asgard
Groovy | 192 lines | 142 code | 35 blank | 15 comment | 12 complexity | fa14b0716c328394aebe6214fb74a24d MD5 | raw file
  1. /*
  2. * Copyright 2014 Netflix, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.netflix.asgard
  17. import com.amazonaws.services.simpledb.model.Attribute
  18. import com.amazonaws.services.simpledb.model.Item
  19. import com.amazonaws.services.simpleworkflow.flow.StartWorkflowOptions
  20. import com.amazonaws.services.simpleworkflow.flow.WorkflowClientExternal
  21. import com.amazonaws.services.simpleworkflow.flow.generic.GenericWorkflowClientExternal
  22. import com.amazonaws.services.simpleworkflow.model.ChildPolicy
  23. import com.amazonaws.services.simpleworkflow.model.WorkflowExecution
  24. import com.amazonaws.services.simpleworkflow.model.WorkflowExecutionInfo
  25. import com.amazonaws.services.simpleworkflow.model.WorkflowType
  26. import com.netflix.asgard.deployment.DeploymentWorkflow
  27. import com.netflix.asgard.deployment.DeploymentWorkflowDescriptionTemplate
  28. import com.netflix.asgard.deployment.DeploymentWorkflowOptions
  29. import com.netflix.asgard.model.AutoScalingGroupBeanOptions
  30. import com.netflix.asgard.model.Deployment
  31. import com.netflix.asgard.model.LaunchConfigurationBeanOptions
  32. import com.netflix.asgard.model.SwfWorkflow
  33. import com.netflix.asgard.model.SwfWorkflowTags
  34. import com.netflix.asgard.model.WorkflowExecutionBeanOptions
  35. import com.netflix.glisten.InterfaceBasedWorkflowClient
  36. import spock.lang.Specification
  37. class DeploymentServiceUnitSpec extends Specification {
  38. AwsSimpleWorkflowService awsSimpleWorkflowService = Mock(AwsSimpleWorkflowService)
  39. FlowService flowService = Mock(FlowService)
  40. DeploymentService deploymentService = new DeploymentService(awsSimpleWorkflowService: awsSimpleWorkflowService,
  41. flowService: flowService, awsSimpleDbService: Mock(AwsSimpleDbService))
  42. Closure<WorkflowExecutionInfo> newWorkflowExecutionInfo = { int sequenceNumber ->
  43. new WorkflowExecutionInfo(tagList: new SwfWorkflowTags(id: sequenceNumber as String).constructTags(),
  44. workflowType: new WorkflowType(name: 'DeploymentWorkflow.deploy'),
  45. startTimestamp: new Date(sequenceNumber))
  46. }
  47. Closure<Deployment> newDeployment = { int sequenceNumber ->
  48. new Deployment(sequenceNumber as String, null, null, null, null, null, new Date(sequenceNumber),
  49. new Date(sequenceNumber), 'running', [], [])
  50. }
  51. def setup() {
  52. Retriable.mixin(NoDelayRetriableMixin)
  53. }
  54. void 'should get running deployments'() {
  55. when:
  56. List<Deployment> deployments = deploymentService.runningDeployments
  57. then:
  58. deployments == [3, 2, 1].collect(newDeployment)
  59. and:
  60. 1 * deploymentService.awsSimpleWorkflowService.getOpenWorkflowExecutions() >> [1, 2, 3].
  61. collect(newWorkflowExecutionInfo)
  62. }
  63. void 'should get finished deployments'() {
  64. when:
  65. List<Deployment> deployments = deploymentService.finishedDeployments
  66. then:
  67. deployments == [3, 2, 1].collect(newDeployment)
  68. and:
  69. 1 * deploymentService.awsSimpleWorkflowService.getClosedWorkflowExecutions() >> [1, 2, 3].
  70. collect(newWorkflowExecutionInfo)
  71. }
  72. void 'should get last 100 finished deployments'() {
  73. when:
  74. List<Deployment> deployments = deploymentService.finishedDeployments
  75. then:
  76. deployments == (107..8).collect(newDeployment)
  77. and:
  78. 1 * deploymentService.awsSimpleWorkflowService.getClosedWorkflowExecutions() >> (1..107).
  79. collect(newWorkflowExecutionInfo)
  80. }
  81. void 'should get deployment by id'() {
  82. when:
  83. Deployment deployment = deploymentService.getDeploymentById('1')
  84. then:
  85. deployment == newDeployment(1)
  86. and:
  87. 1 * deploymentService.awsSimpleWorkflowService.getWorkflowExecutionInfoByTaskId('1') >>
  88. new WorkflowExecutionBeanOptions(newWorkflowExecutionInfo(1))
  89. 1 * deploymentService.awsSimpleDbService.selectOne('ASGARD_SWF_TOKEN_FOR_DEPLOYMENT', '1') >> new Item(
  90. name: '1', attributes: [new Attribute(name: 'token', value: '1')])
  91. 0 * _
  92. }
  93. void 'should not get deployment if it does not exist'() {
  94. when:
  95. Deployment deployment = deploymentService.getDeploymentById('1')
  96. then:
  97. deployment == null
  98. and:
  99. 3 * deploymentService.awsSimpleWorkflowService.getWorkflowExecutionInfoByTaskId('1')
  100. 0 * _
  101. }
  102. void 'should not get deployment without an id'() {
  103. expect:
  104. null == deploymentService.getDeploymentById(null)
  105. }
  106. void 'should cancel deployment'() {
  107. WorkflowExecution workflowExecution = new WorkflowExecution(workflowId: '1')
  108. deploymentService.flowService = Mock(FlowService)
  109. WorkflowClientExternal mockWorkflowClientExternal = Mock(WorkflowClientExternal)
  110. when:
  111. deploymentService.cancelDeployment(new UserContext(username: 'akiedis', clientHostName: 'rhcp.com'),
  112. new Deployment(null, null, null, workflowExecution))
  113. then:
  114. 1 * deploymentService.flowService.getWorkflowClient(workflowExecution) >> mockWorkflowClientExternal
  115. 1 * mockWorkflowClientExternal.terminateWorkflowExecution('Cancelled by akiedis@rhcp.com', _,
  116. ChildPolicy.TERMINATE)
  117. }
  118. def 'starting a deployment should make workflow client, call getter to update cache, and return task ID'() {
  119. UserContext userContext = UserContext.auto(Region.US_EAST_1)
  120. DeploymentWorkflowOptions deployOpts = new DeploymentWorkflowOptions(clusterName: 'Calysteral')
  121. LaunchConfigurationBeanOptions lcOpts = new LaunchConfigurationBeanOptions()
  122. AutoScalingGroupBeanOptions asgOpts = new AutoScalingGroupBeanOptions()
  123. when:
  124. String taskId = deploymentService.startDeployment(userContext, deployOpts, lcOpts, asgOpts)
  125. then:
  126. taskId == '07700900461'
  127. 1 * flowService.getNewWorkflowClient(userContext, DeploymentWorkflow,
  128. new Link(EntityType.cluster, 'Calysteral')) >> {
  129. WorkflowExecution workflowExecution = new WorkflowExecution(workflowId: '1716231163')
  130. GenericWorkflowClientExternal genericClient = Mock(GenericWorkflowClientExternal) {
  131. startWorkflow(_) >> workflowExecution
  132. }
  133. new SwfWorkflow(new InterfaceBasedWorkflowClient(DeploymentWorkflow,
  134. new DeploymentWorkflowDescriptionTemplate(),
  135. workflowExecution, new WorkflowType(), new StartWorkflowOptions(), null,
  136. genericClient, new SwfWorkflowTags(id: '07700900461')))
  137. }
  138. 0 * _
  139. }
  140. def 'should indicate that a workflow execution is in progress for the specified cluster'() {
  141. Link link = Link.to(EntityType.cluster, 'helloworld-example')
  142. when:
  143. Deployment deployment = deploymentService.getRunningDeploymentForCluster(Region.US_WEST_1, 'helloworld-example')
  144. then:
  145. deployment == new Deployment('123', null, null, null, null, null, null, null, 'running', [], [])
  146. 1 * awsSimpleWorkflowService.getOpenWorkflowExecutionForObjectLink(Region.US_WEST_1, link) >>
  147. new WorkflowExecutionInfo(tagList: new SwfWorkflowTags(id: '123').constructTags())
  148. }
  149. def 'should indicate that a workflow execution is not in progress for the specified cluster'() {
  150. Link link = Link.to(EntityType.cluster, 'helloworld-example')
  151. when:
  152. Deployment deployment = deploymentService.getRunningDeploymentForCluster(Region.US_WEST_1, 'helloworld-example')
  153. then:
  154. deployment == null
  155. 1 * awsSimpleWorkflowService.getOpenWorkflowExecutionForObjectLink(Region.US_WEST_1, link) >> null
  156. }
  157. }