Skip to content

Commit

Permalink
Fix terraform state replace-provider test for opentofu registry
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Nov 16, 2023
1 parent ee29c5e commit bbfae23
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions tfexec/terraform_state_replace_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,27 @@ resource "null_resource" "bar" {}
t.Fatalf("failed to run terraform state pull: %s", err)
}

stateProvider := "registry.terraform.io/hashicorp/null"
execType, _, err := terraformCLI.Version(context.Background())
if err != nil {
t.Fatalf("failed to detect execType: %s", err)
}
fromProviderFQN := ""
switch execType {
case "terraform":
fromProviderFQN = "registry.terraform.io/hashicorp/null"
case "opentofu":
fromProviderFQN = "registry.opentofu.org/hashicorp/null"
}

if !strings.Contains(string(state.Bytes()), stateProvider) {
t.Errorf("state does not contain provider: %s", stateProvider)
if !strings.Contains(string(state.Bytes()), fromProviderFQN) {
t.Errorf("state does not contain provider: %s", fromProviderFQN)
}

gotProviders, err := terraformCLI.Providers(context.Background())
if err != nil {
t.Fatalf("failed to run terraform providers: %s", err)
}

execType, _, err := terraformCLI.Version(context.Background())
if err != nil {
t.Fatalf("failed to detect execType: %s", err)
}
wantProviders := ""
switch execType {
case "terraform":
Expand All @@ -335,7 +341,7 @@ resource "null_resource" "bar" {}
t.Errorf("got: %s, want: %s", gotProviders, wantProviders)
}

updatedState, err := terraformCLI.StateReplaceProvider(context.Background(), state, "registry.terraform.io/hashicorp/null", "registry.tfmigrate.io/hashicorp/null", "-auto-approve")
updatedState, err := terraformCLI.StateReplaceProvider(context.Background(), state, fromProviderFQN, "registry.tfmigrate.io/hashicorp/null", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform state replace-provider: %s", err)
}
Expand All @@ -344,7 +350,7 @@ resource "null_resource" "bar" {}
t.Errorf("state does not contain updated provider: %s", "registry.tfmigrate.io/hashicorp/null")
}

if strings.Contains(string(updatedState.Bytes()), "registry.terraform.io/hashicorp/null") {
t.Errorf("state contains old provider: %s", "registry.terraform.io/hashicorp/null")
if strings.Contains(string(updatedState.Bytes()), fromProviderFQN) {
t.Errorf("state contains old provider: %s", fromProviderFQN)
}
}

0 comments on commit bbfae23

Please sign in to comment.