112 lines
5.1 KiB
Go
112 lines
5.1 KiB
Go
package installapply
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aritmolab/thothii/tools/tht/internal/compose"
|
|
"github.com/aritmolab/thothii/tools/tht/internal/safeio"
|
|
)
|
|
|
|
// Uses already-published images and Linux volume ownership even on Docker Desktop.
|
|
// This proves runtime identity/PG init and retry; it is not OS installation acceptance.
|
|
func TestPublishedImagesAcceptProtectedOperatorIdentity(t *testing.T) {
|
|
if os.Getenv("THT_INSTALLATION_DOCKER_TEST") != "1" {
|
|
t.Skip("set THT_INSTALLATION_DOCKER_TEST=1 with Docker and published images pulled")
|
|
}
|
|
_, plan := fixture(t)
|
|
i := plan.Installation
|
|
if err := safeio.EnsurePrivateDirectory(i.ProjectDirectory + "/.tht"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := safeio.EnsurePrivateDirectory(i.ControlDirectory()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := prepareIdentity(i); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
core := "docker.io/tylconsulting/thothii-core@sha256:7ac0b3362c93837c02d9f8d3153d3ab3441cc43594ee7786dafc845a69d5c956"
|
|
catalog := "docker.io/library/postgres@sha256:45cd22f8d32e189d245403954882f88e7a8714301fda80dab6da90f1265b25a3"
|
|
plan.Release.Images["core"][plan.Platform] = core
|
|
plan.Release.Images["catalog"][plan.Platform] = catalog
|
|
runner := compose.NewRunner("")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)
|
|
defer cancel()
|
|
container := i.ProjectName() + "-pg-proof"
|
|
t.Cleanup(func() {
|
|
cleanup, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
_, _ = runner.Run(cleanup, []string{"rm", "-f", container}, nil)
|
|
for _, volume := range ownedVolumes {
|
|
_, _ = runner.Run(cleanup, []string{"volume", "rm", i.ProjectName() + "_" + volume.name}, nil)
|
|
}
|
|
})
|
|
run := func(args ...string) string {
|
|
t.Helper()
|
|
result, err := runner.Run(ctx, args, nil)
|
|
if err != nil || result.ExitCode != 0 {
|
|
t.Fatalf("Docker proof failed: %v %s", err, result.Stderr)
|
|
}
|
|
return result.Stdout
|
|
}
|
|
if err := initializeIdentity(ctx, interruptedInitializer{runner}, i, plan.Release, plan.Platform); err == nil {
|
|
t.Fatal("simulated interruption was ignored")
|
|
}
|
|
if err := initializeIdentity(ctx, runner, i, plan.Release, plan.Platform); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
user := fmt.Sprintf("%d:%d", os.Geteuid(), os.Getegid())
|
|
// The password is created inside a Linux volume, so Desktop cannot mask UID/mode bugs.
|
|
run("run", "--rm", "--pull", "never", "--platform", plan.Platform, "--network", "none", "--read-only", "--user", user,
|
|
"--mount", "type=volume,src="+i.ProjectName()+"_auth-state,dst=/proof",
|
|
"--mount", "type=volume,src="+i.ProjectName()+"_pi-state,dst=/home/thoth/.pi",
|
|
"--mount", "type=volume,src="+i.ProjectName()+"_harness-pi,dst=/app/harness/.pi",
|
|
"--mount", "type=bind,src="+i.ControlDirectory()+"/core.passwd,dst=/etc/passwd,readonly",
|
|
"--entrypoint", "/usr/local/bin/node", core, "-e", `const fs=require('fs'),os=require('os'); if(os.userInfo().username!=='thoth')throw Error('identity'); fs.writeFileSync('/proof/password','test-only-password',{mode:0o600}); fs.writeFileSync('/home/thoth/.pi/agent/permission-proof','ok'); fs.writeFileSync('/app/harness/.pi/permission-proof','ok'); if(fs.statSync('/proof/password').uid!==Number(process.argv[1]))throw Error('owner');`, strconv.Itoa(os.Geteuid()))
|
|
start := func() {
|
|
run("run", "-d", "--name", container, "--pull", "never", "--platform", plan.Platform, "--network", "none", "--user", user,
|
|
"--mount", "type=volume,src="+i.ProjectName()+"_catalog-data,dst=/var/lib/postgresql/data",
|
|
"--mount", "type=volume,src="+i.ProjectName()+"_auth-state,dst=/proof,readonly",
|
|
"--mount", "type=bind,src="+i.ControlDirectory()+"/catalog.passwd,dst=/etc/passwd,readonly",
|
|
"-e", "PGDATA=/var/lib/postgresql/data/data", "-e", "POSTGRES_PASSWORD_FILE=/proof/password", catalog)
|
|
deadline := time.Now().Add(45 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
value, err := runner.Run(ctx, []string{"exec", container, "pg_isready", "-h", "127.0.0.1", "-U", "postgres"}, nil)
|
|
if err == nil && value.ExitCode == 0 {
|
|
return
|
|
}
|
|
time.Sleep(250 * time.Millisecond)
|
|
}
|
|
t.Fatal("PostgreSQL did not become ready")
|
|
}
|
|
start()
|
|
run("exec", container, "psql", "-U", "postgres", "-v", "ON_ERROR_STOP=1", "-c", "CREATE TABLE installation_proof(value integer); INSERT INTO installation_proof VALUES (47);")
|
|
run("stop", "--time", "10", container)
|
|
run("rm", container)
|
|
if err := initializeIdentity(ctx, runner, i, plan.Release, plan.Platform); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start()
|
|
if result := run("exec", container, "psql", "-U", "postgres", "-Atqc", "SELECT value FROM installation_proof"); strings.TrimSpace(result) != "47" {
|
|
t.Fatal("retry lost existing catalog data")
|
|
}
|
|
}
|
|
|
|
type interruptedInitializer struct{ compose.Runner }
|
|
|
|
func (r interruptedInitializer) Run(ctx context.Context, args []string, input io.Reader) (compose.Result, error) {
|
|
args = append([]string{}, args...)
|
|
for n, value := range args {
|
|
if value == initializeVolumes {
|
|
args[n] = strings.ReplaceAll(value, `save(marker, dict(identity, status="ready"))`, `os._exit(47)`)
|
|
}
|
|
}
|
|
return r.Runner.Run(ctx, args, input)
|
|
}
|