mesh-catalog #293 failed with 'go.mod cannot be read to check its mesh-sdk pin: nats: no responders', put at the build node, and stayed failed: the forge's tool runner was not answering for a moment, which is not the change's failure. The forge's reads now mark such a call unheard, as the build steps' calls are (issue 1820).
106 lines
4.2 KiB
Go
106 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"mesh-delivery-checker/internal/check"
|
|
)
|
|
|
|
// capturedAnswer is what the forge's tool answered (internal/check/testdata/captured/README).
|
|
func capturedAnswer(t *testing.T, name string) json.RawMessage {
|
|
t.Helper()
|
|
raw, err := os.ReadFile(filepath.Join("..", "..", "internal", "check", "testdata", "captured", name+".json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func TestTheForgeReadsParseWhatTheForgeAnswered(t *testing.T) { // 1624/AC4
|
|
var asked []string
|
|
f := forgeReads{call: func(key string, body any) (json.RawMessage, error) {
|
|
b, _ := json.Marshal(body)
|
|
asked = append(asked, key+" "+string(b))
|
|
switch {
|
|
case key == getFileTool:
|
|
return capturedAnswer(t, "forge-get-file-go-mod"), nil
|
|
case strings.Contains(string(b), "cb54756c3865"):
|
|
return capturedAnswer(t, "forge-compare-trunk"), nil
|
|
}
|
|
return capturedAnswer(t, "forge-compare-branch"), nil
|
|
}}
|
|
text, err := f.File(context.Background(), "novox/mesh-delivery", "modules/mesh-delivery-builder/go.mod", "main")
|
|
if err != nil || !strings.Contains(text, "git.novox.be/novox/mesh-sdk/go v0.1.14-0.20261021100209-cb54756c3865") {
|
|
t.Errorf("file = %q, %v", text, err)
|
|
}
|
|
if on, err := f.OnTrunk(context.Background(), "novox/mesh-sdk", "cb54756c3865"); err != nil || !on {
|
|
t.Errorf("a commit on the trunk: %v, %v", on, err)
|
|
}
|
|
if on, err := f.OnTrunk(context.Background(), "novox/mesh-sdk", "9aaa5c80ac5c"); err != nil || on {
|
|
t.Errorf("a commit of the branch: %v, %v", on, err)
|
|
}
|
|
if len(asked) != 3 || !strings.Contains(asked[0], `"owner":"novox"`) || !strings.Contains(asked[2], `"base":"main"`) {
|
|
t.Errorf("asked %v", asked)
|
|
}
|
|
bad := forgeReads{call: func(string, any) (json.RawMessage, error) { return json.RawMessage(`{}`), nil }}
|
|
if _, err := bad.File(context.Background(), "novox/x", "go.mod", "main"); err == nil {
|
|
t.Error("an answer with no file was taken")
|
|
}
|
|
if _, err := bad.OnTrunk(context.Background(), "novox/x", "abc"); err == nil {
|
|
t.Error("an answer with no total was taken")
|
|
}
|
|
}
|
|
|
|
func TestTheForgeNamesTheWholeShaOfAShortCommit(t *testing.T) { // review D2
|
|
f := forgeReads{call: func(key string, body any) (json.RawMessage, error) {
|
|
if key != commitsTool {
|
|
t.Errorf("called %s", key)
|
|
}
|
|
return capturedAnswer(t, "forge-commits"), nil
|
|
}}
|
|
if sha, err := f.FullSHA(context.Background(), "novox/mesh-sdk", "cb54756c3865"); err != nil || sha != "cb54756c3865df5ac9c0dc0481ad9ebe2bf013a1" {
|
|
t.Errorf("sha = %q, %v", sha, err)
|
|
}
|
|
if _, err := f.FullSHA(context.Background(), "novox/mesh-sdk", "deadbeefdead"); err == nil {
|
|
t.Error("a commit that does not start with the pin was taken")
|
|
}
|
|
}
|
|
|
|
func TestAFileTheRefDoesNotHoldIsNotFoundAndOtherFailuresAreNot(t *testing.T) { // 1845/AC1, AC4
|
|
f := forgeReads{call: func(string, any) (json.RawMessage, error) {
|
|
return nil, errors.New("Gitea API contents/checks/particulars/go.mod?ref=98ca97fc: 404 object does not exist")
|
|
}}
|
|
if _, err := f.File(context.Background(), "novox/mesh-catalog", "checks/particulars/go.mod", "98ca97fc"); !errors.Is(err, check.ErrNotFound) {
|
|
t.Errorf("a 404 is %v, want ErrNotFound", err)
|
|
}
|
|
f = forgeReads{call: func(string, any) (json.RawMessage, error) {
|
|
return nil, errors.New("Gitea API contents/go.mod?ref=1111111111111111111111111111111404abcde: 500 internal error")
|
|
}}
|
|
if _, err := f.File(context.Background(), "novox/mesh-catalog", "go.mod", "main"); err == nil || errors.Is(err, check.ErrNotFound) {
|
|
t.Errorf("a 500 is %v, want a failure that is not ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestAForgeReadThatReachesNobodyIsUnheard(t *testing.T) { // 1624 C7: "nats: no responders" on mesh-catalog #293
|
|
f := forgeReads{call: func(string, any) (json.RawMessage, error) { return nil, nats.ErrNoResponders }}
|
|
_, err := f.File(context.Background(), "novox/mesh-catalog", "checks/agenthome/go.mod", "950e37a46e8d")
|
|
var u *check.Unheard
|
|
if !errors.As(err, &u) {
|
|
t.Errorf("err = %v, want it marked as reaching nobody", err)
|
|
}
|
|
if _, err := f.OnTrunk(context.Background(), "novox/mesh-sdk", "abc"); !errors.As(err, &u) {
|
|
t.Errorf("compare: %v", err)
|
|
}
|
|
if _, err := f.FullSHA(context.Background(), "novox/mesh-sdk", "abc"); !errors.As(err, &u) {
|
|
t.Errorf("commits: %v", err)
|
|
}
|
|
}
|