A directory the engine used as found kept its owner and mode in silence, so a module that cannot write it would just fail (hq issue 339 review). Each one is now a WARNING in every apply's log and its module's resource of kind directory, stated unhealthy on every look with its owner and what was declared, which the controller raises as the module's condition; `mesh-host hand-over <dir>` at the machine hands it to the mesh, and the next apply gives it the declared owner and mode. The guard also refuses the spool, /opt, the container runtimes' data (its configured data-root too), and an account's .ssh as an access, a mount, or a directory anywhere but its own home.
67 lines
2.4 KiB
Go
67 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/novox/mesh-host/internal/apply"
|
|
"github.com/novox/mesh-host/internal/link"
|
|
"github.com/novox/mesh-host/internal/store"
|
|
)
|
|
|
|
// Every directory used as found is its module's resource of kind directory, unhealthy, naming its owner, on
|
|
// every statement (novox/hq issue 339): the controller raises that as the module's condition.
|
|
func TestAFoundDirectoryIsStatedUnhealthy(t *testing.T) {
|
|
dir := t.TempDir()
|
|
known := store.State{Resources: []store.Applied{
|
|
{ID: "notes.data", Type: "directory", Target: dir, AsFound: true, Declared: "root, mode 755", AppliedAt: time.Now()},
|
|
{ID: "notes.cache", Type: "directory", Target: dir},
|
|
}}
|
|
h := withFoundDirectories(&link.Health{}, foundDirectories(known))
|
|
if len(h.Resources) != 1 {
|
|
t.Fatalf("stated: %+v", h.Resources)
|
|
}
|
|
r := h.Resources[0]
|
|
if r.Module != "notes" || r.Kind != link.KindDirectory || r.State != link.StateUnhealthy || r.Target != dir ||
|
|
!strings.HasPrefix(r.Reason, link.ReasonUsedAsFound) || !strings.Contains(r.Reason, "owned by ") ||
|
|
!strings.Contains(r.Reason, "root, mode 755") || !strings.Contains(r.Reason, "hand-over") {
|
|
t.Fatalf("stated as: %+v", r)
|
|
}
|
|
}
|
|
|
|
// `mesh-host hand-over` records a directory used as found, and refuses one that is not.
|
|
func TestHandOverRecordsOnlyAFoundDirectory(t *testing.T) {
|
|
dir := t.TempDir()
|
|
statePath := filepath.Join(dir, "state.json")
|
|
found := filepath.Join(dir, "found")
|
|
if err := store.Save(statePath, store.State{Resources: []store.Applied{
|
|
{ID: "notes.data", Type: "directory", Target: found, AsFound: true, Declared: "root, mode 755"},
|
|
{ID: "notes.mine", Type: "directory", Target: filepath.Join(dir, "mine")},
|
|
}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out bytes.Buffer
|
|
if err := handOverCommand(options{state: statePath, file: filepath.Join(dir, "mine"), out: &out}); err == nil {
|
|
t.Fatal("a directory the mesh made was handed over")
|
|
}
|
|
if err := handOverCommand(options{state: statePath, file: found + "/", out: &out}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
all, err := store.LoadHandedOver(statePath)
|
|
if err != nil || len(all) != 1 || all[0].Path != found {
|
|
t.Fatalf("recorded: %+v %v", all, err)
|
|
}
|
|
handedOverFrom(statePath)
|
|
if !apply.HandedOver()[found] {
|
|
t.Fatal("the apply does not see the hand-over")
|
|
}
|
|
if !strings.Contains(out.String(), "root, mode 755") {
|
|
t.Fatalf("said: %s", out.String())
|
|
}
|
|
_ = os.Remove(statePath)
|
|
}
|