Skip to content

Commit

Permalink
[TOOL-1905] Include step inputs in model (#709)
Browse files Browse the repository at this point in the history
* include inputs in model

* expand implemented

* filter sensitive data

* extracted input expansion

* tests

* added tests + resolve from inputs first

* PR suggestions

* PR suggestion

* no string conversion needed

* %s
  • Loading branch information
Szabadember authored Mar 30, 2020
1 parent 1ca1a1f commit fd73aa2
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 14 deletions.
65 changes: 58 additions & 7 deletions cli/run_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,56 @@ func activateStepLibStep(stepIDData models.StepIDData, destination, stepYMLCopyP
return info, didStepLibUpdate, nil
}

func expandStepInputs(
inputs []envmanModels.EnvironmentItemModel,
environments []envmanModels.EnvironmentItemModel,
) map[string]string {
stepInputs := make(map[string]string)

// Retrieve all non-sensitive input values
for _, input := range inputs {
if err := input.FillMissingDefaults(); err != nil {
log.Warnf("Failed to fill missing defaults, skipping input: %s", err)
continue
}

options, err := input.GetOptions()
if err == nil && *options.IsSensitive == false {
if inputName, inputValue, err := input.GetKeyValuePair(); err == nil {
stepInputs[inputName] = inputValue
} else {
log.Warnf("Failed to get input value for '%s', skipping input: %s", inputName, err)
}
} else if err != nil {
log.Warnf("Failed to get input options, skipping input: %s", err)
}
}

var mappingFunc func(string) string
mappingFunc = func(key string) string {
for inputName, inputValue := range stepInputs {
if inputName == key {
return os.Expand(inputValue, mappingFunc)
}
}

for _, environmentItem := range environments {
envValue := environmentItem[key]
if envValue != nil {
return os.Expand(envValue.(string), mappingFunc)
}
}

return os.Getenv(key)
}

for inputName, inputValue := range stepInputs {
stepInputs[inputName] = os.Expand(inputValue, mappingFunc)
}

return stepInputs
}

func activateAndRunSteps(
workflow models.WorkflowModel,
defaultStepLibSource string,
Expand Down Expand Up @@ -618,13 +668,14 @@ func activateAndRunSteps(
}

stepResults := models.StepRunResultsModel{
StepInfo: stepInfoCopy,
Status: resultCode,
Idx: buildRunResults.ResultsCount(),
RunTime: time.Now().Sub(stepStartTime),
ErrorStr: errStr,
ExitCode: exitCode,
StartTime: stepStartTime,
StepInfo: stepInfoCopy,
StepInputs: expandStepInputs(step.Inputs, *environments),
Status: resultCode,
Idx: buildRunResults.ResultsCount(),
RunTime: time.Now().Sub(stepStartTime),
ErrorStr: errStr,
ExitCode: exitCode,
StartTime: stepStartTime,
}

isExitStatusError := true
Expand Down
116 changes: 116 additions & 0 deletions cli/run_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,119 @@ func Test_activateStepLibStep(t *testing.T) {
})
}
}

func TestExpandStepInputsMissingOptionsDoNotCauseCrash(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "13.3", "opts": map[string]interface{}{}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 Plus", "opts": map[string]interface{}{}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Equal(t, "13.3", expandedInputs["simulator_os_version"])
require.Equal(t, "iPhone 8 Plus", expandedInputs["simulator_device"])
}

func TestExpandStepInputsDoNotNeedExpansion(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "13.3", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 Plus", "opts": map[string]interface{}{"is_sensitive": false}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Equal(t, "13.3", expandedInputs["simulator_os_version"])
require.Equal(t, "iPhone 8 Plus", expandedInputs["simulator_device"])
}

func TestExpandStepInputsSecretRemoved(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "13.3", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 Plus", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"secret_input": "top secret", "opts": map[string]interface{}{"is_sensitive": true}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Empty(t, expandedInputs["secret_input"])
require.Equal(t, "13.3", expandedInputs["simulator_os_version"])
}

func TestExpandStepInputsNeedExpansion(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "$SIMULATOR_OS_VERSION", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 Plus", "opts": map[string]interface{}{"is_sensitive": false}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"SIMULATOR_OS_VERSION": "13.3", "opts": map[string]interface{}{"is_sensitive": false}},
}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Equal(t, "13.3", expandedInputs["simulator_os_version"])
}

func TestExpandStepInputsNeedExpansionWithinExpansion(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "$SIMULATOR_OS_VERSION", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 Plus", "opts": map[string]interface{}{"is_sensitive": false}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"SIMULATOR_OS_VERSION": "$SIMULATOR_OS_MAJOR_VERSION.$SIMULATOR_OS_MINOR_VERSION", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"SIMULATOR_OS_MAJOR_VERSION": "13", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"SIMULATOR_OS_MINOR_VERSION": "3", "opts": map[string]interface{}{"is_sensitive": false}},
}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Empty(t, expandedInputs["secret_input"])
require.Equal(t, "13.3", expandedInputs["simulator_os_version"])
}

func TestExpandStepInputsNeedCrossInputExpansion(t *testing.T) {
// Arrange
testInputs := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "13.3", "opts": map[string]interface{}{"is_sensitive": false}},
envmanModels.EnvironmentItemModel{"simulator_device": "iPhone 8 ($simulator_os_version)", "opts": map[string]interface{}{"is_sensitive": false}},
}

testEnvironment := []envmanModels.EnvironmentItemModel{
envmanModels.EnvironmentItemModel{"simulator_os_version": "12.1", "opts": map[string]interface{}{"is_sensitive": false}},
}

// Act
expandedInputs := expandStepInputs(testInputs, testEnvironment)

// Assert
require.NotNil(t, expandedInputs)
require.Empty(t, expandedInputs["secret_input"])
require.Equal(t, "iPhone 8 (13.3)", expandedInputs["simulator_device"])
}
15 changes: 8 additions & 7 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ type BuildRunResultsModel struct {

// StepRunResultsModel ...
type StepRunResultsModel struct {
StepInfo stepmanModels.StepInfoModel `json:"step_info" yaml:"step_info"`
Status int `json:"status" yaml:"status"`
Idx int `json:"idx" yaml:"idx"`
RunTime time.Duration `json:"run_time" yaml:"run_time"`
StartTime time.Time `json:"start_time" yaml:"start_time"`
ErrorStr string `json:"error_str" yaml:"error_str"`
ExitCode int `json:"exit_code" yaml:"exit_code"`
StepInfo stepmanModels.StepInfoModel `json:"step_info" yaml:"step_info"`
StepInputs map[string]string `json:"step_inputs" yaml:"step_inputs"`
Status int `json:"status" yaml:"status"`
Idx int `json:"idx" yaml:"idx"`
RunTime time.Duration `json:"run_time" yaml:"run_time"`
StartTime time.Time `json:"start_time" yaml:"start_time"`
ErrorStr string `json:"error_str" yaml:"error_str"`
ExitCode int `json:"exit_code" yaml:"exit_code"`
}

// TestResultStepInfo ...
Expand Down

0 comments on commit fd73aa2

Please sign in to comment.