You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	feat(getDefaults): add parameter for CRD-style config (#3736)
* export LoadConditionsV1() in config pkg * getDefaults: read stage conditions as PipelineDefinitionV1 * add getDefaults useV1 tests * add comment (pointed out by code climate analysis) * small addition to comment (from previous commit) Co-authored-by: I557621 <jordi.van.liempt@sap.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
		| @@ -17,6 +17,7 @@ type defaultsCommandOptions struct { | ||||
| 	output        string //output format of default configs, currently only YAML | ||||
| 	outputFile    string //if set: path to file where the output should be written to | ||||
| 	defaultsFiles []string | ||||
| 	useV1         bool | ||||
| 	openFile      func(s string, t map[string]string) (io.ReadCloser, error) | ||||
| } | ||||
|  | ||||
| @@ -77,15 +78,28 @@ func getDefaults() ([]map[string]string, error) { | ||||
| 			return yamlDefaults, errors.Wrapf(err, "defaults: retrieving defaults file failed: '%v'", f) | ||||
| 		} | ||||
| 		if err == nil { | ||||
| 			var c config.Config | ||||
| 			c.ReadConfig(fc) | ||||
| 			var yamlContent string | ||||
|  | ||||
| 			yaml, err := config.GetYAML(c) | ||||
| 			if err != nil { | ||||
| 				return yamlDefaults, errors.Wrapf(err, "defaults: could not marshal YAML default file: '%v", f) | ||||
| 			if !defaultsOptions.useV1 { | ||||
| 				var c config.Config | ||||
| 				c.ReadConfig(fc) | ||||
|  | ||||
| 				yamlContent, err = config.GetYAML(c) | ||||
| 				if err != nil { | ||||
| 					return yamlDefaults, errors.Wrapf(err, "defaults: could not marshal YAML default file: '%v", f) | ||||
| 				} | ||||
| 			} else { | ||||
| 				var rc config.RunConfigV1 | ||||
| 				rc.StageConfigFile = fc | ||||
| 				rc.LoadConditionsV1() | ||||
|  | ||||
| 				yamlContent, err = config.GetYAML(rc.PipelineConfig) | ||||
| 				if err != nil { | ||||
| 					return yamlDefaults, errors.Wrapf(err, "defaults: could not marshal YAML default file: '%v", f) | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			yamlDefaults = append(yamlDefaults, map[string]string{"content": yaml, "filepath": f}) | ||||
| 			yamlDefaults = append(yamlDefaults, map[string]string{"content": yamlContent, "filepath": f}) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -128,6 +142,6 @@ func addDefaultsFlags(cmd *cobra.Command) { | ||||
| 	cmd.Flags().StringVar(&defaultsOptions.output, "output", "yaml", "Defines the format of the configs embedded into a JSON object") | ||||
| 	cmd.Flags().StringVar(&defaultsOptions.outputFile, "outputFile", "", "Defines the output filename") | ||||
| 	cmd.Flags().StringArrayVar(&defaultsOptions.defaultsFiles, "defaultsFile", []string{}, "Defines the input defaults file(s)") | ||||
|  | ||||
| 	cmd.Flags().BoolVar(&defaultsOptions.useV1, "useV1", false, "Input files are CRD-style stage configuration") | ||||
| 	cmd.MarkFlagRequired("defaultsFile") | ||||
| } | ||||
|   | ||||
| @@ -11,6 +11,29 @@ import ( | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| var stageConditionsExample string = `#Piper general purpose pipeline stage configuration including conditions | ||||
| apiVersion: project-piper.io/v1 | ||||
| kind: PipelineDefinition | ||||
| metadata: | ||||
|   name: sap-piper.general.purpose.pipeline | ||||
|   displayName: Piper general purpose pipeline | ||||
|   description: |- | ||||
|     This is a multiline | ||||
|     test description | ||||
| spec: | ||||
|   stages: | ||||
| # Init stage | ||||
|   - name: init | ||||
|     displayName: Init | ||||
|     description: |- | ||||
|       Test description | ||||
|     steps: | ||||
|     - name: getConfig | ||||
|       description: Read pipeline stage configuration.` | ||||
|  | ||||
| var stageConditionsExpected string = `"apiVersion: project-piper.io/v1\nkind: PipelineDefinition\nmetadata:\n  description: |-\n    This is a multiline\n    test description\n  displayName: Piper general purpose pipeline\n  name: sap-piper.general.purpose.pipeline\nspec:\n` + | ||||
| 	`  stages:\n  - description: Test description\n    displayName: Init\n    name: init\n    steps:\n    - description: Read pipeline stage configuration.\n      name: getConfig\n"` | ||||
|  | ||||
| func defaultsOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) { | ||||
| 	var r string | ||||
| 	switch name { | ||||
| @@ -18,6 +41,8 @@ func defaultsOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, | ||||
| 		r = "default1" | ||||
| 	case "TestAddCustomDefaults_default2": | ||||
| 		r = "default3" | ||||
| 	case "stage_conditions.yaml": | ||||
| 		r = stageConditionsExample | ||||
| 	default: | ||||
| 		r = "" | ||||
| 	} | ||||
| @@ -45,7 +70,7 @@ func TestDefaultsCommand(t *testing.T) { | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Optional flags", func(t *testing.T) { | ||||
| 		exp := []string{"output", "outputFile"} | ||||
| 		exp := []string{"output", "outputFile", "useV1"} | ||||
| 		assert.Equal(t, exp, gotOpt, "optional flags incorrect") | ||||
| 	}) | ||||
|  | ||||
| @@ -62,6 +87,7 @@ func TestGenerateDefaults(t *testing.T) { | ||||
| 	testParams := []struct { | ||||
| 		name          string | ||||
| 		defaultsFiles []string | ||||
| 		useV1         bool | ||||
| 		expected      string | ||||
| 	}{ | ||||
| 		{ | ||||
| @@ -75,6 +101,19 @@ func TestGenerateDefaults(t *testing.T) { | ||||
| 			expected: `[{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test1"},` + | ||||
| 				`{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test2"}]`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:          "Single file + useV1", | ||||
| 			defaultsFiles: []string{"stage_conditions.yaml"}, | ||||
| 			useV1:         true, | ||||
| 			expected:      `{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"}`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:          "Multiple files + useV1", | ||||
| 			defaultsFiles: []string{"stage_conditions.yaml", "stage_conditions.yaml"}, | ||||
| 			useV1:         true, | ||||
| 			expected: `[{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"},` + | ||||
| 				`{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"}]`, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	utils := newGetDefaultsUtilsUtils() | ||||
| @@ -83,6 +122,7 @@ func TestGenerateDefaults(t *testing.T) { | ||||
| 	for _, test := range testParams { | ||||
| 		t.Run(test.name, func(t *testing.T) { | ||||
| 			defaultsOptions.defaultsFiles = test.defaultsFiles | ||||
| 			defaultsOptions.useV1 = test.useV1 | ||||
| 			result, _ := generateDefaults(utils) | ||||
| 			assert.Equal(t, test.expected, string(result)) | ||||
| 		}) | ||||
|   | ||||
| @@ -81,7 +81,7 @@ func (r *RunConfigV1) InitRunConfigV1(config *Config, filters map[string]StepFil | ||||
| 	secrets map[string][]StepSecrets, stepAliases map[string][]Alias, utils piperutils.FileUtils, envRootPath string) error { | ||||
|  | ||||
| 	if len(r.PipelineConfig.Spec.Stages) == 0 { | ||||
| 		if err := r.loadConditionsV1(); err != nil { | ||||
| 		if err := r.LoadConditionsV1(); err != nil { | ||||
| 			return fmt.Errorf("failed to load pipeline run conditions: %w", err) | ||||
| 		} | ||||
| 	} | ||||
| @@ -153,7 +153,8 @@ func (r *RunConfig) loadConditions() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *RunConfigV1) loadConditionsV1() error { | ||||
| // LoadConditionsV1 loads stage conditions (in CRD-style) into PipelineConfig | ||||
| func (r *RunConfigV1) LoadConditionsV1() error { | ||||
| 	defer r.StageConfigFile.Close() | ||||
| 	content, err := ioutil.ReadAll(r.StageConfigFile) | ||||
| 	if err != nil { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user