mirror of
https://github.com/hashicorp/terraform.git
synced 2026-02-18 10:20:20 -05:00
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
package remote
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
"github.com/hashicorp/terraform/states/statefile"
|
|
)
|
|
|
|
// TestClient is a generic function to test any client.
|
|
func TestClient(t *testing.T, c Client) {
|
|
var buf bytes.Buffer
|
|
s := state.TestStateInitial()
|
|
sf := statefile.New(s, "stub-lineage", 2)
|
|
err := statefile.Write(sf, &buf)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
data := buf.Bytes()
|
|
|
|
if err := c.Put(data); err != nil {
|
|
t.Fatalf("put: %s", err)
|
|
}
|
|
|
|
p, err := c.Get()
|
|
if err != nil {
|
|
t.Fatalf("get: %s", err)
|
|
}
|
|
if !bytes.Equal(p.Data, data) {
|
|
t.Fatalf("expected full state %q\n\ngot: %q", string(p.Data), string(data))
|
|
}
|
|
|
|
if err := c.Delete(); err != nil {
|
|
t.Fatalf("delete: %s", err)
|
|
}
|
|
|
|
p, err = c.Get()
|
|
if err != nil {
|
|
t.Fatalf("get: %s", err)
|
|
}
|
|
if p != nil {
|
|
t.Fatalf("expected empty state, got: %q", string(p.Data))
|
|
}
|
|
}
|
|
|
|
// Test the lock implementation for a remote.Client.
|
|
// This test requires 2 client instances, in oder to have multiple remote
|
|
// clients since some implementations may tie the client to the lock, or may
|
|
// have reentrant locks.
|
|
func TestRemoteLocks(t *testing.T, a, b Client) {
|
|
lockerA, ok := a.(state.Locker)
|
|
if !ok {
|
|
t.Fatal("client A not a state.Locker")
|
|
}
|
|
|
|
lockerB, ok := b.(state.Locker)
|
|
if !ok {
|
|
t.Fatal("client B not a state.Locker")
|
|
}
|
|
|
|
infoA := state.NewLockInfo()
|
|
infoA.Operation = "test"
|
|
infoA.Who = "clientA"
|
|
|
|
infoB := state.NewLockInfo()
|
|
infoB.Operation = "test"
|
|
infoB.Who = "clientB"
|
|
|
|
lockIDA, err := lockerA.Lock(infoA)
|
|
if err != nil {
|
|
t.Fatal("unable to get initial lock:", err)
|
|
}
|
|
|
|
_, err = lockerB.Lock(infoB)
|
|
if err == nil {
|
|
lockerA.Unlock(lockIDA)
|
|
t.Fatal("client B obtained lock while held by client A")
|
|
}
|
|
|
|
if err := lockerA.Unlock(lockIDA); err != nil {
|
|
t.Fatal("error unlocking client A", err)
|
|
}
|
|
|
|
lockIDB, err := lockerB.Lock(infoB)
|
|
if err != nil {
|
|
t.Fatal("unable to obtain lock from client B")
|
|
}
|
|
|
|
if lockIDB == lockIDA {
|
|
t.Fatalf("duplicate lock IDs: %q", lockIDB)
|
|
}
|
|
|
|
if err = lockerB.Unlock(lockIDB); err != nil {
|
|
t.Fatal("error unlocking client B:", err)
|
|
}
|
|
|
|
// TODO: Should we enforce that Unlock requires the correct ID?
|
|
}
|