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

     1  import hudson.AbortException
     2  import org.junit.Before
     3  import org.junit.Rule
     4  import org.junit.Test
     5  import org.junit.rules.ExpectedException
     6  import org.junit.rules.RuleChain
     7  import util.*
     8  
     9  import static org.hamcrest.Matchers.*
    10  import static org.junit.Assert.assertThat
    11  
    12  class SeleniumExecuteTestsTest extends BasePiperTest {
    13      private ExpectedException thrown = ExpectedException.none()
    14      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    15      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    16      private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
    17      private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
    18  
    19      private List credentials = []
    20  
    21      @Rule
    22      public RuleChain rules = Rules
    23          .getCommonRules(this)
    24          .around(new JenkinsReadYamlRule(this))
    25          .around(thrown)
    26          .around(dockerExecuteRule)
    27          .around(stepRule) // needs to be activated after dockerExecuteRule, otherwise executeDocker is not mocked
    28  
    29      boolean bodyExecuted = false
    30  
    31      def gitMap
    32  
    33      @Before
    34      void init() throws Exception {
    35          credentials = []
    36          bodyExecuted = false
    37          helper.registerAllowedMethod('stash', [String.class], null)
    38          helper.registerAllowedMethod('git', [Map.class], {m ->
    39              gitMap = m
    40          })
    41          helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
    42          helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
    43              l.each {m ->
    44                  credentials.add(m)
    45                  if (m.credentialsId == 'MyCredentialId') {
    46                      binding.setProperty('PIPER_SELENIUM_HUB_USER', 'seleniumUser')
    47                      binding.setProperty('PIPER_SELENIUM_HUB_PASSWORD', '********')
    48                  }
    49              }
    50              try {
    51                  c()
    52              } finally {
    53                  binding.setProperty('PIPER_SELENIUM_HUB_USER', null)
    54                  binding.setProperty('PIPER_SELENIUM_HUB_PASSWORD', null)
    55              }
    56          })
    57      }
    58  
    59      @Test
    60      void testExecuteSeleniumDefault() {
    61          def expectedDefaultEnvVars = [
    62              'PIPER_SELENIUM_HOSTNAME': 'npm',
    63              'PIPER_SELENIUM_WEBDRIVER_HOSTNAME': 'selenium',
    64              'PIPER_SELENIUM_WEBDRIVER_PORT': '4444'
    65          ]
    66  
    67          stepRule.step.seleniumExecuteTests(
    68              script: nullScript,
    69              juStabUtils: utils
    70          ) {
    71              bodyExecuted = true
    72          }
    73          assertThat(bodyExecuted, is(true))
    74          assertThat(dockerExecuteRule.dockerParams.containerPortMappings, is(['selenium/standalone-chrome': [[containerPort: 4444, hostPort: 4444]]]))
    75          assertThat(dockerExecuteRule.dockerParams.dockerImage, is('node:lts-buster'))
    76          assertThat(dockerExecuteRule.dockerParams.dockerName, is('npm'))
    77          assertThat(dockerExecuteRule.dockerParams.dockerWorkspace, is('/home/node'))
    78          assertThat(dockerExecuteRule.dockerParams.sidecarEnvVars, is(null))
    79          assertThat(dockerExecuteRule.dockerParams.sidecarImage, is('selenium/standalone-chrome'))
    80          assertThat(dockerExecuteRule.dockerParams.sidecarName, is('selenium'))
    81          assertThat(dockerExecuteRule.dockerParams.sidecarVolumeBind, is(['/dev/shm': '/dev/shm']))
    82          expectedDefaultEnvVars.each { key, value ->
    83              assert dockerExecuteRule.dockerParams.dockerEnvVars[key] == value
    84          }
    85      }
    86  
    87      @Test
    88      void testNoNullPointerExceptionWithEmptyContainerPortMapping() {
    89          nullScript.commonPipelineEnvironment.configuration = [steps:[seleniumExecuteTests:[
    90              containerPortMappings: []
    91          ]]]
    92  
    93          stepRule.step.seleniumExecuteTests(
    94              script: nullScript,
    95              juStabUtils: utils
    96          ) {
    97              bodyExecuted = true
    98          }
    99          assertThat(bodyExecuted, is(true))
   100          assert dockerExecuteRule.dockerParams.dockerEnvVars.PIPER_SELENIUM_WEBDRIVER_PORT == null
   101      }
   102  
   103      @Test
   104      void testNoNullPointerExceptionWithUnrelatedSidecarImageAndContainerPortMapping() {
   105          nullScript.commonPipelineEnvironment.configuration = [steps:[seleniumExecuteTests:[
   106              sidecarImage: 'myCustomImage',
   107              containerPortMappings: [
   108                  'someImageOtherThanMyCustomImage': [
   109                      [containerPort: 5555, hostPort: 5555]
   110                  ]
   111              ]
   112          ]]]
   113          stepRule.step.seleniumExecuteTests(
   114              script: nullScript,
   115              juStabUtils: utils
   116          ) {
   117              bodyExecuted = true
   118          }
   119          assertThat(bodyExecuted, is(true))
   120          assert dockerExecuteRule.dockerParams.dockerEnvVars.PIPER_SELENIUM_WEBDRIVER_PORT == null
   121      }
   122  
   123      @Test
   124      void testDockerFromCustomStepConfiguration() {
   125  
   126          def expectedImage = 'image:test'
   127          def expectedEnvVars = ['env1': 'value1', 'env2': 'value2']
   128          def expectedOptions = '--opt1=val1 --opt2=val2 --opt3'
   129          def expectedWorkspace = '/path/to/workspace'
   130  
   131          nullScript.commonPipelineEnvironment.configuration = [steps:[seleniumExecuteTests:[
   132              dockerImage: expectedImage,
   133              dockerOptions: expectedOptions,
   134              dockerEnvVars: expectedEnvVars,
   135              dockerWorkspace: expectedWorkspace
   136              ]]]
   137  
   138          stepRule.step.seleniumExecuteTests(
   139              script: nullScript,
   140              juStabUtils: utils
   141          ) {
   142          }
   143  
   144          assert expectedImage == dockerExecuteRule.dockerParams.dockerImage
   145          assert expectedOptions == dockerExecuteRule.dockerParams.dockerOptions
   146          assert expectedWorkspace == dockerExecuteRule.dockerParams.dockerWorkspace
   147          expectedEnvVars.each { key, value ->
   148              assert dockerExecuteRule.dockerParams.dockerEnvVars[key] == value
   149          }
   150      }
   151  
   152      @Test
   153      void testExecuteSeleniumCustomBuildTool() {
   154          stepRule.step.seleniumExecuteTests(
   155              script: nullScript,
   156              buildTool: 'maven',
   157              juStabUtils: utils
   158          ) {
   159              bodyExecuted = true
   160          }
   161          assertThat(bodyExecuted, is(true))
   162          assertThat(dockerExecuteRule.dockerParams.dockerImage, is('maven:3.5-jdk-8'))
   163          assertThat(dockerExecuteRule.dockerParams.dockerName, is('maven'))
   164          assertThat(dockerExecuteRule.dockerParams.dockerWorkspace, is(''))
   165      }
   166      @Test
   167      void testExecuteSeleniumError() {
   168          thrown.expectMessage('Error occured')
   169          stepRule.step.seleniumExecuteTests(
   170              script: nullScript,
   171              juStabUtils: utils
   172          ) {
   173              throw new AbortException('Error occured')
   174          }
   175      }
   176  
   177      @Test
   178      void testExecuteSeleniumIgnoreError() {
   179          stepRule.step.seleniumExecuteTests(
   180              script: nullScript,
   181              failOnError: false,
   182              juStabUtils: utils
   183          ) {
   184              bodyExecuted = true
   185              throw new AbortException('Error occured')
   186          }
   187          assertThat(bodyExecuted, is(true))
   188      }
   189  
   190      @Test
   191      void testExecuteSeleniumCustomRepo() {
   192          stepRule.step.seleniumExecuteTests(
   193              script: nullScript,
   194              gitBranch: 'test',
   195              gitSshKeyCredentialsId: 'testCredentials',
   196              juStabUtils: utils,
   197              testRepository: 'git@test/test.git'
   198          ) {
   199              bodyExecuted = true
   200          }
   201          assertThat(bodyExecuted, is(true))
   202          assertThat(gitMap, hasEntry('branch', 'test'))
   203          assertThat(gitMap, hasEntry('credentialsId', 'testCredentials'))
   204          assertThat(gitMap, hasEntry('url', 'git@test/test.git'))
   205      }
   206  
   207      @Test
   208      void testSeleniumHubCredentials() {
   209          nullScript.commonPipelineEnvironment.configuration = [steps:[seleniumExecuteTests:[
   210              seleniumHubCredentialsId: 'MyCredentialId'
   211          ]]]
   212  
   213          stepRule.step.seleniumExecuteTests(
   214              script: nullScript,
   215              juStabUtils: utils
   216          ) {
   217              bodyExecuted = true
   218          }
   219  
   220          assertThat(bodyExecuted, is(true))
   221          assertThat(credentials.size(), is(1))
   222      }
   223  }