okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

nekomimi.petfix(cli): poll a warming host by target, not the known-host list6abefbf

main
2.0 KiB57 linesraw
1// Host polling must address the probe target, not search known hosts. A
2// controlled server makes the initial probing state deterministic.
3package daemon
4
5import (
6	"context"
7	"encoding/json"
8	"net/http"
9	"sync/atomic"
10	"testing"
11	"time"
12)
13
14func TestWarmHostPollsUnconfiguredTarget(t *testing.T) {
15	for _, target := range []string{"new-host", "ana@new-host", "[::1]"} {
16		t.Run(target, func(t *testing.T) {
17			var probed atomic.Bool
18			connection := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) {
19				w.Header().Set("Content-Type", "application/json")
20				host := map[string]any{
21					"target": target, "state": "ready", "detail": nil,
22					"os": nil, "architecture": nil, "home": nil,
23					"observed_at": nil, "authentication": nil,
24				}
25				switch {
26				case r.Method == http.MethodPost && r.URL.Path == "/hosts/"+target+"/probe":
27					probed.Store(true)
28					host["state"] = "probing"
29					w.WriteHeader(http.StatusAccepted)
30					_ = json.NewEncoder(w).Encode(host)
31				case r.Method == http.MethodGet && r.URL.Path == "/hosts":
32					if r.URL.Query().Get("target") == "" {
33						_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{}, "next": nil})
34						return
35					}
36					if !probed.Load() || r.URL.Query().Get("target") != target {
37						t.Errorf("unexpected host lookup: probed=%v query=%s", probed.Load(), r.URL.RawQuery)
38					}
39					_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{host}, "next": nil})
40				default:
41					t.Errorf("unexpected request: %s %s", r.Method, r.URL)
42					http.NotFound(w, r)
43				}
44			})
45			known, err := ListHosts(t.Context(), connection)
46			if err != nil || len(known) != 0 {
47				t.Fatalf("unconfigured host appeared in listing: %+v, %v", known, err)
48			}
49			ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
50			defer cancel()
51			status, err := WarmHost(ctx, connection, target)
52			if err != nil || status.Host != target || status.State != "ready" {
53				t.Fatalf("unconfigured probe did not settle: %+v, %v", status, err)
54			}
55		})
56	}
57}