2017-04-26 03:10:10 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-09 19:32:56 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/copy"
|
2017-04-26 03:10:10 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProviders(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2020-04-10 19:26:38 +02:00
|
|
|
if err := os.Chdir(testFixturePath("providers/basic")); err != nil {
|
2017-04-26 03:10:10 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ProvidersCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:26:38 +02:00
|
|
|
wantOutput := []string{
|
|
|
|
"provider[registry.terraform.io/hashicorp/foo]",
|
|
|
|
"provider[registry.terraform.io/hashicorp/bar]",
|
|
|
|
"provider[registry.terraform.io/hashicorp/baz]",
|
2017-04-26 03:10:10 +02:00
|
|
|
}
|
2020-04-10 19:26:38 +02:00
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
for _, want := range wantOutput {
|
|
|
|
if !strings.Contains(output, want) {
|
|
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
|
|
}
|
2017-04-26 03:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 13:02:01 +02:00
|
|
|
|
|
|
|
func TestProviders_noConfigs(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ProvidersCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected command to return non-zero exit code" +
|
|
|
|
" when no configs are available")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
2018-10-15 01:59:51 +02:00
|
|
|
expectedErrMsg := "No configuration files"
|
2017-06-10 13:02:01 +02:00
|
|
|
if !strings.Contains(output, expectedErrMsg) {
|
|
|
|
t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output)
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 19:26:38 +02:00
|
|
|
|
|
|
|
func TestProviders_modules(t *testing.T) {
|
2020-06-09 19:32:56 +02:00
|
|
|
td := tempDir(t)
|
|
|
|
copy.CopyDir(testFixturePath("providers/modules"), td)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
2020-04-10 19:26:38 +02:00
|
|
|
|
|
|
|
// first run init with mock provider sources to install the module
|
|
|
|
initUi := new(cli.MockUi)
|
|
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
|
|
"foo": {"1.0.0"},
|
|
|
|
"bar": {"2.0.0"},
|
|
|
|
"baz": {"1.2.2"},
|
|
|
|
})
|
|
|
|
defer close()
|
|
|
|
m := Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: initUi,
|
|
|
|
ProviderSource: providerSource,
|
|
|
|
}
|
|
|
|
ic := &InitCommand{
|
|
|
|
Meta: m,
|
|
|
|
}
|
|
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
|
|
t.Fatalf("init failed\n%s", initUi.ErrorWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Providers command
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ProvidersCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
wantOutput := []string{
|
|
|
|
"provider[registry.terraform.io/hashicorp/foo] 1.0.*", // from required_providers
|
|
|
|
"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from provider config
|
2020-06-09 19:32:56 +02:00
|
|
|
"── module.kiddo", // tree node for child module
|
|
|
|
"provider[registry.terraform.io/hashicorp/baz]", // implied by a resource in the child module
|
2020-04-10 19:26:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
for _, want := range wantOutput {
|
|
|
|
if !strings.Contains(output, want) {
|
|
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProviders_state(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("providers/state")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ProvidersCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
wantOutput := []string{
|
|
|
|
"provider[registry.terraform.io/hashicorp/foo] 1.0.*", // from required_providers
|
|
|
|
"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from a provider config block
|
2020-06-09 19:32:56 +02:00
|
|
|
"Providers required by state", // header for state providers
|
2020-04-10 19:26:38 +02:00
|
|
|
"provider[registry.terraform.io/hashicorp/baz]", // from a resouce in state (only)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
for _, want := range wantOutput {
|
|
|
|
if !strings.Contains(output, want) {
|
|
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|