1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-10-30 23:57:50 +02:00

fix(general purpose pipeline): enable checkIfStepActive to handle use case with no .pipeline/config.yaml (#3993)

* Fix for making config.yaml not mandatory in checkifstepactive

* Create customConfig.yaml

* Revert resource

* Remove nil for customConfig

* Fix tests
This commit is contained in:
Ashly Mathew
2022-09-05 10:20:32 +02:00
committed by GitHub
parent 72e257e83e
commit c7342cfb0e
2 changed files with 64 additions and 4 deletions

View File

@@ -16,6 +16,7 @@ import (
type checkStepActiveCommandOptions struct {
openFile func(s string, t map[string]string) (io.ReadCloser, error)
fileExists func(filename string) (bool, error)
stageConfigFile string
stepName string
stageName string
@@ -29,6 +30,7 @@ var checkStepActiveOptions checkStepActiveCommandOptions
// CheckStepActiveCommand is the entry command for checking if a step is active in a defined stage
func CheckStepActiveCommand() *cobra.Command {
checkStepActiveOptions.openFile = config.OpenPiperFile
checkStepActiveOptions.fileExists = piperutils.FileExists
var checkStepActiveCmd = &cobra.Command{
Use: "checkIfStepActive",
Short: "Checks if a step is active in a defined stage.",
@@ -152,11 +154,19 @@ func addCheckStepActiveFlags(cmd *cobra.Command) {
func initializeConfig(pConfig *config.Config) (*config.Config, error) {
projectConfigFile := getProjectConfigFile(GeneralConfig.CustomConfig)
customConfig, err := checkStepActiveOptions.openFile(projectConfigFile, GeneralConfig.GitHubAccessTokens)
if err != nil {
return nil, errors.Wrapf(err, "config: open configuration file '%v' failed", projectConfigFile)
var customConfig io.ReadCloser
var err error
//accept that config file cannot be loaded as its not mandatory here
if exists, err := checkStepActiveOptions.fileExists(projectConfigFile); exists {
log.Entry().Infof("Project config: '%s'", projectConfigFile)
customConfig, err = checkStepActiveOptions.openFile(projectConfigFile, GeneralConfig.GitHubAccessTokens)
if err != nil {
return nil, errors.Wrapf(err, "config: open configuration file '%v' failed", projectConfigFile)
}
defer customConfig.Close()
} else {
log.Entry().Infof("Project config: NONE ('%s' does not exist)", projectConfigFile)
}
defer customConfig.Close()
defaultConfig := []io.ReadCloser{}
for _, f := range GeneralConfig.DefaultConfig {

View File

@@ -3,6 +3,8 @@ package cmd
import (
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
@@ -37,6 +39,15 @@ steps:
return ioutil.NopCloser(strings.NewReader(fileContent)), nil
}
func checkStepActiveFileExistsMock(filename string) (bool, error) {
switch filename {
case ".pipeline/config.yml":
return true, nil
default:
return false, nil
}
}
func TestCheckStepActiveCommand(t *testing.T) {
cmd := CheckStepActiveCommand()
@@ -65,6 +76,7 @@ func TestCheckStepActiveCommand(t *testing.T) {
t.Run("Run", func(t *testing.T) {
t.Run("Success case - set stage and stageName parameters", func(t *testing.T) {
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
checkStepActiveOptions.stageName = "testStage"
checkStepActiveOptions.stepName = "testStep"
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
@@ -75,6 +87,7 @@ func TestCheckStepActiveCommand(t *testing.T) {
})
t.Run("Success case - set only stage parameter", func(t *testing.T) {
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
checkStepActiveOptions.stageName = "testStage"
checkStepActiveOptions.stepName = "testStep"
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
@@ -84,6 +97,7 @@ func TestCheckStepActiveCommand(t *testing.T) {
})
t.Run("Success case - set only stageName parameter", func(t *testing.T) {
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
checkStepActiveOptions.stepName = "testStep"
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
GeneralConfig.CustomConfig = ".pipeline/config.yml"
@@ -93,3 +107,39 @@ func TestCheckStepActiveCommand(t *testing.T) {
})
})
}
func TestFailIfNoConfigFound(t *testing.T) {
if os.Getenv("TEST_FAIL_IF_NO_CONFIG_FOUND") == "1" {
cmd := CheckStepActiveCommand()
gotReq := []string{}
gotOpt := []string{}
cmd.Flags().VisitAll(func(pflag *flag.Flag) {
annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag]
if found && annotations[0] == "true" {
gotReq = append(gotReq, pflag.Name)
} else {
gotOpt = append(gotOpt, pflag.Name)
}
})
checkStepActiveOptions.openFile = checkStepActiveOpenFileMock
checkStepActiveOptions.fileExists = checkStepActiveFileExistsMock
checkStepActiveOptions.stageName = "testStage"
checkStepActiveOptions.stepName = "testStep"
checkStepActiveOptions.stageConfigFile = "stage-config.yml"
GeneralConfig.CustomConfig = ".pipeline/unknown.yml"
GeneralConfig.DefaultConfig = []string{".pipeline/defaults.yaml"}
GeneralConfig.StageName = "testStage1"
cmd.Run(cmd, []string{})
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailIfNoConfigFound")
cmd.Env = append(os.Environ(), "TEST_FAIL_IF_NO_CONFIG_FOUND=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
t.Log(e.Error())
t.Log("Stderr: ", string(e.Stderr))
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}