2017-01-19 05:51:15 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/state"
|
terraform: Ugly huge change to weave in new State and Plan types
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 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statefile"
|
2017-01-19 05:51:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestClient is a generic function to test any client.
|
|
|
|
func TestClient(t *testing.T, c Client) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
s := state.TestStateInitial()
|
terraform: Ugly huge change to weave in new State and Plan types
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 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
sf := statefile.New(s, "stub-lineage", 2)
|
|
|
|
err := statefile.Write(sf, &buf)
|
|
|
|
if err != nil {
|
2017-01-19 05:51:15 +01:00
|
|
|
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) {
|
2018-07-04 17:24:49 +02:00
|
|
|
t.Fatalf("expected full state %q\n\ngot: %q", string(p.Data), string(data))
|
2017-01-19 05:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-07-04 17:24:49 +02:00
|
|
|
t.Fatalf("expected empty state, got: %q", string(p.Data))
|
2017-01-19 05:51:15 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 16:33:05 +01:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:25:04 +01:00
|
|
|
infoA := state.NewLockInfo()
|
|
|
|
infoA.Operation = "test"
|
|
|
|
infoA.Who = "clientA"
|
|
|
|
|
|
|
|
infoB := state.NewLockInfo()
|
|
|
|
infoB.Operation = "test"
|
|
|
|
infoB.Who = "clientB"
|
2017-02-14 23:48:57 +01:00
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
lockIDA, err := lockerA.Lock(infoA)
|
|
|
|
if err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("unable to get initial lock:", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
_, err = lockerB.Lock(infoB)
|
|
|
|
if err == nil {
|
|
|
|
lockerA.Unlock(lockIDA)
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("client B obtained lock while held by client A")
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
if err := lockerA.Unlock(lockIDA); err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("error unlocking client A", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
lockIDB, err := lockerB.Lock(infoB)
|
|
|
|
if err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("unable to obtain lock from client B")
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:02:37 +01:00
|
|
|
if lockIDB == lockIDA {
|
|
|
|
t.Fatalf("duplicate lock IDs: %q", lockIDB)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
if err = lockerB.Unlock(lockIDB); err != nil {
|
2017-02-07 16:33:05 +01:00
|
|
|
t.Fatal("error unlocking client B:", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 16:39:35 +01:00
|
|
|
// TODO: Should we enforce that Unlock requires the correct ID?
|
2017-02-07 16:33:05 +01:00
|
|
|
}
|