diff --git a/internal/command/init_test.go b/internal/command/init_test.go index 0eaca44391..7ea583c65c 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -202,6 +202,47 @@ func TestInit_two_step_provider_download(t *testing.T) { - Using previously-installed hashicorp/random v1.0.0`, }, }, + // Same tests with providers shared between config and state, + // but now the version constraint in config specifies a pre-release + "pre-release required by only the state file": { + workDirPath: "init-provider-download-prerelease/state-file-only", + expectedDownloadMsgs: []string{ + views.MessageRegistry[views.OutputInitSuccessCLIMessage].JSONValue, + `Initializing provider plugins found in the configuration... + Initializing the backend...`, // No providers found in the configuration so next output is backend-related + `Initializing provider plugins found in the state... + - Finding latest version of hashicorp/random... + - Installing hashicorp/random v9.9.9...`, // The latest version is expected, as state has no version constraints + }, + }, + "pre-release not re-downloaded if present in both config and state": { + workDirPath: "init-provider-download-prerelease/config-and-state-same-providers", + expectedDownloadMsgs: []string{ + // Config + `Initializing provider plugins found in the configuration... + - Finding hashicorp/random versions matching "1.2.3-beta"... + - Installing hashicorp/random v1.2.3-beta... + - Installed hashicorp/random v1.2.3-beta`, + // State + `Initializing provider plugins found in the state... + - Reusing previous version of hashicorp/random + - Using previously-installed hashicorp/random v1.2.3-beta`, + }, + }, + "reuses pre-release provider already represented in a dependency lock file": { + workDirPath: "init-provider-download-prerelease/config-state-file-and-lockfile", + expectedDownloadMsgs: []string{ + // Config + `Initializing provider plugins found in the configuration... + - Reusing previous version of hashicorp/random from the dependency lock file + - Installing hashicorp/random v1.2.3-beta... + - Installed hashicorp/random v1.2.3-beta`, + // State + `Initializing provider plugins found in the state... + - Reusing previous version of hashicorp/random + - Using previously-installed hashicorp/random v1.2.3-beta`, + }, + }, } for tn, tc := range cases { diff --git a/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/main.tf b/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/main.tf new file mode 100644 index 0000000000..cfb4744fe8 --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/main.tf @@ -0,0 +1,14 @@ +terraform { + required_providers { + random = { + source = "hashicorp/random" + version = "1.2.3-beta" + } + } + + backend "local" { + path = "./state-using-random-provider.tfstate" + } +} + +resource "random_pet" "maurice" {} diff --git a/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/state-using-random-provider.tfstate b/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/state-using-random-provider.tfstate new file mode 100644 index 0000000000..76b543b06a --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/config-and-state-same-providers/state-using-random-provider.tfstate @@ -0,0 +1,28 @@ +{ + "version": 4, + "terraform_version": "1.14.0", + "serial": 1, + "lineage": "foobar", + "resources": [ + { + "mode": "managed", + "type": "random_pet", + "name": "maurice", + "provider": "provider[\"registry.terraform.io/hashicorp/random\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "id": "sassy-ferret", + "keepers": null, + "length": 2, + "prefix": null, + "separator": "-" + }, + "sensitive_attributes": [], + "identity_schema_version": 0 + } + ] + } + ] +} \ No newline at end of file diff --git a/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/.terraform.lock.hcl b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/.terraform.lock.hcl new file mode 100644 index 0000000000..3883a6abbe --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/.terraform.lock.hcl @@ -0,0 +1,6 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/random" { + version = "1.2.3-beta" +} diff --git a/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/main.tf b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/main.tf new file mode 100644 index 0000000000..cfb4744fe8 --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/main.tf @@ -0,0 +1,14 @@ +terraform { + required_providers { + random = { + source = "hashicorp/random" + version = "1.2.3-beta" + } + } + + backend "local" { + path = "./state-using-random-provider.tfstate" + } +} + +resource "random_pet" "maurice" {} diff --git a/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/state-using-random-provider.tfstate b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/state-using-random-provider.tfstate new file mode 100644 index 0000000000..76b543b06a --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/config-state-file-and-lockfile/state-using-random-provider.tfstate @@ -0,0 +1,28 @@ +{ + "version": 4, + "terraform_version": "1.14.0", + "serial": 1, + "lineage": "foobar", + "resources": [ + { + "mode": "managed", + "type": "random_pet", + "name": "maurice", + "provider": "provider[\"registry.terraform.io/hashicorp/random\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "id": "sassy-ferret", + "keepers": null, + "length": 2, + "prefix": null, + "separator": "-" + }, + "sensitive_attributes": [], + "identity_schema_version": 0 + } + ] + } + ] +} \ No newline at end of file diff --git a/internal/command/testdata/init-provider-download-prerelease/state-file-only/main.tf b/internal/command/testdata/init-provider-download-prerelease/state-file-only/main.tf new file mode 100644 index 0000000000..d568a66bf2 --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/state-file-only/main.tf @@ -0,0 +1,5 @@ +terraform { + backend "local" { + path = "./state-using-random-provider.tfstate" + } +} diff --git a/internal/command/testdata/init-provider-download-prerelease/state-file-only/state-using-random-provider.tfstate b/internal/command/testdata/init-provider-download-prerelease/state-file-only/state-using-random-provider.tfstate new file mode 100644 index 0000000000..76b543b06a --- /dev/null +++ b/internal/command/testdata/init-provider-download-prerelease/state-file-only/state-using-random-provider.tfstate @@ -0,0 +1,28 @@ +{ + "version": 4, + "terraform_version": "1.14.0", + "serial": 1, + "lineage": "foobar", + "resources": [ + { + "mode": "managed", + "type": "random_pet", + "name": "maurice", + "provider": "provider[\"registry.terraform.io/hashicorp/random\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "id": "sassy-ferret", + "keepers": null, + "length": 2, + "prefix": null, + "separator": "-" + }, + "sensitive_attributes": [], + "identity_schema_version": 0 + } + ] + } + ] +} \ No newline at end of file