-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing: Replace state extract with new value comparers #345
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ import ( | |
r "github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/hashicorp/terraform-plugin-testing/compare" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
"github.com/hashicorp/terraform-provider-time/internal/timetesting" | ||
) | ||
|
||
// Since the acceptance testing framework can introduce uncontrollable time delays, | ||
|
@@ -161,10 +161,8 @@ func TestResourceTimeSleepDelete(t *testing.T) { | |
func TestAccTimeSleep_CreateDuration(t *testing.T) { | ||
resourceName := "time_sleep.test" | ||
|
||
// These ID comparisons can eventually be replaced by the multiple value checks once released | ||
// in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 | ||
captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
// The id attribute should not change between test steps | ||
assertIDSame := statecheck.CompareValue(compare.ValuesSame()) | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: protoV5ProviderFactories(), | ||
|
@@ -175,7 +173,7 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { | |
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("1ms")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
captureTimeState1, | ||
assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
// This test may work in local execution but typically does not work in CI because of its reliance | ||
|
@@ -192,25 +190,18 @@ func TestAccTimeSleep_CreateDuration(t *testing.T) { | |
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.StringExact("2ms")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
captureTimeState2, | ||
assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// Ensure the id time value is different due to the sleep | ||
if captureTimeState1.Value == captureTimeState2.Value { | ||
t.Fatal("attribute values are the same") | ||
} | ||
} | ||
|
||
func TestAccTimeSleep_DestroyDuration(t *testing.T) { | ||
resourceName := "time_sleep.test" | ||
|
||
// These ID comparisons can eventually be replaced by the multiple value checks once released | ||
// in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 | ||
captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
// The id attribute should not change between test steps | ||
assertIDSame := statecheck.CompareValue(compare.ValuesSame()) | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: protoV5ProviderFactories(), | ||
|
@@ -221,7 +212,7 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { | |
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("1ms")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
captureTimeState1, | ||
assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
// This test may work in local execution but typically does not work in CI because of its reliance | ||
|
@@ -238,25 +229,18 @@ func TestAccTimeSleep_DestroyDuration(t *testing.T) { | |
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destroy_duration"), knownvalue.StringExact("2ms")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
captureTimeState2, | ||
assertIDSame.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// Ensure the id time value is different due to the sleep | ||
if captureTimeState1.Value == captureTimeState2.Value { | ||
t.Fatal("attribute values are the same") | ||
} | ||
Comment on lines
-247
to
-250
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
func TestAccTimeSleep_Triggers(t *testing.T) { | ||
resourceName := "time_sleep.test" | ||
|
||
// These ID comparisons can eventually be replaced by the multiple value checks once released | ||
// in terraform-plugin-testing: https://github.com/hashicorp/terraform-plugin-testing/issues/295 | ||
captureTimeState1 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
captureTimeState2 := timetesting.NewExtractState(resourceName, tfjsonpath.New("id")) | ||
// Due to the time.Sleep, the id attribute should differ between test steps | ||
assertIDUpdated := statecheck.CompareValue(compare.ValuesDiffer()) | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: protoV5ProviderFactories(), | ||
|
@@ -269,7 +253,7 @@ func TestAccTimeSleep_Triggers(t *testing.T) { | |
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.NotNull()), | ||
captureTimeState1, | ||
assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
// This test may work in local execution but typically does not work in CI because of its reliance | ||
|
@@ -289,16 +273,11 @@ func TestAccTimeSleep_Triggers(t *testing.T) { | |
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("triggers").AtMapKey("key1"), knownvalue.StringExact("value1updated")), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("id"), knownvalue.NotNull()), | ||
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("create_duration"), knownvalue.NotNull()), | ||
captureTimeState2, | ||
assertIDUpdated.AddStateValue(resourceName, tfjsonpath.New("id")), | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// Ensure the id time value is different due to the sleep | ||
if captureTimeState1.Value == captureTimeState2.Value { | ||
t.Fatal("attribute values are the same") | ||
} | ||
} | ||
|
||
func TestAccTimeSleep_Upgrade(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick note here since I'm reversing the assertion of this test. The original test was actually asserting the values were the same, but I must've missed that in #295, where I attempted to add this (which it would have failed if I wrote it correctly 😆). This test has been passing since because it's comparing pointer addresses and not the actual string values.