remote: Adding tests

This commit is contained in:
Armon Dadgar 2014-10-01 21:29:37 -07:00 committed by Mitchell Hashimoto
parent 7a530cdae5
commit e36b46de07
2 changed files with 114 additions and 0 deletions

View File

@ -38,6 +38,9 @@ func EnsureDirectory() error {
}
path := filepath.Join(cwd, LocalDirectory)
if err := os.Mkdir(path, 0770); err != nil {
if os.IsExist(err) {
return nil
}
return fmt.Errorf("Failed to make directory '%s': %v", path, err)
}
return nil

View File

@ -1,9 +1,13 @@
package remote
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/terraform/terraform"
)
func TestEnsureDirectory(t *testing.T) {
@ -34,3 +38,110 @@ func TestHiddenStatePath(t *testing.T) {
t.Fatalf("bad: %v", path)
}
}
func TestValidConfig(t *testing.T) {
conf := &terraform.RemoteState{}
if err := validConfig(conf); err != nil {
t.Fatalf("blank should be valid: %v", err)
}
conf.Server = "http://foo.com"
if err := validConfig(conf); err == nil {
t.Fatalf("server without name")
}
conf.Server = ""
conf.AuthToken = "foo"
if err := validConfig(conf); err == nil {
t.Fatalf("auth without name")
}
conf.Name = "test"
conf.Server = ""
conf.AuthToken = ""
if err := validConfig(conf); err != nil {
t.Fatalf("should be valid")
}
if conf.Server != DefaultServer {
t.Fatalf("should default server")
}
}
func TestValidateConfig(t *testing.T) {
// TODO:
}
func TestReadState(t *testing.T) {
// TODO
}
func TestBlankState(t *testing.T) {
remote := &terraform.RemoteState{
Name: "foo",
Server: "http://foo.com/",
AuthToken: "foobar",
}
r, err := blankState(remote)
if err != nil {
t.Fatalf("err: %v", err)
}
s, err := terraform.ReadState(r)
if err != nil {
t.Fatalf("err: %v", err)
}
if !remote.Equals(s.Remote) {
t.Fatalf("remote mismatch")
}
}
func TestPersist(t *testing.T) {
tmp, err := ioutil.TempDir("", "remote")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(tmp)
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %v", err)
}
os.Chdir(tmp)
defer os.Chdir(cwd)
EnsureDirectory()
// Place old state file, should backup
old := filepath.Join(tmp, LocalDirectory, HiddenStateFile)
ioutil.WriteFile(old, []byte("test"), 0777)
remote := &terraform.RemoteState{
Name: "foo",
Server: "http://foo.com/",
AuthToken: "foobar",
}
blank, _ := blankState(remote)
if err := Persist(blank); err != nil {
t.Fatalf("err: %v", err)
}
// Check for backup
backup := filepath.Join(tmp, LocalDirectory, BackupHiddenStateFile)
out, err := ioutil.ReadFile(backup)
if err != nil {
t.Fatalf("Err: %v", err)
}
if string(out) != "test" {
t.Fatalf("bad: %v", out)
}
// Read the state
out, err = ioutil.ReadFile(old)
if err != nil {
t.Fatalf("Err: %v", err)
}
s, err := terraform.ReadState(bytes.NewReader(out))
if err != nil {
t.Fatalf("Err: %v", err)
}
// Check the remote
if !remote.Equals(s.Remote) {
t.Fatalf("remote mismatch")
}
}