mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
This extends statemgr.Persistent, statemgr.Locker and remote.Client to all expect context.Context parameters, and then updates all of the existing implementations of those interfaces to support them. All of the calls to statemgr.Persistent and statemgr.Locker methods outside of tests are consistently context.TODO() for now, because the caller landscape of these interfaces has some complications: 1. statemgr.Locker is also used by the clistate package for its state implementation that was derived from statemgr.Filesystem's predecessor, even though what clistate manages is not actually "state" in the sense of package statemgr. The callers of that are not yet ready to provide real contexts. In a future commit we'll either need to plumb context through to all of the clistate callers, or continue the effort to separate statemgr from clistate by introducing a clistate-specific "locker" API for it to use instead. 2. We call statemgr.Persistent and statemgr.Locker methods in situations where the active context might have already been cancelled, and so we'll need to make sure to ignore cancellation when calling those. This is mainly limited to PersistState and Unlock, since both need to be able to complete after a cancellation, but there are various codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so it isn't yet clear where is the best place to enforce the invariant that Persist and Unlock must not be called with a cancelable context. We'll deal with that more in subsequent commits. Within the various state manager and remote client implementations the contexts _are_ wired together as best as possible with how these subsystems are already laid out, and so once we deal with the problems above and make callers provide suitable contexts they should be able to reach all of the leaf API clients that might want to generate OpenTelemetry traces. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
325 lines
8.9 KiB
Go
325 lines
8.9 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package local
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/opentofu/opentofu/internal/states"
|
|
"github.com/opentofu/opentofu/internal/states/statemgr"
|
|
"github.com/opentofu/opentofu/internal/tofu"
|
|
)
|
|
|
|
func TestStateHook_impl(t *testing.T) {
|
|
var _ tofu.Hook = new(StateHook)
|
|
}
|
|
|
|
func TestStateHook(t *testing.T) {
|
|
is := statemgr.NewTransientInMemory(nil)
|
|
var hook tofu.Hook = &StateHook{StateMgr: is}
|
|
|
|
s := statemgr.TestFullInitialState()
|
|
action, err := hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if action != tofu.HookActionContinue {
|
|
t.Fatalf("bad: %v", action)
|
|
}
|
|
if !is.State().Equal(s) {
|
|
t.Fatalf("bad state: %#v", is.State())
|
|
}
|
|
}
|
|
|
|
func TestStateHookStopping(t *testing.T) {
|
|
is := &testPersistentState{}
|
|
hook := &StateHook{
|
|
StateMgr: is,
|
|
Schemas: &tofu.Schemas{},
|
|
PersistInterval: 4 * time.Hour,
|
|
intermediatePersist: IntermediateStatePersistInfo{
|
|
LastPersist: time.Now(),
|
|
},
|
|
}
|
|
|
|
s := statemgr.TestFullInitialState()
|
|
action, err := hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if got, want := action, tofu.HookActionContinue; got != want {
|
|
t.Fatalf("wrong hookaction %#v; want %#v", got, want)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted != nil {
|
|
t.Fatalf("persisted too soon")
|
|
}
|
|
|
|
// We'll now force lastPersist to be long enough ago that persisting
|
|
// should be due on the next call.
|
|
hook.intermediatePersist.LastPersist = time.Now().Add(-5 * time.Hour)
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
|
|
gotLog := is.CallLog
|
|
wantLog := []string{
|
|
// Initial call before we reset lastPersist
|
|
"WriteState",
|
|
|
|
// Write and then persist after we reset lastPersist
|
|
"WriteState",
|
|
"PersistState",
|
|
|
|
// Final call when persisting wasn't due yet.
|
|
"WriteState",
|
|
}
|
|
if diff := cmp.Diff(wantLog, gotLog); diff != "" {
|
|
t.Fatalf("wrong call log so far\n%s", diff)
|
|
}
|
|
|
|
// We'll reset the log now before we try seeing what happens after
|
|
// we use "Stopped".
|
|
is.CallLog = is.CallLog[:0]
|
|
is.Persisted = nil
|
|
|
|
hook.Stopping()
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
|
|
is.Persisted = nil
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
is.Persisted = nil
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
|
|
gotLog = is.CallLog
|
|
wantLog = []string{
|
|
// "Stopping" immediately persisted
|
|
"PersistState",
|
|
|
|
// PostStateUpdate then writes and persists on every call,
|
|
// on the assumption that we're now bailing out after
|
|
// being cancelled and trying to save as much state as we can.
|
|
"WriteState",
|
|
"PersistState",
|
|
"WriteState",
|
|
"PersistState",
|
|
}
|
|
if diff := cmp.Diff(wantLog, gotLog); diff != "" {
|
|
t.Fatalf("wrong call log once in stopping mode\n%s", diff)
|
|
}
|
|
}
|
|
|
|
func TestStateHookCustomPersistRule(t *testing.T) {
|
|
is := &testPersistentStateThatRefusesToPersist{}
|
|
hook := &StateHook{
|
|
StateMgr: is,
|
|
Schemas: &tofu.Schemas{},
|
|
PersistInterval: 4 * time.Hour,
|
|
intermediatePersist: IntermediateStatePersistInfo{
|
|
LastPersist: time.Now(),
|
|
},
|
|
}
|
|
|
|
s := statemgr.TestFullInitialState()
|
|
action, err := hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if got, want := action, tofu.HookActionContinue; got != want {
|
|
t.Fatalf("wrong hookaction %#v; want %#v", got, want)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted != nil {
|
|
t.Fatalf("persisted too soon")
|
|
}
|
|
|
|
// We'll now force lastPersist to be long enough ago that persisting
|
|
// should be due on the next call.
|
|
hook.intermediatePersist.LastPersist = time.Now().Add(-5 * time.Hour)
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted != nil {
|
|
t.Fatalf("has a persisted state, but shouldn't")
|
|
}
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Written == nil || !is.Written.Equal(s) {
|
|
t.Fatalf("mismatching state written")
|
|
}
|
|
if is.Persisted != nil {
|
|
t.Fatalf("has a persisted state, but shouldn't")
|
|
}
|
|
|
|
gotLog := is.CallLog
|
|
wantLog := []string{
|
|
// Initial call before we reset lastPersist
|
|
"WriteState",
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return false, preventing a "PersistState" call
|
|
|
|
// Write and then decline to persist
|
|
"WriteState",
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return false, preventing a "PersistState" call
|
|
|
|
// Final call before we start "stopping".
|
|
"WriteState",
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return false, preventing a "PersistState" call
|
|
}
|
|
if diff := cmp.Diff(wantLog, gotLog); diff != "" {
|
|
t.Fatalf("wrong call log so far\n%s", diff)
|
|
}
|
|
|
|
// We'll reset the log now before we try seeing what happens after
|
|
// we use "Stopped".
|
|
is.CallLog = is.CallLog[:0]
|
|
is.Persisted = nil
|
|
|
|
hook.Stopping()
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
|
|
is.Persisted = nil
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
is.Persisted = nil
|
|
_, err = hook.PostStateUpdate(s)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error from PostStateUpdate: %s", err)
|
|
}
|
|
if is.Persisted == nil || !is.Persisted.Equal(s) {
|
|
t.Fatalf("mismatching state persisted")
|
|
}
|
|
|
|
gotLog = is.CallLog
|
|
wantLog = []string{
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return true, allowing the following "PersistState" call
|
|
"PersistState",
|
|
"WriteState",
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return true, allowing the following "PersistState" call
|
|
"PersistState",
|
|
"WriteState",
|
|
"ShouldPersistIntermediateState",
|
|
// Previous call should return true, allowing the following "PersistState" call
|
|
"PersistState",
|
|
}
|
|
if diff := cmp.Diff(wantLog, gotLog); diff != "" {
|
|
t.Fatalf("wrong call log once in stopping mode\n%s", diff)
|
|
}
|
|
}
|
|
|
|
type testPersistentState struct {
|
|
CallLog []string
|
|
|
|
Written *states.State
|
|
Persisted *states.State
|
|
}
|
|
|
|
var _ statemgr.Writer = (*testPersistentState)(nil)
|
|
var _ statemgr.Persister = (*testPersistentState)(nil)
|
|
|
|
func (sm *testPersistentState) WriteState(state *states.State) error {
|
|
sm.CallLog = append(sm.CallLog, "WriteState")
|
|
sm.Written = state
|
|
return nil
|
|
}
|
|
|
|
func (sm *testPersistentState) PersistState(_ context.Context, schemas *tofu.Schemas) error {
|
|
if schemas == nil {
|
|
return fmt.Errorf("no schemas")
|
|
}
|
|
sm.CallLog = append(sm.CallLog, "PersistState")
|
|
sm.Persisted = sm.Written
|
|
return nil
|
|
}
|
|
|
|
type testPersistentStateThatRefusesToPersist struct {
|
|
CallLog []string
|
|
|
|
Written *states.State
|
|
Persisted *states.State
|
|
}
|
|
|
|
var _ statemgr.Writer = (*testPersistentStateThatRefusesToPersist)(nil)
|
|
var _ statemgr.Persister = (*testPersistentStateThatRefusesToPersist)(nil)
|
|
var _ IntermediateStateConditionalPersister = (*testPersistentStateThatRefusesToPersist)(nil)
|
|
|
|
func (sm *testPersistentStateThatRefusesToPersist) WriteState(state *states.State) error {
|
|
sm.CallLog = append(sm.CallLog, "WriteState")
|
|
sm.Written = state
|
|
return nil
|
|
}
|
|
|
|
func (sm *testPersistentStateThatRefusesToPersist) PersistState(_ context.Context, schemas *tofu.Schemas) error {
|
|
if schemas == nil {
|
|
return fmt.Errorf("no schemas")
|
|
}
|
|
sm.CallLog = append(sm.CallLog, "PersistState")
|
|
sm.Persisted = sm.Written
|
|
return nil
|
|
}
|
|
|
|
// ShouldPersistIntermediateState implements IntermediateStateConditionalPersister
|
|
func (sm *testPersistentStateThatRefusesToPersist) ShouldPersistIntermediateState(info *IntermediateStatePersistInfo) bool {
|
|
sm.CallLog = append(sm.CallLog, "ShouldPersistIntermediateState")
|
|
return info.ForcePersist
|
|
}
|