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

     1  import static org.hamcrest.Matchers.containsString
     2  import static org.hamcrest.Matchers.hasItem
     3  import static org.hamcrest.Matchers.allOf
     4  
     5  import org.junit.Before
     6  import org.junit.Rule
     7  import org.junit.Test
     8  import org.junit.rules.RuleChain
     9  import org.junit.rules.ExpectedException
    10  import static org.junit.Assert.assertThat
    11  
    12  import util.BasePiperTest
    13  import util.JenkinsDockerExecuteRule
    14  import util.JenkinsShellCallRule
    15  import util.JenkinsReadYamlRule
    16  import util.JenkinsStepRule
    17  import util.JenkinsLoggingRule
    18  import util.Rules
    19  
    20  class SonarExecuteScanTest extends BasePiperTest {
    21      private ExpectedException thrown = ExpectedException.none()
    22      private JenkinsReadYamlRule readYamlRule = new JenkinsReadYamlRule(this)
    23      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    24      private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
    25      private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
    26      private JenkinsDockerExecuteRule jedr = new JenkinsDockerExecuteRule(this)
    27  
    28      @Rule
    29      public RuleChain rules = Rules
    30          .getCommonRules(this)
    31          .around(readYamlRule)
    32          .around(thrown)
    33          .around(jedr)
    34          .around(jscr)
    35          .around(jlr)
    36          .around(jsr)
    37  
    38      def sonarInstance
    39  
    40      @Before
    41      void init() throws Exception {
    42          sonarInstance = null
    43          helper.registerAllowedMethod("withSonarQubeEnv", [String.class, Closure.class], { string, closure ->
    44              sonarInstance = string
    45              return closure()
    46          })
    47          helper.registerAllowedMethod("unstash", [String.class], { stashInput -> return [] })
    48          helper.registerAllowedMethod("fileExists", [String.class], { file -> return file })
    49          helper.registerAllowedMethod('string', [Map], { m -> m })
    50          helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
    51              try {
    52                  binding.setProperty(l[0].variable, 'TOKEN_' + l[0].credentialsId)
    53                  c()
    54              } finally {
    55                  binding.setProperty(l[0].variable, null)
    56              }
    57          })
    58          helper.registerAllowedMethod('withEnv', [List.class, Closure.class], { List envVars, Closure body ->
    59              body()
    60          })
    61          nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3-20180101')
    62      }
    63  
    64      @Test
    65      void testWithCustomTlsCertificates() throws Exception {
    66          jsr.step.sonarExecuteScan.loadCertificates(
    67              customTlsCertificateLinks: [
    68                  'http://url.to/my.cert'
    69              ]
    70          )
    71          // asserts
    72          assertThat(jscr.shell, allOf(
    73              hasItem(containsString('wget --directory-prefix .certificates/ --no-verbose http://url.to/my.cert')),
    74              hasItem(containsString('keytool -import -noprompt -storepass changeit -keystore .certificates/cacerts -alias \'my.cert\' -file \'.certificates/my.cert\''))
    75          ))
    76          assertJobStatusSuccess()
    77      }
    78  }