2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2021-03-29 16:22:23 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type terraformExecuteMockUtils struct {
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTerraformExecuteTestsUtils() terraformExecuteMockUtils {
|
|
|
|
utils := terraformExecuteMockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
|
|
|
return utils
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunTerraformExecute(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
terraformExecuteOptions
|
2021-10-29 13:58:34 +02:00
|
|
|
expectedArgs []string
|
|
|
|
expectedEnvVars []string
|
2021-03-29 16:22:23 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"apply", "-auto-approve", "-no-color"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
TerraformSecrets: "/tmp/test",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "plan",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"plan", "-no-color"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "plan",
|
|
|
|
TerraformSecrets: "/tmp/test",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"plan", "-var-file=/tmp/test", "-no-color"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "plan",
|
|
|
|
TerraformSecrets: "/tmp/test",
|
|
|
|
AdditionalArgs: []string{"-arg1"},
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"plan", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
TerraformSecrets: "/tmp/test",
|
|
|
|
AdditionalArgs: []string{"-arg1"},
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
|
2021-03-29 16:22:23 +02:00
|
|
|
},
|
2021-10-28 16:20:14 +02:00
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
TerraformSecrets: "/tmp/test",
|
|
|
|
AdditionalArgs: []string{"-arg1"},
|
|
|
|
GlobalOptions: []string{"-chgdir=src"},
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"-chgdir=src", "apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
|
2021-10-28 16:20:14 +02:00
|
|
|
},
|
2021-10-29 12:06:06 +02:00
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
Init: true,
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"apply", "-auto-approve", "-no-color"}, []string{},
|
2021-10-29 12:06:06 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
GlobalOptions: []string{"-chgdir=src"},
|
|
|
|
Init: true,
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"-chgdir=src", "apply", "-auto-approve", "-no-color"}, []string{},
|
2021-10-29 13:58:34 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "apply",
|
|
|
|
CliConfigFile: ".pipeline/.terraformrc",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"apply", "-auto-approve", "-no-color"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"},
|
2021-10-29 12:06:06 +02:00
|
|
|
},
|
2021-11-04 14:03:45 +02:00
|
|
|
{
|
|
|
|
terraformExecuteOptions{
|
|
|
|
Command: "plan",
|
|
|
|
Workspace: "any-workspace",
|
2021-11-04 19:08:52 +02:00
|
|
|
}, []string{"plan", "-no-color"}, []string{"TF_WORKSPACE=any-workspace"},
|
2021-11-04 14:03:45 +02:00
|
|
|
},
|
2021-03-29 16:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tt {
|
2021-11-04 11:28:41 +02:00
|
|
|
t.Run(fmt.Sprintf("That arguments are correct %d", i), func(t *testing.T) {
|
2021-03-29 16:22:23 +02:00
|
|
|
t.Parallel()
|
|
|
|
// init
|
|
|
|
config := test.terraformExecuteOptions
|
|
|
|
utils := newTerraformExecuteTestsUtils()
|
2021-11-04 11:28:41 +02:00
|
|
|
utils.StdoutReturn = map[string]string{}
|
|
|
|
utils.StdoutReturn["terraform output -json"] = "{}"
|
|
|
|
utils.StdoutReturn["terraform -chgdir=src output -json"] = "{}"
|
|
|
|
|
2021-10-29 13:58:34 +02:00
|
|
|
runner := utils.ExecMockRunner
|
2021-03-29 16:22:23 +02:00
|
|
|
|
|
|
|
// test
|
2021-11-04 11:28:41 +02:00
|
|
|
err := runTerraformExecute(&config, nil, utils, &terraformExecuteCommonPipelineEnvironment{})
|
2021-03-29 16:22:23 +02:00
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
2021-10-29 12:06:06 +02:00
|
|
|
|
|
|
|
if config.Init {
|
2021-11-04 19:08:52 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: append(config.GlobalOptions, "init", "-no-color")}, utils.Calls[0])
|
2021-10-29 12:06:06 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[1])
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[0])
|
|
|
|
}
|
2021-10-29 13:58:34 +02:00
|
|
|
|
|
|
|
assert.Subset(t, runner.Env, test.expectedEnvVars)
|
2021-03-29 16:22:23 +02:00
|
|
|
})
|
|
|
|
}
|
2021-11-04 11:28:41 +02:00
|
|
|
|
|
|
|
t.Run("Outputs get injected into CPE", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cpe := terraformExecuteCommonPipelineEnvironment{}
|
|
|
|
|
|
|
|
config := terraformExecuteOptions{
|
|
|
|
Command: "plan",
|
|
|
|
}
|
|
|
|
utils := newTerraformExecuteTestsUtils()
|
|
|
|
utils.StdoutReturn = map[string]string{}
|
|
|
|
utils.StdoutReturn["terraform output -json"] = `{
|
|
|
|
"sample_var": {
|
|
|
|
"sensitive": true,
|
|
|
|
"value": "a secret value",
|
|
|
|
"type": "string"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// test
|
|
|
|
err := runTerraformExecute(&config, nil, utils, &cpe)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(cpe.custom.terraformOutputs))
|
|
|
|
assert.Equal(t, "a secret value", cpe.custom.terraformOutputs["sample_var"])
|
|
|
|
})
|
2021-03-29 16:22:23 +02:00
|
|
|
}
|