|
|
|
|
@ -3,6 +3,8 @@ package remote
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
@ -156,3 +158,119 @@ func TestGetState(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|