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

     1  #!groovy
     2  import org.junit.After
     3  import org.junit.Before
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.ExpectedException
     7  import org.junit.rules.RuleChain
     8  import util.BasePiperTest
     9  import util.JenkinsDockerExecuteRule
    10  import util.JenkinsReadYamlRule
    11  import util.JenkinsShellCallRule
    12  import util.JenkinsStepRule
    13  import util.Rules
    14  
    15  import com.sap.piper.Utils
    16  
    17  import static org.hamcrest.CoreMatchers.containsString
    18  import static org.hamcrest.CoreMatchers.hasItem
    19  import static org.hamcrest.CoreMatchers.is
    20  import static org.hamcrest.CoreMatchers.nullValue
    21  import static org.junit.Assert.assertEquals
    22  import static org.junit.Assert.assertFalse
    23  import static org.junit.Assert.assertNotNull
    24  import static org.junit.Assert.assertThat
    25  import static org.junit.Assert.assertTrue
    26  import static org.junit.Assert.fail
    27  
    28  class BuildExecuteTest extends BasePiperTest {
    29      private ExpectedException exception = ExpectedException.none()
    30      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    31      private JenkinsDockerExecuteRule dockerRule = new JenkinsDockerExecuteRule(this)
    32      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    33  
    34      @Rule
    35      public RuleChain rules = Rules
    36          .getCommonRules(this)
    37          .around(new JenkinsReadYamlRule(this))
    38          .around(exception)
    39          .around(shellCallRule)
    40          .around(dockerRule)
    41          .around(stepRule)
    42  
    43      def dockerMockArgs = [:]
    44      class DockerMock {
    45          DockerMock(name) {
    46              dockerMockArgs.name = name
    47          }
    48  
    49          def build(image, options) {
    50              return [image: image, options: options]
    51          }
    52      }
    53  
    54      @Before
    55      void init() {
    56          Utils.metaClass.echo = { def m -> }
    57      }
    58  
    59      @After
    60      public void tearDown() {
    61          Utils.metaClass = null
    62      }
    63  
    64      @Test
    65      void testDefaultError() {
    66          exception.expectMessage(containsString('buildTool not set and no dockerImage & dockerCommand provided'))
    67          stepRule.step.buildExecute(
    68              script: nullScript,
    69          )
    70      }
    71  
    72      @Test
    73      void testDefaultWithDockerImage() {
    74          stepRule.step.buildExecute(
    75              script: nullScript,
    76              dockerImage: 'path/to/myImage:tag',
    77              dockerCommand: 'myTestCommand'
    78          )
    79          assertThat(dockerRule.dockerParams.dockerImage, is('path/to/myImage:tag'))
    80          assertThat(shellCallRule.shell, hasItem('myTestCommand'))
    81      }
    82  
    83      @Test
    84      void inferBuildToolMaven() {
    85          boolean buildToolCalled = false
    86          helper.registerAllowedMethod('fileExists', [String.class], { s ->
    87              return s == "pom.xml"
    88          })
    89          helper.registerAllowedMethod('mavenBuild', [Map.class], { m ->
    90              buildToolCalled = true
    91              return
    92          })
    93  
    94          setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
    95          stepRule.step.buildExecute(
    96              script: nullScript,
    97          )
    98  
    99          assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
   100          assertEquals('maven', nullScript.commonPipelineEnvironment.getBuildTool())
   101          assertTrue(buildToolCalled)
   102      }
   103  
   104      @Test
   105      void inferBuildToolNpm() {
   106          boolean buildToolCalled = false
   107          helper.registerAllowedMethod('fileExists', [String.class], { s ->
   108              return s == "package.json"
   109          })
   110          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   111              buildToolCalled = true
   112              return
   113          })
   114  
   115          setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
   116          stepRule.step.buildExecute(
   117              script: nullScript,
   118          )
   119  
   120          assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
   121          assertEquals('npm', nullScript.commonPipelineEnvironment.getBuildTool())
   122          assertTrue(buildToolCalled)
   123      }
   124  
   125      @Test
   126      void inferBuildToolMTA() {
   127          boolean buildToolCalled = false
   128          helper.registerAllowedMethod('fileExists', [String.class], { s ->
   129              return s == "mta.yaml"
   130          })
   131          helper.registerAllowedMethod('mtaBuild', [Map.class], { m ->
   132              buildToolCalled = true
   133              return
   134          })
   135  
   136          setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
   137          stepRule.step.buildExecute(
   138              script: nullScript,
   139          )
   140  
   141          assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
   142          assertEquals('mta', nullScript.commonPipelineEnvironment.getBuildTool())
   143          assertTrue(buildToolCalled)
   144      }
   145  
   146      @Test
   147      void 'Do not infer build tool, do not set build tool, with docker dockerImage and dockerCommand, should run docker'() {
   148          helper.registerAllowedMethod('fileExists', [String.class], { s ->
   149              return s == "package.json"
   150          })
   151          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   152              fail("Called npmExecuteScripts which should not happen when no buildTool was defined but dockerImage and dockerCommand were.")
   153          })
   154  
   155          // Does nothing because feature toggle is not active
   156          setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: false])
   157  
   158          stepRule.step.buildExecute(
   159              script: nullScript,
   160              dockerImage: 'path/to/myImage:tag',
   161              dockerCommand: 'myTestCommand'
   162          )
   163  
   164          assertThat(dockerRule.dockerParams.dockerImage, is('path/to/myImage:tag'))
   165          assertThat(shellCallRule.shell, hasItem('myTestCommand'))
   166      }
   167  
   168      @Test
   169      void 'Do infer build tool, do not set build tool, with docker dockerImage and dockerCommand, should run npm'() {
   170          boolean npmCalled = false
   171          helper.registerAllowedMethod('fileExists', [String.class], { s ->
   172              return s == "package.json"
   173          })
   174          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   175              npmCalled = true
   176              return
   177          })
   178  
   179          setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
   180  
   181          stepRule.step.buildExecute(
   182              script: nullScript,
   183              dockerImage: 'path/to/myImage:tag',
   184              dockerCommand: 'myTestCommand'
   185          )
   186  
   187          assertTrue(npmCalled)
   188          assertEquals(0, shellCallRule.shell.size())
   189      }
   190  
   191      @Test
   192      void testMaven() {
   193          boolean buildToolCalled = false
   194          boolean installOptionSet = false
   195          helper.registerAllowedMethod('mavenBuild', [Map.class], { m ->
   196              buildToolCalled = true
   197              return
   198          })
   199          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   200              installOptionSet = m['install']
   201              return
   202          })
   203          helper.registerAllowedMethod('fileExists', [String.class], { s ->
   204              return s == 'package.json'
   205          })
   206          stepRule.step.buildExecute(
   207              script: nullScript,
   208              buildTool: 'maven',
   209          )
   210          assertTrue(buildToolCalled)
   211          assertTrue(installOptionSet)
   212      }
   213  
   214      @Test
   215      void testMta() {
   216          def buildToolCalled = false
   217          helper.registerAllowedMethod('mtaBuild', [Map.class], { m ->
   218              buildToolCalled = true
   219              return
   220          })
   221          stepRule.step.buildExecute(
   222              script: nullScript,
   223              buildTool: 'mta',
   224          )
   225          assertThat(buildToolCalled, is(true))
   226      }
   227  
   228      @Test
   229      void testNpm() {
   230          def buildToolCalled = false
   231          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   232              buildToolCalled = true
   233              return
   234          })
   235          stepRule.step.buildExecute(
   236              script: nullScript,
   237              buildTool: 'npm',
   238          )
   239          assertThat(buildToolCalled, is(true))
   240      }
   241  
   242      @Test
   243      void testNpmWithScripts() {
   244          boolean actualValue = false
   245          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   246              actualValue = (m['runScripts'][0] == 'foo' && m['runScripts'][1] == 'bar')
   247              return
   248          })
   249          stepRule.step.buildExecute(
   250              script: nullScript,
   251              buildTool: 'npm',
   252              npmRunScripts: ['foo', 'bar']
   253          )
   254          assertTrue(actualValue)
   255      }
   256  
   257      @Test
   258      void testNpmWithInstallFalse() {
   259          boolean actualValue = true
   260          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   261              actualValue = m['install']
   262              return
   263          })
   264          stepRule.step.buildExecute(
   265              script: nullScript,
   266              buildTool: 'npm',
   267              npmInstall: false
   268          )
   269          assertFalse(actualValue)
   270      }
   271  
   272      @Test
   273      void testNpmWithInstallTrue() {
   274          boolean actualValue = false
   275          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   276              actualValue = m['install']
   277              return
   278          })
   279          stepRule.step.buildExecute(
   280              script: nullScript,
   281              buildTool: 'npm',
   282              npmInstall: true
   283          )
   284          assertTrue(actualValue)
   285      }
   286  
   287      @Test
   288      void testDocker() {
   289          boolean buildToolCalled = false
   290          binding.setVariable('docker', new DockerMock('test'))
   291          def pushParams = [:]
   292          helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
   293              buildToolCalled = true
   294              return
   295          })
   296          stepRule.step.buildExecute(
   297              script: nullScript,
   298              buildTool: 'docker',
   299          )
   300          assertThat(buildToolCalled, is(true))
   301      }
   302  
   303      @Test
   304      void testDockerWithoutCNB() {
   305          boolean kanikoExecuteCalled = false
   306          boolean cnbBuildCalled = false
   307          binding.setVariable('docker', new DockerMock('test'))
   308          def pushParams = [:]
   309          helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
   310              kanikoExecuteCalled = true
   311              return
   312          })
   313          helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
   314              cnbBuildCalled = true
   315              return
   316          })
   317          stepRule.step.buildExecute(
   318                  script: nullScript,
   319                  buildTool: 'docker',
   320          )
   321          assertThat(cnbBuildCalled, is(false))
   322          assertThat(kanikoExecuteCalled, is(true))
   323      }
   324  
   325      @Test
   326      void testDockerWithCNB() {
   327          boolean kanikoExecuteCalled = false
   328          boolean cnbBuildCalled = false
   329          binding.setVariable('docker', new DockerMock('test'))
   330          def pushParams = [:]
   331          helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
   332              kanikoExecuteCalled = true
   333              return
   334          })
   335          helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
   336              cnbBuildCalled = true
   337              return
   338          })
   339          stepRule.step.buildExecute(
   340                  script: nullScript,
   341                  buildTool: 'docker',
   342                  cnbBuild: true
   343          )
   344          assertThat(cnbBuildCalled, is(true))
   345          assertThat(kanikoExecuteCalled, is(false))
   346      }
   347  
   348      @Test
   349      void testKaniko() {
   350          binding.setVariable('docker', new DockerMock('test'))
   351          def buildToolCalled = false
   352          helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
   353              buildToolCalled = true
   354              return
   355          })
   356          stepRule.step.buildExecute(
   357              script: nullScript,
   358              buildTool: 'kaniko',
   359          )
   360          assertThat(buildToolCalled, is(true))
   361      }
   362  
   363      @Test
   364      void testCnbBuildCalledWhenConfigured() {
   365          def cnbBuildCalled = false
   366          def npmExecuteScriptsCalled = false
   367          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   368              npmExecuteScriptsCalled = true
   369          })
   370          helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
   371              cnbBuildCalled = true
   372              return
   373          })
   374          assertThat(nullScript.commonPipelineEnvironment.getContainerProperty('buildpacks'), nullValue())
   375  
   376          stepRule.step.buildExecute(
   377              script: nullScript,
   378              buildTool: 'npm',
   379              cnbBuild: true
   380          )
   381  
   382          assertThat(npmExecuteScriptsCalled, is(true))
   383          assertThat(cnbBuildCalled, is(true))
   384      }
   385  
   386      @Test
   387      void testCnbBuildNotCalledWhenNotConfigured() {
   388          def cnbBuildCalled = false
   389          def npmExecuteScriptsCalled = false
   390          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   391              npmExecuteScriptsCalled = true
   392          })
   393          helper.registerAllowedMethod('cnbBuild', [Map.class], { m ->
   394              cnbBuildCalled = true
   395              return
   396          })
   397          stepRule.step.buildExecute(
   398              script: nullScript,
   399              buildTool: 'npm',
   400              cnbBuild: false
   401          )
   402          assertThat(npmExecuteScriptsCalled, is(true))
   403          assertThat(cnbBuildCalled, is(false))
   404      }
   405  
   406      @Test
   407      void testHelmExecuteCalledWhenConfigured() {
   408          def helmExecuteCalled = false
   409          helper.registerAllowedMethod('helmExecute', [Map.class], { m ->
   410              helmExecuteCalled = true
   411              return
   412          })
   413          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   414          })
   415  
   416          stepRule.step.buildExecute(
   417              script: nullScript,
   418              buildTool: 'npm',
   419              helmExecute: true
   420          )
   421  
   422          assertThat(helmExecuteCalled, is(true))
   423      }
   424  
   425      @Test
   426      void testHelmExecuteNotCalledWhenNotConfigured() {
   427          def helmExecuteCalled = false
   428          helper.registerAllowedMethod('helmExecute', [Map.class], { m ->
   429              helmExecuteCalled = true
   430              return
   431          })
   432          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
   433          })
   434          stepRule.step.buildExecute(
   435              script: nullScript,
   436              buildTool: 'npm',
   437              helmExecute: false
   438          )
   439  
   440          assertThat(helmExecuteCalled, is(false))
   441      }
   442  }