Skip to content

Commit 4dd71c6

Browse files
committed
fix(nginx): update tests to handle partial success from Create
Create now returns (domain, error) when nginx config write fails. Tests use mustCreate helper that accepts domain+error (partial success) instead of failing on any error from Create.
1 parent ae6ed0a commit 4dd71c6

1 file changed

Lines changed: 19 additions & 33 deletions

File tree

internal/nginx/service_test.go

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ func setupNginxTest(t *testing.T) *Service {
2121
return NewService(repo, sitesDir)
2222
}
2323

24+
// mustCreate creates a domain, ignoring nginx config errors (expected in test env without root).
25+
func mustCreate(t *testing.T, svc *Service, req CreateDomainRequest) *Domain {
26+
t.Helper()
27+
domain, err := svc.Create(req)
28+
if err != nil && domain == nil {
29+
t.Fatalf("Create: %v", err)
30+
}
31+
return domain
32+
}
33+
2434
func TestCreateDomain_Success(t *testing.T) {
2535
svc := setupNginxTest(t)
2636

27-
domain, err := svc.Create(CreateDomainRequest{
37+
domain := mustCreate(t, svc, CreateDomainRequest{
2838
Domain: "example.com",
2939
UpstreamIP: "10.0.0.1",
3040
UpstreamPort: 8080,
3141
})
32-
if err != nil {
33-
t.Fatalf("Create: %v", err)
34-
}
3542
if domain.ID == 0 {
3643
t.Error("expected non-zero ID")
3744
}
@@ -49,14 +56,11 @@ func TestCreateDomain_Success(t *testing.T) {
4956
func TestCreateDomain_DefaultPort(t *testing.T) {
5057
svc := setupNginxTest(t)
5158

52-
domain, err := svc.Create(CreateDomainRequest{
59+
domain := mustCreate(t, svc, CreateDomainRequest{
5360
Domain: "example.com",
5461
UpstreamIP: "10.0.0.1",
5562
UpstreamPort: 0, // should default to 80
5663
})
57-
if err != nil {
58-
t.Fatalf("Create: %v", err)
59-
}
6064
if domain.UpstreamPort != 80 {
6165
t.Errorf("expected default upstream port 80, got %d", domain.UpstreamPort)
6266
}
@@ -103,14 +107,8 @@ func TestGetAllDomains_Empty(t *testing.T) {
103107
func TestGetAllDomains_Multiple(t *testing.T) {
104108
svc := setupNginxTest(t)
105109

106-
_, err := svc.Create(CreateDomainRequest{Domain: "a.example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
107-
if err != nil {
108-
t.Fatalf("Create 1: %v", err)
109-
}
110-
_, err = svc.Create(CreateDomainRequest{Domain: "b.example.com", UpstreamIP: "10.0.0.2", UpstreamPort: 80})
111-
if err != nil {
112-
t.Fatalf("Create 2: %v", err)
113-
}
110+
mustCreate(t, svc, CreateDomainRequest{Domain: "a.example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
111+
mustCreate(t, svc, CreateDomainRequest{Domain: "b.example.com", UpstreamIP: "10.0.0.2", UpstreamPort: 80})
114112

115113
domains, err := svc.GetAll()
116114
if err != nil {
@@ -124,10 +122,7 @@ func TestGetAllDomains_Multiple(t *testing.T) {
124122
func TestUpdateDomain_Success(t *testing.T) {
125123
svc := setupNginxTest(t)
126124

127-
created, err := svc.Create(CreateDomainRequest{Domain: "old.example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
128-
if err != nil {
129-
t.Fatalf("Create: %v", err)
130-
}
125+
created := mustCreate(t, svc, CreateDomainRequest{Domain: "old.example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
131126

132127
updated, err := svc.Update(created.ID, UpdateDomainRequest{
133128
Domain: "new.example.com",
@@ -148,12 +143,9 @@ func TestUpdateDomain_Success(t *testing.T) {
148143
func TestUpdateDomain_InvalidDomain(t *testing.T) {
149144
svc := setupNginxTest(t)
150145

151-
created, err := svc.Create(CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
152-
if err != nil {
153-
t.Fatalf("Create: %v", err)
154-
}
146+
created := mustCreate(t, svc, CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
155147

156-
_, err = svc.Update(created.ID, UpdateDomainRequest{Domain: "invalid", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
148+
_, err := svc.Update(created.ID, UpdateDomainRequest{Domain: "invalid", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
157149
if err == nil {
158150
t.Fatal("expected error for invalid domain, got nil")
159151
}
@@ -171,10 +163,7 @@ func TestUpdateDomain_NotFound(t *testing.T) {
171163
func TestDeleteDomain(t *testing.T) {
172164
svc := setupNginxTest(t)
173165

174-
created, err := svc.Create(CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
175-
if err != nil {
176-
t.Fatalf("Create: %v", err)
177-
}
166+
created := mustCreate(t, svc, CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
178167

179168
if err := svc.Delete(created.ID); err != nil {
180169
t.Fatalf("Delete: %v", err)
@@ -201,10 +190,7 @@ func TestDeleteDomain_NotFound(t *testing.T) {
201190
func TestSetEnabled_Enable(t *testing.T) {
202191
svc := setupNginxTest(t)
203192

204-
created, err := svc.Create(CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
205-
if err != nil {
206-
t.Fatalf("Create: %v", err)
207-
}
193+
created := mustCreate(t, svc, CreateDomainRequest{Domain: "example.com", UpstreamIP: "10.0.0.1", UpstreamPort: 80})
208194

209195
// Disable first
210196
disabled, err := svc.SetEnabled(created.ID, false)

0 commit comments

Comments
 (0)