Files
felhom-controller/controller/internal/stacks/r553_deploy_error_kinds_test.go
T
admin e3bb775ac5 FileBrowser is published only after its default login is checked (R-1126); the memory refusal says what to do (R-1125)
R-1126 (D11 = A): FileBrowser starts off traefik-public, on its own network felhom-filebrowser; the controller joins it
there, checks this run's default login, and only then puts FileBrowser on traefik-public (files.<domain> served).
FileBrowser's own admin-password setting re-applies at every start (1.5.6 cmd/user.go:50-57, measured on 9202), so it
cannot carry the box's password without overwriting the household's own. Tests red-proved.
R-1125 (D10 = A): err.stacks.not_enough_memory says the memory is held by the installed apps and to remove one or use
a machine with more memory (HU + EN, reworded on purpose). TestR1125_NotEnoughMemorySaysWhatToDo, red-proved.

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-10-17 12:49:34 +02:00

180 lines
7.5 KiB
Go

package stacks
import (
"errors"
"io"
"log"
"os"
"path/filepath"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
"gitea.dooplex.hu/admin/felhom-controller/internal/util"
)
// r553Manager builds a real Manager over a temp stacks dir holding one app fixture, so DeployStack's
// own validation runs — no stub, no modelled predicate.
func r553Manager(t *testing.T, felhomYml string) *Manager {
t.Helper()
dir := t.TempDir()
cfg := &config.Config{}
cfg.Paths.StacksDir = filepath.Join(dir, "stacks")
cfg.Paths.SystemDataPath = filepath.Join(dir, "system")
cfg.Stacks.ComposeCommand = "docker compose" // no detection; nothing is executed in this test
appDir := filepath.Join(cfg.Paths.StacksDir, "r553app")
if err := os.MkdirAll(appDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(appDir, "docker-compose.yml"), []byte("services:\n app:\n image: busybox\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(appDir, ".felhom.yml"), []byte(felhomYml), 0o644); err != nil {
t.Fatal(err)
}
m, err := NewManager(cfg, log.New(io.Discard, "", 0))
if err != nil {
t.Fatal(err)
}
if err := m.ScanStacks(); err != nil {
t.Fatal(err)
}
if _, ok := m.GetStack("r553app"); !ok {
t.Fatal("fixture invalid: the app was not scanned, so DeployStack would refuse for the wrong reason")
}
return m
}
// R-553 — the deploy refusals carry their KIND, and their Hungarian sentences are byte-for-byte what
// they were before the kinds existed (a localisation-adjacent change may not move one byte of copy).
//
// RED-PROOF (REPORT): drop the kind from either producer in deploy.go (back to plain fmt.Errorf) and
// the errors.Is assertion fails, while the message assertion still passes — which is exactly the
// silent state this row exists to prevent.
func TestR553_DeployProducersCarryKindAndKeepTheirWords(t *testing.T) {
cases := []struct {
name string
yml string
values map[string]string
kind error
wantMsg string
}{
{
name: "required field left empty",
yml: "display_name: R553\ndeploy_fields:\n - env_var: DATA_DIR\n label: Adatmappa\n type: text\n required: true\n",
values: map[string]string{},
kind: ErrRequiredField,
wantMsg: `a(z) "Adatmappa" (DATA_DIR) mező kitöltése kötelező`,
},
{
name: "password field left empty",
yml: "display_name: R553\ndeploy_fields:\n - env_var: ADMIN_PW\n label: Jelszó\n type: password\n",
values: map[string]string{},
kind: ErrRequiredField,
wantMsg: `a(z) "Jelszó" mező kitöltése kötelező — használd a Generálás gombot vagy írj be egy jelszót`,
},
{
name: "path field naming something that is not there",
yml: "display_name: R553\ndeploy_fields:\n - env_var: MEDIA\n label: Média\n type: path\n",
values: map[string]string{"MEDIA": "/definitely/not/here/r553"},
kind: ErrPathMissing,
wantMsg: `path "/definitely/not/here/r553" does not exist for field "Média"`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
m := r553Manager(t, c.yml)
_, err := m.DeployStack(DeployRequest{StackName: "r553app", Values: c.values})
if err == nil {
t.Fatal("deploy was admitted — the fixture no longer exercises the refusal")
}
if !errors.Is(err, c.kind) {
t.Errorf("refusal carries no kind: %v (want errors.Is … %v)", err, c.kind)
}
if err.Error() != c.wantMsg {
t.Errorf("the customer's sentence CHANGED:\n got %q\nwant %q", err.Error(), c.wantMsg)
}
})
}
}
// The already-deployed refusal, on the same Manager: its words are unchanged and it carries its kind
// (the API answers 409 from that kind).
func TestR553_AlreadyDeployedCarriesKindAndKeepsItsWords(t *testing.T) {
m := r553Manager(t, "display_name: R553\n")
m.mu.Lock()
m.stacks["r553app"].Deployed = true
m.mu.Unlock()
_, err := m.DeployStack(DeployRequest{StackName: "r553app"})
if err == nil {
t.Fatal("a deployed stack was admitted for a second deploy")
}
if !errors.Is(err, ErrAlreadyDeployed) {
t.Errorf("refusal carries no kind: %v", err)
}
if want := `stack "r553app" is already deployed; use update instead`; err.Error() != want {
t.Errorf("message CHANGED:\n got %q\nwant %q", err.Error(), want)
}
}
// The memory refusal is built by memoryVerdict from the HOST's real memory, so a unit test cannot
// make it fire. Two halves therefore, and v0.253.0 (R-557) turned the weaker one into the stronger:
//
// - BEHAVIOUR: the value memoryVerdict now returns is an ERROR it builds itself, so the test can
// build the same value and check it — the kind is reachable by errors.Is AND the sentence is the
// one the customer used to read, byte for byte, in Hungarian. That is no longer a source check.
// - SOURCE: memoryVerdict still hands that error back rather than a sentence. The defect this
// guards is a call written the old way, and only the source can show that.
func TestR553_MemoryRefusalIsWrappedWithItsKind(t *testing.T) {
// The exact value the producer returns, for a 900 MB request that does not fit.
e := util.MsgErrorf(ErrNotEnoughMemory, "err.stacks.not_enough_memory", 900, 100, 2048, 1436, 512)
if !errors.Is(e, ErrNotEnoughMemory) {
t.Error("the memory refusal does not carry ErrNotEnoughMemory — the API would answer 500 " +
"for a refusal the customer can act on")
}
// R-1125 (v0.327.0): reworded — the memory is held by the installed apps, and the sentence says what to do.
const wantHU = "Nincs elég memória ehhez az alkalmazáshoz a már telepítettek mellett: kell 900 MB, szabad 100 MB " +
"(összesen 2048 MB, ebből 1436 MB-ot a telepített alkalmazások foglalnak, 512 MB a rendszeré). " +
"Távolíts el egy alkalmazást, amelyre nincs szükséged, vagy használj több memóriájú gépet."
if e.Error() != wantHU {
t.Errorf("the Hungarian refusal moved:\n got %q\nwant %q", e.Error(), wantHU)
}
if en := util.ErrText("en", e); en == wantHU || en == "" {
t.Errorf("the refusal has no English of its own: %q", en)
}
src, err := os.ReadFile("deploy.go")
if err != nil {
t.Fatal(err)
}
body := string(src)
if !strings.Contains(body, `util.MsgErrorf(ErrNotEnoughMemory, "err.stacks.not_enough_memory"`) {
t.Error("memoryVerdict no longer builds its refusal with ErrNotEnoughMemory and its key")
}
if strings.Contains(body, `errors.New(refusal)`) || strings.Contains(body, `fmt.Errorf(refusal)`) {
t.Error("the memory refusal is back to a bare error: its kind is gone (R-553)")
}
}
// R-1125 (operator decision D10 = A): the install check counts the apps' real memory, so a household meets this refusal
// more often. It must say, in both languages, that the memory is held by the apps already installed and what to do.
// RED-PROOF (2026-10-17): the old sentence („Szükséges … Elérhető …", no advice) fails every check below.
func TestR1125_NotEnoughMemorySaysWhatToDo(t *testing.T) {
e := util.MsgErrorf(ErrNotEnoughMemory, "err.stacks.not_enough_memory", 600, 200, 3712, 3310, 384)
for _, c := range []struct {
lang string
want []string
}{
{"hu", []string{"a már telepítettek mellett", "kell 600 MB", "szabad 200 MB", "Távolíts el egy alkalmazást", "több memóriájú gépet"}},
{"en", []string{"next to the apps already installed", "needs 600 MB", "200 MB is free", "Remove an app", "a machine with more memory"}},
} {
got := util.ErrText(c.lang, e)
for _, w := range c.want {
if !strings.Contains(got, w) {
t.Errorf("%s: the refusal does not say %q: %q", c.lang, w, got)
}
}
}
}