-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Configuring when state checks are executed. * Testing that state checks are executed.
- Loading branch information
1 parent
198c751
commit f67b3e7
Showing
7 changed files
with
242 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package resource | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/mitchellh/go-testing-interface" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
) | ||
|
||
func runStateChecks(ctx context.Context, t testing.T, state *tfjson.State, stateChecks []statecheck.StateCheck) error { | ||
t.Helper() | ||
|
||
var result []error | ||
|
||
for _, stateCheck := range stateChecks { | ||
resp := statecheck.CheckStateResponse{} | ||
stateCheck.CheckState(ctx, statecheck.CheckStateRequest{State: state}, &resp) | ||
|
||
result = append(result, resp.Error) | ||
} | ||
|
||
return errors.Join(result...) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package resource | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
) | ||
|
||
var _ statecheck.StateCheck = &stateCheckSpy{} | ||
|
||
type stateCheckSpy struct { | ||
err error | ||
called bool | ||
} | ||
|
||
func (s *stateCheckSpy) CheckState(ctx context.Context, req statecheck.CheckStateRequest, resp *statecheck.CheckStateResponse) { | ||
s.called = true | ||
resp.Error = s.err | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// Package statecheck contains the state check interface, request/response structs, and common state check implementations. | ||
package statecheck |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package statecheck | ||
|
||
import ( | ||
"context" | ||
|
||
tfjson "github.com/hashicorp/terraform-json" | ||
) | ||
|
||
// StateCheck defines an interface for implementing test logic that checks a state file and then returns an error | ||
// if the state file does not match what is expected. | ||
type StateCheck interface { | ||
// CheckState should perform the state check. | ||
CheckState(context.Context, CheckStateRequest, *CheckStateResponse) | ||
} | ||
|
||
// CheckStateRequest is a request for an invoke of the CheckState function. | ||
type CheckStateRequest struct { | ||
// State represents a parsed state file, retrieved via the `terraform show -json` command. | ||
State *tfjson.State | ||
} | ||
|
||
// CheckStateResponse is a response to an invoke of the CheckState function. | ||
type CheckStateResponse struct { | ||
// Error is used to report the failure of a state check assertion and is combined with other StateCheck errors | ||
// to be reported as a test failure. | ||
Error error | ||
} |