publish / publish (push) Successful in 1m6s
Each template the operator declares becomes an action with the effect declared for it, so judgement decides on the declaration. A launch carries Archie's idempotency key in the task message; invoking again finds that task instead of launching another, so a resumed run never launches twice. A task that ends in error is a known failure; one still running at the deadline is unknown.
119 lines
4.1 KiB
Go
119 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archipelago/sdk/connector"
|
|
)
|
|
|
|
// fakeSemaphore keeps tasks in memory. A launched task ends with finalStatus
|
|
// on its first status read, unless finalStatus is empty, when it runs for
|
|
// ever.
|
|
type fakeSemaphore struct {
|
|
mu sync.Mutex
|
|
tasks []task
|
|
launches int
|
|
finalStatus string
|
|
}
|
|
|
|
func (f *fakeSemaphore) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if r.Header.Get("Authorization") != "Bearer plainly-fake-token" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch {
|
|
case r.Method == http.MethodGet && r.URL.Path == "/api/project/3/tasks/last":
|
|
_ = json.MarshalWrite(w, f.tasks)
|
|
case r.Method == http.MethodPost && r.URL.Path == "/api/project/3/tasks":
|
|
var body struct {
|
|
TemplateID int `json:"template_id"`
|
|
Message string `json:"message"`
|
|
}
|
|
_ = json.UnmarshalRead(r.Body, &body)
|
|
f.launches++
|
|
t := task{ID: len(f.tasks) + 1, TemplateID: body.TemplateID, Status: "waiting", Message: body.Message}
|
|
f.tasks = append(f.tasks, t)
|
|
_ = json.MarshalWrite(w, t)
|
|
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/output"):
|
|
_ = json.MarshalWrite(w, []outputLine{{Output: "removed 3 files"}})
|
|
case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/api/project/3/tasks/"):
|
|
id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/project/3/tasks/"))
|
|
if f.finalStatus != "" {
|
|
f.tasks[id-1].Status = f.finalStatus
|
|
}
|
|
_ = json.MarshalWrite(w, f.tasks[id-1])
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func newTestConnector(t *testing.T, url string) *semaphore {
|
|
t.Helper()
|
|
s, err := newSemaphore(connector.Config{
|
|
Settings: map[string]string{
|
|
"base_url": url,
|
|
"project_id": "3",
|
|
"templates": `[{"action":"clean_tmp","template_id":7,"effect":{"reversibility":"irreversible","summary":"delete old files"},"target":{"kind":"host","environment":"non-production"},"vars":{"older_than_hours":"Age of files to delete"}}]`,
|
|
},
|
|
Secrets: map[string]string{"api.token": "plainly-fake-token"}, //nolint:gosec // a fake token the fake server checks
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.pollEvery, s.deadline = time.Millisecond, 50*time.Millisecond
|
|
return s
|
|
}
|
|
|
|
// TestLaunchHappensOncePerKey guards #2001's idempotency: a launch carries
|
|
// Archie's idempotency key, so invoking it again, from a connector that has
|
|
// restarted, finds the task already started instead of launching another. A
|
|
// task that ends in error is a known failure; one still running at the
|
|
// deadline is an unknown outcome.
|
|
func TestLaunchHappensOncePerKey(t *testing.T) {
|
|
input := jsontext.Value(`{"host":"web-1","older_than_hours":"24"}`)
|
|
tests := []struct {
|
|
name string
|
|
final string
|
|
invokes int
|
|
wantLaunches int
|
|
wantErr bool
|
|
wantUnknown bool
|
|
}{
|
|
{name: "one invoke launches once", final: "success", invokes: 1, wantLaunches: 1},
|
|
{name: "invoking again after a restart doesn't launch again", final: "success", invokes: 2, wantLaunches: 1},
|
|
{name: "a task that fails is a known failure", final: "error", invokes: 1, wantLaunches: 1, wantErr: true},
|
|
{name: "a task still running at the deadline is unknown", final: "", invokes: 2, wantLaunches: 1, wantErr: true, wantUnknown: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
fake := &fakeSemaphore{finalStatus: tt.final}
|
|
srv := httptest.NewServer(fake)
|
|
defer srv.Close()
|
|
var (
|
|
err error
|
|
unknown bool
|
|
)
|
|
for range tt.invokes {
|
|
// A fresh connector each time, as after a restart.
|
|
_, unknown, err = newTestConnector(t, srv.URL).Invoke(t.Context(), "clean_tmp", input, "run-1:investigate:4")
|
|
}
|
|
if fake.launches != tt.wantLaunches {
|
|
t.Fatalf("launched %d times, want %d", fake.launches, tt.wantLaunches)
|
|
}
|
|
if (err != nil) != tt.wantErr || unknown != tt.wantUnknown {
|
|
t.Fatalf("err = %v, unknown = %v; want error %v, unknown %v", err, unknown, tt.wantErr, tt.wantUnknown)
|
|
}
|
|
})
|
|
}
|
|
}
|