Merge pull request #250 from opentffoundation/test-refresh-only-test

Add tests for `opentf test` with `plan_options`
This commit is contained in:
RLRabinowitz 2023-09-04 14:53:32 +03:00 committed by GitHub
commit cd1e084fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 167 additions and 29 deletions

View File

@ -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) {
@ -223,6 +227,10 @@ func TestTest_Broken_Full_Output(t *testing.T) {
expected: "Blocks of type \"check\" are not expected here.",
code: 1,
},
"refresh_conflicting_config": {
expected: "Incompatible plan options",
code: 1,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
@ -991,28 +999,13 @@ 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
expectedErr string
expectedCode int
}{
"partial_updates": {
expectedOut: `main.tftest.hcl... pass
run "first"... pass
Warning: Resource targeting is in effect
@ -1039,15 +1032,87 @@ 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.
Failure! 0 passed, 1 failed.
`,
expectedErr: `
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.
`,
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)
actualOut, expectedOut := output.Stdout(), tc.expectedOut
actualErr, expectedErr := output.Stderr(), tc.expectedErr
expectedCode := tc.expectedCode
if code != expectedCode {
t.Errorf("expected status code %d but got %d", expectedCode, code)
}
if diff := cmp.Diff(actualOut, expectedOut); len(diff) > 0 {
t.Errorf("output didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expectedOut, actualOut, diff)
}
if diff := cmp.Diff(actualErr, expectedErr); len(diff) > 0 {
t.Errorf("error didn't match expected:\nexpected:\n%s\nactual:\n%s\ndiff:\n%s", expectedErr, actualErr, diff)
}
if provider.ResourceCount() > 0 {
t.Errorf("should have deleted all resources on completion but left %v", provider.ResourceString())
}
})
}
}

View File

@ -0,0 +1,7 @@
resource "test_resource" "foo" {
value = "foo"
}
resource "test_resource" "bar" {
value = "bar"
}

View File

@ -0,0 +1,10 @@
run "partial" {
plan_options {
target = [test_resource.foo]
}
assert {
condition = test_resource.bar.value == "bar"
error_message = "should fail"
}
}

View File

@ -7,4 +7,4 @@ run "first" {
}
}
run "second" {}
run "second" {}

View File

@ -0,0 +1,3 @@
resource "test_resource" "foo" {
value = "bar"
}

View File

@ -0,0 +1,6 @@
run "apply" {
plan_options {
mode=refresh-only
refresh=false
}
}

View File

@ -0,0 +1,5 @@
variable "input" {}
resource "test_resource" "foo" {
value = var.input
}

View File

@ -0,0 +1,42 @@
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"
}
}