package deploy import ( "os" "strings" "testing" ) func TestFetchCloudMapRegistry(t *testing.T) { SetCloudMapHTTPGetForTest(func(url string) ([]byte, error) { return []byte(`{"namespace":"prod.local","service":"seeder","instances":[{"agent_id":"seed-a","dns_name":"seeder.svc.prod.local","healthy":true,"fetch_url":"http://10.0.1.50/manifest"}]}`), nil }) defer SetCloudMapHTTPGetForTest(nil) doc, err := FetchCloudMapRegistry("http://registry.local/v1/seeder") if err != nil { t.Fatal(err) } records := CloudMapKnowNodeRecords(doc) if len(records) != 1 || records[0].TargetAgentID != "seed-a" { t.Fatalf("records=%+v", records) } } func TestAppendSpreadRouteTelemetryIncludesRouteVia(t *testing.T) { got := appendSpreadRouteTelemetry("joined", &SpreadRouteHint{EgressAgentID: "egress-1", RouteVia: "seeder.svc.prod.local"}) if got == "" || !strings.Contains(got, "route_via=seeder.svc.prod.local") { t.Fatalf("got=%q", got) } } func TestCloudMapEndpointFromEnv(t *testing.T) { t.Setenv(cloudMapEnvEndpoint, "http://127.0.0.1:8080/registry.json") if got := CloudMapEndpoint(); got != "http://127.0.0.1:8080/registry.json" { t.Fatalf("got=%q", got) } } func TestCloudMapEndpointUnset(t *testing.T) { _ = os.Unsetenv(cloudMapEnvEndpoint) if got := CloudMapEndpoint(); got != "" { t.Fatalf("got=%q", got) } } func TestCloudMapLANSeederHintsSkipsUnhealthy(t *testing.T) { doc := CloudMapRegistryDocument{ Instances: []CloudMapRegistryInstance{ {AgentID: "good", IP: "10.0.1.5", FetchURL: "http://10.0.1.5/manifest", Healthy: true}, {AgentID: "bad", IP: "10.0.1.6", FetchURL: "http://10.0.1.6/manifest", Healthy: false}, }, } hints := CloudMapLANSeederHints(doc) if len(hints) != 1 || hints[0].AgentID != "good" { t.Fatalf("hints=%+v", hints) } } func TestSyncCloudMapRegistryMergesGossip(t *testing.T) { SetCloudMapHTTPGetForTest(func(url string) ([]byte, error) { return []byte(`{"instances":[{"agent_id":"seed-x","healthy":true,"fetch_url":"http://10.0.2.1/manifest","ip":"10.0.2.1"}]}`), nil }) defer SetCloudMapHTTPGetForTest(nil) var merged []FleetGossipRecord if err := SyncCloudMapRegistry("http://registry.local/v1", func(recs []FleetGossipRecord) { merged = append(merged, recs...) }); err != nil { t.Fatal(err) } if len(merged) != 1 || merged[0].TargetAgentID != "seed-x" { t.Fatalf("merged=%+v", merged) } }