Files
mesh-controller/cmd/mesh-controller/predefined_head_test.go
T

76 lines
3.1 KiB
Go

package main
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"github.com/novox/mesh-controller/internal/inventory"
)
// aForgeAnsweringTheCatalogue stands in for the forge's wire alone (askGitea): the head of the catalogue's trunk and its
// predefined.json. It says which repository path was asked.
func aForgeAnsweringTheCatalogue(t *testing.T) *[]string {
t.Helper()
var asked []string
was := askGitea
askGitea = func(_ context.Context, _ string, args map[string]any, into any) error {
p, _ := args["path"].(string)
asked = append(asked, p)
switch {
case strings.Contains(p, "/commits"):
return json.Unmarshal([]byte(`[{"sha":"abcdef0123456789","commit":{"committer":{"date":"2026-10-23T12:00:00Z"}}}]`), into)
case strings.Contains(p, "/contents/"):
body, _ := json.Marshal(map[string]string{"encoding": "base64", "content": base64.StdEncoding.EncodeToString([]byte(`{}`))})
return json.Unmarshal(body, into)
}
return errors.New("asked " + p)
}
t.Cleanup(func() { askGitea = was })
return &asked
}
func aFormsOnTheForge() *sourceForms {
return sourceFormsRead(context.Background(), time.Now, func(context.Context) (catalogue.World, error) {
return forgeHolding(float64(20000)), nil
})
}
// Issue 1902: the catalogue's repository is found among the modules' sources in the URL form (what every live row has) and
// in the seat form, `.git` dropped; the real lookup runs, only the forge's wire is a stand-in.
func TestTheCataloguesRepositoryIsFoundInTheURLFormAndTheSeatForm(t *testing.T) {
for name, source := range map[string]inventory.Source{
"the URL form": {Repository: "http://anchor.internal:20000/novox/mesh-catalog.git", Path: "modules/sudo"},
"the seat form": {Repository: "novox/mesh-catalog", Seat: "git", Path: "modules/sudo"},
} {
asked := aForgeAnsweringTheCatalogue(t)
entries := []inventory.Entry{
{Manifest: catalogue.Manifest{Module: "other"}, Source: inventory.Source{Repository: "http://anchor.internal:20000/novox/mesh-delivery.git"}},
{Manifest: catalogue.Manifest{Module: "sudo"}, Source: source},
}
head, err := readPredefinedHead(t.Context(), entries, aFormsOnTheForge())
if err != nil || head.Repository != "novox/mesh-catalog" || head.Commit != "abcdef0123456789" {
t.Fatalf("%s: the head is %+v (%v)", name, head, err)
}
if len(*asked) != 2 || !strings.HasPrefix((*asked)[0], "/repos/novox/mesh-catalog/commits") {
t.Errorf("%s: the forge was asked %v", name, *asked)
}
}
}
// And a mesh where nothing is built from the catalogue's repository says so by name, as an error that is not a wait.
func TestNoModuleBuiltFromTheCataloguesRepositoryIsNamedNotWaitedFor(t *testing.T) {
aForgeAnsweringTheCatalogue(t)
entries := []inventory.Entry{{Manifest: catalogue.Manifest{Module: "other"},
Source: inventory.Source{Repository: "http://anchor.internal:20000/novox/mesh-delivery.git"}}}
_, err := readPredefinedHead(t.Context(), entries, aFormsOnTheForge())
if !errors.Is(err, errNoCatalogueRepository) || errors.Is(err, errKeyWaits) {
t.Fatalf("a mesh with no catalogue module: %v", err)
}
}