terraform/state/remote/http_test.go

160 lines
4.2 KiB
Go
Raw Normal View History

2015-02-22 03:09:46 +01:00
package remote
import (
2015-02-23 17:32:55 +01:00
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
2015-02-22 03:09:46 +01:00
"testing"
2015-10-22 20:03:25 +02:00
"github.com/hashicorp/go-cleanhttp"
2015-02-22 03:09:46 +01:00
)
func TestHTTPClient_impl(t *testing.T) {
var _ Client = new(HTTPClient)
2017-08-13 18:16:42 +02:00
var _ 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("err: %s", err)
}
// Test basic get/store
2015-10-22 20:03:25 +02:00
client := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient()}
2015-02-23 17:32:55 +01:00
testClient(t, client)
2017-08-13 18:16:42 +02:00
// Test locking and alternative StoreMethod
a := &HTTPClient{
URL: url,
StoreMethod: "PUT",
LockURL: url,
LockMethod: "LOCK",
UnlockURL: url,
UnlockMethod: "UNLOCK",
Client: cleanhttp.DefaultClient(),
}
b := &HTTPClient{
URL: url,
StoreMethod: "PUT",
LockURL: url,
LockMethod: "LOCK",
UnlockURL: url,
UnlockMethod: "UNLOCK",
Client: cleanhttp.DefaultClient(),
}
2017-08-13 18:16:42 +02:00
TestRemoteLocks(t, a, b)
}
func assertError(t *testing.T, err error, expected string) {
if err == nil {
t.Fatalf("Expected empty config to err")
} else if err.Error() != expected {
t.Fatalf("Expected err.Error() to be \"%s\", got \"%s\"", expected, err.Error())
}
}
func TestHTTPClientFactory(t *testing.T) {
// missing address
_, err := httpFactory(map[string]string{})
assertError(t, err, "missing 'address' configuration")
// defaults
conf := map[string]string{
"address": "http://127.0.0.1:8888/foo",
}
c, err := httpFactory(conf)
client, _ := c.(*HTTPClient)
if client == nil || err != nil {
t.Fatal("Unexpected failure, address")
}
if client.URL.String() != conf["address"] {
t.Fatalf("Expected address \"%s\", got \"%s\"", conf["address"], client.URL.String())
}
if client.StoreMethod != "POST" {
t.Fatalf("Expected store_method \"%s\", got \"%s\"", "POST", client.StoreMethod)
}
if client.LockURL != nil || client.LockMethod != "LOCK" {
t.Fatal("Unexpected lock_address or lock_method")
}
if client.UnlockURL != nil || client.UnlockMethod != "UNLOCK" {
t.Fatal("Unexpected unlock_address or unlock_method")
}
if client.Username != "" || client.Password != "" {
t.Fatal("Unexpected username or password")
}
// custom
conf = map[string]string{
"address": "http://127.0.0.1:8888/foo",
"store_method": "BLAH",
"lock_address": "http://127.0.0.1:8888/bar",
"lock_method": "BLIP",
"unlock_address": "http://127.0.0.1:8888/baz",
"unlock_method": "BLOOP",
"username": "user",
"password": "pass",
}
c, err = httpFactory(conf)
client, _ = c.(*HTTPClient)
if client == nil || err != nil {
t.Fatal("Unexpected failure, store_method")
}
if client.StoreMethod != "BLAH" {
t.Fatalf("Expected store_method \"%s\", got \"%s\"", "BLAH", client.StoreMethod)
}
if client.LockURL.String() != conf["lock_address"] || client.LockMethod != "BLIP" {
t.Fatalf("Unexpected lock_address \"%s\" vs \"%s\" or lock_method \"%s\" vs \"%s\"", client.LockURL.String(),
conf["lock_address"], client.LockMethod, conf["lock_method"])
}
if client.UnlockURL.String() != conf["unlock_address"] || client.UnlockMethod != "BLOOP" {
t.Fatalf("Unexpected unlock_address \"%s\" vs \"%s\" or unlock_method \"%s\" vs \"%s\"", client.UnlockURL.String(),
conf["unlock_address"], client.UnlockMethod, conf["unlock_method"])
}
if client.Username != "user" || client.Password != "pass" {
t.Fatalf("Unexpected username \"%s\" vs \"%s\" or password \"%s\" vs \"%s\"", client.Username, conf["username"],
client.Password, conf["password"])
}
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 "POST", "PUT":
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
}