|
| 1 | +package nginx |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestGetLoadModuleRegex(t *testing.T) { |
| 8 | + testCases := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expected []string // expected module names |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "quoted absolute path", |
| 15 | + input: `load_module "/usr/local/nginx/modules/ngx_stream_module.so";`, |
| 16 | + expected: []string{"ngx_stream_module"}, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "unquoted relative path", |
| 20 | + input: `load_module modules/ngx_http_upstream_fair_module.so;`, |
| 21 | + expected: []string{"ngx_http_upstream_fair_module"}, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "quoted relative path", |
| 25 | + input: `load_module "modules/ngx_http_geoip_module.so";`, |
| 26 | + expected: []string{"ngx_http_geoip_module"}, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "unquoted absolute path", |
| 30 | + input: `load_module /etc/nginx/modules/ngx_http_cache_purge_module.so;`, |
| 31 | + expected: []string{"ngx_http_cache_purge_module"}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "multiple modules", |
| 35 | + input: `load_module "/path/ngx_module1.so";\nload_module modules/ngx_module2.so;`, |
| 36 | + expected: []string{"ngx_module1", "ngx_module2"}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "with extra whitespace", |
| 40 | + input: `load_module "modules/ngx_test_module.so" ;`, |
| 41 | + expected: []string{"ngx_test_module"}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "no matches", |
| 45 | + input: `some other nginx config`, |
| 46 | + expected: []string{}, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + regex := GetLoadModuleRegex() |
| 51 | + |
| 52 | + for _, tc := range testCases { |
| 53 | + t.Run(tc.name, func(t *testing.T) { |
| 54 | + matches := regex.FindAllStringSubmatch(tc.input, -1) |
| 55 | + |
| 56 | + if len(matches) != len(tc.expected) { |
| 57 | + t.Errorf("Expected %d matches, got %d", len(tc.expected), len(matches)) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + for i, match := range matches { |
| 62 | + if len(match) < 2 { |
| 63 | + t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match)) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + moduleName := match[1] |
| 68 | + expectedModule := tc.expected[i] |
| 69 | + |
| 70 | + if moduleName != expectedModule { |
| 71 | + t.Errorf("Expected module name %s, got %s", expectedModule, moduleName) |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestModulesLoaded(t *testing.T) { |
| 79 | + text := ` |
| 80 | +load_module "/usr/local/nginx/modules/ngx_stream_module.so"; |
| 81 | +load_module modules/ngx_http_upstream_fair_module.so; |
| 82 | +load_module "modules/ngx_http_geoip_module.so"; |
| 83 | +load_module /etc/nginx/modules/ngx_http_cache_purge_module.so; |
| 84 | +` |
| 85 | + |
| 86 | + loadModuleRe := GetLoadModuleRegex() |
| 87 | + matches := loadModuleRe.FindAllStringSubmatch(text, -1) |
| 88 | + |
| 89 | + t.Log("matches", matches) |
| 90 | + |
| 91 | + // Expected module names |
| 92 | + expectedModules := []string{ |
| 93 | + "ngx_stream_module", |
| 94 | + "ngx_http_upstream_fair_module", |
| 95 | + "ngx_http_geoip_module", |
| 96 | + "ngx_http_cache_purge_module", |
| 97 | + } |
| 98 | + |
| 99 | + if len(matches) != len(expectedModules) { |
| 100 | + t.Errorf("Expected %d matches, got %d", len(expectedModules), len(matches)) |
| 101 | + } |
| 102 | + |
| 103 | + for i, match := range matches { |
| 104 | + if len(match) < 2 { |
| 105 | + t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match)) |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + moduleName := match[1] |
| 110 | + expectedModule := expectedModules[i] |
| 111 | + |
| 112 | + t.Logf("Match %d: %s", i, moduleName) |
| 113 | + |
| 114 | + if moduleName != expectedModule { |
| 115 | + t.Errorf("Expected module name %s, got %s", expectedModule, moduleName) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments