81 lines
3.0 KiB
Go
81 lines
3.0 KiB
Go
package installapply
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/aritmolab/thothii/tools/tht/internal/compose"
|
|
"github.com/aritmolab/thothii/tools/tht/internal/preflight"
|
|
)
|
|
|
|
func TestIdentityPreservesAccountsAndRefusesCollisions(t *testing.T) {
|
|
original := "root:x:0:0:root:/root:/bin/bash\nthoth:x:10001:10001::/home/thoth:/bin/bash\n"
|
|
got, err := mapPasswd(original, "thoth", 1000, 1000)
|
|
if err != nil || got != strings.Replace(original, "10001:10001", "1000:1000", 1) {
|
|
t.Fatalf("mapped account: %q %v", got, err)
|
|
}
|
|
for _, broken := range []string{original + "other:x:1000:1000::/:/bin/false\n", strings.ReplaceAll(original, "thoth:", "missing:"), original + "thoth:x:10002:10002::/:/bin/false\n"} {
|
|
if _, err := mapPasswd(broken, "thoth", 1000, 1000); err == nil {
|
|
t.Fatal("ambiguous identity accepted")
|
|
}
|
|
}
|
|
if _, err := mapPasswd(original, "thoth", 0, 0); err == nil {
|
|
t.Fatal("root runtime accepted")
|
|
}
|
|
}
|
|
|
|
type foreignVolume struct{ dockerFixture }
|
|
|
|
func (r *foreignVolume) Run(ctx context.Context, args []string, input io.Reader) (compose.Result, error) {
|
|
if len(args) > 1 && args[0] == "volume" && args[1] == "inspect" {
|
|
return compose.Result{Stdout: `{"io.thothii.installation":"someone-else"}`}, nil
|
|
}
|
|
return r.dockerFixture.Run(ctx, args, input)
|
|
}
|
|
|
|
func TestForeignVolumesStopBeforeOwnershipOrServiceChanges(t *testing.T) {
|
|
path, plan := fixture(t)
|
|
r := &foreignVolume{}
|
|
result, err := Run(context.Background(), Request{path, plan.Installation.Path}, r, func(context.Context, preflight.Plan) error { return nil })
|
|
if err == nil || result.Stage != "runtime-identity" {
|
|
t.Fatalf("foreign volume: %+v %v", result, err)
|
|
}
|
|
for _, line := range flatten(r.calls) {
|
|
if strings.Contains(line, " up ") || strings.Contains(line, " -c "+initializeVolumes) {
|
|
t.Fatal("changed foreign volume or started service")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRuntimeIdentityIsDurableAcrossOperatorCommands(t *testing.T) {
|
|
path, plan := fixture(t)
|
|
_, err := Run(context.Background(), Request{path, plan.Installation.Path}, &dockerFixture{}, func(context.Context, preflight.Plan) error { return nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(plan.Installation.LocalIdentityOverridePath())
|
|
var override struct {
|
|
Services map[string]struct {
|
|
User string `json:"user"`
|
|
} `json:"services"`
|
|
}
|
|
if err != nil || json.Unmarshal(b, &override) != nil || override.Services["core"].User == "0:0" || override.Services["core"].User == "" {
|
|
t.Fatal("non-root runtime identity missing")
|
|
}
|
|
if !strings.Contains(strings.Join(plan.Installation.ComposeArgs("start"), " "), plan.Installation.LocalIdentityOverridePath()) {
|
|
t.Fatal("lifecycle loses runtime identity")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(plan.Installation.ControlDirectory(), "core.passwd"), []byte("modified"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = Run(context.Background(), Request{path, plan.Installation.Path}, &dockerFixture{}, func(context.Context, preflight.Plan) error { return nil })
|
|
if err == nil {
|
|
t.Fatal("modified identity accepted")
|
|
}
|
|
}
|