2014-12-04 05:05:29 +01:00
|
|
|
package remote
|
|
|
|
|
2014-12-05 02:47:49 +01:00
|
|
|
import (
|
2014-12-06 00:19:04 +01:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2014-12-05 02:47:49 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
2014-12-04 05:05:29 +01:00
|
|
|
|
|
|
|
func TestAtlasRemote_Interface(t *testing.T) {
|
|
|
|
var client interface{} = &AtlasRemoteClient{}
|
|
|
|
if _, ok := client.(RemoteClient); !ok {
|
|
|
|
t.Fatalf("does not implement interface")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 02:47:49 +01:00
|
|
|
func checkAtlas(t *testing.T) {
|
|
|
|
if os.Getenv("ATLAS_TOKEN") == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtlasRemote_Validate(t *testing.T) {
|
|
|
|
conf := map[string]string{}
|
|
|
|
if _, err := NewAtlasRemoteClient(conf); err == nil {
|
|
|
|
t.Fatalf("expect error")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf["access_token"] = "test"
|
|
|
|
conf["name"] = "hashicorp/test-state"
|
|
|
|
if _, err := NewAtlasRemoteClient(conf); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 04:19:04 +01:00
|
|
|
func TestAtlasRemote_Validate_envVar(t *testing.T) {
|
|
|
|
conf := map[string]string{}
|
|
|
|
if _, err := NewAtlasRemoteClient(conf); err == nil {
|
|
|
|
t.Fatalf("expect error")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Setenv("ATLAS_TOKEN", os.Getenv("ATLAS_TOKEN"))
|
|
|
|
os.Setenv("ATLAS_TOKEN", "foo")
|
|
|
|
|
|
|
|
conf["name"] = "hashicorp/test-state"
|
|
|
|
if _, err := NewAtlasRemoteClient(conf); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 05:05:29 +01:00
|
|
|
func TestAtlasRemote(t *testing.T) {
|
2014-12-05 02:47:49 +01:00
|
|
|
checkAtlas(t)
|
|
|
|
remote := &terraform.RemoteState{
|
|
|
|
Type: "atlas",
|
|
|
|
Config: map[string]string{
|
|
|
|
"access_token": os.Getenv("ATLAS_TOKEN"),
|
|
|
|
"name": "hashicorp/test-remote-state",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r, err := NewClientByState(remote)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a valid input
|
|
|
|
inp, err := blankState(remote)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
2014-12-06 00:19:04 +01:00
|
|
|
inpMD5 := md5.Sum(inp)
|
|
|
|
hash := inpMD5[:16]
|
2014-12-05 02:47:49 +01:00
|
|
|
|
|
|
|
// Delete the state, should be none
|
|
|
|
err = r.DeleteState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure no state
|
|
|
|
payload, err := r.GetState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if payload != nil {
|
|
|
|
t.Fatalf("unexpected payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the state
|
|
|
|
err = r.PutState(inp, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get it back
|
|
|
|
payload, err = r.GetState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
2014-12-05 20:47:56 +01:00
|
|
|
if payload == nil {
|
2014-12-05 02:47:49 +01:00
|
|
|
t.Fatalf("unexpected payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the payload
|
2014-12-06 00:19:04 +01:00
|
|
|
if !bytes.Equal(payload.MD5, hash) {
|
|
|
|
t.Fatalf("bad hash: %x %x", payload.MD5, hash)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(payload.State, inp) {
|
|
|
|
t.Errorf("inp: %s", inp)
|
|
|
|
t.Fatalf("bad response: %s", payload.State)
|
|
|
|
}
|
2014-12-05 02:47:49 +01:00
|
|
|
|
|
|
|
// Delete the state
|
|
|
|
err = r.DeleteState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be gone
|
|
|
|
payload, err = r.GetState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Err: %v", err)
|
|
|
|
}
|
|
|
|
if payload != nil {
|
|
|
|
t.Fatalf("unexpected payload")
|
|
|
|
}
|
2014-12-04 05:05:29 +01:00
|
|
|
}
|