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