114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
package chaos
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestClientCreateLatencyAndReset(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
toxics := map[string][]Toxic{}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == http.MethodPost && r.URL.Path == "/proxies":
|
|
var p Proxy
|
|
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
|
t.Errorf("decode proxy: %v", err)
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
if p.Listen == "0.0.0.0:0" {
|
|
p.Listen = "0.0.0.0:18080"
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(p)
|
|
|
|
case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/toxics"):
|
|
var toxic Toxic
|
|
if err := json.NewDecoder(r.Body).Decode(&toxic); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
name := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/proxies/"), "/toxics")
|
|
toxics[name] = append(toxics[name], toxic)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(toxic)
|
|
|
|
case r.Method == http.MethodDelete:
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
c := NewClient(srv.URL)
|
|
p, err := c.CreateProxy("q-demo", "0.0.0.0:0", "127.0.0.1:19000")
|
|
if err != nil {
|
|
t.Fatalf("CreateProxy: %v", err)
|
|
}
|
|
if p.Upstream != "127.0.0.1:19000" {
|
|
t.Fatalf("upstream = %q", p.Upstream)
|
|
}
|
|
if p.Listen != "0.0.0.0:18080" {
|
|
t.Fatalf("listen = %q", p.Listen)
|
|
}
|
|
|
|
lat, err := c.AddLatency("q-demo", "lag", 200, 50, "")
|
|
if err != nil {
|
|
t.Fatalf("AddLatency: %v", err)
|
|
}
|
|
if lat.Type != "latency" {
|
|
t.Fatalf("type = %q", lat.Type)
|
|
}
|
|
if _, err := c.AddResetPeer("q-demo", "drop", 0, ""); err != nil {
|
|
t.Fatalf("AddResetPeer: %v", err)
|
|
}
|
|
if got := toxics["q-demo"]; len(got) != 2 {
|
|
t.Fatalf("toxics len = %d", len(got))
|
|
}
|
|
if got := toxics["q-demo"][0]; got.Type != "latency" {
|
|
t.Fatalf("first toxic type = %q", got.Type)
|
|
}
|
|
if err := c.RemoveToxic("q-demo", "lag"); err != nil {
|
|
t.Fatalf("RemoveToxic: %v", err)
|
|
}
|
|
if err := c.DeleteProxy("q-demo"); err != nil {
|
|
t.Fatalf("DeleteProxy: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestClientRejectsEmptyFields(t *testing.T) {
|
|
t.Parallel()
|
|
c := NewClient("http://127.0.0.1:1")
|
|
if _, err := c.CreateProxy("", "0.0.0.0:0", "127.0.0.1:1"); err == nil {
|
|
t.Fatal("expected error for empty name")
|
|
}
|
|
if _, err := c.CreateProxy("x", "", "127.0.0.1:1"); err == nil {
|
|
t.Fatal("expected error for empty listen")
|
|
}
|
|
if _, err := c.CreateProxy("x", "0.0.0.0:0", ""); err == nil {
|
|
t.Fatal("expected error for empty upstream")
|
|
}
|
|
}
|
|
|
|
func TestCreateProxyErrorStatus(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusConflict)
|
|
_, _ = io.WriteString(w, "already exists")
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := NewClient(srv.URL)
|
|
if _, err := c.CreateProxy("q", "0.0.0.0:0", "127.0.0.1:9"); err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|