mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-23 03:34:30 -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>
137 lines
3.3 KiB
Go
137 lines
3.3 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 remote
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestRemoteClient_noPayload(t *testing.T) {
|
|
s := &State{
|
|
Client: nilClient{},
|
|
}
|
|
if err := s.RefreshState(t.Context()); err != nil {
|
|
t.Fatal("error refreshing empty remote state")
|
|
}
|
|
}
|
|
|
|
// nilClient returns nil for everything
|
|
type nilClient struct{}
|
|
|
|
func (nilClient) Get(context.Context) (*Payload, error) { return nil, nil }
|
|
|
|
func (c nilClient) Put(context.Context, []byte) error { return nil }
|
|
|
|
func (c nilClient) Delete(context.Context) error { return nil }
|
|
|
|
// mockClient is a client that tracks persisted state snapshots only in
|
|
// memory and also logs what it has been asked to do for use in test
|
|
// assertions.
|
|
type mockClient struct {
|
|
current []byte
|
|
log []mockClientRequest
|
|
}
|
|
|
|
type mockClientRequest struct {
|
|
Method string
|
|
Content map[string]interface{}
|
|
}
|
|
|
|
func (c *mockClient) Get(_ context.Context) (*Payload, error) {
|
|
c.appendLog("Get", c.current)
|
|
if c.current == nil {
|
|
return nil, nil
|
|
}
|
|
checksum := md5.Sum(c.current)
|
|
return &Payload{
|
|
Data: c.current,
|
|
MD5: checksum[:],
|
|
}, nil
|
|
}
|
|
|
|
func (c *mockClient) Put(_ context.Context, data []byte) error {
|
|
c.appendLog("Put", data)
|
|
c.current = data
|
|
return nil
|
|
}
|
|
|
|
func (c *mockClient) Delete(_ context.Context) error {
|
|
c.appendLog("Delete", c.current)
|
|
c.current = nil
|
|
return nil
|
|
}
|
|
|
|
func (c *mockClient) appendLog(method string, content []byte) {
|
|
// For easier test assertions, we actually log the result of decoding
|
|
// the content JSON rather than the raw bytes. Callers are in principle
|
|
// allowed to provide any arbitrary bytes here, but we know we're only
|
|
// using this to test our own State implementation here and that always
|
|
// uses the JSON state format, so this is fine.
|
|
|
|
var contentVal map[string]interface{}
|
|
if content != nil {
|
|
err := json.Unmarshal(content, &contentVal)
|
|
if err != nil {
|
|
panic(err) // should never happen because our tests control this input
|
|
}
|
|
}
|
|
c.log = append(c.log, mockClientRequest{method, contentVal})
|
|
}
|
|
|
|
// mockClientForcePusher is like mockClient, but also implements
|
|
// EnableForcePush, allowing testing for this behavior
|
|
type mockClientForcePusher struct {
|
|
current []byte
|
|
force bool
|
|
log []mockClientRequest
|
|
}
|
|
|
|
func (c *mockClientForcePusher) Get(_ context.Context) (*Payload, error) {
|
|
c.appendLog("Get", c.current)
|
|
if c.current == nil {
|
|
return nil, nil
|
|
}
|
|
checksum := md5.Sum(c.current)
|
|
return &Payload{
|
|
Data: c.current,
|
|
MD5: checksum[:],
|
|
}, nil
|
|
}
|
|
|
|
func (c *mockClientForcePusher) Put(_ context.Context, data []byte) error {
|
|
if c.force {
|
|
c.appendLog("Force Put", data)
|
|
} else {
|
|
c.appendLog("Put", data)
|
|
}
|
|
c.current = data
|
|
return nil
|
|
}
|
|
|
|
// Implements remote.ClientForcePusher
|
|
func (c *mockClientForcePusher) EnableForcePush() {
|
|
c.force = true
|
|
}
|
|
|
|
func (c *mockClientForcePusher) Delete(_ context.Context) error {
|
|
c.appendLog("Delete", c.current)
|
|
c.current = nil
|
|
return nil
|
|
}
|
|
func (c *mockClientForcePusher) appendLog(method string, content []byte) {
|
|
var contentVal map[string]interface{}
|
|
if content != nil {
|
|
err := json.Unmarshal(content, &contentVal)
|
|
if err != nil {
|
|
panic(err) // should never happen because our tests control this input
|
|
}
|
|
}
|
|
c.log = append(c.log, mockClientRequest{method, contentVal})
|
|
}
|