|
| 1 | +package caddy |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +// resetModuleWorkers resets the moduleWorkers slice for testing |
| 11 | +func resetModuleWorkers() { |
| 12 | + moduleWorkers = make([]workerConfig, 0) |
| 13 | +} |
| 14 | + |
| 15 | +func TestModuleWorkerDuplicateFilenamesFail(t *testing.T) { |
| 16 | + // Create a test configuration with duplicate worker filenames |
| 17 | + configWithDuplicateFilenames := ` |
| 18 | + { |
| 19 | + php { |
| 20 | + worker { |
| 21 | + file worker-with-env.php |
| 22 | + num 1 |
| 23 | + } |
| 24 | + worker { |
| 25 | + file worker-with-env.php |
| 26 | + num 2 |
| 27 | + } |
| 28 | + } |
| 29 | + }` |
| 30 | + |
| 31 | + // Parse the configuration |
| 32 | + d := caddyfile.NewTestDispenser(configWithDuplicateFilenames) |
| 33 | + module := &FrankenPHPModule{} |
| 34 | + |
| 35 | + // Unmarshal the configuration |
| 36 | + err := module.UnmarshalCaddyfile(d) |
| 37 | + |
| 38 | + // Verify that an error was returned |
| 39 | + require.Error(t, err, "Expected an error when two workers in the same module have the same filename") |
| 40 | + require.Contains(t, err.Error(), "workers must not have duplicate filenames", "Error message should mention duplicate filenames") |
| 41 | + resetModuleWorkers() |
| 42 | +} |
| 43 | + |
| 44 | +func TestModuleWorkersDuplicateNameFail(t *testing.T) { |
| 45 | + // Create a test configuration with a worker name |
| 46 | + configWithWorkerName1 := ` |
| 47 | + { |
| 48 | + php_server { |
| 49 | + worker { |
| 50 | + name test-worker |
| 51 | + file ../testdata/worker-with-env.php |
| 52 | + num 1 |
| 53 | + } |
| 54 | + } |
| 55 | + }` |
| 56 | + |
| 57 | + // Parse the first configuration |
| 58 | + d1 := caddyfile.NewTestDispenser(configWithWorkerName1) |
| 59 | + module1 := &FrankenPHPModule{} |
| 60 | + |
| 61 | + // Unmarshal the first configuration |
| 62 | + err := module1.UnmarshalCaddyfile(d1) |
| 63 | + require.NoError(t, err, "First module should be configured without errors") |
| 64 | + |
| 65 | + // Create a second test configuration with the same worker name |
| 66 | + configWithWorkerName2 := ` |
| 67 | + { |
| 68 | + php_server { |
| 69 | + worker { |
| 70 | + name test-worker |
| 71 | + file ../testdata/worker-with-env.php |
| 72 | + num 1 |
| 73 | + } |
| 74 | + } |
| 75 | + }` |
| 76 | + |
| 77 | + // Parse the second configuration |
| 78 | + d2 := caddyfile.NewTestDispenser(configWithWorkerName2) |
| 79 | + module2 := &FrankenPHPModule{} |
| 80 | + |
| 81 | + // Unmarshal the second configuration |
| 82 | + err = module2.UnmarshalCaddyfile(d2) |
| 83 | + |
| 84 | + // Verify that an error was returned |
| 85 | + require.Error(t, err, "Expected an error when two workers have the same name") |
| 86 | + require.Contains(t, err.Error(), "workers must not have duplicate names", "Error message should mention duplicate names") |
| 87 | + resetModuleWorkers() |
| 88 | +} |
0 commit comments