terraform/backend/remote-state/http/client_test.go

176 lines
3.8 KiB
Go
Raw Normal View History

package http
2015-02-22 03:09:46 +01:00
import (
2015-02-23 17:32:55 +01:00
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
2018-01-19 03:30:21 +01:00
"reflect"
2015-02-22 03:09:46 +01:00
"testing"
2015-10-22 20:03:25 +02:00
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform/states/remote"
2015-02-22 03:09:46 +01:00
)
func TestHTTPClient_impl(t *testing.T) {
var _ remote.Client = new(httpClient)
var _ remote.ClientLocker = new(httpClient)
2015-02-22 03:09:46 +01:00
}
func TestHTTPClient(t *testing.T) {
2015-02-23 17:32:55 +01:00
handler := new(testHTTPHandler)
ts := httptest.NewServer(http.HandlerFunc(handler.Handle))
defer ts.Close()
url, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Parse: %s", err)
2015-02-23 17:32:55 +01:00
}
// Test basic get/update
client := &httpClient{URL: url, Client: retryablehttp.NewClient()}
remote.TestClient(t, client)
2017-08-13 18:16:42 +02:00
// test just a single PUT
p := &httpClient{
URL: url,
UpdateMethod: "PUT",
Client: retryablehttp.NewClient(),
}
remote.TestClient(t, p)
// Test locking and alternative UpdateMethod
a := &httpClient{
URL: url,
UpdateMethod: "PUT",
LockURL: url,
LockMethod: "LOCK",
UnlockURL: url,
UnlockMethod: "UNLOCK",
Client: retryablehttp.NewClient(),
}
b := &httpClient{
URL: url,
UpdateMethod: "PUT",
LockURL: url,
LockMethod: "LOCK",
UnlockURL: url,
UnlockMethod: "UNLOCK",
Client: retryablehttp.NewClient(),
}
remote.TestRemoteLocks(t, a, b)
2018-01-19 03:30:21 +01:00
// test a WebDAV-ish backend
davhandler := new(testHTTPHandler)
ts = httptest.NewServer(http.HandlerFunc(davhandler.HandleWebDAV))
defer ts.Close()
url, err = url.Parse(ts.URL)
client = &httpClient{
2018-01-19 03:30:21 +01:00
URL: url,
UpdateMethod: "PUT",
Client: retryablehttp.NewClient(),
2018-01-19 03:30:21 +01:00
}
if err != nil {
t.Fatalf("Parse: %s", err)
}
remote.TestClient(t, client) // first time through: 201
remote.TestClient(t, client) // second time, with identical data: 204
// test a broken backend
brokenHandler := new(testBrokenHTTPHandler)
brokenHandler.handler = new(testHTTPHandler)
ts = httptest.NewServer(http.HandlerFunc(brokenHandler.Handle))
defer ts.Close()
url, err = url.Parse(ts.URL)
if err != nil {
t.Fatalf("Parse: %s", err)
}
client = &httpClient{URL: url, Client: retryablehttp.NewClient()}
remote.TestClient(t, client)
}
2015-02-23 17:32:55 +01:00
type testHTTPHandler struct {
2017-08-13 18:16:42 +02:00
Data []byte
Locked bool
2015-02-23 17:32:55 +01:00
}
func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Write(h.Data)
case "PUT":
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
}
w.WriteHeader(201)
h.Data = buf.Bytes()
case "POST":
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
2015-02-23 17:32:55 +01:00
}
h.Data = buf.Bytes()
case "LOCK":
if h.Locked {
w.WriteHeader(423)
} else {
h.Locked = true
}
case "UNLOCK":
h.Locked = false
2015-02-23 17:32:55 +01:00
case "DELETE":
h.Data = nil
w.WriteHeader(200)
default:
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
}
2015-02-22 03:09:46 +01:00
}
2018-01-19 03:30:21 +01:00
// mod_dav-ish behavior
func (h *testHTTPHandler) HandleWebDAV(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Write(h.Data)
case "PUT":
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
}
if reflect.DeepEqual(h.Data, buf.Bytes()) {
h.Data = buf.Bytes()
w.WriteHeader(204)
} else {
h.Data = buf.Bytes()
w.WriteHeader(201)
}
case "DELETE":
h.Data = nil
w.WriteHeader(200)
default:
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
}
}
type testBrokenHTTPHandler struct {
lastRequestWasBroken bool
handler *testHTTPHandler
}
func (h *testBrokenHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
if h.lastRequestWasBroken {
h.lastRequestWasBroken = false
h.handler.Handle(w, r)
} else {
h.lastRequestWasBroken = true
w.WriteHeader(500)
}
}