package isolation import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func TestRepeatedCustomNetworkPortIsIdempotent(t *testing.T) { root := t.TempDir() domain := "sv0123456789.hosts.example.test" file := filepath.Join(root, domain+".conf") if err := os.WriteFile(file, []byte("server {\n listen 80;\n server_name "+domain+";\n}\n"), 0600); err != nil { t.Fatal(err) } commands := 0 a := Agent{NginxVhostRoot: root, NginxBinary: filepath.Join(root, "nginx"), runCommand: func(context.Context, string, ...string) error { commands++; return nil }} spec := NginxNetworkSpec{Project: ProjectSpec{TenantID: 1, HostName: "sv0123456789", StorageBytes: 1, ProcessLimit: 1}, Domain: domain, HTTPPort: 18080, HTTPSPort: 18443} data, _ := json.Marshal(spec) for i := 0; i < 2; i++ { response := httptest.NewRecorder() a.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/v1/nginx/network", bytes.NewReader(data))) if response.Code != http.StatusOK { t.Fatalf("request %d: %d %s", i, response.Code, response.Body.String()) } } if commands != 2 { t.Fatalf("repeated request unnecessarily reloaded Nginx: %d", commands) } content, err := os.ReadFile(file) if err != nil || !strings.Contains(string(content), "listen 18080;") { t.Fatal("custom port was not retained") } }