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

     1  package com.sap.piper.k8s
     2  
     3  import com.sap.piper.DefaultValueCache
     4  import org.junit.Before
     5  import org.junit.Rule
     6  import org.junit.Test
     7  import org.junit.rules.RuleChain
     8  import util.*
     9  
    10  import static org.hamcrest.Matchers.hasItem
    11  import static org.junit.Assert.*
    12  
    13  class ContainerMapTest extends BasePiperTest {
    14  
    15      String exampleConfigYaml = """
    16  # Mapping of Go step names to their YAML metadata resource file
    17  stepMetadata:
    18    artifactPrepareVersion: artifactPrepareVersion.yaml
    19  containerMaps:
    20    init:
    21      - artifactPrepareVersion
    22      - mavenExecute
    23  """
    24  
    25      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    26  
    27      @Rule
    28      public RuleChain ruleChain = Rules
    29          .getCommonRules(this)
    30          .around(new JenkinsReadYamlRule(this))
    31          .around(new JenkinsPiperExecuteBinRule(this))
    32          .around(new JenkinsWriteFileRule(this))
    33          .around(new JenkinsReadJsonRule(this))
    34          .around(shellCallRule)
    35  
    36      private List envs
    37  
    38      @Before
    39      void init() {
    40          helper.registerAllowedMethod('libraryResource', [String.class], {
    41              return exampleConfigYaml
    42          })
    43  
    44          helper.registerAllowedMethod('withEnv', [List.class, Closure.class], { List envs, Closure body ->
    45              this.envs = envs.collect {it.toString()}
    46              body()
    47          })
    48  
    49          DefaultValueCache.createInstance([
    50              steps: [
    51                  mavenExecute: [
    52                      dockerImage: 'maven:3.5-jdk-8-alpine'
    53                  ]
    54              ]
    55          ])
    56  
    57          shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/artifactPrepareVersion.yaml\'', '{"dockerImage":"artifact-image"}')
    58      }
    59  
    60      @Test
    61      void testIfObjectCreated() {
    62          assertNotNull(ContainerMap.instance)
    63      }
    64  
    65      @Test
    66      void testSetMap() {
    67          ContainerMap.instance.setMap(['testpod': ['maven:3.5-jdk-8-alpine': 'mavenexec']])
    68          assertEquals(['testpod': ['maven:3.5-jdk-8-alpine': 'mavenexec']],ContainerMap.instance.getMap())
    69      }
    70  
    71      @Test
    72      void testGetMap() {
    73          assertNotNull(ContainerMap.instance.getMap())
    74      }
    75  
    76      @Test
    77      void testInitFromResource(){
    78          ContainerMap.instance.initFromResource(nullScript, 'containersMapYaml', 'maven', utils)
    79          assertThat(envs, hasItem('STAGE_NAME=init'))
    80          assertThat(envs, hasItem('PIPER_parametersJSON={"buildTool":"maven"}'))
    81  
    82          assertEquals(['init': ['artifact-image':'artifactprepareversion', 'maven:3.5-jdk-8-alpine': 'mavenexecute']],ContainerMap.instance.getMap())
    83      }
    84  }