github.com/SAP/jenkins-library@v1.362.0/cmd/checkIfStepActive_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  	"os/exec"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/spf13/cobra"
    14  	flag "github.com/spf13/pflag"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func checkStepActiveOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
    19  	var fileContent string
    20  	switch name {
    21  	case ".pipeline/defaults.yaml":
    22  		fileContent = `
    23  general:
    24  stages:
    25  steps:`
    26  	case "stage-config.yml":
    27  		fileContent = `
    28  spec:
    29    stages:
    30      - name: testStage
    31        displayName: testStage
    32        steps:
    33          - name: testStep
    34            conditions:
    35              - configKey: testConfig`
    36  	case ".pipeline/config.yml":
    37  		fileContent = `
    38  steps: 
    39    testStep: 
    40      testConfig: 'testValue'`
    41  	default:
    42  		fileContent = ""
    43  	}
    44  	return io.NopCloser(strings.NewReader(fileContent)), nil
    45  }
    46  
    47  func checkStepActiveFileExistsMock(filename string) (bool, error) {
    48  	switch filename {
    49  	case ".pipeline/config.yml":
    50  		return true, nil
    51  	default:
    52  		return false, nil
    53  	}
    54  }
    55  
    56  func TestCheckStepActiveCommand(t *testing.T) {
    57  	cmd := CheckStepActiveCommand()
    58  
    59  	gotReq := []string{}
    60  	gotOpt := []string{}
    61  
    62  	cmd.Flags().VisitAll(func(pflag *flag.Flag) {
    63  		annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
    64  		if found && annotations[0] == "true" {
    65  			gotReq = append(gotReq, pflag.Name)
    66  		} else {
    67  			gotOpt = append(gotOpt, pflag.Name)
    68  		}
    69  	})
    70  
    71  	t.Run("Required flags", func(t *testing.T) {
    72  		exp := []string{"step"}
    73  		assert.Equal(t, exp, gotReq, "required flags incorrect")
    74  	})
    75  
    76  	t.Run("Optional flags", func(t *testing.T) {
    77  		exp := []string{"stage", "stageConfig", "stageOutputFile", "stepOutputFile", "useV1"}
    78  		assert.Equal(t, exp, gotOpt, "optional flags incorrect")
    79  	})
    80  
    81  	t.Run("Run", func(t *testing.T) {
    82  		t.Run("Success case - set stage and stageName parameters", func(t *testing.T) {
    83  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
    84  			checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
    85  			checkStepActiveOptions.stageName = "testStage"
    86  			checkStepActiveOptions.stepName = "testStep"
    87  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
    88  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
    89  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
    90  			GeneralConfig.StageName = "testStage1"
    91  			cmd.Run(cmd, []string{})
    92  		})
    93  		t.Run("Success case - set only stage parameter", func(t *testing.T) {
    94  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
    95  			checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
    96  			checkStepActiveOptions.stageName = "testStage"
    97  			checkStepActiveOptions.stepName = "testStep"
    98  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
    99  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
   100  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
   101  			cmd.Run(cmd, []string{})
   102  		})
   103  		t.Run("Success case - set only stageName parameter", func(t *testing.T) {
   104  			checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
   105  			checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
   106  			checkStepActiveOptions.stepName = "testStep"
   107  			checkStepActiveOptions.stageConfigFile = "stage-config.yml"
   108  			GeneralConfig.CustomConfig = ".pipeline/config.yml"
   109  			GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
   110  			GeneralConfig.StageName = "testStage"
   111  			cmd.Run(cmd, []string{})
   112  		})
   113  	})
   114  }
   115  func TestFailIfNoConfigFound(t *testing.T) {
   116  	if os.Getenv("TEST_FAIL_IF_NO_CONFIG_FOUND") == "1" {
   117  		cmd := CheckStepActiveCommand()
   118  
   119  		gotReq := []string{}
   120  		gotOpt := []string{}
   121  
   122  		cmd.Flags().VisitAll(func(pflag *flag.Flag) {
   123  			annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
   124  			if found && annotations[0] == "true" {
   125  				gotReq = append(gotReq, pflag.Name)
   126  			} else {
   127  				gotOpt = append(gotOpt, pflag.Name)
   128  			}
   129  		})
   130  		checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
   131  		checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
   132  		checkStepActiveOptions.stageName = "testStage"
   133  		checkStepActiveOptions.stepName = "testStep"
   134  		checkStepActiveOptions.stageConfigFile = "stage-config.yml"
   135  		GeneralConfig.CustomConfig = ".pipeline/unknown.yml"
   136  		GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
   137  		GeneralConfig.StageName = "testStage1"
   138  		cmd.Run(cmd, []string{})
   139  		return
   140  	}
   141  	cmd := exec.Command(os.Args[0], "-test.run=TestFailIfNoConfigFound")
   142  	cmd.Env = append(os.Environ(), "TEST_FAIL_IF_NO_CONFIG_FOUND=1")
   143  	err := cmd.Run()
   144  	if e, ok := err.(*exec.ExitError); ok && !e.Success() {
   145  		t.Log(e.Error())
   146  		t.Log("Stderr: ", string(e.Stderr))
   147  		return
   148  	}
   149  	t.Fatalf("process ran with err %v, want exit status 1", err)
   150  }