github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/providers_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestProviders(t *testing.T) {
    12  	cwd, err := os.Getwd()
    13  	if err != nil {
    14  		t.Fatalf("err: %s", err)
    15  	}
    16  	if err := os.Chdir(testFixturePath("providers/basic")); err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  	defer os.Chdir(cwd)
    20  
    21  	ui := new(cli.MockUi)
    22  	c := &ProvidersCommand{
    23  		Meta: Meta{
    24  			Ui: ui,
    25  		},
    26  	}
    27  
    28  	args := []string{}
    29  	if code := c.Run(args); code != 0 {
    30  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    31  	}
    32  
    33  	wantOutput := []string{
    34  		"provider[registry.terraform.io/hashicorp/foo]",
    35  		"provider[registry.terraform.io/hashicorp/bar]",
    36  		"provider[registry.terraform.io/hashicorp/baz]",
    37  	}
    38  
    39  	output := ui.OutputWriter.String()
    40  	for _, want := range wantOutput {
    41  		if !strings.Contains(output, want) {
    42  			t.Errorf("output missing %s:\n%s", want, output)
    43  		}
    44  	}
    45  }
    46  
    47  func TestProviders_noConfigs(t *testing.T) {
    48  	cwd, err := os.Getwd()
    49  	if err != nil {
    50  		t.Fatalf("err: %s", err)
    51  	}
    52  	if err := os.Chdir(testFixturePath("")); err != nil {
    53  		t.Fatalf("err: %s", err)
    54  	}
    55  	defer os.Chdir(cwd)
    56  
    57  	ui := new(cli.MockUi)
    58  	c := &ProvidersCommand{
    59  		Meta: Meta{
    60  			Ui: ui,
    61  		},
    62  	}
    63  
    64  	args := []string{}
    65  	if code := c.Run(args); code == 0 {
    66  		t.Fatal("expected command to return non-zero exit code" +
    67  			" when no configs are available")
    68  	}
    69  
    70  	output := ui.ErrorWriter.String()
    71  	expectedErrMsg := "No configuration files"
    72  	if !strings.Contains(output, expectedErrMsg) {
    73  		t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output)
    74  	}
    75  }
    76  
    77  func TestProviders_modules(t *testing.T) {
    78  	td := t.TempDir()
    79  	testCopyDir(t, testFixturePath("providers/modules"), td)
    80  	defer testChdir(t, td)()
    81  
    82  	// first run init with mock provider sources to install the module
    83  	initUi := new(cli.MockUi)
    84  	providerSource, close := newMockProviderSource(t, map[string][]string{
    85  		"foo": {"1.0.0"},
    86  		"bar": {"2.0.0"},
    87  		"baz": {"1.2.2"},
    88  	})
    89  	defer close()
    90  	m := Meta{
    91  		testingOverrides: metaOverridesForProvider(testProvider()),
    92  		Ui:               initUi,
    93  		ProviderSource:   providerSource,
    94  	}
    95  	ic := &InitCommand{
    96  		Meta: m,
    97  	}
    98  	if code := ic.Run([]string{}); code != 0 {
    99  		t.Fatalf("init failed\n%s", initUi.ErrorWriter)
   100  	}
   101  
   102  	// Providers command
   103  	ui := new(cli.MockUi)
   104  	c := &ProvidersCommand{
   105  		Meta: Meta{
   106  			Ui: ui,
   107  		},
   108  	}
   109  
   110  	args := []string{}
   111  	if code := c.Run(args); code != 0 {
   112  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   113  	}
   114  
   115  	wantOutput := []string{
   116  		"provider[registry.terraform.io/hashicorp/foo] 1.0.0", // from required_providers
   117  		"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from provider config
   118  		"── module.kiddo",                               // tree node for child module
   119  		"provider[registry.terraform.io/hashicorp/baz]", // implied by a resource in the child module
   120  	}
   121  
   122  	output := ui.OutputWriter.String()
   123  	for _, want := range wantOutput {
   124  		if !strings.Contains(output, want) {
   125  			t.Errorf("output missing %s:\n%s", want, output)
   126  		}
   127  	}
   128  }
   129  
   130  func TestProviders_state(t *testing.T) {
   131  	cwd, err := os.Getwd()
   132  	if err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  	if err := os.Chdir(testFixturePath("providers/state")); err != nil {
   136  		t.Fatalf("err: %s", err)
   137  	}
   138  	defer os.Chdir(cwd)
   139  
   140  	ui := new(cli.MockUi)
   141  	c := &ProvidersCommand{
   142  		Meta: Meta{
   143  			Ui: ui,
   144  		},
   145  	}
   146  
   147  	args := []string{}
   148  	if code := c.Run(args); code != 0 {
   149  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   150  	}
   151  
   152  	wantOutput := []string{
   153  		"provider[registry.terraform.io/hashicorp/foo] 1.0.0", // from required_providers
   154  		"provider[registry.terraform.io/hashicorp/bar] 2.0.0", // from a provider config block
   155  		"Providers required by state",                         // header for state providers
   156  		"provider[registry.terraform.io/hashicorp/baz]",       // from a resouce in state (only)
   157  	}
   158  
   159  	output := ui.OutputWriter.String()
   160  	for _, want := range wantOutput {
   161  		if !strings.Contains(output, want) {
   162  			t.Errorf("output missing %s:\n%s", want, output)
   163  		}
   164  	}
   165  }