okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
6abefbf
main
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 ) { 15for _ ,target := range []string {"new-host" ,"ana@new-host" ,"[::1]" } { 16t .Run (target ,func (t * testing.T ) { 17var probed atomic.Bool 18connection := controlledConnection (t ,func (w http.ResponseWriter ,r * http.Request ) { 19w .Header ().Set ("Content-Type" ,"application/json" ) 20host := map [string ]any { 21"target" :target ,"state" :"ready" ,"detail" :nil , 22"os" :nil ,"architecture" :nil ,"home" :nil , 23"observed_at" :nil ,"authentication" :nil , 24 } 25switch { 26case r .Method == http .MethodPost && r .URL .Path == "/hosts/" + target + "/probe" : 27probed .Store (true ) 28host ["state" ]= "probing" 29w .WriteHeader (http .StatusAccepted ) 30_ = json .NewEncoder (w ).Encode (host ) 31case r .Method == http .MethodGet && r .URL .Path == "/hosts" : 32if r .URL .Query ().Get ("target" )== "" { 33_ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []any {},"next" :nil }) 34return 35 } 36if ! probed .Load ()|| r .URL .Query ().Get ("target" )!= target { 37t .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 }) 40default : 41t .Errorf ("unexpected request: %s %s" ,r .Method ,r .URL ) 42http .NotFound (w ,r ) 43 } 44 }) 45known ,err := ListHosts (t .Context (),connection ) 46if err != nil || len (known )!= 0 { 47t .Fatalf ("unconfigured host appeared in listing: %+v, %v" ,known ,err ) 48 } 49ctx ,cancel := context .WithTimeout (t .Context (),3 * time .Second ) 50defer cancel () 51status ,err := WarmHost (ctx ,connection ,target ) 52if err != nil || status .Host != target || status .State != "ready" { 53t .Fatalf ("unconfigured probe did not settle: %+v, %v" ,status ,err ) 54 } 55 }) 56 } 57}