// SPDX-FileCopyrightText: 2026 Coffey Labs LLC // SPDX-License-Identifier: AGPL-3.0-or-later package compose import ( "os" "path/filepath" "strings" "testing" ) func stack(hostServer bool) *Stack { return &Stack{ Version: "test", Project: "inbuxa", Domain: "example.com", Email: "postmaster@example.com", MailHost: "mail.example.com", ConsoleHost: "admin.example.com", WebmailHost: "webmail.example.com", Server: !hostServer, HostServer: hostServer, Console: true, Webmail: true, Proxy: true, ServerImage: "s", ConsoleImage: "c", WebmailImage: "w", CaddyImage: "k", ServerBind: "127.0.0.1:8081", ConsoleBind: "127.0.0.1:8082", WebmailBind: "127.0.0.1:8080", Subnet: "172.31.253.0/24", MailPorts: []int{25}, ServerPublicURL: "https://mail.example.com", ConsoleURL: "https://admin.example.com", WebmailURL: "https://webmail.example.com", } } func render(t *testing.T, s *Stack) (compose, caddy string) { t.Helper() dir := t.TempDir() if _, err := Write(dir, s); err != nil { t.Fatal(err) } a, _ := os.ReadFile(filepath.Join(dir, "compose.yaml")) b, _ := os.ReadFile(filepath.Join(dir, "Caddyfile")) return string(a), string(b) } func TestContainerServerIsInTheProject(t *testing.T) { c, k := render(t, stack(false)) for _, want := range []string{"\n server:\n", "depends_on: [server]", "MAIL_SERVER_URL: http://server:8080", "inbuxa-data:/var/lib/inbuxa", "net.ipv4.ip_unprivileged_port_start: \"0\""} { if !strings.Contains(c, want) { t.Errorf("compose.yaml lacks %q", want) } } if strings.Contains(c, "network_mode: host") { t.Error("a container install put something on the host's network") } if !strings.Contains(k, "reverse_proxy server:8080") || !strings.Contains(k, "reverse_proxy webmail:8080") { t.Errorf("Caddyfile does not use the project's names:\n%s", k) } } func TestHostServerIsReachedOnLoopback(t *testing.T) { s := stack(true) c, k := render(t, s) for _, bad := range []string{"\n server:\n", "depends_on", "inbuxa-data:", "http://server:8080"} { if strings.Contains(c, bad) { t.Errorf("compose.yaml still has %q", bad) } } for _, want := range []string{"network_mode: host", "HOST: 127.0.0.1", "MAIL_SERVER_URL: http://127.0.0.1:8081", "\"127.0.0.1:8082:8080\""} { if !strings.Contains(c, want) { t.Errorf("compose.yaml lacks %q", want) } } for _, want := range []string{"reverse_proxy 127.0.0.1:8081", "reverse_proxy 127.0.0.1:8080", "reverse_proxy 127.0.0.1:8082"} { if !strings.Contains(k, want) { t.Errorf("Caddyfile lacks %q", want) } } if s.WebmailIP != "127.0.0.1" || s.CaddyIP != "127.0.0.1" { t.Errorf("the server would exempt %s and %s, not loopback", s.WebmailIP, s.CaddyIP) } } func TestSecretsSurviveARewrite(t *testing.T) { dir := t.TempDir() a, err := EnsureSecrets(dir) if err != nil { t.Fatal(err) } b, err := Write(dir, stack(true)) if err != nil { t.Fatal(err) } if a != b { t.Error("writing the deployment rolled the secrets") } } func TestJoinedFrontEndsReachTheServerElsewhere(t *testing.T) { s := stack(false) s.Server = false s.RemoteServerURL = "https://mail.example.com" c, k := render(t, s) for _, bad := range []string{"\n server:\n", "depends_on", "network_mode: host", "inbuxa-data:"} { if strings.Contains(c, bad) { t.Errorf("compose.yaml has %q", bad) } } if !strings.Contains(c, "MAIL_SERVER_URL: https://mail.example.com") { t.Errorf("the webmail does not reach the server elsewhere:\n%s", c) } if strings.Contains(k, "autoconfig.") || strings.Contains(k, "\nmail.example.com") { t.Errorf("Caddy here serves the server's names:\n%s", k) } if !strings.Contains(k, "reverse_proxy webmail:8080") || !strings.Contains(k, "reverse_proxy console:8080") { t.Errorf("Caddyfile:\n%s", k) } } func TestJoinedToALoopbackServerTheWebmailSharesTheHost(t *testing.T) { s := stack(false) s.Server, s.Proxy, s.Local = false, false, true s.RemoteServerURL = "http://127.0.0.1:8081" c, _ := render(t, s) if !strings.Contains(c, "network_mode: host") || !strings.Contains(c, "MAIL_SERVER_URL: http://127.0.0.1:8081") { t.Errorf("compose.yaml:\n%s", c) } if s.WebmailIP != "127.0.0.1" { t.Errorf("the webmail would be exempted at %s", s.WebmailIP) } } func TestProxySnippetsPointAtLoopback(t *testing.T) { dir := t.TempDir() s := stack(false) if err := s.Addresses(); err != nil { t.Fatal(err) } out, err := WriteProxySnippets(dir, *s) if err != nil { t.Fatal(err) } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) caddy, _ := os.ReadFile(filepath.Join(out, "Caddyfile.fragment")) for _, want := range []string{ "server_name mail.example.com autoconfig.example.com", "proxy_pass http://127.0.0.1:8081;", "proxy_pass http://127.0.0.1:8082;", "proxy_pass http://127.0.0.1:8080;", "location /.well-known/acme-challenge/", "proxy_set_header X-Forwarded-For $remote_addr;", "server_name admin.example.com;", "server_name webmail.example.com;", } { if !strings.Contains(string(nginx), want) { t.Errorf("nginx-inbuxa.conf lacks %q", want) } } for _, want := range []string{"reverse_proxy 127.0.0.1:8081", "disable_http_challenge", "import " + dir + "/proxy/Caddyfile.fragment"} { if !strings.Contains(string(caddy), want) { t.Errorf("Caddyfile.fragment lacks %q", want) } } // A fragment is imported beside other sites: no global options block for _, line := range strings.Split(string(caddy), "\n") { if strings.TrimSpace(line) == "{" { t.Error("the Caddyfile fragment has a global options block") } } if s.GatewayIP != "172.31.253.1" { t.Errorf("gateway = %s", s.GatewayIP) } } func TestProxySnippetsLeaveOutWhatIsNotHere(t *testing.T) { s := stack(true) s.Webmail = false out, err := WriteProxySnippets(t.TempDir(), *s) if err != nil { t.Fatal(err) } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) if strings.Contains(string(nginx), "webmail.example.com") { t.Error("a webmail that isn't installed is in the nginx configuration") } } func TestFrontEndsOnTheHost(t *testing.T) { s := stack(false) // the server in a container s.Console, s.Webmail = false, false s.ConsoleFiles, s.WebmailUnit = "/var/lib/inbuxa/console", true c, k := render(t, s) for _, bad := range []string{"\n console:\n", "\n webmail:\n"} { if strings.Contains(c, bad) { t.Errorf("compose.yaml still runs %q", bad) } } for _, want := range []string{"network_mode: host", "/var/lib/inbuxa/console:/srv/console:ro", "\n server:\n"} { if !strings.Contains(c, want) { t.Errorf("compose.yaml lacks %q", want) } } for _, want := range []string{"root * /srv/console", "try_files {path} /index.html", "file_server", "reverse_proxy 127.0.0.1:8080", "reverse_proxy 127.0.0.1:8081"} { if !strings.Contains(k, want) { t.Errorf("Caddyfile lacks %q", want) } } // A server in a container sees host processes at the gateway if s.WebmailIP != "172.31.253.1" || s.CaddyIP != "172.31.253.1" { t.Errorf("exempts %s and %s, not the gateway", s.WebmailIP, s.CaddyIP) } out, err := WriteProxySnippets(t.TempDir(), *s) if err != nil { t.Fatal(err) } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) if !strings.Contains(string(nginx), "root /var/lib/inbuxa/console;") || !strings.Contains(string(nginx), "proxy_pass http://127.0.0.1:8080;") { t.Errorf("nginx-inbuxa.conf:\n%s", nginx) } } // #48: with names routed on 443, the server's names are not terminated in // nginx at all; the router passes them, and any name it does not know, to the // server's own HTTPS with the PROXY protocol header. func TestProxySnippetsRouteByName(t *testing.T) { dir := t.TempDir() s := stack(false) s.Proxy, s.PassThrough = false, true out, err := WriteProxySnippets(dir, *s) if err != nil { t.Fatal(err) } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) stream, err := os.ReadFile(filepath.Join(out, "nginx-inbuxa-stream.conf")) if err != nil { t.Fatal("no stream configuration written") } for _, want := range []string{ "stream {", "ssl_preread on;", "proxy_protocol on;", "admin.example.com inbuxa_front;", "webmail.example.com inbuxa_front;", "default inbuxa_server;", "server 127.0.0.1:8443;", "server 127.0.0.1:8444;", "listen 443;", "include " + dir + "/proxy/nginx-inbuxa-stream.conf;", "libnginx-mod-stream", } { if !strings.Contains(string(stream), want) { t.Errorf("nginx-inbuxa-stream.conf lacks %q", want) } } for _, want := range []string{ "listen 127.0.0.1:8444 ssl proxy_protocol;", "real_ip_header proxy_protocol;", "~^(autoconfig|autodiscover|mta-sts|ua-auto-config)\\.;", "location /.well-known/acme-challenge/", "listen 127.0.0.1:8444 ssl proxy_protocol default_server;", "ssl_reject_handshake on;", "return 421;", } { if !strings.Contains(string(nginx), want) { t.Errorf("nginx-inbuxa.conf lacks %q", want) } } for _, bad := range []string{"listen 443", "/etc/ssl/inbuxa/mail.example.com"} { if strings.Contains(string(nginx), bad) { t.Errorf("nginx-inbuxa.conf still has %q: the router holds 443 and the server's names are its own", bad) } } if strings.Count(string(nginx), "listen 127.0.0.1:8444 ssl proxy_protocol;") != 2 { t.Errorf("the console and the webmail are not both behind the router:\n%s", nginx) } c, _ := render(t, s) if !strings.Contains(c, `- "127.0.0.1:8443:443"`) { t.Errorf("the server's HTTPS is not published for the router:\n%s", c) } } // An install from before #48, not yet run again with --key: the layout it has // stays, and says how to move. func TestProxySnippetsWithoutRoutingStayAsTheyWere(t *testing.T) { dir := t.TempDir() s := stack(false) s.Proxy = false os.MkdirAll(filepath.Join(dir, "proxy"), 0o755) os.WriteFile(filepath.Join(dir, "proxy", "nginx-inbuxa-stream.conf"), []byte("stale"), 0o644) out, err := WriteProxySnippets(dir, *s) if err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(out, "nginx-inbuxa-stream.conf")); err == nil { t.Error("a stream configuration is left for a layout that has none") } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) for _, want := range []string{"listen 443 ssl;", "/etc/ssl/inbuxa/mail.example.com.fullchain.pem", "--key", "#48"} { if !strings.Contains(string(nginx), want) { t.Errorf("nginx-inbuxa.conf lacks %q", want) } } if strings.Contains(string(nginx), "return 421;") { t.Error("the fallback for the router's port is written without the router") } c, _ := render(t, s) if strings.Contains(c, ":443\"") { t.Errorf("the server's HTTPS is published without the router:\n%s", c) } } // The front ends joined to a server elsewhere: nothing to pass through, so no // router, whatever PassThrough says. func TestProxySnippetsForJoinedFrontEndsDoNotRoute(t *testing.T) { s := stack(false) s.Server, s.Proxy, s.PassThrough = false, false, true s.RemoteServerURL = "https://mail.example.com" out, err := WriteProxySnippets(t.TempDir(), *s) if err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(out, "nginx-inbuxa-stream.conf")); err == nil { t.Error("a router was written with no server here") } nginx, _ := os.ReadFile(filepath.Join(out, "nginx-inbuxa.conf")) if !strings.Contains(string(nginx), "listen 443 ssl;") { t.Errorf("nginx-inbuxa.conf:\n%s", nginx) } } // #65: on a machine with IPv6 the project network carries it too, or Docker // relays every IPv6 client from the gateway; without IPv6 it stays as it was. func TestNetworkCarriesIPv6WhenTheMachineHasIt(t *testing.T) { s := stack(false) c, _ := render(t, s) if strings.Contains(c, "enable_ipv6") || strings.Contains(c, "fd69:") { t.Errorf("a machine without IPv6 got an IPv6 network:\n%s", c) } s = stack(false) s.IPv6Subnet = "fd69:6e62:7578::/64" c, _ = render(t, s) for _, want := range []string{" enable_ipv6: true\n", " - subnet: 172.31.253.0/24\n - subnet: fd69:6e62:7578::/64\n"} { if !strings.Contains(c, want) { t.Errorf("compose.yaml lacks %q:\n%s", want, c) } } if s.GatewayIP != "172.31.253.1" { t.Errorf("the IPv4 gateway moved: %q", s.GatewayIP) } }