refactor: testing from the beginning. a journey in 150k lines of tests to none
This commit is contained in:
651 files changed
-146443
No files matched your search
@@ -1,162 +0,0 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
"github.com/moby/moby/client"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/kit"
|
||||
)
|
||||
|
||||
const kitProbeImage = "alpine:3"
|
||||
|
||||
func kitClient(t *testing.T) *client.Client {
|
||||
t.Helper()
|
||||
cli, err := client.New(client.FromEnv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = cli.Close() })
|
||||
if _, err := cli.ImageInspect(t.Context(), kitProbeImage); err != nil {
|
||||
rc, err := cli.ImagePull(t.Context(), kitProbeImage, client.ImagePullOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, rc)
|
||||
_ = rc.Close()
|
||||
}
|
||||
return cli
|
||||
}
|
||||
|
||||
func kitSpec(t *testing.T, install ...kit.Hook) KitSpec {
|
||||
t.Helper()
|
||||
name := fmt.Sprintf("archie-kit-test-%d", time.Now().UnixNano())
|
||||
overwrite := false
|
||||
return KitSpec{
|
||||
Name: name,
|
||||
Image: kitProbeImage,
|
||||
Network: "none",
|
||||
Worker: []string{"sleep", "300"},
|
||||
WorkerEnv: []string{"STATE_STORE_TOKEN=worker-secret", "NATS_TOKEN=worker-secret"},
|
||||
Labels: map[string]string{"archie-kit-test": name},
|
||||
Launch: kit.Launch{
|
||||
Harness: agentexec.HarnessSpec{User: kit.DefaultHarnessUser},
|
||||
Install: install,
|
||||
Startup: []kit.Hook{{Argv: []string{"sh", "-c", "id -u > /tmp/startup-uid"}, User: kit.DefaultHarnessUser}},
|
||||
Files: []spec.File{
|
||||
{Path: "/tmp/kit/settings.json", Content: `{"theme":1}`, Mode: "0600"},
|
||||
{Path: "/tmp/kit/keep.json", Content: "second", Overwrite: &overwrite},
|
||||
},
|
||||
Volumes: []kit.Volume{{Name: name + "-state", Path: "/data"}},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func startKit(t *testing.T, cli *client.Client, s KitSpec) string {
|
||||
t.Helper()
|
||||
id, err := StartKit(t.Context(), cli, s)
|
||||
t.Cleanup(func() {
|
||||
ctx := context.WithoutCancel(t.Context())
|
||||
if id != "" {
|
||||
_, _ = cli.ContainerRemove(ctx, id, client.ContainerRemoveOptions{Force: true})
|
||||
}
|
||||
_ = RemoveKitVolumes(ctx, cli, s.Launch.Volumes)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("StartKit: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func run(t *testing.T, cli *client.Client, id, user string, argv ...string) (int, string) {
|
||||
t.Helper()
|
||||
code, out, err := ExecIn(t.Context(), cli, id, user, []string{"PATH=/usr/bin:/bin"}, argv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return code, out
|
||||
}
|
||||
|
||||
// TestKitContainerSeparatesTheHarnessFromTheWorker proves the one-container
|
||||
// design's boundary on a real container: the worker's credentials are in
|
||||
// its environment, and the harness user cannot read them.
|
||||
func TestKitContainerSeparatesTheHarnessFromTheWorker(t *testing.T) {
|
||||
cli := kitClient(t)
|
||||
id := startKit(t, cli, kitSpec(t, kit.Hook{
|
||||
Argv: []string{"sh", "-c", `echo "$WORKSPACE_DIR" > /etc/seeded; echo first > /tmp/kit-keep-seed`},
|
||||
User: "0", Env: []string{"WORKSPACE_DIR=/workspace"},
|
||||
}))
|
||||
|
||||
inspected, err := cli.ContainerInspect(t.Context(), id, client.ContainerInspectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
workerUser := inspected.Container.Config.User
|
||||
if code, out := run(t, cli, id, workerUser, "cat", "/proc/1/environ"); code != 0 || !strings.Contains(out, "worker-secret") {
|
||||
t.Fatalf("the worker's own user cannot see its environment (%d %q); the probe below would prove nothing", code, out)
|
||||
}
|
||||
if code, out := run(t, cli, id, kit.DefaultHarnessUser, "cat", "/proc/1/environ"); code == 0 || strings.Contains(out, "worker-secret") {
|
||||
t.Fatalf("the harness user read the worker's environment: %q", out)
|
||||
}
|
||||
|
||||
if _, out := run(t, cli, id, "0", "cat", "/etc/seeded"); out != "/workspace" {
|
||||
t.Errorf("install hook did not run as root with its declared env: /etc/seeded = %q", out)
|
||||
}
|
||||
if _, out := run(t, cli, id, "0", "cat", "/tmp/startup-uid"); out != kit.DefaultHarnessUser {
|
||||
t.Errorf("startup hook ran as uid %q, want %s", out, kit.DefaultHarnessUser)
|
||||
}
|
||||
if _, out := run(t, cli, id, "0", "stat", "-c", "%u %a", "/tmp/kit/settings.json"); out != kit.DefaultHarnessUser+" 600" {
|
||||
t.Errorf("kit file owner and mode %q, want the harness user and 600", out)
|
||||
}
|
||||
if _, out := run(t, cli, id, "0", "cat", "/tmp/kit/keep.json"); out != "second" {
|
||||
t.Errorf("kit file content %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKitContainerFailingInstallHookIsRemoved(t *testing.T) {
|
||||
cli := kitClient(t)
|
||||
s := kitSpec(t, kit.Hook{Argv: []string{"sh", "-c", "echo broken registry >&2; exit 3"}, User: "0"})
|
||||
id, err := StartKit(t.Context(), cli, s)
|
||||
t.Cleanup(func() { _ = RemoveKitVolumes(context.WithoutCancel(t.Context()), cli, s.Launch.Volumes) })
|
||||
if err == nil || !strings.Contains(err.Error(), "install hook 1") || !strings.Contains(err.Error(), "broken registry") {
|
||||
t.Fatalf("StartKit error %v, want the failing hook and its output", err)
|
||||
}
|
||||
if id != "" {
|
||||
t.Fatalf("StartKit returned container %s for a failed start", id)
|
||||
}
|
||||
list, err := cli.ContainerList(t.Context(), client.ContainerListOptions{All: true, Filters: client.Filters{}.Add("label", "archie-kit-test="+s.Name)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(list.Items) != 0 {
|
||||
t.Fatalf("a failed Kit start left %d container(s) behind", len(list.Items))
|
||||
}
|
||||
}
|
||||
|
||||
func TestKitContainerEndsTheInstallPhaseBetweenInstallAndStartup(t *testing.T) {
|
||||
cli := kitClient(t)
|
||||
s := kitSpec(t, kit.Hook{Argv: []string{"sh", "-c", "touch /tmp/installed"}, User: "0"})
|
||||
var containerName string
|
||||
var sawInstalled, sawStartup bool
|
||||
s.InstallDone = func() {
|
||||
containerName = s.Name
|
||||
code, _, err := ExecIn(context.WithoutCancel(t.Context()), cli, containerName, "0", nil, []string{"test", "-e", "/tmp/installed"})
|
||||
sawInstalled = err == nil && code == 0
|
||||
code, _, err = ExecIn(context.WithoutCancel(t.Context()), cli, containerName, "0", nil, []string{"test", "-e", "/tmp/startup-uid"})
|
||||
sawStartup = err == nil && code == 0
|
||||
}
|
||||
startKit(t, cli, s)
|
||||
if containerName == "" {
|
||||
t.Fatal("InstallDone was never called")
|
||||
}
|
||||
if !sawInstalled || sawStartup {
|
||||
t.Fatalf("InstallDone ran with install done=%v and startup done=%v; want after install, before startup", sawInstalled, sawStartup)
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/client"
|
||||
)
|
||||
|
||||
// A native archied process is not itself attached to a Docker network. When
|
||||
// embedded NATS must be reachable by managed workers, the pool therefore uses
|
||||
// Docker's default bridge and exposes its host-side gateway to composition.
|
||||
func TestNewPoolResolvesDefaultBridgeGatewayForNativeDaemon(t *testing.T) {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const gateway = "172.31.0.1"
|
||||
|
||||
dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/containers/"+hostname+"/json"):
|
||||
http.Error(w, `{"message":"no such container"}`, http.StatusNotFound)
|
||||
case strings.HasSuffix(r.URL.Path, "/networks/bridge"):
|
||||
writeDockerJSON(t, w, map[string]any{
|
||||
"Name": "bridge",
|
||||
"Driver": "bridge",
|
||||
"Scope": "local",
|
||||
"IPAM": map[string]any{
|
||||
"Config": []map[string]string{{
|
||||
"Subnet": "172.31.0.0/16",
|
||||
"Gateway": gateway,
|
||||
}},
|
||||
},
|
||||
})
|
||||
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
||||
writeDockerJSON(t, w, []any{})
|
||||
default:
|
||||
http.Error(w, fmt.Sprintf("unexpected Docker API path %s", r.URL.Path), http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(dockerAPI.Close)
|
||||
|
||||
dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = dockerClient.Close() })
|
||||
|
||||
pool, err := NewPool(t.Context(), Config{
|
||||
DockerClient: dockerClient,
|
||||
RequireHostGateway: true,
|
||||
}, nil, discardLogger())
|
||||
if err != nil {
|
||||
t.Fatalf("NewPool = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = pool.Close() })
|
||||
|
||||
access, ok := any(pool).(interface {
|
||||
NetworkName() string
|
||||
HostGateway() string
|
||||
})
|
||||
if !ok {
|
||||
t.Fatal("Pool does not expose its resolved worker network and host gateway")
|
||||
}
|
||||
if got := access.NetworkName(); got != "bridge" {
|
||||
t.Errorf("NetworkName() = %q, want bridge", got)
|
||||
}
|
||||
if got := access.HostGateway(); got != gateway {
|
||||
t.Errorf("HostGateway() = %q, want %q", got, gateway)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPoolRejectsUnreachableEmbeddedNetwork(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
statusCode int
|
||||
network map[string]any
|
||||
}{
|
||||
{
|
||||
name: "inspect failure",
|
||||
statusCode: http.StatusInternalServerError,
|
||||
},
|
||||
{
|
||||
name: "non bridge driver",
|
||||
network: map[string]any{
|
||||
"Name": "workers", "Driver": "overlay", "Scope": "local",
|
||||
"IPAM": map[string]any{"Config": []map[string]string{{"Gateway": "172.31.0.1"}}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "non local scope",
|
||||
network: map[string]any{
|
||||
"Name": "workers", "Driver": "bridge", "Scope": "swarm",
|
||||
"IPAM": map[string]any{"Config": []map[string]string{{"Gateway": "172.31.0.1"}}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no IPv4 gateway",
|
||||
network: map[string]any{
|
||||
"Name": "workers", "Driver": "bridge", "Scope": "local",
|
||||
"IPAM": map[string]any{"Config": []map[string]string{{"Gateway": "fd00::1"}}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dockerAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/networks/workers"):
|
||||
if tt.statusCode != 0 {
|
||||
http.Error(w, `{"message":"network inspect failed"}`, tt.statusCode)
|
||||
return
|
||||
}
|
||||
writeDockerJSON(t, w, tt.network)
|
||||
case strings.HasSuffix(r.URL.Path, "/containers/json"):
|
||||
writeDockerJSON(t, w, []any{})
|
||||
default:
|
||||
http.Error(w, fmt.Sprintf("unexpected Docker API path %s", r.URL.Path), http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(dockerAPI.Close)
|
||||
|
||||
dockerClient, err := client.New(client.WithHost(dockerAPI.URL), client.WithAPIVersion("1.55"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = dockerClient.Close() })
|
||||
|
||||
pool, err := NewPool(t.Context(), Config{
|
||||
DockerClient: dockerClient,
|
||||
Network: "workers",
|
||||
RequireHostGateway: true,
|
||||
}, nil, discardLogger())
|
||||
if err == nil {
|
||||
_ = pool.Close()
|
||||
t.Fatal("NewPool = nil error, want embedded reachability validation failure")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func writeDockerJSON(t *testing.T, w http.ResponseWriter, value any) {
|
||||
t.Helper()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(value); err != nil {
|
||||
t.Errorf("encode Docker response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func discardLogger() *slog.Logger {
|
||||
return slog.New(slog.DiscardHandler)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user