// 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"} { 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) } } // #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) } }