Skip to content

Commit 0ea0c6f

Browse files
committed
Add test case to cover use of the -upgrade flag
1 parent e56ecdf commit 0ea0c6f

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

internal/command/init.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -946,16 +946,17 @@ func (c *InitCommand) getProvidersFromState(ctx context.Context, state *states.S
946946
}
947947
}
948948

949-
// Previous locks from dep locks file are needed so we don't re-download any providers
950-
// Here we also need to incorporate the locks derived from the config, to avoid downloading
951-
// a provider twice.
949+
// The locks below are used to avoid re-downloading any providers in the
950+
// second download step.
951+
// We combine any locks from the dependency lock file and locks identified
952+
// from the configuration
953+
var moreDiags tfdiags.Diagnostics
952954
previousLocks, moreDiags := c.lockedDependencies()
953955
diags = diags.Append(moreDiags)
954-
inProgressLocks := c.mergeLockedDependencies(configLocks, previousLocks)
955-
956956
if diags.HasErrors() {
957957
return false, nil, diags
958958
}
959+
inProgressLocks := c.mergeLockedDependencies(configLocks, previousLocks)
959960

960961
var inst *providercache.Installer
961962
if len(pluginDirs) == 0 {
@@ -992,7 +993,10 @@ func (c *InitCommand) getProvidersFromState(ctx context.Context, state *states.S
992993
return true, nil, diags
993994
}
994995

995-
mode = providercache.InstallUpgrades
996+
// We don't set `mode = providercache.InstallUpgrades` here when downloading providers from
997+
// state, as it'll cause Terraform to download provider versions that don't match version
998+
// constraints in the config.
999+
// Instead, leave `mode` as `providercache.InstallNewProvidersOnly`.
9961000
}
9971001
newLocks, err := inst.EnsureProviderVersions(ctx, inProgressLocks, reqs, mode)
9981002
if ctx.Err() == context.Canceled {

internal/command/init_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func TestInit_two_step_provider_download(t *testing.T) {
115115

116116
cases := map[string]struct {
117117
workDirPath string
118+
flags []string
118119
expectedDownloadMsgs []string
119120
}{
120121
"providers required by only the state file": {
@@ -173,6 +174,21 @@ func TestInit_two_step_provider_download(t *testing.T) {
173174
- Using previously-installed hashicorp/random v1.0.0`,
174175
},
175176
},
177+
"using the -upgrade flag causes provider download to ignore the lock file": {
178+
workDirPath: "init-provider-download/config-state-file-and-lockfile",
179+
flags: []string{"-upgrade"},
180+
expectedDownloadMsgs: []string{
181+
// Config - lock file is not mentioned due to the -upgrade flag
182+
`Initializing provider plugins found in the configuration...
183+
- Finding hashicorp/random versions matching "< 9.0.0"...
184+
- Installing hashicorp/random v1.0.0...
185+
- Installed hashicorp/random v1.0.0`,
186+
// State - reuses the provider download from the config
187+
`Initializing provider plugins found in the state...
188+
- Reusing previous version of hashicorp/random from the dependency lock file
189+
- Using previously-installed hashicorp/random v1.0.0`,
190+
},
191+
},
176192
}
177193

178194
for tn, tc := range cases {
@@ -204,7 +220,7 @@ func TestInit_two_step_provider_download(t *testing.T) {
204220
},
205221
}
206222

207-
args := []string{"-enable-pluggable-state-storage-experiment"} // Needed to test init changes for PSS project
223+
args := append(tc.flags, "-enable-pluggable-state-storage-experiment") // Needed to test init changes for PSS project
208224
if code := c.Run(args); code != 0 {
209225
t.Fatalf("bad: \n%s", done(t).All())
210226
}

0 commit comments

Comments
 (0)