2019-10-22 15:41:27 +02:00
package config
import (
"errors"
"fmt"
"io"
"io/ioutil"
2020-01-15 13:16:25 +02:00
"os"
"path/filepath"
2019-10-22 15:41:27 +02:00
"strings"
"testing"
2020-01-15 13:16:25 +02:00
"github.com/SAP/jenkins-library/pkg/piperenv"
2019-10-22 15:41:27 +02:00
"github.com/stretchr/testify/assert"
)
type errReadCloser int
func ( errReadCloser ) Read ( p [ ] byte ) ( n int , err error ) {
return 0 , errors . New ( "read error" )
}
func ( errReadCloser ) Close ( ) error {
return nil
}
2019-11-21 17:09:57 +02:00
func customDefaultsOpenFileMock ( name string ) ( io . ReadCloser , error ) {
2020-06-16 09:06:37 +02:00
return ioutil . NopCloser ( strings . NewReader ( "general:\n p0: p0_custom_default\nstages:\n stage1:\n p1: p1_custom_default" ) ) , nil
2019-11-21 17:09:57 +02:00
}
2019-10-22 15:41:27 +02:00
func TestReadConfig ( t * testing . T ) {
var c Config
t . Run ( "Success case" , func ( t * testing . T ) {
myConfig := strings . NewReader ( "general:\n generalTestKey: generalTestValue\nsteps:\n testStep:\n testStepKey: testStepValue" )
err := c . ReadConfig ( ioutil . NopCloser ( myConfig ) ) // NopCloser "no-ops" the closing interface since strings do not need to be closed
if err != nil {
t . Errorf ( "Got error although no error expected: %v" , err )
}
if c . General [ "generalTestKey" ] != "generalTestValue" {
t . Errorf ( "General config- got: %v, expected: %v" , c . General [ "generalTestKey" ] , "generalTestValue" )
}
if c . Steps [ "testStep" ] [ "testStepKey" ] != "testStepValue" {
t . Errorf ( "Step config - got: %v, expected: %v" , c . Steps [ "testStep" ] [ "testStepKey" ] , "testStepValue" )
}
} )
t . Run ( "Read failure" , func ( t * testing . T ) {
var rc errReadCloser
err := c . ReadConfig ( rc )
if err == nil {
t . Errorf ( "Got no error although error expected." )
}
} )
t . Run ( "Unmarshalling failure" , func ( t * testing . T ) {
myConfig := strings . NewReader ( "general:\n generalTestKey: generalTestValue\nsteps:\n testStep:\n\ttestStepKey: testStepValue" )
err := c . ReadConfig ( ioutil . NopCloser ( myConfig ) )
if err == nil {
t . Errorf ( "Got no error although error expected." )
}
} )
}
func TestGetStepConfig ( t * testing . T ) {
t . Run ( "Success case" , func ( t * testing . T ) {
testConfig := ` general :
p3 : p3_general
px3 : px3_general
p4 : p4_general
steps :
step1 :
p4 : p4_step
px4 : px4_step
p5 : p5_step
2019-11-05 17:30:41 +02:00
dependentParameter : dependentValue
2020-03-19 18:24:35 +02:00
stepAlias :
p8 : p8_stepAlias
2019-10-22 15:41:27 +02:00
stages :
stage1 :
p5 : p5_stage
px5 : px5_stage
p6 : p6_stage
`
filters := StepFilters {
General : [ ] string { "p0" , "p1" , "p2" , "p3" , "p4" } ,
2020-03-19 18:24:35 +02:00
Steps : [ ] string { "p0" , "p1" , "p2" , "p3" , "p4" , "p5" , "p8" , "dependentParameter" , "pd1" , "dependentValue" , "pd2" } ,
2019-10-22 15:41:27 +02:00
Stages : [ ] string { "p0" , "p1" , "p2" , "p3" , "p4" , "p5" , "p6" } ,
Parameters : [ ] string { "p0" , "p1" , "p2" , "p3" , "p4" , "p5" , "p6" , "p7" } ,
Env : [ ] string { "p0" , "p1" , "p2" , "p3" , "p4" , "p5" } ,
}
defaults1 := ` general :
p0 : p0_general_default
px0 : px0_general_default
p1 : p1_general_default
steps :
step1 :
p1 : p1_step_default
px1 : px1_step_default
p2 : p2_step_default
2019-11-05 17:30:41 +02:00
dependentValue :
pd1 : pd1_dependent_default
2019-10-22 15:41:27 +02:00
`
defaults2 := ` general :
p2 : p2_general_default
px2 : px2_general_default
2020-06-16 09:06:37 +02:00
p3 : p3_general_default
2019-10-22 15:41:27 +02:00
`
2019-11-21 17:09:57 +02:00
2019-10-22 15:41:27 +02:00
paramJSON := ` { "p6":"p6_param","p7":"p7_param"} `
flags := map [ string ] interface { } { "p7" : "p7_flag" }
var c Config
defaults := [ ] io . ReadCloser { ioutil . NopCloser ( strings . NewReader ( defaults1 ) ) , ioutil . NopCloser ( strings . NewReader ( defaults2 ) ) }
myConfig := ioutil . NopCloser ( strings . NewReader ( testConfig ) )
2019-11-05 17:30:41 +02:00
parameterMetadata := [ ] StepParameters {
{
Name : "pd1" ,
Scope : [ ] string { "STEPS" } ,
Conditions : [ ] Condition {
{
Params : [ ] Param {
{ Name : "dependentParameter" , Value : "dependentValue" } ,
} ,
} ,
} ,
} ,
{
Name : "pd2" ,
Default : "pd2_metadata_default" ,
Scope : [ ] string { "STEPS" } ,
Conditions : [ ] Condition {
{
Params : [ ] Param {
{ Name : "dependentParameter" , Value : "dependentValue" } ,
} ,
} ,
} ,
} ,
2020-01-15 13:16:25 +02:00
{
Name : "pe1" ,
Scope : [ ] string { "STEPS" } ,
ResourceRef : [ ] ResourceReference { { Name : "commonPipelineEnvironment" , Param : "test_pe1" } } ,
2020-07-15 10:09:42 +02:00
Type : "string" ,
2020-01-15 13:16:25 +02:00
} ,
2019-11-05 17:30:41 +02:00
}
2020-04-01 20:46:33 +02:00
secretMetadata := [ ] StepSecrets {
{
Name : "sd1" ,
Type : "jenkins" ,
} ,
}
2019-11-05 17:30:41 +02:00
2020-04-01 20:46:33 +02:00
stepMeta := StepData { Spec : StepSpec { Inputs : StepInputs { Parameters : parameterMetadata , Secrets : secretMetadata } } }
2020-01-15 13:16:25 +02:00
dir , err := ioutil . TempDir ( "" , "" )
if err != nil {
t . Fatal ( "Failed to create temporary directory" )
}
// clean up tmp dir
defer os . RemoveAll ( dir )
piperenv . SetParameter ( filepath . Join ( dir , "commonPipelineEnvironment" ) , "test_pe1" , "pe1_val" )
2020-03-19 18:24:35 +02:00
stepAliases := [ ] Alias { { Name : "stepAlias" } }
2020-05-14 10:50:58 +02:00
stepConfig , err := c . GetStepConfig ( flags , paramJSON , myConfig , defaults , false , filters , parameterMetadata , secretMetadata , stepMeta . GetResourceParameters ( dir , "commonPipelineEnvironment" ) , "stage1" , "step1" , stepAliases )
2019-10-22 15:41:27 +02:00
2020-05-14 10:50:58 +02:00
assert . Equal ( t , nil , err , "error occurred but none expected" )
2019-10-22 15:41:27 +02:00
t . Run ( "Config" , func ( t * testing . T ) {
expected := map [ string ] string {
2019-11-05 17:30:41 +02:00
"p0" : "p0_general_default" ,
"p1" : "p1_step_default" ,
"p2" : "p2_general_default" ,
"p3" : "p3_general" ,
"p4" : "p4_step" ,
"p5" : "p5_stage" ,
"p6" : "p6_param" ,
"p7" : "p7_flag" ,
2020-03-19 18:24:35 +02:00
"p8" : "p8_stepAlias" ,
2019-11-05 17:30:41 +02:00
"pd1" : "pd1_dependent_default" ,
"pd2" : "pd2_metadata_default" ,
2020-01-15 13:16:25 +02:00
"pe1" : "pe1_val" ,
2019-10-22 15:41:27 +02:00
}
2020-01-15 13:16:25 +02:00
2019-10-22 15:41:27 +02:00
for k , v := range expected {
t . Run ( k , func ( t * testing . T ) {
if stepConfig . Config [ k ] != v {
t . Errorf ( "got: %v, expected: %v" , stepConfig . Config [ k ] , v )
}
} )
}
} )
t . Run ( "Config not expected" , func ( t * testing . T ) {
notExpectedKeys := [ ] string { "px0" , "px1" , "px2" , "px3" , "px4" , "px5" }
for _ , p := range notExpectedKeys {
t . Run ( p , func ( t * testing . T ) {
if stepConfig . Config [ p ] != nil {
t . Errorf ( "unexpected: %v" , p )
}
} )
}
} )
} )
2021-03-09 10:30:18 +02:00
t . Run ( "Success case - environment nil" , func ( t * testing . T ) {
testConfig := ""
filters := StepFilters {
General : [ ] string { "p0" } ,
}
defaults1 := ` general :
p0 : p0_general_default
`
var c Config
defaults := [ ] io . ReadCloser { ioutil . NopCloser ( strings . NewReader ( defaults1 ) ) }
myConfig := ioutil . NopCloser ( strings . NewReader ( testConfig ) )
stepMeta := StepData { Spec : StepSpec { Inputs : StepInputs { Parameters : [ ] StepParameters {
{ Name : "p0" , ResourceRef : [ ] ResourceReference { { Name : "commonPipelineEnvironment" , Param : "p0" } } } ,
} } } }
dir , err := ioutil . TempDir ( "" , "" )
if err != nil {
t . Fatal ( "Failed to create temporary directory" )
}
// clean up tmp dir
defer os . RemoveAll ( dir )
stepConfig , err := c . GetStepConfig ( map [ string ] interface { } { } , "" , myConfig , defaults , false , filters , [ ] StepParameters { } , [ ] StepSecrets { } , stepMeta . GetResourceParameters ( dir , "commonPipelineEnvironment" ) , "stage1" , "step1" , [ ] Alias { } )
assert . Equal ( t , nil , err , "error occurred but none expected" )
assert . Equal ( t , "p0_general_default" , stepConfig . Config [ "p0" ] )
} )
2019-11-21 17:09:57 +02:00
t . Run ( "Consider custom defaults from config" , func ( t * testing . T ) {
var c Config
testConfDefaults := "customDefaults:\n- testDefaults.yaml"
c . openFile = customDefaultsOpenFileMock
2020-07-22 11:15:48 +02:00
stepConfig , err := c . GetStepConfig ( nil , "" , ioutil . NopCloser ( strings . NewReader ( testConfDefaults ) ) , nil , false , StepFilters { General : [ ] string { "p0" } } , [ ] StepParameters { } , nil , nil , "stage1" , "step1" , [ ] Alias { } )
2019-11-21 17:09:57 +02:00
2020-05-14 10:50:58 +02:00
assert . NoError ( t , err , "Error occurred but no error expected" )
2019-11-21 17:09:57 +02:00
assert . Equal ( t , "p0_custom_default" , stepConfig . Config [ "p0" ] )
2020-06-16 09:06:37 +02:00
assert . Equal ( t , "p1_custom_default" , stepConfig . Config [ "p1" ] )
2019-11-21 17:09:57 +02:00
} )
2020-05-14 10:50:58 +02:00
t . Run ( "Don't consider custom defaults from config" , func ( t * testing . T ) {
var c Config
testConfDefaults := "customDefaults:\n- testDefaults.yaml"
c . openFile = customDefaultsOpenFileMock
2020-07-22 11:15:48 +02:00
stepConfig , err := c . GetStepConfig ( nil , "" , ioutil . NopCloser ( strings . NewReader ( testConfDefaults ) ) , nil , true , StepFilters { General : [ ] string { "p0" } } , [ ] StepParameters { } , nil , nil , "stage1" , "step1" , [ ] Alias { } )
2020-05-14 10:50:58 +02:00
assert . NoError ( t , err , "Error occurred but no error expected" )
assert . Equal ( t , nil , stepConfig . Config [ "p0" ] )
2020-06-16 09:06:37 +02:00
assert . Equal ( t , nil , stepConfig . Config [ "p1" ] )
2020-05-14 10:50:58 +02:00
} )
2019-11-22 11:30:44 +02:00
t . Run ( "Consider defaults from step config" , func ( t * testing . T ) {
var c Config
2020-09-24 08:58:53 +02:00
stepParams := [ ] StepParameters { { Name : "p0" , Scope : [ ] string { "GENERAL" } , Type : "string" , Default : "p0_step_default" , Aliases : [ ] Alias { { Name : "p0_alias" } } } }
2019-11-22 11:30:44 +02:00
testConf := "general:\n p1: p1_conf"
2020-05-14 10:50:58 +02:00
stepConfig , err := c . GetStepConfig ( nil , "" , ioutil . NopCloser ( strings . NewReader ( testConf ) ) , nil , false , StepFilters { General : [ ] string { "p0" , "p1" } } , stepParams , nil , nil , "stage1" , "step1" , [ ] Alias { } )
2019-11-22 11:30:44 +02:00
2020-05-14 10:50:58 +02:00
assert . NoError ( t , err , "Error occurred but no error expected" )
2019-11-22 11:30:44 +02:00
assert . Equal ( t , "p0_step_default" , stepConfig . Config [ "p0" ] )
assert . Equal ( t , "p1_conf" , stepConfig . Config [ "p1" ] )
} )
2020-07-09 14:13:06 +02:00
t . Run ( "Ignore alias if wrong type" , func ( t * testing . T ) {
var c Config
stepParams := [ ] StepParameters {
2020-09-24 08:58:53 +02:00
{ Name : "p0" , Scope : [ ] string { "GENERAL" } , Type : "bool" , Aliases : [ ] Alias { } } ,
{ Name : "p1" , Scope : [ ] string { "GENERAL" } , Type : "string" , Aliases : [ ] Alias { { Name : "p0/subParam" } } } }
2020-07-09 14:13:06 +02:00
testConf := "general:\n p0: true"
stepConfig , err := c . GetStepConfig ( nil , "" , ioutil . NopCloser ( strings . NewReader ( testConf ) ) , nil , false , StepFilters { General : [ ] string { "p0" , "p1" } } , stepParams , nil , nil , "stage1" , "step1" , [ ] Alias { { } } )
assert . NoError ( t , err , "Error occurred but no error expected" )
assert . Equal ( t , true , stepConfig . Config [ "p0" ] )
assert . Equal ( t , nil , stepConfig . Config [ "p1" ] )
} )
2020-07-15 21:05:22 +02:00
t . Run ( "Apply alias to paramJSON" , func ( t * testing . T ) {
var c Config
secrets := [ ] StepSecrets {
2020-09-24 08:58:53 +02:00
{ Name : "p0" , Type : "string" , Aliases : [ ] Alias { { Name : "p1/subParam" } } } }
2020-07-15 21:05:22 +02:00
testConf := ""
paramJSON := "{\"p1\":{\"subParam\":\"p1_value\"}}"
stepConfig , err := c . GetStepConfig ( nil , paramJSON , ioutil . NopCloser ( strings . NewReader ( testConf ) ) , nil , true , StepFilters { Parameters : [ ] string { "p0" } } , nil , secrets , nil , "stage1" , "step1" , [ ] Alias { { } } )
assert . NoError ( t , err , "Error occurred but no error expected" )
assert . Equal ( t , "p1_value" , stepConfig . Config [ "p0" ] )
} )
2019-10-22 15:41:27 +02:00
t . Run ( "Failure case config" , func ( t * testing . T ) {
var c Config
myConfig := ioutil . NopCloser ( strings . NewReader ( "invalid config" ) )
2020-05-14 10:50:58 +02:00
_ , err := c . GetStepConfig ( nil , "" , myConfig , nil , false , StepFilters { } , [ ] StepParameters { } , nil , nil , "stage1" , "step1" , [ ] Alias { } )
2021-05-06 10:27:23 +02:00
assert . EqualError ( t , err , "failed to parse custom pipeline configuration: format of configuration is invalid \"invalid config\": error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type config.Config" , "default error expected" )
2019-10-22 15:41:27 +02:00
} )
t . Run ( "Failure case defaults" , func ( t * testing . T ) {
var c Config
myConfig := ioutil . NopCloser ( strings . NewReader ( "" ) )
myDefaults := [ ] io . ReadCloser { ioutil . NopCloser ( strings . NewReader ( "invalid defaults" ) ) }
2020-05-14 10:50:58 +02:00
_ , err := c . GetStepConfig ( nil , "" , myConfig , myDefaults , false , StepFilters { } , [ ] StepParameters { } , nil , nil , "stage1" , "step1" , [ ] Alias { } )
2021-05-06 10:27:23 +02:00
assert . EqualError ( t , err , "failed to read default configuration: error unmarshalling \"invalid defaults\": error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type config.Config" , "default error expected" )
2019-10-22 15:41:27 +02:00
} )
//ToDo: test merging of env and parameters/flags
}
func TestGetStepConfigWithJSON ( t * testing . T ) {
filters := StepFilters { All : [ ] string { "key1" } }
t . Run ( "Without flags" , func ( t * testing . T ) {
sc := GetStepConfigWithJSON ( nil , ` "key1":"value1","key2":"value2" ` , filters )
if sc . Config [ "key1" ] != "value1" && sc . Config [ "key2" ] == "value2" {
t . Errorf ( "got: %v, expected: %v" , sc . Config , StepConfig { Config : map [ string ] interface { } { "key1" : "value1" } } )
}
} )
t . Run ( "With flags" , func ( t * testing . T ) {
flags := map [ string ] interface { } { "key1" : "flagVal1" }
sc := GetStepConfigWithJSON ( flags , ` "key1":"value1","key2":"value2" ` , filters )
if sc . Config [ "key1" ] != "flagVal1" {
t . Errorf ( "got: %v, expected: %v" , sc . Config [ "key1" ] , "flagVal1" )
}
} )
}
2019-10-29 11:58:24 +02:00
func TestApplyAliasConfig ( t * testing . T ) {
p := [ ] StepParameters {
{
Name : "p0" ,
Aliases : [ ] Alias {
{ Name : "p0_notused" } ,
} ,
} ,
{
Name : "p1" ,
Aliases : [ ] Alias {
{ Name : "p1_alias" } ,
} ,
} ,
{
Name : "p2" ,
Aliases : [ ] Alias {
{ Name : "p2_alias/deep/test" } ,
} ,
} ,
{
Name : "p3" ,
Aliases : [ ] Alias {
{ Name : "p3_notused" } ,
} ,
} ,
{
Name : "p4" ,
Aliases : [ ] Alias {
{ Name : "p4_alias" } ,
{ Name : "p4_2nd_alias" } ,
} ,
} ,
{
Name : "p5" ,
Aliases : [ ] Alias {
{ Name : "p5_notused" } ,
} ,
} ,
{
Name : "p6" ,
Aliases : [ ] Alias {
{ Name : "p6_1st_alias" } ,
{ Name : "p6_alias" } ,
} ,
} ,
2019-11-22 11:30:44 +02:00
{
Name : "p7" ,
Aliases : [ ] Alias {
{ Name : "p7_alias" } ,
} ,
} ,
2020-03-19 18:24:35 +02:00
{
Name : "p8" ,
Aliases : [ ] Alias {
{ Name : "p8_alias" } ,
} ,
} ,
{
Name : "p9" ,
} ,
2019-10-29 11:58:24 +02:00
}
2020-04-01 20:46:33 +02:00
s := [ ] StepSecrets {
{
Name : "s1" ,
Aliases : [ ] Alias {
{ Name : "s1_alias" } ,
} ,
} ,
}
2019-10-29 11:58:24 +02:00
filters := StepFilters {
General : [ ] string { "p1" , "p2" } ,
Stages : [ ] string { "p4" } ,
2020-04-01 20:46:33 +02:00
Steps : [ ] string { "p6" , "p8" , "s1" } ,
2019-10-29 11:58:24 +02:00
}
c := Config {
General : map [ string ] interface { } {
"p0_notused" : "p0_general" ,
"p1_alias" : "p1_general" ,
"p2_alias" : map [ string ] interface { } {
"deep" : map [ string ] interface { } {
"test" : "p2_general" ,
} ,
} ,
} ,
Stages : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"stage1" : {
2019-10-29 11:58:24 +02:00
"p3_notused" : "p3_stage" ,
"p4_alias" : "p4_stage" ,
} ,
} ,
Steps : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"step1" : {
2019-10-29 11:58:24 +02:00
"p5_notused" : "p5_step" ,
"p6_alias" : "p6_step" ,
2019-11-22 11:30:44 +02:00
"p7" : "p7_step" ,
2019-10-29 11:58:24 +02:00
} ,
2020-09-24 08:58:53 +02:00
"stepAlias1" : {
2020-03-19 18:24:35 +02:00
"p7" : "p7_stepAlias" ,
"p8_alias" : "p8_stepAlias" ,
"p9" : "p9_stepAlias" ,
2020-04-01 20:46:33 +02:00
"s1_alias" : "s1_stepAlias" ,
2020-03-19 18:24:35 +02:00
} ,
2019-10-29 11:58:24 +02:00
} ,
}
2020-03-19 18:24:35 +02:00
stepAliases := [ ] Alias { { Name : "stepAlias1" } }
2020-04-01 20:46:33 +02:00
c . ApplyAliasConfig ( p , s , filters , "stage1" , "step1" , stepAliases )
2019-10-29 11:58:24 +02:00
t . Run ( "Global" , func ( t * testing . T ) {
assert . Nil ( t , c . General [ "p0" ] )
assert . Equal ( t , "p1_general" , c . General [ "p1" ] )
assert . Equal ( t , "p2_general" , c . General [ "p2" ] )
} )
t . Run ( "Stage" , func ( t * testing . T ) {
assert . Nil ( t , c . General [ "p3" ] )
assert . Equal ( t , "p4_stage" , c . Stages [ "stage1" ] [ "p4" ] )
} )
2020-03-19 18:24:35 +02:00
t . Run ( "Steps" , func ( t * testing . T ) {
2019-10-29 11:58:24 +02:00
assert . Nil ( t , c . General [ "p5" ] )
assert . Equal ( t , "p6_step" , c . Steps [ "step1" ] [ "p6" ] )
2019-11-22 11:30:44 +02:00
assert . Equal ( t , "p7_step" , c . Steps [ "step1" ] [ "p7" ] )
2020-03-19 18:24:35 +02:00
assert . Equal ( t , "p8_stepAlias" , c . Steps [ "step1" ] [ "p8" ] )
assert . Equal ( t , "p9_stepAlias" , c . Steps [ "step1" ] [ "p9" ] )
2020-04-01 20:46:33 +02:00
assert . Equal ( t , "s1_stepAlias" , c . Steps [ "step1" ] [ "s1" ] )
2019-10-29 11:58:24 +02:00
} )
}
func TestGetDeepAliasValue ( t * testing . T ) {
c := map [ string ] interface { } {
"p0" : "p0_val" ,
"p1" : 11 ,
"p2" : map [ string ] interface { } {
"p2_0" : "p2_0_val" ,
"p2_1" : map [ string ] interface { } {
"p2_1_0" : "p2_1_0_val" ,
} ,
} ,
}
tt := [ ] struct {
key string
expected interface { }
} {
{ key : "p0" , expected : "p0_val" } ,
{ key : "p1" , expected : 11 } ,
{ key : "p2/p2_0" , expected : "p2_0_val" } ,
{ key : "p2/p2_1/p2_1_0" , expected : "p2_1_0_val" } ,
}
for k , v := range tt {
assert . Equal ( t , v . expected , getDeepAliasValue ( c , v . key ) , fmt . Sprintf ( "wrong return value for run %v" , k + 1 ) )
}
}
2020-03-19 18:24:35 +02:00
func TestCopyStepAliasConfig ( t * testing . T ) {
t . Run ( "Step config available" , func ( t * testing . T ) {
c := Config {
Steps : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"step1" : {
2020-03-19 18:24:35 +02:00
"p1" : "p1_step" ,
"p2" : "p2_step" ,
} ,
2020-09-24 08:58:53 +02:00
"stepAlias1" : {
2020-03-19 18:24:35 +02:00
"p2" : "p2_stepAlias" ,
"p3" : "p3_stepAlias" ,
} ,
2020-09-24 08:58:53 +02:00
"stepAlias2" : {
2020-03-19 18:24:35 +02:00
"p3" : "p3_stepAlias2" ,
"p4" : "p4_stepAlias2" ,
} ,
} ,
}
expected := Config {
Steps : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"step1" : {
2020-03-19 18:24:35 +02:00
"p1" : "p1_step" ,
"p2" : "p2_step" ,
"p3" : "p3_stepAlias" ,
"p4" : "p4_stepAlias2" ,
} ,
2020-09-24 08:58:53 +02:00
"stepAlias1" : {
2020-03-19 18:24:35 +02:00
"p2" : "p2_stepAlias" ,
"p3" : "p3_stepAlias" ,
} ,
2020-09-24 08:58:53 +02:00
"stepAlias2" : {
2020-03-19 18:24:35 +02:00
"p3" : "p3_stepAlias2" ,
"p4" : "p4_stepAlias2" ,
} ,
} ,
}
c . copyStepAliasConfig ( "step1" , [ ] Alias { { Name : "stepAlias1" } , { Name : "stepAlias2" } } )
assert . Equal ( t , expected , c )
} )
t . Run ( "Step config not available" , func ( t * testing . T ) {
c := Config {
Steps : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"stepAlias1" : {
2020-03-19 18:24:35 +02:00
"p2" : "p2_stepAlias" ,
} ,
} ,
}
expected := Config {
Steps : map [ string ] map [ string ] interface { } {
2020-09-24 08:58:53 +02:00
"step1" : {
2020-03-19 18:24:35 +02:00
"p2" : "p2_stepAlias" ,
} ,
2020-09-24 08:58:53 +02:00
"stepAlias1" : {
2020-03-19 18:24:35 +02:00
"p2" : "p2_stepAlias" ,
} ,
} ,
}
c . copyStepAliasConfig ( "step1" , [ ] Alias { { Name : "stepAlias1" } } )
assert . Equal ( t , expected , c )
} )
}
2019-10-22 15:41:27 +02:00
func TestGetJSON ( t * testing . T ) {
t . Run ( "Success case" , func ( t * testing . T ) {
custom := map [ string ] interface { } { "key1" : "value1" }
json , err := GetJSON ( custom )
if err != nil {
t . Errorf ( "Got error although no error expected: %v" , err )
}
if json != ` { "key1":"value1"} ` {
t . Errorf ( "got: %v, expected: %v" , json , ` { "key1":"value1"} ` )
}
} )
t . Run ( "Marshalling failure" , func ( t * testing . T ) {
_ , err := GetJSON ( make ( chan int ) )
if err == nil {
t . Errorf ( "Got no error although error expected" )
}
} )
}
func TestMerge ( t * testing . T ) {
testTable := [ ] struct {
Source map [ string ] interface { }
Filter [ ] string
MergeData map [ string ] interface { }
ExpectedOutput map [ string ] interface { }
} {
{
Source : map [ string ] interface { } { "key1" : "baseValue" } ,
Filter : [ ] string { } ,
MergeData : map [ string ] interface { } { "key1" : "overwrittenValue" } ,
ExpectedOutput : map [ string ] interface { } { "key1" : "overwrittenValue" } ,
} ,
{
Source : map [ string ] interface { } { "key1" : "value1" } ,
Filter : [ ] string { } ,
MergeData : map [ string ] interface { } { "key2" : "value2" } ,
ExpectedOutput : map [ string ] interface { } { "key1" : "value1" , "key2" : "value2" } ,
} ,
{
Source : map [ string ] interface { } { "key1" : "value1" } ,
Filter : [ ] string { "key1" } ,
MergeData : map [ string ] interface { } { "key2" : "value2" } ,
ExpectedOutput : map [ string ] interface { } { "key1" : "value1" } ,
} ,
{
Source : map [ string ] interface { } { "key1" : map [ string ] interface { } { "key1_1" : "value1" } } ,
Filter : [ ] string { } ,
MergeData : map [ string ] interface { } { "key1" : map [ string ] interface { } { "key1_2" : "value2" } } ,
ExpectedOutput : map [ string ] interface { } { "key1" : map [ string ] interface { } { "key1_1" : "value1" , "key1_2" : "value2" } } ,
} ,
}
for _ , row := range testTable {
t . Run ( fmt . Sprintf ( "Merging %v into %v" , row . MergeData , row . Source ) , func ( t * testing . T ) {
stepConfig := StepConfig { Config : row . Source }
stepConfig . mixIn ( row . MergeData , row . Filter )
assert . Equal ( t , row . ExpectedOutput , stepConfig . Config , "Mixin was incorrect" )
} )
}
}