command: test plan -refresh= arg ordering (#33483)

* main: disambiguate arg ordering test

Make it extra clear what order of args we are asserting.

* command: fix plan -refresh=false test

The test for plan -refresh=false was not functioning, since ReadResource will not be called if the resource is not in prior state.

Add a new fixture directory with state, and also test the converse, to prevent regression.

* command: add test for refresh flag precedence

A consumer relies on the fact that running terraform plan -refresh=false -refresh true gives the same result as terraform plan -refresh=true.
This commit is contained in:
kmoe 2023-07-06 19:28:09 +01:00 committed by GitHub
parent ea162f6ab5
commit d1a5dfa1ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 4 deletions

View File

@ -472,7 +472,7 @@ func TestPlan_outBackend(t *testing.T) {
func TestPlan_refreshFalse(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan"), td)
testCopyDir(t, testFixturePath("plan-existing-state"), td)
defer testChdir(t, td)()
p := planFixtureProvider()
@ -498,6 +498,71 @@ func TestPlan_refreshFalse(t *testing.T) {
}
}
func TestPlan_refreshTrue(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-existing-state"), td)
defer testChdir(t, td)()
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-refresh=true",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !p.ReadResourceCalled {
t.Fatalf("ReadResource should have been called")
}
}
// A consumer relies on the fact that running
// terraform plan -refresh=false -refresh=true gives the same result as
// terraform plan -refresh=true.
// While the flag logic itself is handled by the stdlib flags package (and code
// in main() that is tested elsewhere), we verify the overall plan command
// behaviour here in case we accidentally break this with additional logic.
func TestPlan_refreshFalseRefreshTrue(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-existing-state"), td)
defer testChdir(t, td)()
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-refresh=false",
"-refresh=true",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !p.ReadResourceCalled {
t.Fatal("ReadResource should have been called")
}
}
func TestPlan_state(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()

View File

@ -0,0 +1,13 @@
resource "test_instance" "foo" {
ami = "bar"
# This is here because at some point it caused a test failure
network_interface {
device_index = 0
description = "Main network interface"
}
}
data "test_data_source" "a" {
id = "zzzzz"
}

View File

@ -0,0 +1,23 @@
{
"version": 4,
"terraform_version": "1.6.0",
"serial": 1,
"lineage": "d496625c-bde2-aebc-f5f4-ebbf54eabed2",
"outputs": {},
"resources": [
{
"module": "module.child",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider": "provider[\"registry.terraform.io/hashicorp/test\"]",
"instances": [
{
"schema_version": 0,
"attributes": {}
}
]
}
],
"check_results": null
}

View File

@ -49,8 +49,8 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
{
"both env var and CLI",
[]string{testCommandName, "foo", "bar"},
"-foo bar",
[]string{"-foo", "bar", "foo", "bar"},
"-foo baz",
[]string{"-foo", "baz", "foo", "bar"},
false,
},
@ -143,7 +143,7 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
// Verify
if !reflect.DeepEqual(testCommand.Args, tc.Expected) {
t.Fatalf("bad: %#v", testCommand.Args)
t.Fatalf("expected args %#v but got %#v", tc.Expected, testCommand.Args)
}
})
}