2019-10-24 10:59:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-01-24 15:30:27 +02:00
|
|
|
"fmt"
|
2019-10-24 10:59:58 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-10-14 11:13:08 +02:00
|
|
|
"path/filepath"
|
2019-10-24 10:59:58 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-07-08 15:26:07 +02:00
|
|
|
func configOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) {
|
2019-10-24 10:59:58 +02:00
|
|
|
var r string
|
|
|
|
switch name {
|
|
|
|
case "TestAddCustomDefaults_default1":
|
|
|
|
r = "default1"
|
|
|
|
case "TestAddCustomDefaults_default2":
|
|
|
|
r = "default3"
|
|
|
|
default:
|
|
|
|
r = ""
|
|
|
|
}
|
|
|
|
return ioutil.NopCloser(strings.NewReader(r)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigCommand(t *testing.T) {
|
|
|
|
cmd := ConfigCommand()
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Required flags", func(t *testing.T) {
|
2021-06-16 08:43:30 +02:00
|
|
|
exp := []string{}
|
2019-10-24 10:59:58 +02:00
|
|
|
assert.Equal(t, exp, gotReq, "required flags incorrect")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Optional flags", func(t *testing.T) {
|
2021-10-21 14:00:51 +02:00
|
|
|
exp := []string{"contextConfig", "output", "outputFile", "parametersJSON", "stageConfig", "stageConfigAcceptedParams", "stepMetadata", "stepName"}
|
2019-10-24 10:59:58 +02:00
|
|
|
assert.Equal(t, exp, gotOpt, "optional flags incorrect")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Run", func(t *testing.T) {
|
|
|
|
t.Run("Success case", func(t *testing.T) {
|
2019-10-25 14:58:59 +02:00
|
|
|
configOptions.openFile = configOpenFileMock
|
2021-06-16 08:43:30 +02:00
|
|
|
configOptions.stepName = "githubCreateIssue"
|
2020-05-26 07:58:03 +02:00
|
|
|
cmd.Run(cmd, []string{})
|
2019-10-24 10:59:58 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultsAndFilters(t *testing.T) {
|
|
|
|
metadata := config.StepData{
|
|
|
|
Spec: config.StepSpec{
|
|
|
|
Inputs: config.StepInputs{
|
|
|
|
Parameters: []config.StepParameters{
|
|
|
|
{Name: "paramOne", Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS", "ENV"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Context config", func(t *testing.T) {
|
|
|
|
configOptions.contextConfig = true
|
|
|
|
defer func() { configOptions.contextConfig = false }()
|
2019-11-22 11:14:21 +02:00
|
|
|
defaults, filters, err := defaultsAndFilters(&metadata, "stepName")
|
2019-10-24 10:59:58 +02:00
|
|
|
|
|
|
|
assert.Equal(t, 1, len(defaults), "getting defaults failed")
|
2021-04-09 10:04:35 +02:00
|
|
|
assert.Equal(t, 3, len(filters.All), "wrong number of filter values")
|
2020-09-24 07:41:06 +02:00
|
|
|
assert.NoError(t, err, "error occurred but none expected")
|
2019-10-24 10:59:58 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Step config", func(t *testing.T) {
|
2019-11-22 11:14:21 +02:00
|
|
|
defaults, filters, err := defaultsAndFilters(&metadata, "stepName")
|
2019-10-24 10:59:58 +02:00
|
|
|
assert.Equal(t, 0, len(defaults), "getting defaults failed")
|
2020-01-28 00:40:53 +02:00
|
|
|
assert.Equal(t, 2, len(filters.All), "wrong number of filter values")
|
2020-09-24 07:41:06 +02:00
|
|
|
assert.NoError(t, err, "error occurred but none expected")
|
2019-10-24 10:59:58 +02:00
|
|
|
})
|
2020-01-24 15:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyContextConditions(t *testing.T) {
|
2019-10-24 10:59:58 +02:00
|
|
|
|
2020-01-24 15:30:27 +02:00
|
|
|
tt := []struct {
|
2020-05-20 10:50:35 +02:00
|
|
|
name string
|
2020-01-24 15:30:27 +02:00
|
|
|
metadata config.StepData
|
|
|
|
conf config.StepConfig
|
|
|
|
expected map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
name: "no context conditions",
|
2020-01-24 15:30:27 +02:00
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{}},
|
|
|
|
expected: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
name: "context condition not met",
|
2020-01-24 15:30:27 +02:00
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
Image: "myDefaultImage:latest",
|
2020-01-24 15:30:27 +02:00
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"val1": map[string]interface{}{"dockerImage": "myTestImage:latest"},
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"val1": map[string]interface{}{"dockerImage": "myTestImage:latest"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
name: "context condition met",
|
2020-01-24 15:30:27 +02:00
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
Image: "myDefaultImage:latest",
|
2020-01-24 15:30:27 +02:00
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"val1": map[string]interface{}{"dockerImage": "myTestImage:latest"},
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 10:50:35 +02:00
|
|
|
name: "context condition met - root defined already",
|
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
|
|
|
|
{
|
|
|
|
Image: "myDefaultImage:latest",
|
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "context condition met - root defined and deep value defined",
|
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
|
|
|
|
{
|
|
|
|
Image: "myDefaultImage:latest",
|
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"val1": map[string]interface{}{"dockerImage": "mySubTestImage:latest"},
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "context condition met - root defined as empty",
|
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Containers: []config.Container{
|
|
|
|
{
|
|
|
|
Image: "myDefaultImage:latest",
|
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "",
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
//ToDo: Sidecar behavior not properly working, expects sidecarImage, ... parameters and not dockerImage
|
|
|
|
{
|
|
|
|
name: "sidecar context condition met",
|
2020-01-24 15:30:27 +02:00
|
|
|
metadata: config.StepData{Spec: config.StepSpec{Sidecars: []config.Container{
|
|
|
|
{
|
|
|
|
Image: "myTestImage:latest",
|
|
|
|
Conditions: []config.Condition{
|
|
|
|
{
|
|
|
|
ConditionRef: "strings-equal",
|
|
|
|
Params: []config.Param{
|
|
|
|
{Name: "param1", Value: "val1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
conf: config.StepConfig{Config: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"val1": map[string]interface{}{"dockerImage": "myTestImage:latest"},
|
|
|
|
}},
|
|
|
|
expected: map[string]interface{}{
|
|
|
|
"param1": "val1",
|
|
|
|
"dockerImage": "myTestImage:latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for run, test := range tt {
|
2020-05-20 10:50:35 +02:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
applyContextConditions(test.metadata, &test.conf)
|
|
|
|
assert.Equalf(t, test.expected, test.conf.Config, fmt.Sprintf("Run %v failed", run))
|
|
|
|
})
|
2020-01-24 15:30:27 +02:00
|
|
|
}
|
2019-10-24 10:59:58 +02:00
|
|
|
}
|
2020-10-14 11:13:08 +02:00
|
|
|
|
|
|
|
func TestPrepareOutputEnvironment(t *testing.T) {
|
|
|
|
outputResources := []config.StepResources{
|
|
|
|
{
|
|
|
|
Name: "commonPipelineEnvironment",
|
|
|
|
Type: "piperEnvironment",
|
|
|
|
Parameters: []map[string]interface{}{
|
|
|
|
{"name": "param0"},
|
|
|
|
{"name": "path1/param1"},
|
|
|
|
{"name": "path2/param2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "influx",
|
|
|
|
Type: "influx",
|
|
|
|
Parameters: []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"name": "measurement0",
|
|
|
|
"fields": []map[string]string{
|
|
|
|
{"name": "influx0_0"},
|
|
|
|
{"name": "influx0_1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "measurement1",
|
|
|
|
"fields": []map[string]string{
|
|
|
|
{"name": "influx1_0"},
|
|
|
|
{"name": "influx1_1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2020-10-14 11:13:08 +02:00
|
|
|
|
|
|
|
prepareOutputEnvironment(outputResources, dir)
|
|
|
|
assert.DirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path1"))
|
|
|
|
assert.DirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path2"))
|
|
|
|
assert.DirExists(t, filepath.Join(dir, "influx", "measurement0"))
|
|
|
|
assert.DirExists(t, filepath.Join(dir, "influx", "measurement1"))
|
|
|
|
assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "param0"))
|
|
|
|
assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path1", "param1"))
|
|
|
|
assert.NoDirExists(t, filepath.Join(dir, "commonPipelineEnvironment", "path2", "param2"))
|
|
|
|
assert.NoDirExists(t, filepath.Join(dir, "influx", "measurement0", "influx0_0"))
|
|
|
|
assert.NoDirExists(t, filepath.Join(dir, "influx", "measurement1", "influx0_1"))
|
|
|
|
}
|