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

     1  package com.sap.piper
     2  
     3  import org.junit.Assert
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.RuleChain
     7  import util.BasePiperTest
     8  import util.JenkinsLoggingRule
     9  import util.Rules
    10  
    11  class DebugReportTest extends BasePiperTest {
    12  
    13      @Rule
    14      public RuleChain ruleChain = Rules.getCommonRules(this)
    15          .around(new JenkinsLoggingRule(this))
    16  
    17      @Test
    18      void testInitFromEnvironment() {
    19          Map env = createEnv()
    20          DebugReport.instance.initFromEnvironment(env)
    21  
    22          Assert.assertTrue(DebugReport.instance.environment.containsKey('build_details'))
    23          Assert.assertEquals('Kubernetes', DebugReport.instance.environment.get('environment'))
    24  
    25          Set<String> buildDetails = DebugReport.instance.environment.build_details as Set<String>
    26          Assert.assertTrue(buildDetails.size() > 0)
    27  
    28          boolean foundJenkinsVersion = false
    29          boolean foundJavaVersion = false
    30  
    31          for (String details in buildDetails) {
    32              if (details.contains(env.get('JENKINS_VERSION') as String))
    33                  foundJenkinsVersion = true
    34              if (details.contains(env.get('JAVA_VERSION') as String))
    35                  foundJavaVersion = true
    36          }
    37          Assert.assertTrue(foundJenkinsVersion)
    38          Assert.assertTrue(foundJavaVersion)
    39      }
    40  
    41      @Test
    42      void testLogOutput() {
    43          DebugReport.instance.initFromEnvironment(createEnv())
    44          DebugReport.instance.setGitRepoInfo('GIT_URL' : 'git://url', 'GIT_LOCAL_BRANCH' : 'some-branch')
    45  
    46          String debugReport = DebugReport.instance.generateReport(mockScript(), false)
    47  
    48          Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
    49          Assert.assertTrue(debugReport.contains('## Local Extensions'))
    50          Assert.assertTrue(debugReport.contains('#### Environment\n' +
    51              '`Kubernetes`'))
    52          Assert.assertFalse(debugReport.contains('Repository | Branch'))
    53          Assert.assertFalse(debugReport.contains('some-branch'))
    54      }
    55  
    56      @Test
    57      void testLogOutputConfidential() {
    58          DebugReport.instance.initFromEnvironment(createEnv())
    59          DebugReport.instance.setGitRepoInfo('GIT_URL' : 'git://url', 'GIT_LOCAL_BRANCH' : 'some-branch')
    60  
    61          String debugReport = DebugReport.instance.generateReport(mockScript(), true)
    62  
    63          Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
    64          Assert.assertTrue(debugReport.contains('## Local Extensions'))
    65          Assert.assertTrue(debugReport.contains('#### Environment\n' +
    66              '`Kubernetes`'))
    67          Assert.assertTrue(debugReport.contains('Repository | Branch'))
    68          Assert.assertTrue(debugReport.contains('some-branch'))
    69      }
    70  
    71      private Script mockScript() {
    72          helper.registerAllowedMethod("libraryResource", [String.class], { path ->
    73  
    74              File resource = new File(new File('resources'), path)
    75              if (resource.exists()) {
    76                  return resource.getText()
    77              }
    78  
    79              return ''
    80          })
    81  
    82          return nullScript
    83      }
    84  
    85      private static Map createEnv() {
    86          Map env = [:]
    87          env.put('JENKINS_VERSION', '42')
    88          env.put('JAVA_VERSION', '8')
    89          env.put('ON_K8S', 'true')
    90          return env
    91      }
    92  }