package main import ( "strings" "testing" "github.com/novox/mesh-controller/internal/catalogue" ) // Defends novox/hq ADR 0111: a build source is on the git seat, or it is external. func forgeHolding(port any) catalogue.World { return catalogue.World{ Held: []catalogue.Held{{Claim: "git", Scope: catalogue.ScopeMesh, Node: "anchor", Module: "gitea"}}, Offered: map[string][]catalogue.Provider{"git": { // A second forge that does not hold the seat, so taking the first one found would be wrong. {Node: "archive", At: "archive.internal", Module: "gitea-mirror", Serves: map[string]any{"scheme": "http", "port": float64(3000)}}, {Node: "anchor", At: "anchor.internal", Module: "gitea", Serves: map[string]any{"scheme": "http", "port": port}}, }}, } } func TestARepositoryOnTheSeatIsClonedFromItsHolder(t *testing.T) { got, err := clonedFromSeat(forgeHolding(float64(3000)), "git", "novox/mesh-catalog") if err != nil { t.Fatal(err) } if got != "http://anchor.internal:3000/novox/mesh-catalog.git" { t.Fatalf("cloned from %s", got) } } func TestAMovedForgeIsFollowedWithoutRewritingAnything(t *testing.T) { // The whole point: the node gave the forge another port, and the same recorded path clones // from the new one. Nothing recorded contained the old one to be wrong. got, err := clonedFromSeat(forgeHolding(float64(3100)), "git", "novox/mesh-catalog") if err != nil { t.Fatal(err) } if !strings.Contains(got, ":3100/") { t.Fatalf("the moved port was not followed: %s", got) } } func TestWithNobodyHoldingTheSeatASelfHostedBuildIsRefusedAndSaysWhy(t *testing.T) { _, err := clonedFromSeat(catalogue.World{}, "git", "novox/mesh-catalog") if err == nil { t.Fatal("a repository was cloned from a forge the mesh does not have") } for _, want := range []string{"nobody holds the git seat", "without --self"} { if !strings.Contains(err.Error(), want) { t.Fatalf("the refusal does not say %q: %v", want, err) } } } func TestAnExternalRepositoryIsClonedExactlyAsGiven(t *testing.T) { // Unaffected by the seat, held or not: GitHub and GitLab are the ordinary cases. given := "https://github.com/someone/something.git" got, err := cloneFrom(t.Context(), buildSource{Repository: given}) if err != nil { t.Fatal(err) } if got != given { t.Fatalf("an external repository became %s", got) } } func TestAHolderOffThePrivateNetworkIsRefused(t *testing.T) { world := forgeHolding(float64(3000)) world.Offered["git"][1].At = "" if _, err := clonedFromSeat(world, "git", "novox/mesh-catalog"); err == nil || !strings.Contains(err.Error(), "private network") { t.Fatalf("a forge nothing can reach was cloned from: %v", err) } } func TestAHolderServingNoPortIsRefusedRatherThanGuessed(t *testing.T) { // A default port would be the forge's address guessed, which is what this exists to stop. if _, err := clonedFromSeat(forgeHolding(nil), "git", "novox/mesh-catalog"); err == nil { t.Fatal("a port was guessed for a forge that serves none") } } func TestAnAddressGivenAsAPathOnTheForgeIsRefused(t *testing.T) { for _, bad := range []string{ "https://github.com/someone/something.git", "git@anchor:novox/mesh-catalog.git", "/srv/git/mesh-catalog", "", } { if err := onASeat(bad); err == nil { t.Errorf("--self accepted %q as a path on the forge", bad) } } if err := onASeat("novox/mesh-catalog"); err != nil { t.Errorf("a path on the forge was refused: %v", err) } } func TestASourceOnTheSeatReadsAsAPathNotAnAddress(t *testing.T) { s := buildSource{Repository: "novox/mesh-catalog", Seat: "git"} if got := s.String(); got != "novox/mesh-catalog on the git seat" { t.Fatalf("read as %q", got) } }