package adminapi import ( "net/http/httptest" "testing" ) func TestNodeExporterTargetsAreTheControlPlaneAndTheStore(t *testing.T) { a := &API{} a.SetStoreStatus(func() ([]StoreMember, error) { return []StoreMember{{Name: "pg01", Host: "10.0.0.149"}, {Name: "ctl", Host: "10.0.0.140"}}, nil }) r := httptest.NewRequest("GET", "http://10.0.0.140:9091/prometheus/targets", nil) got := a.nodeExporterTargetsWith(r, nil) if len(got) != 2 { t.Fatalf("%+v", got) } for _, g := range got { if g.Targets[0] == "10.0.0.140:9100" && g.Labels["role"] != "control-plane" { t.Errorf("the control plane twice: %+v", got) } } } func TestEveryControlPlaneListsTheOthersAlike(t *testing.T) { // 140 called 150 a store member and 150 called itself a control plane: // Prometheus, asking both, scraped 150 twice and counted it twice. targets := func(self string) map[string]string { a := &API{} a.SetStoreStatus(func() ([]StoreMember, error) { return []StoreMember{{Name: "llmgw-pg01", Host: "10.0.0.149"}, {Name: "llmgw-ctl02", Host: "10.0.0.150"}}, nil }) a.SetControlPlanes(func() ControlPlanes { return ControlPlanes{Members: []ControlPlane{{Name: "llmgw-ctl", Address: "10.0.0.140:7443"}, {Name: "llmgw-ctl02", Address: "10.0.0.150:7443"}}} }) r := httptest.NewRequest("GET", "http://"+self+":9091/prometheus/targets", nil) out := map[string]string{} for _, g := range a.nodeExporterTargetsWith(r, nil) { out[g.Targets[0]] = g.Labels["role"] } return out } from140, from150 := targets("10.0.0.140"), targets("10.0.0.150") if from140["10.0.0.150:9100"] != "control-plane" || from150["10.0.0.140:9100"] != "control-plane" || from140["10.0.0.149:9100"] != "store" { t.Errorf("from 140 %v, from 150 %v", from140, from150) } }