mirror of
https://github.com/opentofu/opentofu.git
synced 2026-02-18 18:17:54 -05:00
Some checks are pending
build / Build for freebsd_386 (push) Waiting to run
build / Build for linux_386 (push) Waiting to run
build / Build for openbsd_386 (push) Waiting to run
build / Build for windows_386 (push) Waiting to run
build / Build for freebsd_amd64 (push) Waiting to run
build / Build for linux_amd64 (push) Waiting to run
build / Build for openbsd_amd64 (push) Waiting to run
build / Build for solaris_amd64 (push) Waiting to run
build / Build for windows_amd64 (push) Waiting to run
build / Build for freebsd_arm (push) Waiting to run
build / Build for linux_arm (push) Waiting to run
build / Build for linux_arm64 (push) Waiting to run
build / Build for darwin_amd64 (push) Waiting to run
build / Build for darwin_arm64 (push) Waiting to run
build / End-to-end Tests for linux_386 (push) Waiting to run
build / End-to-end Tests for windows_386 (push) Waiting to run
build / End-to-end Tests for darwin_amd64 (push) Waiting to run
build / End-to-end Tests for linux_amd64 (push) Waiting to run
build / End-to-end Tests for windows_amd64 (push) Waiting to run
Quick Checks / List files changed for pull request (push) Waiting to run
Quick Checks / Unit tests for linux_386 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for windows_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm (push) Blocked by required conditions
Quick Checks / Unit tests for darwin_arm64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm64 (push) Blocked by required conditions
Quick Checks / Race Tests (push) Blocked by required conditions
Quick Checks / End-to-end Tests (push) Blocked by required conditions
Quick Checks / Code Consistency Checks (push) Blocked by required conditions
Quick Checks / License Checks (push) Waiting to run
Website checks / List files changed for pull request (push) Waiting to run
Website checks / Build (push) Blocked by required conditions
Website checks / Test Installation Instructions (push) Blocked by required conditions
Signed-off-by: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
186 lines
4.5 KiB
Go
186 lines
4.5 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package command
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
"github.com/opentofu/opentofu/internal/command/workdir"
|
|
)
|
|
|
|
func TestProviders(t *testing.T) {
|
|
t.Chdir(testFixturePath("providers/basic"))
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
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.opentofu.org/hashicorp/foo]",
|
|
"provider[registry.opentofu.org/hashicorp/bar]",
|
|
"provider[registry.opentofu.org/hashicorp/baz]",
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviders_noConfigs(t *testing.T) {
|
|
t.Chdir(testFixturePath(""))
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
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()
|
|
expectedErrMsg := "No configuration files"
|
|
if !strings.Contains(output, expectedErrMsg) {
|
|
t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output)
|
|
}
|
|
}
|
|
|
|
func TestProviders_modules(t *testing.T) {
|
|
td := t.TempDir()
|
|
testCopyDir(t, testFixturePath("providers/modules"), td)
|
|
t.Chdir(td)
|
|
|
|
// first run init with mock provider sources to install the module
|
|
providerSource, close := newMockProviderSource(t, map[string][]string{
|
|
"foo": {"1.0.0"},
|
|
"bar": {"2.0.0"},
|
|
"baz": {"1.2.2"},
|
|
})
|
|
defer close()
|
|
view, done := testView(t)
|
|
initMeta := Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
View: view,
|
|
ProviderSource: providerSource,
|
|
}
|
|
ic := &InitCommand{
|
|
Meta: initMeta,
|
|
}
|
|
code := ic.Run([]string{})
|
|
output := done(t)
|
|
if code != 0 {
|
|
t.Fatalf("init failed\n%s", output.Stderr())
|
|
}
|
|
|
|
// Providers command
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
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.opentofu.org/hashicorp/foo] 1.0.0", // from required_providers
|
|
"provider[registry.opentofu.org/hashicorp/bar] 2.0.0", // from provider config
|
|
"── module.kiddo", // tree node for child module
|
|
"provider[registry.opentofu.org/hashicorp/baz]", // implied by a resource in the child module
|
|
}
|
|
|
|
stdout := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(stdout, want) {
|
|
t.Errorf("output missing %s:\n%s", want, stdout)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviders_state(t *testing.T) {
|
|
t.Chdir(testFixturePath("providers/state"))
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
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.opentofu.org/hashicorp/foo] 1.0.0", // from required_providers
|
|
"provider[registry.opentofu.org/hashicorp/bar] 2.0.0", // from a provider config block
|
|
"Providers required by state", // header for state providers
|
|
"provider[registry.opentofu.org/hashicorp/baz]", // from a resource 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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProviders_tests(t *testing.T) {
|
|
t.Chdir(testFixturePath("providers/tests"))
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ProvidersCommand{
|
|
Meta: Meta{
|
|
WorkingDir: workdir.NewDir("."),
|
|
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.opentofu.org/hashicorp/foo]",
|
|
"test.main",
|
|
"provider[registry.opentofu.org/hashicorp/bar]",
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
for _, want := range wantOutput {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("output missing %s:\n%s", want, output)
|
|
}
|
|
}
|
|
}
|