remote: testing remote state fetch

This commit is contained in:
Armon Dadgar 2014-10-02 12:22:06 -07:00 committed by Mitchell Hashimoto
parent cc4e48e0ec
commit 3de8e2343e
2 changed files with 87 additions and 2 deletions

View File

@ -57,11 +57,11 @@ func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) {
case http.StatusUnauthorized: case http.StatusUnauthorized:
return nil, fmt.Errorf("Remote server requires authentication") return nil, fmt.Errorf("Remote server requires authentication")
case http.StatusForbidden: case http.StatusForbidden:
return nil, fmt.Errorf("Invalid authentication token") return nil, fmt.Errorf("Invalid authentication")
case http.StatusInternalServerError: case http.StatusInternalServerError:
return nil, fmt.Errorf("Remote server reporting internal error") return nil, fmt.Errorf("Remote server reporting internal error")
default: default:
return nil, fmt.Errorf("Received unexpected HTTP response code %d", resp.StatusCode) return nil, fmt.Errorf("Unexpected HTTP response code %d", resp.StatusCode)
} }
// Read in the body // Read in the body

View File

@ -5,6 +5,7 @@ import (
"crypto/md5" "crypto/md5"
"io" "io"
"net/http" "net/http"
"net/http/httptest"
"strings" "strings"
"testing" "testing"
@ -74,3 +75,87 @@ REQ:
goto REQ 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,
}
payload, err := GetState(remote)
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 {
buf := bytes.NewBuffer(nil)
io.Copy(buf, payload.R)
if !bytes.Equal(buf.Bytes(), tc.Body) {
t.Fatalf("bad: %#v", payload)
}
}
}
}