135 lines
6.1 KiB
Go
135 lines
6.1 KiB
Go
//go:build linux
|
|
|
|
package apply
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/declaration"
|
|
"github.com/novox/mesh-host/internal/store"
|
|
)
|
|
|
|
// novox/hq issue 1873: a module's directory in the mesh's own tree (<data root>/mesh/<module>, place "mesh") that is
|
|
// there before its declaration, owned by the engine's own account, was made by the mesh on the way to something below
|
|
// it: it is the mesh's, given its declared mode and owner, never used as found. Only the engine's account makes a
|
|
// directory in a tree that is the engine's (0755, root); on ace and novox /var/lib/mesh/postgres was there as
|
|
// root:root 0700 before postgres's data directory was applied, and the engine left it to root, so the postgres
|
|
// provider, running as the operator's account, could not reach its own files.
|
|
func testADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs(t *testing.T) {
|
|
root := t.TempDir()
|
|
before := meshDataRoot
|
|
meshDataRoot = filepath.Join(root, "mesh")
|
|
t.Cleanup(func() { meshDataRoot = before })
|
|
made := filepath.Join(meshDataRoot, "postgres")
|
|
if err := os.MkdirAll(made, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(made, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
decl := `{"id":"postgres.data","type":"directory","path":"` + made + `","mode":"0750"}`
|
|
var said []string
|
|
_, state, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared,
|
|
noServices, func(line string) { said = append(said, line) }, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(strings.Join(said, "\n"), "WARNING postgres.data") {
|
|
t.Errorf("a directory the engine's account made in the mesh's tree was used as found: %q", said)
|
|
}
|
|
if a, _ := state.Find("postgres.data"); a.AsFound {
|
|
t.Error("it is recorded as found")
|
|
}
|
|
if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 {
|
|
t.Errorf("it was not given its declared mode: %04o", info.Mode().Perm())
|
|
}
|
|
// H-A-374, H-A-392: a record-held directory whose mode drifted on disk is set right at the engine's start, with one
|
|
// line, though its composition is unchanged; as declared, it says nothing.
|
|
if err := os.Chmod(made, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
postgres := declare(t, `{"id":"postgres.data","type":"directory","path":"`+made+`","mode":"0750"}`)
|
|
if drifts := DirectoryDrifts(postgres, state); len(drifts) != 1 || drifts[0].Drift != "mode 0700, 0750 declared" {
|
|
t.Errorf("the drift read is %+v", drifts)
|
|
}
|
|
if lines := ConvergeDirectories(context.Background(), archHost(t), postgres, state, noServices); len(lines) != 1 || !strings.Contains(lines[0], "was mode 0700, 0750 declared, and is set right") {
|
|
t.Errorf("a drifted directory the mesh made was converged as %q", lines)
|
|
}
|
|
if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 {
|
|
t.Errorf("it was not set right at the start: %04o", info.Mode().Perm())
|
|
}
|
|
if lines := ConvergeDirectories(context.Background(), archHost(t), postgres, state, noServices); len(lines) != 0 {
|
|
t.Errorf("a directory as declared was said at the start: %q", lines)
|
|
}
|
|
|
|
// Outside the mesh's tree the rule of issue 339 stands: found, used as found; and left so at a start, said.
|
|
other := filepath.Join(root, "var-lib", "notes")
|
|
if err := os.MkdirAll(other, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
said = nil
|
|
decl = `{"id":"notes.data","type":"directory","path":"` + other + `","mode":"0750"}`
|
|
_, found, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared,
|
|
noServices, func(line string) { said = append(said, line) }, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(strings.Join(said, "\n"), "WARNING notes.data") {
|
|
t.Errorf("a directory found outside the mesh's tree was not used as found: %q", said)
|
|
}
|
|
if lines := ConvergeDirectories(context.Background(), archHost(t), declare(t, decl), found, noServices); len(lines) != 1 || !strings.Contains(lines[0], "left as found") {
|
|
t.Errorf("a directory the mesh never made was converged as %q", lines)
|
|
}
|
|
if drifts := DirectoryDrifts(declare(t, decl), found); len(drifts) != 0 {
|
|
t.Errorf("a directory the mesh never made is named as drift: %+v", drifts)
|
|
}
|
|
if info, _ := os.Stat(other); info.Mode().Perm() != 0o700 {
|
|
t.Errorf("a directory the mesh never made was changed at the start: %04o", info.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
// Review of issue 1873's fix: the tree is judged by where the path leads, not by its text. A root-only link inside the
|
|
// mesh's tree pointing out of it (to /var/lib/systemd, say) would otherwise make a directory outside the tree, owned by
|
|
// the engine's account, the mesh's to chown: issue 1873's fault again. It is used as found.
|
|
func testALinkInTheMeshsTreeLeadingOutOfItIsNotTheTree(t *testing.T) {
|
|
rootAlone(t, "")
|
|
root := t.TempDir()
|
|
before := meshDataRoot
|
|
meshDataRoot = filepath.Join(root, "mesh")
|
|
t.Cleanup(func() { meshDataRoot = before })
|
|
outside := filepath.Join(root, "var-lib", "systemd")
|
|
if err := os.MkdirAll(filepath.Join(outside, "data"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(filepath.Join(outside, "data"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(meshDataRoot, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(outside, filepath.Join(meshDataRoot, "postgres")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := &declaration.Directory{ID: "postgres.data", Path: filepath.Join(meshDataRoot, "postgres", "data"), Mode: "0750"}
|
|
out, err := applyDirectory(r, store.Applied{}, true, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !out.asFound {
|
|
t.Errorf("a directory reached through a link out of the mesh's tree was taken as the mesh's: %+v", out)
|
|
}
|
|
if got := permOf(t, filepath.Join(outside, "data")); got != 0o700 {
|
|
t.Errorf("the directory outside the tree was given mode %o (was 700)", got)
|
|
}
|
|
}
|
|
|
|
// TestMeshTreeFound runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestMeshTreeFound(t *testing.T) {
|
|
t.Run("ADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs", testADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs)
|
|
t.Run("ALinkInTheMeshsTreeLeadingOutOfItIsNotTheTree", testALinkInTheMeshsTreeLeadingOutOfItIsNotTheTree)
|
|
}
|