remote: Cleanup the tests from refactor

This commit is contained in:
Armon Dadgar 2014-12-04 17:54:07 -08:00 committed by Mitchell Hashimoto
parent f7b1299a9f
commit 6a182c2684
3 changed files with 24 additions and 311 deletions

View File

@ -1,283 +1 @@
package remote
import (
"bytes"
"crypto/md5"
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/armon/consul-api"
"github.com/hashicorp/terraform/terraform"
)
var haveInternet bool
func init() {
// Use google to check if we are on the net
_, err := http.Get("http://www.google.com")
haveInternet = (err == nil)
}
func TestGetState_Consul(t *testing.T) {
if !haveInternet {
t.SkipNow()
}
// Use the Consul demo cluster
conf := consulapi.DefaultConfig()
conf.Address = "demo.consul.io:80"
client, err := consulapi.NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
// Write some test data
pair := &consulapi.KVPair{
Key: "test/tf/remote/foobar",
Value: []byte("testing"),
}
kv := client.KV()
if _, err := kv.Put(pair, nil); err != nil {
t.Fatalf("err: %v", err)
}
defer kv.Delete(pair.Key, nil)
// Check we can get the state
remote := &terraform.RemoteState{
Name: "foobar",
Server: "http://demo.consul.io/v1/kv/test/tf/remote",
}
REQ:
r := &remoteStateClient{conf: remote}
payload, err := r.GetState()
if err != nil {
t.Fatalf("err: %v", err)
}
// Check the MD5
expect := md5.Sum(pair.Value)
if !bytes.Equal(payload.MD5, expect[:md5.Size]) {
t.Fatalf("Bad md5")
}
// Check the body
if string(payload.State) != "testing" {
t.Fatalf("Bad body")
}
// Try doing a ?raw lookup
if !strings.Contains(remote.Server, "?raw") {
remote.Server += "?raw"
goto REQ
}
}
func TestGetState(t *testing.T) {
type tcase struct {
Code int
Header http.Header
Body []byte
ExpectMD5 []byte
ExpectErr string
}
inp := []byte("testing")
inpMD5 := md5.Sum(inp)
hash := inpMD5[:16]
cases := []*tcase{
&tcase{
Code: http.StatusOK,
Body: inp,
ExpectMD5: hash,
},
&tcase{
Code: http.StatusNoContent,
},
&tcase{
Code: http.StatusNotFound,
},
&tcase{
Code: http.StatusUnauthorized,
ExpectErr: "Remote server requires authentication",
},
&tcase{
Code: http.StatusForbidden,
ExpectErr: "Invalid authentication",
},
&tcase{
Code: http.StatusInternalServerError,
ExpectErr: "Remote server reporting internal error",
},
&tcase{
Code: 418,
ExpectErr: "Unexpected HTTP response code 418",
},
}
for _, tc := range cases {
cb := func(resp http.ResponseWriter, req *http.Request) {
for k, v := range tc.Header {
resp.Header()[k] = v
}
resp.WriteHeader(tc.Code)
if tc.Body != nil {
resp.Write(tc.Body)
}
}
s := httptest.NewServer(http.HandlerFunc(cb))
defer s.Close()
remote := &terraform.RemoteState{
Name: "foobar",
Server: s.URL,
}
r := &remoteStateClient{remote}
payload, err := r.GetState()
errStr := ""
if err != nil {
errStr = err.Error()
}
if errStr != tc.ExpectErr {
t.Fatalf("bad err: %v %v", errStr, tc.ExpectErr)
}
if tc.ExpectMD5 != nil {
if payload == nil || !bytes.Equal(payload.MD5, tc.ExpectMD5) {
t.Fatalf("bad: %#v", payload)
}
}
if tc.Body != nil {
if !bytes.Equal(payload.State, tc.Body) {
t.Fatalf("bad: %#v", payload)
}
}
}
}
func TestPutState(t *testing.T) {
type tcase struct {
Code int
Path string
Header http.Header
Body []byte
ExpectMD5 []byte
Force bool
ExpectErr string
}
inp := []byte("testing")
inpMD5 := md5.Sum(inp)
hash := inpMD5[:16]
cases := []*tcase{
&tcase{
Code: http.StatusOK,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
},
&tcase{
Code: http.StatusOK,
Path: "/foobar?force=true",
Body: inp,
Force: true,
ExpectMD5: hash,
},
&tcase{
Code: http.StatusConflict,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: ErrConflict.Error(),
},
&tcase{
Code: http.StatusPreconditionFailed,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: ErrServerNewer.Error(),
},
&tcase{
Code: http.StatusUnauthorized,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: ErrRequireAuth.Error(),
},
&tcase{
Code: http.StatusForbidden,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: ErrInvalidAuth.Error(),
},
&tcase{
Code: http.StatusInternalServerError,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: ErrRemoteInternal.Error(),
},
&tcase{
Code: 418,
Path: "/foobar",
Body: inp,
ExpectMD5: hash,
ExpectErr: "Unexpected HTTP response code 418",
},
}
for _, tc := range cases {
cb := func(resp http.ResponseWriter, req *http.Request) {
for k, v := range tc.Header {
resp.Header()[k] = v
}
resp.WriteHeader(tc.Code)
// Verify the body
buf := bytes.NewBuffer(nil)
io.Copy(buf, req.Body)
if !bytes.Equal(buf.Bytes(), tc.Body) {
t.Fatalf("bad body: %v", buf.Bytes())
}
// Verify the path
req.URL.Host = ""
if req.URL.String() != tc.Path {
t.Fatalf("Bad path: %v %v", req.URL.String(), tc.Path)
}
// Verify the content length
if req.ContentLength != int64(len(tc.Body)) {
t.Fatalf("bad content length: %d", req.ContentLength)
}
// Verify the Content-MD5
b64 := req.Header.Get("Content-MD5")
raw, _ := base64.StdEncoding.DecodeString(b64)
if !bytes.Equal(raw, tc.ExpectMD5) {
t.Fatalf("bad md5: %v", raw)
}
}
s := httptest.NewServer(http.HandlerFunc(cb))
defer s.Close()
remote := &terraform.RemoteState{
Name: "foobar",
Server: s.URL,
}
r := &remoteStateClient{remote}
err := r.PutState(tc.Body, tc.Force)
errStr := ""
if err != nil {
errStr = err.Error()
}
if errStr != tc.ExpectErr {
t.Fatalf("bad err: %v %v", errStr, tc.ExpectErr)
}
}
}

View File

@ -24,10 +24,6 @@ const (
// BackupHiddenStateFile is the path we backup the state
// file to before modifications are made
BackupHiddenStateFile = "terraform.tfstate.backup"
// DefaultServer is used when no server is provided. We use
// the hosted cloud URL.
DefaultServer = "http://www.hashicorp.com/"
)
// StateChangeResult is used to communicate to a caller

View File

@ -45,27 +45,20 @@ func TestHiddenStatePath(t *testing.T) {
}
func TestValidConfig(t *testing.T) {
conf := &terraform.RemoteState{}
conf := &terraform.RemoteState{
Type: "",
Config: map[string]string{},
}
if err := ValidConfig(conf); err == nil {
t.Fatalf("blank should be not 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 = ""
conf.Config["name"] = "hashicorp/test-remote-state"
conf.Config["access_token"] = "abcd"
if err := ValidConfig(conf); err != nil {
t.Fatalf("should be valid")
}
if conf.Server != DefaultServer {
t.Fatalf("should default server")
if conf.Type != "atlas" {
t.Fatalf("should default to atlas")
}
}
@ -296,9 +289,10 @@ func TestPushState_Error(t *testing.T) {
func TestBlankState(t *testing.T) {
remote := &terraform.RemoteState{
Name: "foo",
Server: "http://foo.com/",
AuthToken: "foobar",
Type: "http",
Config: map[string]string{
"url": "http://foo.com/",
},
}
r, err := blankState(remote)
if err != nil {
@ -324,9 +318,10 @@ func TestPersist(t *testing.T) {
ioutil.WriteFile(old, []byte("test"), 0777)
remote := &terraform.RemoteState{
Name: "foo",
Server: "http://foo.com/",
AuthToken: "foobar",
Type: "http",
Config: map[string]string{
"url": "http://foo.com/",
},
}
blank, _ := blankState(remote)
if err := Persist(bytes.NewReader(blank)); err != nil {
@ -384,8 +379,10 @@ func testRemote(t *testing.T, s *terraform.State) (*terraform.RemoteState, *http
}
srv := httptest.NewServer(http.HandlerFunc(cb))
remote := &terraform.RemoteState{
Name: "foo",
Server: srv.URL,
Type: "http",
Config: map[string]string{
"url": srv.URL,
},
}
return remote, srv
}
@ -398,8 +395,10 @@ func testRemotePush(t *testing.T, c int) (*terraform.RemoteState, *httptest.Serv
}
srv := httptest.NewServer(http.HandlerFunc(cb))
remote := &terraform.RemoteState{
Name: "foo",
Server: srv.URL,
Type: "http",
Config: map[string]string{
"url": srv.URL,
},
}
return remote, srv
}