Skip to content

Commit fc4dc7a

Browse files
command/e2etest: provider installation with explicit install methods
This exercises the ability to customize the installation methods used by the provider plugin installer, in this case forcing the use of a custom local directory with a result essentially the same as what happens when you pass -plugin-dir to "terraform init".
1 parent 16e97a9 commit fc4dc7a

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

command/e2etest/init_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,56 @@ func TestInitProvidersLocalOnly(t *testing.T) {
173173
}
174174
}
175175

176+
func TestInitProvidersCustomMethod(t *testing.T) {
177+
t.Parallel()
178+
179+
// This test should not reach out to the network if it is behaving as
180+
// intended. If it _does_ try to access an upstream registry and encounter
181+
// an error doing so then that's a legitimate test failure that should be
182+
// fixed. (If it incorrectly reaches out anywhere then it's likely to be
183+
// to the host "example.com", which is the placeholder domain we use in
184+
// the test fixture.)
185+
186+
for _, configFile := range []string{"cliconfig.tfrc", "cliconfig.tfrc.json"} {
187+
t.Run(configFile, func(t *testing.T) {
188+
fixturePath := filepath.Join("testdata", "custom-provider-install-method")
189+
tf := e2e.NewBinary(terraformBin, fixturePath)
190+
defer tf.Close()
191+
192+
// Our fixture dir has a generic os_arch dir, which we need to customize
193+
// to the actual OS/arch where this test is running in order to get the
194+
// desired result.
195+
fixtMachineDir := tf.Path("fs-mirror/example.com/awesomecorp/happycloud/1.2.0/os_arch")
196+
wantMachineDir := tf.Path("fs-mirror/example.com/awesomecorp/happycloud/1.2.0/", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
197+
err := os.Rename(fixtMachineDir, wantMachineDir)
198+
if err != nil {
199+
t.Fatalf("unexpected error: %s", err)
200+
}
201+
202+
// We'll use a local CLI configuration file taken from our fixture
203+
// directory so we can force a custom installation method config.
204+
tf.AddEnv("TF_CLI_CONFIG_FILE=" + tf.Path(configFile))
205+
206+
stdout, stderr, err := tf.Run("init")
207+
if err != nil {
208+
t.Errorf("unexpected error: %s", err)
209+
}
210+
211+
if stderr != "" {
212+
t.Errorf("unexpected stderr output:\n%s", stderr)
213+
}
214+
215+
if !strings.Contains(stdout, "Terraform has been successfully initialized!") {
216+
t.Errorf("success message is missing from output:\n%s", stdout)
217+
}
218+
219+
if !strings.Contains(stdout, "- Installing example.com/awesomecorp/happycloud v1.2.0") {
220+
t.Errorf("provider download message is missing from output:\n%s", stdout)
221+
}
222+
})
223+
}
224+
}
225+
176226
func TestInitProviders_pluginCache(t *testing.T) {
177227
t.Parallel()
178228

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
provider_installation {
2+
filesystem_mirror {
3+
path = "./fs-mirror"
4+
}
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"provider_installation": {
3+
"filesystem_mirror": [
4+
{
5+
"path": "./fs-mirror"
6+
}
7+
]
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is not a real plugin executable. It's just here to be discovered by the
2+
provider installation process.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The purpose of this test is to refer to a provider whose address contains
2+
# a hostname that is only used for namespacing purposes and doesn't actually
3+
# have a provider registry deployed at it.
4+
#
5+
# A user can install such a provider in one of the implied local filesystem
6+
# directories and Terraform should accept that as the selection for that
7+
# provider without producing any errors about the fact that example.com
8+
# does not have a provider registry.
9+
#
10+
# For this test in particular we're using the "vendor" directory that is
11+
# the documented way to include provider plugins directly inside a
12+
# configuration uploaded to Terraform Cloud, but this functionality applies
13+
# to all of the implicit local filesystem search directories.
14+
15+
terraform {
16+
required_providers {
17+
happycloud = {
18+
source = "example.com/awesomecorp/happycloud"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)