Skip to content

Commit 3b15199

Browse files
committed
add caddy/config_tests
1 parent 2c2f677 commit 3b15199

4 files changed

Lines changed: 202 additions & 4 deletions

File tree

caddy/admin_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !nocaddy
2+
// +build !nocaddy
3+
14
package caddy_test
25

36
import (

caddy/caddy.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,17 +576,20 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
576576
}
577577
}
578578

579-
if wc.Name == "" && len(wc.Env) > 0 {
579+
if wc.Name == "" {
580580
if len(wc.Env) > 0 {
581581
envString := ""
582582
for k, v := range wc.Env {
583583
envString += k + "=" + v + ","
584584
}
585585
envString = strings.TrimSuffix(envString, ",")
586+
// Environment is required in order not to collide with other FrankenPHPModules
587+
// Filename is required to avoid collisions with other workers in this module
586588
wc.Name += "env:" + envString
587589
}
590+
wc.Name += "_" + wc.FileName
588591
}
589-
if wc.Name != "" && !strings.HasPrefix(wc.Name, "m#") {
592+
if !strings.HasPrefix(wc.Name, "m#") {
590593
wc.Name = "m#" + wc.Name
591594
}
592595

@@ -604,7 +607,7 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
604607
}
605608

606609
f.Workers = append(f.Workers, wc)
607-
moduleWorkers = append(moduleWorkers, f.Workers...)
610+
moduleWorkers = append(moduleWorkers, wc)
608611
}
609612
}
610613
}

caddy/caddy_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !nocaddy
2+
// +build !nocaddy
3+
14
package caddy_test
25

36
import (

caddy/config_test.go

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package caddy
1+
//go:build !noconfig
2+
// +build !noconfig
3+
4+
package caddy
25

36
import (
47
"testing"
@@ -86,3 +89,189 @@ func TestModuleWorkersDuplicateNameFail(t *testing.T) {
8689
require.Contains(t, err.Error(), "workers must not have duplicate names", "Error message should mention duplicate names")
8790
resetModuleWorkers()
8891
}
92+
93+
func TestModuleWorkersWithDifferentFilenames(t *testing.T) {
94+
// Create a test configuration with different worker filenames
95+
configWithDifferentFilenames := `
96+
{
97+
php {
98+
worker ../testdata/worker-with-env.php
99+
worker ../testdata/worker-with-counter.php
100+
}
101+
}`
102+
103+
// Parse the configuration
104+
d := caddyfile.NewTestDispenser(configWithDifferentFilenames)
105+
module := &FrankenPHPModule{}
106+
107+
// Unmarshal the configuration
108+
err := module.UnmarshalCaddyfile(d)
109+
110+
// Verify that no error was returned
111+
require.NoError(t, err, "Expected no error when two workers in the same module have different filenames")
112+
113+
// Verify that both workers were added to the module
114+
require.Len(t, module.Workers, 2, "Expected two workers to be added to the module")
115+
require.Equal(t, "../testdata/worker-with-env.php", module.Workers[0].FileName, "First worker should have the correct filename")
116+
require.Equal(t, "../testdata/worker-with-counter.php", module.Workers[1].FileName, "Second worker should have the correct filename")
117+
118+
resetModuleWorkers()
119+
}
120+
121+
func TestModuleWorkersDifferentNamesSucceed(t *testing.T) {
122+
// Create a test configuration with a worker name
123+
configWithWorkerName1 := `
124+
{
125+
php_server {
126+
worker {
127+
name test-worker-1
128+
file ../testdata/worker-with-env.php
129+
num 1
130+
}
131+
}
132+
}`
133+
134+
// Parse the first configuration
135+
d1 := caddyfile.NewTestDispenser(configWithWorkerName1)
136+
module1 := &FrankenPHPModule{}
137+
138+
// Unmarshal the first configuration
139+
err := module1.UnmarshalCaddyfile(d1)
140+
require.NoError(t, err, "First module should be configured without errors")
141+
142+
// Create a second test configuration with a different worker name
143+
configWithWorkerName2 := `
144+
{
145+
php_server {
146+
worker {
147+
name test-worker-2
148+
file ../testdata/worker-with-env.php
149+
num 1
150+
}
151+
}
152+
}`
153+
154+
// Parse the second configuration
155+
d2 := caddyfile.NewTestDispenser(configWithWorkerName2)
156+
module2 := &FrankenPHPModule{}
157+
158+
// Unmarshal the second configuration
159+
err = module2.UnmarshalCaddyfile(d2)
160+
161+
// Verify that no error was returned
162+
require.NoError(t, err, "Expected no error when two workers have different names")
163+
164+
// Verify that both workers were added to moduleWorkers
165+
require.Len(t, moduleWorkers, 2, "Expected two workers to be added to moduleWorkers")
166+
require.Equal(t, "m#test-worker-1", moduleWorkers[0].Name, "First worker should have the correct name")
167+
require.Equal(t, "m#test-worker-2", moduleWorkers[1].Name, "Second worker should have the correct name")
168+
169+
resetModuleWorkers()
170+
}
171+
172+
func TestModuleWorkerWithEnvironmentVariables(t *testing.T) {
173+
// Create a test configuration with environment variables
174+
configWithEnv := `
175+
{
176+
php {
177+
worker {
178+
file ../testdata/worker-with-env.php
179+
num 1
180+
env APP_ENV production
181+
env DEBUG true
182+
}
183+
}
184+
}`
185+
186+
// Parse the configuration
187+
d := caddyfile.NewTestDispenser(configWithEnv)
188+
module := &FrankenPHPModule{}
189+
190+
// Unmarshal the configuration
191+
err := module.UnmarshalCaddyfile(d)
192+
193+
// Verify that no error was returned
194+
require.NoError(t, err, "Expected no error when configuring a worker with environment variables")
195+
196+
// Verify that the worker was added to the module
197+
require.Len(t, module.Workers, 1, "Expected one worker to be added to the module")
198+
require.Equal(t, "../testdata/worker-with-env.php", module.Workers[0].FileName, "Worker should have the correct filename")
199+
200+
// Verify that the environment variables were set correctly
201+
require.Len(t, module.Workers[0].Env, 2, "Expected two environment variables")
202+
require.Equal(t, "production", module.Workers[0].Env["APP_ENV"], "APP_ENV should be set to production")
203+
require.Equal(t, "true", module.Workers[0].Env["DEBUG"], "DEBUG should be set to true")
204+
205+
resetModuleWorkers()
206+
}
207+
208+
func TestModuleWorkerWithWatchConfiguration(t *testing.T) {
209+
// Create a test configuration with watch directories
210+
configWithWatch := `
211+
{
212+
php {
213+
worker {
214+
file ../testdata/worker-with-env.php
215+
num 1
216+
watch
217+
watch ./src/**/*.php
218+
watch ./config/**/*.yaml
219+
}
220+
}
221+
}`
222+
223+
// Parse the configuration
224+
d := caddyfile.NewTestDispenser(configWithWatch)
225+
module := &FrankenPHPModule{}
226+
227+
// Unmarshal the configuration
228+
err := module.UnmarshalCaddyfile(d)
229+
230+
// Verify that no error was returned
231+
require.NoError(t, err, "Expected no error when configuring a worker with watch directories")
232+
233+
// Verify that the worker was added to the module
234+
require.Len(t, module.Workers, 1, "Expected one worker to be added to the module")
235+
require.Equal(t, "../testdata/worker-with-env.php", module.Workers[0].FileName, "Worker should have the correct filename")
236+
237+
// Verify that the watch directories were set correctly
238+
require.Len(t, module.Workers[0].Watch, 3, "Expected three watch patterns")
239+
require.Equal(t, "./**/*.{php,yaml,yml,twig,env}", module.Workers[0].Watch[0], "First watch pattern should be the default")
240+
require.Equal(t, "./src/**/*.php", module.Workers[0].Watch[1], "Second watch pattern should match the configuration")
241+
require.Equal(t, "./config/**/*.yaml", module.Workers[0].Watch[2], "Third watch pattern should match the configuration")
242+
243+
resetModuleWorkers()
244+
}
245+
246+
func TestModuleWorkerWithCustomName(t *testing.T) {
247+
// Create a test configuration with a custom worker name
248+
configWithCustomName := `
249+
{
250+
php {
251+
worker {
252+
file ../testdata/worker-with-env.php
253+
num 1
254+
name custom-worker-name
255+
}
256+
}
257+
}`
258+
259+
// Parse the configuration
260+
d := caddyfile.NewTestDispenser(configWithCustomName)
261+
module := &FrankenPHPModule{}
262+
263+
// Unmarshal the configuration
264+
err := module.UnmarshalCaddyfile(d)
265+
266+
// Verify that no error was returned
267+
require.NoError(t, err, "Expected no error when configuring a worker with a custom name")
268+
269+
// Verify that the worker was added to the module
270+
require.Len(t, module.Workers, 1, "Expected one worker to be added to the module")
271+
require.Equal(t, "../testdata/worker-with-env.php", module.Workers[0].FileName, "Worker should have the correct filename")
272+
273+
// Verify that the worker was added to moduleWorkers with the m# prefix
274+
require.Equal(t, "m#custom-worker-name", module.Workers[0].Name, "Worker should have the custom name")
275+
276+
resetModuleWorkers()
277+
}

0 commit comments

Comments
 (0)