github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/MulticloudDeployTest.groovy (about)

     1  import static org.junit.Assert.assertFalse
     2  import static org.junit.Assert.assertTrue
     3  import org.junit.After
     4  import org.junit.Before
     5  import org.junit.Rule
     6  import org.junit.Test
     7  import org.junit.rules.ExpectedException
     8  import org.junit.rules.RuleChain
     9  import com.sap.piper.Utils
    10  
    11  import util.*
    12  
    13  
    14  class MulticloudDeployTest extends BasePiperTest {
    15  
    16      private ExpectedException thrown = new ExpectedException().none()
    17      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    18      private JenkinsMockStepRule neoDeployRule = new JenkinsMockStepRule(this, 'neoDeploy')
    19      private JenkinsMockStepRule cloudFoundryDeployRule = new JenkinsMockStepRule(this, 'cloudFoundryDeploy')
    20      private JenkinsMockStepRule cloudFoundryCreateServiceRule = new JenkinsMockStepRule(this, 'cloudFoundryCreateService')
    21      private JenkinsReadMavenPomRule readMavenPomRule = new JenkinsReadMavenPomRule(this, 'test/resources/deploy')
    22  
    23      private Map neo1 = [:]
    24      private Map neo2 = [:]
    25      private Map cloudFoundry1 = [:]
    26      private Map cloudFoundry2 = [:]
    27      private boolean executedOnKubernetes = false
    28      private boolean executedOnNode = false
    29      private boolean executedInParallel = false
    30  
    31      @Rule
    32      public RuleChain ruleChain = Rules
    33          .getCommonRules(this)
    34          .around(new JenkinsReadYamlRule(this))
    35          .around(thrown)
    36          .around(stepRule)
    37          .around(neoDeployRule)
    38          .around(cloudFoundryDeployRule)
    39          .around(cloudFoundryCreateServiceRule)
    40          .around(readMavenPomRule)
    41  
    42      @Before
    43      void init() {
    44          helper.registerAllowedMethod("deleteDir", [], null)
    45          helper.registerAllowedMethod('dockerExecuteOnKubernetes', [Map.class, Closure.class], {params, body ->
    46              executedOnKubernetes = true
    47              body()
    48          })
    49          helper.registerAllowedMethod('node', [String.class, Closure.class], {s, body ->
    50              executedOnNode = true
    51              body()
    52          })
    53          helper.registerAllowedMethod("parallel", [Map.class], { map ->
    54              map.each {key, value ->
    55                  value()
    56              }
    57              executedInParallel = true
    58          })
    59  
    60          neo1 = [
    61                    host: 'test.deploy.host1.com',
    62                    account: 'trialuser1',
    63                    credentialsId: 'credentialsId1'
    64                ]
    65  
    66          neo2 = [
    67                    host: 'test.deploy.host2.com',
    68                    account: 'trialuser2',
    69                    credentialsId: 'credentialsId2'
    70                ]
    71  
    72          cloudFoundry1 = [
    73                             apiEndpoint: 'apiEndpoint1',
    74                             appName:'testAppName1',
    75                             credentialsId: 'cfCredentialsId1',
    76                             manifest: 'test.yml',
    77                             mtaExtensionDescriptor: 'targetMtaDescriptor.mtaext',
    78                             org: 'testOrg1',
    79                             space: 'testSpace1'
    80                         ]
    81  
    82          cloudFoundry2 = [
    83                              apiEndpoint: 'apiEndpoint2',
    84                              appName:'testAppName2',
    85                              credentialsId: 'cfCredentialsId2',
    86                              manifest: 'test.yml',
    87                              org: 'testOrg2',
    88                              space: 'testSpace2'
    89                          ]
    90  
    91          nullScript.commonPipelineEnvironment.configuration = [
    92              general: [
    93                  neoTargets: [
    94                      neo1, neo2
    95                  ],
    96                  cfTargets: [
    97                      cloudFoundry1, cloudFoundry2
    98                  ]
    99              ],
   100              stages: [
   101                  acceptance: [
   102                      org: 'testOrg',
   103                      space: 'testSpace',
   104                      deployUser: 'testUser'
   105                  ]
   106              ],
   107              steps: [
   108                  cloudFoundryDeploy: [
   109                      cloudFoundry : [
   110                          apiEndpoint: 'globalApiEndpoint',
   111                          appName:'globalAppName',
   112                          credentialsId: 'globalCredentialsId',
   113                          mtaExtensionDescriptor: 'globalTargetMtaDescriptor.mtaext',
   114                          org: 'globalOrg',
   115                          space: 'globalSpace'
   116                      ],
   117                      mtaExtensionDescriptor: 'globalMtaDescriptor.mtaext',
   118                      deployTool: 'cf_native',
   119                      deployType: 'blue-green',
   120                      keepOldInstance: true,
   121                      cf_native: [
   122                          dockerImage: 'ppiper/cf-cli:6',
   123                          dockerWorkspace: '/home/piper'
   124                      ]
   125                  ]
   126              ]
   127          ]
   128  
   129          helper.registerAllowedMethod('echo', [CharSequence.class], {})
   130  
   131          Utils.metaClass.echo = { def m -> }
   132      }
   133  
   134      @After
   135      public void tearDown() {
   136          Utils.metaClass = null
   137      }
   138  
   139  
   140      @Test
   141      void errorNoTargetsDefined() {
   142  
   143          nullScript.commonPipelineEnvironment.configuration.general.neoTargets = []
   144          nullScript.commonPipelineEnvironment.configuration.general.cfTargets = []
   145  
   146          thrown.expect(Exception)
   147          thrown.expectMessage('Deployment skipped because no targets defined!')
   148  
   149          stepRule.step.multicloudDeploy(
   150              script: nullScript
   151          )
   152      }
   153  
   154      @Test
   155      void neoDeploymentTest() {
   156  
   157          nullScript.commonPipelineEnvironment.configuration.general.neoTargets = [neo1]
   158          nullScript.commonPipelineEnvironment.configuration.general.cfTargets = []
   159  
   160          stepRule.step.multicloudDeploy(
   161              script: nullScript,
   162              source: 'file.mtar'
   163          )
   164  
   165          assert neoDeployRule.hasParameter('script', nullScript)
   166          assert neoDeployRule.hasParameter('warAction', 'deploy')
   167          assert neoDeployRule.hasParameter('source', 'file.mtar')
   168          assert neoDeployRule.hasParameter('neo', neo1)
   169      }
   170  
   171      @Test
   172      void neoRollingUpdateTest() {
   173  
   174          nullScript.commonPipelineEnvironment.configuration.general.neoTargets = []
   175          nullScript.commonPipelineEnvironment.configuration.general.cfTargets = []
   176  
   177          def neoParam = [
   178                      host: 'test.param.deploy.host.com',
   179                      account: 'trialparamNeoUser',
   180                      credentialsId: 'paramNeoCredentialsId'
   181                  ]
   182  
   183          stepRule.step.multicloudDeploy(
   184              script: nullScript,
   185              neoTargets: [neoParam],
   186              source: 'file.mtar',
   187              enableZeroDowntimeDeployment: true
   188          )
   189  
   190          assert neoDeployRule.hasParameter('script', nullScript)
   191          assert neoDeployRule.hasParameter('warAction', 'rolling-update')
   192          assert neoDeployRule.hasParameter('source', 'file.mtar')
   193          assert neoDeployRule.hasParameter('neo', neoParam)
   194      }
   195  
   196      @Test
   197      void cfDeploymentTest() {
   198  
   199          nullScript.commonPipelineEnvironment.configuration.general.neoTargets = []
   200          nullScript.commonPipelineEnvironment.configuration.general.cfTargets = []
   201  
   202          def cloudFoundry = [
   203                      appName:'paramTestAppName',
   204                      manifest: 'test.yml',
   205                      org: 'paramTestOrg',
   206                      space: 'paramTestSpace',
   207                      credentialsId: 'paramCfCredentialsId'
   208                  ]
   209  
   210          stepRule.step.multicloudDeploy([
   211              script: nullScript,
   212              cfTargets: [cloudFoundry]
   213          ])
   214  
   215          assert cloudFoundryDeployRule.hasParameter('script', nullScript)
   216          assert cloudFoundryDeployRule.hasParameter('deployType', 'standard')
   217          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry)
   218      }
   219  
   220      @Test
   221      void cfBlueGreenDeploymentTest() {
   222  
   223          nullScript.commonPipelineEnvironment.configuration.general.neoTargets = []
   224          nullScript.commonPipelineEnvironment.configuration.general.cfTargets = [cloudFoundry1]
   225  
   226          stepRule.step.multicloudDeploy([
   227              script: nullScript,
   228              enableZeroDowntimeDeployment: true
   229          ])
   230  
   231          assert cloudFoundryDeployRule.hasParameter('script', nullScript)
   232          assert cloudFoundryDeployRule.hasParameter('deployType', 'blue-green')
   233          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry1)
   234      }
   235  
   236      @Test
   237      void multicloudDeploymentTest() {
   238  
   239          stepRule.step.multicloudDeploy([
   240              script: nullScript,
   241              enableZeroDowntimeDeployment: true,
   242              source: 'file.mtar'
   243          ])
   244  
   245          assert neoDeployRule.hasParameter('script', nullScript)
   246          assert neoDeployRule.hasParameter('warAction', 'rolling-update')
   247          assert neoDeployRule.hasParameter('source', 'file.mtar')
   248          assert neoDeployRule.hasParameter('neo', neo1)
   249          assert neoDeployRule.hasParameter('neo', neo2)
   250  
   251          assert cloudFoundryDeployRule.hasParameter('script', nullScript)
   252          assert cloudFoundryDeployRule.hasParameter('deployType', 'blue-green')
   253          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry1)
   254          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry2)
   255      }
   256  
   257      @Test
   258      void multicloudParallelDeploymentTest() {
   259  
   260          stepRule.step.multicloudDeploy([
   261              script: nullScript,
   262              enableZeroDowntimeDeployment: true,
   263              parallelExecution: true,
   264              source: 'file.mtar'
   265          ])
   266  
   267          assert neoDeployRule.hasParameter('script', nullScript)
   268          assert neoDeployRule.hasParameter('warAction', 'rolling-update')
   269          assert neoDeployRule.hasParameter('source', 'file.mtar')
   270          assert neoDeployRule.hasParameter('neo', neo1)
   271          assert neoDeployRule.hasParameter('neo', neo2)
   272  
   273          assert cloudFoundryDeployRule.hasParameter('script', nullScript)
   274          assert cloudFoundryDeployRule.hasParameter('deployType', 'blue-green')
   275          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry1)
   276          assert cloudFoundryDeployRule.hasParameter('cloudFoundry', cloudFoundry2)
   277      }
   278  
   279      @Test
   280      void 'cfCreateServices calls cloudFoundryCreateService step with correct parameters'() {
   281          stepRule.step.multicloudDeploy([
   282              script          : nullScript,
   283              cfCreateServices: [[apiEndpoint: 'http://mycf.org', serviceManifest: 'services-manifest.yml', manifestVariablesFiles: 'vars.yml', space: 'PerformanceTests', org: 'MyOrg', credentialsId: 'MyCred']],
   284              source          : 'file.mtar'
   285          ])
   286  
   287          assert cloudFoundryCreateServiceRule.hasParameter('cloudFoundry', [
   288              serviceManifest       : 'services-manifest.yml',
   289              space                 : 'PerformanceTests',
   290              org                   : 'MyOrg',
   291              credentialsId         : 'MyCred',
   292              apiEndpoint           : 'http://mycf.org',
   293              manifestVariablesFiles: 'vars.yml'
   294          ])
   295      }
   296  
   297      @Test
   298      void 'cfCreateServices with parallelTestExecution defined in compatible parameter - must run in parallel'() {
   299          def closureRun = null
   300  
   301          helper.registerAllowedMethod('error', [String.class], { s->
   302              if (s == "Deployment skipped because no targets defined!") {
   303                  // This error is ok because in this test we're not interested in the deployment
   304              } else {
   305                  throw new RuntimeException("Unexpected error in test with message: ${s}")
   306              }
   307          })
   308          helper.registerAllowedMethod('parallel', [Map.class], {m -> closureRun = m})
   309  
   310          nullScript.commonPipelineEnvironment.configuration.general['features'] = [parallelTestExecution: true]
   311  
   312          stepRule.step.multicloudDeploy([
   313              script: nullScript,
   314              cfCreateServices: [[serviceManifest: 'services-manifest.yml', space: 'PerformanceTests', org: 'foo', credentialsId: 'foo']],
   315              source: 'file.mtar'
   316          ])
   317  
   318          assert closureRun != null
   319      }
   320  
   321      @Test
   322      void 'cfCreateServices with parallelExecution defined - must run in parallel'() {
   323          def closureRun = null
   324  
   325          helper.registerAllowedMethod('error', [String.class], { s->
   326              if (s == "Deployment skipped because no targets defined!") {
   327                  // This error is ok because in this test we're not interested in the deployment
   328              } else {
   329                  throw new RuntimeException("Unexpected error in test with message: ${s}")
   330              }
   331          })
   332          helper.registerAllowedMethod('parallel', [Map.class], {m -> closureRun = m})
   333  
   334          nullScript.commonPipelineEnvironment.configuration.general = [parallelExecution: true]
   335  
   336          stepRule.step.multicloudDeploy([
   337              script: nullScript,
   338              cfCreateServices: [[serviceManifest: 'services-manifest.yml', space: 'PerformanceTests', org: 'foo', credentialsId: 'foo']],
   339              source: 'file.mtar'
   340          ])
   341  
   342          assert closureRun != null
   343      }
   344  
   345      @Test
   346      void multicloudParallelOnK8sTest() {
   347          binding.variables.env.POD_NAME = "name"
   348  
   349          stepRule.step.multicloudDeploy([
   350              script                      : nullScript,
   351              enableZeroDowntimeDeployment: true,
   352              parallelExecution           : true,
   353              source                      : 'file.mtar'
   354          ])
   355  
   356          assertTrue(executedInParallel)
   357          assertTrue(executedOnKubernetes)
   358          assertFalse(executedOnNode)
   359  
   360      }
   361  
   362      @Test
   363      void multicloudParallelOnNodeTest() {
   364          stepRule.step.multicloudDeploy([
   365              script                      : nullScript,
   366              enableZeroDowntimeDeployment: true,
   367              parallelExecution           : true,
   368              source                      : 'file.mtar'
   369          ])
   370  
   371          assertTrue(executedInParallel)
   372          assertTrue(executedOnNode)
   373          assertFalse(executedOnKubernetes)
   374  
   375      }
   376  
   377      @Test
   378      void multicloudPreDeploymentHookTest() {
   379          def closureRun = null
   380  
   381          stepRule.step.multicloudDeploy([
   382              script                      : nullScript,
   383              preDeploymentHook           : {closureRun = true},
   384          ])
   385  
   386          assertTrue(closureRun)
   387      }
   388  }