diff --git a/internal/command/test_test.go b/internal/command/test_test.go index 09139f0913..b9607fb6bb 100644 --- a/internal/command/test_test.go +++ b/internal/command/test_test.go @@ -148,6 +148,10 @@ func TestTest(t *testing.T) { expected: "1 passed, 0 failed.", code: 0, }, + "refresh_only": { + expected: "3 passed, 0 failed.", + code: 0, + }, } for name, tc := range tcs { t.Run(name, func(t *testing.T) { @@ -991,28 +995,12 @@ Success! 2 passed, 0 failed. } func TestTest_PartialUpdates(t *testing.T) { - td := t.TempDir() - testCopyDir(t, testFixturePath(path.Join("test", "partial_updates")), td) - defer testChdir(t, td)() - - provider := testing_command.NewProvider(nil) - view, done := testView(t) - - c := &TestCommand{ - Meta: Meta{ - testingOverrides: metaOverridesForProvider(provider.Provider), - View: view, - }, - } - - code := c.Run([]string{"-no-color"}) - output := done(t) - - if code != 0 { - t.Errorf("expected status code 0 but got %d", code) - } - - expected := `main.tftest.hcl... pass + tcs := map[string]struct { + expectedOut string + expectedCode int + }{ + "partial_updates": { + expectedOut: `main.tftest.hcl... pass run "first"... pass Warning: Resource targeting is in effect @@ -1039,15 +1027,83 @@ or when OpenTF specifically suggests to use it as part of an error message. run "second"... pass Success! 2 passed, 0 failed. -` +`, + expectedCode: 0, + }, + "partial_update_failure": { + expectedOut: `main.tftest.hcl... fail + run "partial"... fail - actual := output.All() +Warning: Resource targeting is in effect - if diff := cmp.Diff(actual, expected); len(diff) > 0 { - t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expected, actual, diff) +You are creating a plan with the -target option, which means that the result +of this plan may not represent all of the changes requested by the current +configuration. + +The -target option is not for routine use, and is provided only for +exceptional situations such as recovering from errors or mistakes, or when +OpenTF specifically suggests to use it as part of an error message. + +Warning: Applied changes may be incomplete + +The plan was created with the -target option in effect, so some changes +requested in the configuration may have been ignored and the output values +may not be fully updated. Run the following command to verify that no other +changes are pending: + opentf plan + +Note that the -target option is not suitable for routine use, and is provided +only for exceptional situations such as recovering from errors or mistakes, +or when OpenTF specifically suggests to use it as part of an error message. + +Error: Unknown condition run + + on main.tftest.hcl line 7, in run "partial": + 7: condition = test_resource.bar.value == "bar" + +Condition expression could not be evaluated at this time. + +Failure! 0 passed, 1 failed. +`, + expectedCode: 1, + }, } - if provider.ResourceCount() > 0 { - t.Errorf("should have deleted all resources on completion but left %v", provider.ResourceString()) + for file, tc := range tcs { + t.Run(file, func(t *testing.T) { + td := t.TempDir() + testCopyDir(t, testFixturePath(path.Join("test", file)), td) + defer testChdir(t, td)() + + provider := testing_command.NewProvider(nil) + view, done := testView(t) + + c := &TestCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(provider.Provider), + View: view, + }, + } + + code := c.Run([]string{"-no-color"}) + output := done(t) + + expectedOut := tc.expectedOut + expectedCode := tc.expectedCode + + if code != expectedCode { + t.Errorf("expected status code %d but got %d", expectedCode, code) + } + + actual := output.All() + + if diff := cmp.Diff(actual, expectedOut); len(diff) > 0 { + t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expectedOut, actual, diff) + } + + if provider.ResourceCount() > 0 { + t.Errorf("should have deleted all resources on completion but left %v", provider.ResourceString()) + } + }) } } diff --git a/internal/command/testdata/test/partial_update_failure/main.tf b/internal/command/testdata/test/partial_update_failure/main.tf new file mode 100644 index 0000000000..317f84ac4e --- /dev/null +++ b/internal/command/testdata/test/partial_update_failure/main.tf @@ -0,0 +1,7 @@ +resource "test_resource" "foo" { + value = "foo" +} + +resource "test_resource" "bar" { + value = "bar" +} \ No newline at end of file diff --git a/internal/command/testdata/test/partial_update_failure/main.tftest.hcl b/internal/command/testdata/test/partial_update_failure/main.tftest.hcl new file mode 100644 index 0000000000..448be252e0 --- /dev/null +++ b/internal/command/testdata/test/partial_update_failure/main.tftest.hcl @@ -0,0 +1,10 @@ +run "partial" { + plan_options { + target = [test_resource.foo] + } + + assert { + condition = test_resource.bar.value == "bar" + error_message = "should fail" + } +} \ No newline at end of file diff --git a/internal/command/testdata/test/refresh_only/main.tf b/internal/command/testdata/test/refresh_only/main.tf new file mode 100644 index 0000000000..07016c4a0b --- /dev/null +++ b/internal/command/testdata/test/refresh_only/main.tf @@ -0,0 +1,5 @@ +variable "input" {} + +resource "test_resource" "foo" { + value = var.input +} \ No newline at end of file diff --git a/internal/command/testdata/test/refresh_only/main.tftest.hcl b/internal/command/testdata/test/refresh_only/main.tftest.hcl new file mode 100644 index 0000000000..7a52f1cf08 --- /dev/null +++ b/internal/command/testdata/test/refresh_only/main.tftest.hcl @@ -0,0 +1,45 @@ +run "first" { + variables { + input = "first" + } + + assert { + condition = test_resource.foo.value == "first" + error_message = "invalid value" + } +} + +run "second" { + command=plan + plan_options { + mode=refresh-only + } + + variables { + input = "second" + } + + assert { + condition = test_resource.foo.value == "first" + error_message = "invalid value" + } +} + +run "third" { + command=plan + plan_options { + mode=normal + } + + variables { + input = "second" + } + + assert { + condition = test_resource.foo.value == "second" + error_message = "invalid value" + } +} + + +