Files
mesh-host/internal/bootstrap/build_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

106 lines
5.4 KiB
Go

package bootstrap
import (
"context"
"strings"
"testing"
)
// An installer that carries a builder and was told nothing refuses, and says what is missing.
//
// **Exercised rather than assumed.** This is the refusal that stands between a person and a
// machine left holding a store, a broker and no control plane — the failure the whole preflight
// exists to prevent — and a refusal nothing tests is a refusal nobody has read.
func testAnInstallerWithNothingToBuildRefuses(t *testing.T) {
err := Source{}.Check()
if err == nil {
t.Fatal("a source naming no repository was accepted; nothing would have been built")
}
for _, want := range []string{"--source", "--source-ref"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("the refusal does not name %s, so it does not say how to fix it: %v", want, err)
}
}
}
// A repository without a commit refuses too, because a branch is somebody else's moving target.
func testABranchIsNotACommit(t *testing.T) {
err := Source{Repository: "https://example.invalid/mesh-controller.git"}.Check()
if err == nil {
t.Fatal("a source with no ref was accepted; genesis would have built whatever a branch pointed at")
}
if !strings.Contains(err.Error(), "--source-ref") {
t.Errorf("the refusal does not name the flag that fixes it: %v", err)
}
}
// And a repository with a commit is enough.
func testARepositoryAndACommitIsEnough(t *testing.T) {
if err := (Source{Repository: "https://example.invalid/mesh-controller.git", Ref: "a1b2c3d4"}).Check(); err != nil {
t.Fatalf("a repository and a commit were refused: %v", err)
}
}
// **The build hands over the manifest, and the installer registers that one.** The control plane
// used to have two manifests — one at the root of its repository, which the mesh reads whenever
// it rebuilds the control plane from source, and a copy in the catalogue, which genesis read — and
// nothing kept them equal (novox/hq 04-ISSUES/072). The builder already reports the manifest it
// built, artifact resolved to the image; genesis takes it from there and reads no second copy.
func testTheBuildHandsOverTheManifestTheMeshWillHold(t *testing.T) {
manifest := `{"module":"mesh-controller","version":"1","resources":[` +
`{"id":"server","type":"container","name":"mesh-controller","image":"` + builtImage + `"}]}`
runtime := &asked{answer: aRepository(t, imageFormManifest, func(string, []string) (string, error) {
return `{"module":"mesh-controller","commit":"a1b2c3d4","manifest":` + manifest +
`,"made":[{"name":"server","kind":"image","reference":"` + builtImage + `"}]}` + "\n", nil
})}
built, err := BuildControlPlane(context.Background(), runtime.run, "mesh-builder:test",
Source{Repository: "https://example.invalid/mesh-controller.git", Ref: "a1b2c3d4"}, false, func(string) {})
if err != nil {
t.Fatal(err)
}
if built.Image != builtImage {
t.Errorf("the built image is %q", built.Image)
}
if string(built.Manifest) != manifest {
t.Errorf("the manifest handed over is not the one the builder reported:\n%s", built.Manifest)
}
}
// A result without a manifest is a build the installer cannot finish, and it is refused beside the
// builder that said it rather than at step 9 with a message about a missing file.
func testABuildReportingNoManifestIsRefused(t *testing.T) {
runtime := &asked{answer: aRepository(t, imageFormManifest, func(string, []string) (string, error) {
return `{"module":"mesh-controller","commit":"a1b2c3d4",` +
`"made":[{"name":"server","kind":"image","reference":"` + builtImage + `"}]}`, nil
})}
_, err := BuildControlPlane(context.Background(), runtime.run, "mesh-builder:test",
Source{Repository: "https://example.invalid/mesh-controller.git", Ref: "a1b2c3d4"}, false, func(string) {})
if err == nil || !strings.Contains(err.Error(), "no manifest") {
t.Fatalf("a build reporting no manifest was accepted, or refused for another reason: %v", err)
}
}
// And a manifest that does not name the image the build produced describes some other build.
func testABuildWhoseManifestNamesAnotherImageIsRefused(t *testing.T) {
runtime := &asked{answer: aRepository(t, imageFormManifest, func(string, []string) (string, error) {
return `{"module":"mesh-controller","commit":"a1b2c3d4","manifest":{"module":"mesh-controller",` +
`"resources":[{"id":"server","type":"container","image":"sha256:` + strings.Repeat("9", 64) + `"}]},` +
`"made":[{"name":"server","kind":"image","reference":"` + builtImage + `"}]}`, nil
})}
_, err := BuildControlPlane(context.Background(), runtime.run, "mesh-builder:test",
Source{Repository: "https://example.invalid/mesh-controller.git", Ref: "a1b2c3d4"}, false, func(string) {})
if err == nil || !strings.Contains(err.Error(), "does not name that image") {
t.Fatalf("a manifest naming another image was accepted, or refused for another reason: %v", err)
}
}
// TestBuild runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestBuild(t *testing.T) {
t.Run("AnInstallerWithNothingToBuildRefuses", testAnInstallerWithNothingToBuildRefuses)
t.Run("ABranchIsNotACommit", testABranchIsNotACommit)
t.Run("ARepositoryAndACommitIsEnough", testARepositoryAndACommitIsEnough)
t.Run("TheBuildHandsOverTheManifestTheMeshWillHold", testTheBuildHandsOverTheManifestTheMeshWillHold)
t.Run("ABuildReportingNoManifestIsRefused", testABuildReportingNoManifestIsRefused)
t.Run("ABuildWhoseManifestNamesAnotherImageIsRefused", testABuildWhoseManifestNamesAnotherImageIsRefused)
}