Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 89 additions & 40 deletions go/agent-runner/internal/railsclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,105 @@
package railsclient

import (
"io"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestReleaseGateAndPostJSON(t *testing.T) {
var posted string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/release-gate/check":
if r.URL.Query().Get("shard") != "shard-a" {
t.Errorf("shard = %s", r.URL.Query().Get("shard"))
func TestGetReleaseGate(t *testing.T) {
tests := []struct {
name string
status int
body string
wantAllowed bool
}{
{name: "open", status: http.StatusOK, body: `{"allowed":true}`, wantAllowed: true},
{name: "locked", status: http.StatusLocked, body: `{"allowed":false}`, wantAllowed: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %s, want GET", r.Method)
}
if r.URL.Path != "/api/release-gate/check" {
t.Errorf("path = %s", r.URL.Path)
}
if got := r.URL.Query().Get("shard"); got != "shard-a" {
t.Errorf("shard = %q, want shard-a", got)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.status)
_, _ = w.Write([]byte(tt.body))
}))
defer server.Close()

code, body, err := New(server.URL).GetReleaseGate("shard-a")
if err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusLocked)
_, _ = w.Write([]byte(`{"allowed":false}`))
case "/api/agents/incident_response/run":
body, _ := io.ReadAll(r.Body)
posted = string(body)
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("content type = %s", r.Header.Get("Content-Type"))
if code != tt.status {
t.Fatalf("status = %d, want %d", code, tt.status)
}
w.WriteHeader(http.StatusAccepted)
default:
http.NotFound(w, r)
}
}))
defer server.Close()

client := New(server.URL)
code, body, err := client.GetReleaseGate("shard-a")
if err != nil {
t.Fatal(err)
}
if code != http.StatusLocked || !strings.Contains(string(body), `"allowed":false`) {
t.Fatalf("gate = %d %s", code, body)
}
if err := client.PostJSON("/api/agents/incident_response/run", map[string]any{"shard": "shard-a"}); err != nil {
t.Fatal(err)

var decoded struct {
Allowed bool `json:"allowed"`
}
if err := json.Unmarshal(body, &decoded); err != nil {
t.Fatalf("decode gate response: %v", err)
}
if decoded.Allowed != tt.wantAllowed {
t.Fatalf("allowed = %t, want %t", decoded.Allowed, tt.wantAllowed)
}
})
}
if !strings.Contains(posted, `"shard":"shard-a"`) {
t.Fatalf("posted body = %s", posted)
}

func TestPostJSON(t *testing.T) {
tests := []struct {
name string
status int
wantErr string
}{
{name: "accepted", status: http.StatusAccepted},
{name: "rails error", status: http.StatusInternalServerError, wantErr: "500"},
}
const path = "/api/agents/incident_response/run"

fail := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusInternalServerError)
}))
defer fail.Close()
if err := New(fail.URL).PostJSON("/api/chaos/heal", map[string]any{}); err == nil || !strings.Contains(err.Error(), "500") {
t.Fatalf("expected post error, got %v", err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if r.URL.Path != path {
t.Errorf("path = %s, want %s", r.URL.Path, path)
}
if got := r.Header.Get("Content-Type"); got != "application/json" {
t.Errorf("Content-Type = %q", got)
}
var payload map[string]any
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
t.Errorf("decode request body: %v", err)
}
if payload["shard"] != "shard-a" {
t.Errorf("shard = %v, want shard-a", payload["shard"])
}
w.WriteHeader(tt.status)
_, _ = w.Write([]byte("response body"))
}))
defer server.Close()

err := New(server.URL).PostJSON(path, map[string]any{"shard": "shard-a"})
if tt.wantErr == "" && err != nil {
t.Fatalf("PostJSON returned error: %v", err)
}
if tt.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tt.wantErr)) {
t.Fatalf("PostJSON error = %v, want substring %q", err, tt.wantErr)
}
})
}
}