package main import ( "encoding/json" "errors" "strings" "testing" "time" "git.novox.be/novox/mesh-sdk/go/health" ) // aToolBundle is a bundle over the stood-in mesh and stream, with a watcher's socket answering state as subscribed // says, and recording the asks it was given. func aToolBundle(t *testing.T, mesh *fakeMesh, subscribed *bool, asked *[]string) (*bundle, *fakeStream) { t.Helper() f := aFolder(t) stream := &fakeStream{refuse: map[string]bool{}} b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish}, names: &namer{folder: f, ask: mesh.ask, now: time.Now}} if subscribed == nil { return b, stream } l, err := f.listen() if err != nil { t.Fatal(err) } t.Cleanup(func() { l.Close() }) go serveSocket(l, func(ask string) (any, error) { *asked = append(*asked, ask) return watcherState{Subscribed: *subscribed, Rating: "PORTAL", AsksOpened: 1, DroppedPastBound: 2}, nil }) return b, stream } func runTool(t *testing.T, b *bundle, name string) any { t.Helper() for _, tool := range b.tools() { if tool.Name == name { out, err := tool.Run(map[string]any{}) if err != nil { t.Fatalf("%s: %v", name, err) } return out } } t.Fatalf("no tool %s", name) return nil } // The health tool is unhealthy while the watcher's socket does not answer, while the watcher is not subscribed and // while the last uplink-names call stands failed, naming which; no holder is healthy. It is a tool that reads and calls // nothing on the bus: the names are not given again at a look (design 08 sign-in-page §13, §23, §25; C-2c). func TestTheHealthToolSaysWhatIsWrong(t *testing.T) { journalLines(t) for _, c := range []struct { name string socket bool subscribed bool holder string fail bool healthy bool why string }{ {"no socket", false, false, "", false, false, "the watcher's socket does not answer"}, {"not subscribed", true, false, "", false, false, "the watcher is not subscribed on the system bus"}, {"a failed call", true, true, "systemd-resolved", true, false, "the last uplink-names call failed"}, {"no holder", true, true, "", false, true, "no holder"}, {"given", true, true, "systemd-resolved", false, true, "ok"}, } { t.Run(c.name, func(t *testing.T) { mesh := &fakeMesh{holder: c.holder, routes: `{"links":[],"mesh":{"domains":[],"servers":[]},"mesh_resolvers":{"silent":false},"uplink_names":{"names":["check.example.net"],"link":"wlan0","refused":""}}`} var asked []string subscribed := c.subscribed var b *bundle if c.socket { b, _ = aToolBundle(t, mesh, &subscribed, &asked) } else { b, _ = aToolBundle(t, mesh, nil, nil) } b.folder.writeCheckAddress("http://check.example.net/") if c.fail { mesh.fail = errors.New("no answer within its bound") } b.names.give("the bundle's start") mesh.fail = nil got := runTool(t, b, "uplink_health").(health.Answer) if got.Healthy != c.healthy || !strings.Contains(got.Why, c.why) { t.Fatalf("answered %+v", got) } }) } mesh := &fakeMesh{holder: "systemd-resolved", routes: `{"uplink_names":{"names":[],"link":"","refused":"resolved refused the domains"}}`} subscribed, asked := true, []string{} b, _ := aToolBundle(t, mesh, &subscribed, &asked) b.folder.writeCheckAddress("http://check.example.net/") b.names.give("the bundle's start") if got := runTool(t, b, "uplink_health").(health.Answer); !got.Healthy || len(mesh.called()) != 1 { t.Fatalf("the health tool answered %+v and called uplink-names %d times", got, len(mesh.called())) } } // Each tool answers: the read of the sign-in reaction, the names given again, the watcher asked rating-again, and the // kept events published again (§23, §25; R3-5). func TestEachToolAnswers(t *testing.T) { journalLines(t) mesh := &fakeMesh{holder: "systemd-resolved", routes: `{"uplink_names":{"names":["check.example.net"],"link":"wlan0","refused":"resolved refused the domains"}}`} subscribed, asked := true, []string{} b, stream := aToolBundle(t, mesh, &subscribed, &asked) b.folder.writeCheckAddress("http://check.example.net/") keepThree(t, b.folder) stream.refuse["laptop.sign-in-ended.1"] = true b.pub.publishKept("the bundle's start") read := runTool(t, b, "uplink_sign_in").(signInRead) raw, _ := json.Marshal(read) if read.Watcher == nil || read.Watcher.Rating != "PORTAL" || read.Watcher.DroppedPastBound != 2 || read.Waiting != 3 || read.RefusedSince != 1 || read.Names != nil || read.ResolverRefused == nil || *read.ResolverRefused != "resolved refused the domains" { t.Fatalf("uplink_sign_in answered %s", raw) } if c := runTool(t, b, "uplink_names_again").(NamesCall); c.Result != resultOK || len(mesh.called()) != 1 { t.Fatalf("uplink_names_again answered %+v", c) } if s := runTool(t, b, "uplink_rating_again").(watcherState); !s.Subscribed || asked[len(asked)-1] != askRatingAgain { t.Fatalf("uplink_rating_again answered %+v, asked %v", s, asked) } if r := runTool(t, b, "uplink_publish_again").(publishResult); r.Published != 3 || r.Waiting != 0 { t.Fatalf("uplink_publish_again answered %+v", r) } var names []string for _, tool := range b.tools() { names = append(names, tool.Name) } if strings.Join(names, " ") != "uplink_sign_in uplink_names_again uplink_rating_again uplink_publish_again uplink_health" { t.Fatalf("serves %v", names) } }