1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/terraformExecute_test.go
Christian Volk 7517873b62
feat(terraformExecute): fetch CLI config from vault or jenkins (#3225)
* feat(terraformExecute): run init prior to command

* Docs & fmt

* cleanup

* feat(terraformExecute): fetch cli config from vault or jenkins

* fix

* cleanup

* cleanup

* Update resources/metadata/terraformExecute.yaml

* rename config param from terraformConfigFile to cliConfigFile

* rename credential params

* cleanup

* fix

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-10-29 13:58:34 +02:00

121 lines
3.1 KiB
Go

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
expectedArgs []string
expectedEnvVars []string
}{
{
terraformExecuteOptions{
Command: "apply",
}, []string{"apply", "-auto-approve"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
}, []string{"plan"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
TerraformSecrets: "/tmp/test",
}, []string{"plan", "-var-file=/tmp/test"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
}, []string{"plan", "-var-file=/tmp/test", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
GlobalOptions: []string{"-chgdir=src"},
}, []string{"-chgdir=src", "apply", "-auto-approve", "-var-file=/tmp/test", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
Init: true,
}, []string{"apply", "-auto-approve"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
GlobalOptions: []string{"-chgdir=src"},
Init: true,
}, []string{"-chgdir=src", "apply", "-auto-approve"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
CliConfigFile: ".pipeline/.terraformrc",
}, []string{"apply", "-auto-approve"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"},
},
}
for i, test := range tt {
t.Run(fmt.Sprintf("That arguemtns are correct %d", i), func(t *testing.T) {
t.Parallel()
// init
config := test.terraformExecuteOptions
utils := newTerraformExecuteTestsUtils()
runner := utils.ExecMockRunner
// test
err := runTerraformExecute(&config, nil, utils)
// assert
assert.NoError(t, err)
if config.Init {
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: append(config.GlobalOptions, "init")}, utils.Calls[0])
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])
}
assert.Subset(t, runner.Env, test.expectedEnvVars)
})
}
}