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
87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
package resolution
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
func needy() mcat.Manifest {
|
|
return mcat.Manifest{
|
|
Module: "postgres", Version: "1",
|
|
OwnSecrets: mcat.OwnSecrets{"superuser": {Path: "/var/lib/mesh/postgres/superuser"}},
|
|
Resources: []map[string]any{
|
|
{"id": "store", "type": "container", "name": "mesh-postgres", "image": "postgres@sha256:x"},
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAModulesOwnSecretLandsSealed(t *testing.T) {
|
|
got, err := Resolve(shelf(needy()), []string{"postgres"}, reachable(), mcat.World{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := Declaration(got, mcat.Rendering{
|
|
Needed: map[string]map[string]string{"postgres": {"superuser": "c2VhbGVk"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, r := range out {
|
|
if r["path"] != "/var/lib/mesh/postgres/superuser" {
|
|
continue
|
|
}
|
|
if r["sealed"] != "c2VhbGVk" {
|
|
t.Fatalf("got %v", r)
|
|
}
|
|
if r["content"] != nil {
|
|
t.Fatal("a module's own secret was written in the clear")
|
|
}
|
|
return
|
|
}
|
|
t.Fatalf("no secret was written: %v", out)
|
|
}
|
|
|
|
func testADeclaredNeedThatWasNotMadeIsRefused(t *testing.T) {
|
|
// Skipping it would start a database with no password it knows, which fails to authenticate
|
|
// three layers from the machine reporting it.
|
|
got, _ := Resolve(shelf(needy()), []string{"postgres"}, reachable(), mcat.World{})
|
|
_, err := Declaration(got, mcat.Rendering{})
|
|
if err == nil {
|
|
t.Fatal("a module needing a secret was declared without one")
|
|
}
|
|
if !strings.Contains(err.Error(), "superuser") {
|
|
t.Fatalf("the refusal does not name what is missing: %v", err)
|
|
}
|
|
}
|
|
|
|
func testAModuleMayNeedSeveralThings(t *testing.T) {
|
|
// A password and a token, say. Telling them apart is the module's business, not the mesh's.
|
|
m := needy()
|
|
m.OwnSecrets["replication"] = mcat.OwnSecret{Path: "/var/lib/mesh/postgres/replication"}
|
|
got, _ := Resolve(shelf(m), []string{"postgres"}, reachable(), mcat.World{})
|
|
out, err := Declaration(got, mcat.Rendering{Needed: map[string]map[string]string{
|
|
"postgres": {"superuser": "b25l", "replication": "dHdv"},
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := map[string]string{}
|
|
for _, r := range out {
|
|
if path, ok := r["path"].(string); ok && strings.Contains(path, "/var/lib/mesh/postgres/") {
|
|
seen[path], _ = r["sealed"].(string)
|
|
}
|
|
}
|
|
if len(seen) != 2 || seen["/var/lib/mesh/postgres/superuser"] == seen["/var/lib/mesh/postgres/replication"] {
|
|
t.Fatalf("two needs did not land as two secrets: %v", seen)
|
|
}
|
|
}
|
|
|
|
// TestNeeds runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestNeeds(t *testing.T) {
|
|
t.Run("AModulesOwnSecretLandsSealed", testAModulesOwnSecretLandsSealed)
|
|
t.Run("ADeclaredNeedThatWasNotMadeIsRefused", testADeclaredNeedThatWasNotMadeIsRefused)
|
|
t.Run("AModuleMayNeedSeveralThings", testAModuleMayNeedSeveralThings)
|
|
}
|