modules_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package nginx
  2. import (
  3. "regexp"
  4. "strings"
  5. "testing"
  6. )
  7. func TestModuleNameNormalization(t *testing.T) {
  8. testCases := []struct {
  9. name string
  10. loadModuleName string
  11. expectedNormalized string
  12. configureArgName string
  13. expectedLoadName string
  14. }{
  15. {
  16. name: "stream module",
  17. loadModuleName: "ngx_stream_module",
  18. expectedNormalized: "stream",
  19. configureArgName: "stream",
  20. expectedLoadName: "ngx_stream_module",
  21. },
  22. {
  23. name: "http_geoip module",
  24. loadModuleName: "ngx_http_geoip_module",
  25. expectedNormalized: "http_geoip",
  26. configureArgName: "http_geoip_module",
  27. expectedLoadName: "ngx_http_geoip_module",
  28. },
  29. {
  30. name: "stream_geoip module",
  31. loadModuleName: "ngx_stream_geoip_module",
  32. expectedNormalized: "stream_geoip",
  33. configureArgName: "stream_geoip_module",
  34. expectedLoadName: "ngx_stream_geoip_module",
  35. },
  36. {
  37. name: "http_image_filter module",
  38. loadModuleName: "ngx_http_image_filter_module",
  39. expectedNormalized: "http_image_filter",
  40. configureArgName: "http_image_filter_module",
  41. expectedLoadName: "ngx_http_image_filter_module",
  42. },
  43. {
  44. name: "mail module",
  45. loadModuleName: "ngx_mail_module",
  46. expectedNormalized: "mail",
  47. configureArgName: "mail",
  48. expectedLoadName: "ngx_mail_module",
  49. },
  50. }
  51. for _, tc := range testCases {
  52. t.Run(tc.name, func(t *testing.T) {
  53. // Test normalization from load_module name
  54. normalizedFromLoad := normalizeModuleNameFromLoadModule(tc.loadModuleName)
  55. if normalizedFromLoad != tc.expectedNormalized {
  56. t.Errorf("normalizeModuleNameFromLoadModule(%s) = %s, expected %s",
  57. tc.loadModuleName, normalizedFromLoad, tc.expectedNormalized)
  58. }
  59. // Test normalization from configure argument name
  60. normalizedFromConfigure := normalizeModuleNameFromConfigure(tc.configureArgName)
  61. if normalizedFromConfigure != tc.expectedNormalized {
  62. t.Errorf("normalizeModuleNameFromConfigure(%s) = %s, expected %s",
  63. tc.configureArgName, normalizedFromConfigure, tc.expectedNormalized)
  64. }
  65. // Test getting expected load_module name
  66. expectedLoad := getExpectedLoadModuleName(tc.configureArgName)
  67. if expectedLoad != tc.expectedLoadName {
  68. t.Errorf("getExpectedLoadModuleName(%s) = %s, expected %s",
  69. tc.configureArgName, expectedLoad, tc.expectedLoadName)
  70. }
  71. })
  72. }
  73. }
  74. func TestGetLoadModuleRegex(t *testing.T) {
  75. testCases := []struct {
  76. name string
  77. input string
  78. expected []string // expected module names
  79. }{
  80. {
  81. name: "quoted absolute path",
  82. input: `load_module "/usr/local/nginx/modules/ngx_stream_module.so";`,
  83. expected: []string{"ngx_stream_module"},
  84. },
  85. {
  86. name: "unquoted relative path",
  87. input: `load_module modules/ngx_http_upstream_fair_module.so;`,
  88. expected: []string{"ngx_http_upstream_fair_module"},
  89. },
  90. {
  91. name: "quoted relative path",
  92. input: `load_module "modules/ngx_http_geoip_module.so";`,
  93. expected: []string{"ngx_http_geoip_module"},
  94. },
  95. {
  96. name: "unquoted absolute path",
  97. input: `load_module /etc/nginx/modules/ngx_http_cache_purge_module.so;`,
  98. expected: []string{"ngx_http_cache_purge_module"},
  99. },
  100. {
  101. name: "multiple modules",
  102. input: `load_module "/path/ngx_module1.so";\nload_module modules/ngx_module2.so;`,
  103. expected: []string{"ngx_module1", "ngx_module2"},
  104. },
  105. {
  106. name: "with extra whitespace",
  107. input: `load_module "modules/ngx_test_module.so" ;`,
  108. expected: []string{"ngx_test_module"},
  109. },
  110. {
  111. name: "no matches",
  112. input: `some other nginx config`,
  113. expected: []string{},
  114. },
  115. }
  116. regex := GetLoadModuleRegex()
  117. for _, tc := range testCases {
  118. t.Run(tc.name, func(t *testing.T) {
  119. matches := regex.FindAllStringSubmatch(tc.input, -1)
  120. if len(matches) != len(tc.expected) {
  121. t.Errorf("Expected %d matches, got %d", len(tc.expected), len(matches))
  122. return
  123. }
  124. for i, match := range matches {
  125. if len(match) < 2 {
  126. t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match))
  127. continue
  128. }
  129. moduleName := match[1]
  130. expectedModule := tc.expected[i]
  131. if moduleName != expectedModule {
  132. t.Errorf("Expected module name %s, got %s", expectedModule, moduleName)
  133. }
  134. }
  135. })
  136. }
  137. }
  138. func TestModulesLoaded(t *testing.T) {
  139. text := `
  140. load_module "/usr/local/nginx/modules/ngx_stream_module.so";
  141. load_module modules/ngx_http_upstream_fair_module.so;
  142. load_module "modules/ngx_http_geoip_module.so";
  143. load_module /etc/nginx/modules/ngx_http_cache_purge_module.so;
  144. `
  145. loadModuleRe := GetLoadModuleRegex()
  146. matches := loadModuleRe.FindAllStringSubmatch(text, -1)
  147. t.Log("matches", matches)
  148. // Expected module names
  149. expectedModules := []string{
  150. "ngx_stream_module",
  151. "ngx_http_upstream_fair_module",
  152. "ngx_http_geoip_module",
  153. "ngx_http_cache_purge_module",
  154. }
  155. if len(matches) != len(expectedModules) {
  156. t.Errorf("Expected %d matches, got %d", len(expectedModules), len(matches))
  157. }
  158. for i, match := range matches {
  159. if len(match) < 2 {
  160. t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match))
  161. continue
  162. }
  163. moduleName := match[1]
  164. expectedModule := expectedModules[i]
  165. t.Logf("Match %d: %s", i, moduleName)
  166. if moduleName != expectedModule {
  167. t.Errorf("Expected module name %s, got %s", expectedModule, moduleName)
  168. }
  169. }
  170. }
  171. func TestRealWorldModuleMapping(t *testing.T) {
  172. // Simulate real nginx configuration scenarios
  173. testScenarios := []struct {
  174. name string
  175. configureArg string // from nginx -V output
  176. loadModuleStmt string // from nginx -T output
  177. expectedNormalized string // internal representation
  178. }{
  179. {
  180. name: "stream module - basic",
  181. configureArg: "--with-stream",
  182. loadModuleStmt: `load_module "/usr/lib/nginx/modules/ngx_stream_module.so";`,
  183. expectedNormalized: "stream",
  184. },
  185. {
  186. name: "stream module - dynamic",
  187. configureArg: "--with-stream=dynamic",
  188. loadModuleStmt: `load_module modules/ngx_stream_module.so;`,
  189. expectedNormalized: "stream",
  190. },
  191. {
  192. name: "http_geoip module",
  193. configureArg: "--with-http_geoip_module=dynamic",
  194. loadModuleStmt: `load_module "modules/ngx_http_geoip_module.so";`,
  195. expectedNormalized: "http_geoip",
  196. },
  197. {
  198. name: "stream_geoip module",
  199. configureArg: "--with-stream_geoip_module=dynamic",
  200. loadModuleStmt: `load_module /usr/lib/nginx/modules/ngx_stream_geoip_module.so;`,
  201. expectedNormalized: "stream_geoip",
  202. },
  203. {
  204. name: "http_image_filter module",
  205. configureArg: "--with-http_image_filter_module=dynamic",
  206. loadModuleStmt: `load_module modules/ngx_http_image_filter_module.so;`,
  207. expectedNormalized: "http_image_filter",
  208. },
  209. {
  210. name: "mail module",
  211. configureArg: "--with-mail=dynamic",
  212. loadModuleStmt: `load_module "modules/ngx_mail_module.so";`,
  213. expectedNormalized: "mail",
  214. },
  215. }
  216. for _, scenario := range testScenarios {
  217. t.Run(scenario.name, func(t *testing.T) {
  218. // Test configure argument parsing
  219. paramRe := regexp.MustCompile(`--with-([a-zA-Z0-9_-]+)(?:_module)?(?:=([^"'\s]+|"[^"]*"|'[^']*'))?`)
  220. configMatches := paramRe.FindAllStringSubmatch(scenario.configureArg, -1)
  221. if len(configMatches) == 0 {
  222. t.Errorf("Failed to parse configure argument: %s", scenario.configureArg)
  223. return
  224. }
  225. configModuleName := configMatches[0][1]
  226. normalizedConfigName := normalizeModuleNameFromConfigure(configModuleName)
  227. // Test load_module statement parsing
  228. loadModuleRe := GetLoadModuleRegex()
  229. loadMatches := loadModuleRe.FindAllStringSubmatch(scenario.loadModuleStmt, -1)
  230. if len(loadMatches) == 0 {
  231. t.Errorf("Failed to parse load_module statement: %s", scenario.loadModuleStmt)
  232. return
  233. }
  234. loadModuleName := loadMatches[0][1]
  235. normalizedLoadName := normalizeModuleNameFromLoadModule(loadModuleName)
  236. // Verify both normalize to the same expected value
  237. if normalizedConfigName != scenario.expectedNormalized {
  238. t.Errorf("Configure arg normalization: expected %s, got %s",
  239. scenario.expectedNormalized, normalizedConfigName)
  240. }
  241. if normalizedLoadName != scenario.expectedNormalized {
  242. t.Errorf("Load module normalization: expected %s, got %s",
  243. scenario.expectedNormalized, normalizedLoadName)
  244. }
  245. // Verify they match each other (this is the key test)
  246. if normalizedConfigName != normalizedLoadName {
  247. t.Errorf("Normalization mismatch: config=%s, load=%s",
  248. normalizedConfigName, normalizedLoadName)
  249. }
  250. t.Logf("✓ %s: config=%s -> load=%s -> normalized=%s",
  251. scenario.name, configModuleName, loadModuleName, scenario.expectedNormalized)
  252. })
  253. }
  254. }
  255. func TestAddLoadedDynamicModules(t *testing.T) {
  256. // Test scenario: modules loaded via load_module but not in configure args
  257. // This simulates the real-world case where external modules are installed
  258. // and loaded dynamically without being compiled into nginx
  259. // We can't directly test addLoadedDynamicModules since it depends on getNginxT()
  260. // But we can test the logic by simulating the behavior
  261. testLoadModuleOutput := `
  262. # Configuration file /etc/nginx/modules-enabled/50-mod-stream.conf:
  263. load_module modules/ngx_stream_module.so;
  264. # Configuration file /etc/nginx/modules-enabled/70-mod-stream-geoip2.conf:
  265. load_module modules/ngx_stream_geoip2_module.so;
  266. load_module "modules/ngx_http_geoip2_module.so";
  267. `
  268. // Test the regex and normalization logic
  269. loadModuleRe := GetLoadModuleRegex()
  270. matches := loadModuleRe.FindAllStringSubmatch(testLoadModuleOutput, -1)
  271. expectedModules := map[string]bool{
  272. "stream": false,
  273. "stream_geoip2": false,
  274. "http_geoip2": false,
  275. }
  276. t.Logf("Found %d load_module matches", len(matches))
  277. for _, match := range matches {
  278. if len(match) > 1 {
  279. loadModuleName := match[1]
  280. normalizedName := normalizeModuleNameFromLoadModule(loadModuleName)
  281. t.Logf("Load module: %s -> normalized: %s", loadModuleName, normalizedName)
  282. if _, expected := expectedModules[normalizedName]; expected {
  283. expectedModules[normalizedName] = true
  284. } else {
  285. t.Errorf("Unexpected module found: %s (from %s)", normalizedName, loadModuleName)
  286. }
  287. }
  288. }
  289. // Check that all expected modules were found
  290. for moduleName, found := range expectedModules {
  291. if !found {
  292. t.Errorf("Expected module %s was not found", moduleName)
  293. }
  294. }
  295. }
  296. func TestExternalModuleDiscovery(t *testing.T) {
  297. // Test the complete normalization pipeline for external modules
  298. testCases := []struct {
  299. name string
  300. loadModuleName string
  301. expectedResult string
  302. }{
  303. {
  304. name: "stream_geoip2 module",
  305. loadModuleName: "ngx_stream_geoip2_module",
  306. expectedResult: "stream_geoip2",
  307. },
  308. {
  309. name: "http_geoip2 module",
  310. loadModuleName: "ngx_http_geoip2_module",
  311. expectedResult: "http_geoip2",
  312. },
  313. {
  314. name: "custom third-party module",
  315. loadModuleName: "ngx_http_custom_module",
  316. expectedResult: "http_custom",
  317. },
  318. {
  319. name: "simple module name",
  320. loadModuleName: "ngx_custom_module",
  321. expectedResult: "custom",
  322. },
  323. }
  324. for _, tc := range testCases {
  325. t.Run(tc.name, func(t *testing.T) {
  326. result := normalizeModuleNameFromLoadModule(tc.loadModuleName)
  327. if result != tc.expectedResult {
  328. t.Errorf("normalizeModuleNameFromLoadModule(%s) = %s, expected %s",
  329. tc.loadModuleName, result, tc.expectedResult)
  330. }
  331. })
  332. }
  333. }
  334. func TestGetModuleMapping(t *testing.T) {
  335. // This test verifies that GetModuleMapping function works without errors
  336. // Since it depends on nginx being available, we'll just test that it doesn't panic
  337. defer func() {
  338. if r := recover(); r != nil {
  339. t.Errorf("GetModuleMapping panicked: %v", r)
  340. }
  341. }()
  342. mapping := GetModuleMapping()
  343. // The mapping should be a valid map (could be empty if nginx is not available)
  344. if mapping == nil {
  345. t.Error("GetModuleMapping returned nil")
  346. }
  347. t.Logf("GetModuleMapping returned %d entries", len(mapping))
  348. // If there are entries, verify they have the expected structure
  349. for moduleName, moduleInfo := range mapping {
  350. if moduleInfo == nil {
  351. t.Errorf("Module %s has nil info", moduleName)
  352. continue
  353. }
  354. requiredFields := []string{"normalized", "expected_load_module", "dynamic", "loaded", "params"}
  355. for _, field := range requiredFields {
  356. if _, exists := moduleInfo[field]; !exists {
  357. t.Errorf("Module %s missing field %s", moduleName, field)
  358. }
  359. }
  360. }
  361. }
  362. func TestOpenRestyModuleParsing(t *testing.T) {
  363. // Test case based on real OpenResty nginx -V output
  364. openRestyOutput := `nginx version: openresty/1.25.3.1
  365. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  366. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  367. TLS SNI support enabled
  368. configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.3 --add-module=../echo-nginx-module-0.63 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.33 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.09 --add-module=../srcache-nginx-module-0.33 --add-module=../ngx_lua-0.10.26 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.37 --add-module=../array-var-nginx-module-0.06 --add-module=../memc-nginx-module-0.20 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.9 --add-module=../rds-json-nginx-module-0.16 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.14 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream --without-pcre2 --with-stream_ssl_module --with-stream_ssl_preread_module`
  369. // Test parsing --add-module arguments
  370. addModuleRe := regexp.MustCompile(`--add-module=([^/\s]+/)([^/\s-]+)-([0-9.]+)`)
  371. matches := addModuleRe.FindAllStringSubmatch(openRestyOutput, -1)
  372. expectedModules := map[string]bool{
  373. "ngx_devel_kit": false,
  374. "echo_nginx_module": false,
  375. "xss_nginx_module": false,
  376. "ngx_coolkit": false,
  377. "set_misc_nginx_module": false,
  378. "form_input_nginx_module": false,
  379. "encrypted_session_nginx_module": false,
  380. "srcache_nginx_module": false,
  381. "ngx_lua": false,
  382. "ngx_lua_upstream": false,
  383. "headers_more_nginx_module": false,
  384. "array_var_nginx_module": false,
  385. "memc_nginx_module": false,
  386. "redis2_nginx_module": false,
  387. "redis_nginx_module": false,
  388. "rds_json_nginx_module": false,
  389. "rds_csv_nginx_module": false,
  390. "ngx_stream_lua": false,
  391. }
  392. t.Logf("Found %d --add-module matches", len(matches))
  393. for _, match := range matches {
  394. if len(match) > 2 {
  395. moduleName := match[2]
  396. t.Logf("Found add-module: %s", moduleName)
  397. if _, expected := expectedModules[moduleName]; expected {
  398. expectedModules[moduleName] = true
  399. } else {
  400. // This might be a valid module we didn't expect
  401. t.Logf("Unexpected add-module found: %s", moduleName)
  402. }
  403. }
  404. }
  405. // Check that we found most expected modules
  406. foundCount := 0
  407. for moduleName, found := range expectedModules {
  408. if found {
  409. foundCount++
  410. } else {
  411. t.Logf("Expected add-module %s was not found", moduleName)
  412. }
  413. }
  414. if foundCount == 0 {
  415. t.Error("No add-modules were parsed successfully")
  416. }
  417. // Test parsing --with- arguments as well
  418. withModuleRe := regexp.MustCompile(`--with-([a-zA-Z0-9_-]+)(?:_module)?(?:=([^"'\s]+|"[^"]*"|'[^']*'))?`)
  419. withMatches := withModuleRe.FindAllStringSubmatch(openRestyOutput, -1)
  420. expectedWithModules := map[string]bool{
  421. "cc-opt": false,
  422. "ld-opt": false,
  423. "http_ssl_module": false,
  424. "http_v2_module": false,
  425. "http_realip_module": false,
  426. "stream": false,
  427. "stream_ssl_module": false,
  428. "stream_ssl_preread_module": false,
  429. }
  430. t.Logf("Found %d --with- matches", len(withMatches))
  431. for _, match := range withMatches {
  432. if len(match) > 1 {
  433. moduleName := match[1]
  434. t.Logf("Found with-module: %s", moduleName)
  435. if _, expected := expectedWithModules[moduleName]; expected {
  436. expectedWithModules[moduleName] = true
  437. }
  438. }
  439. }
  440. // Verify we found the key --with- modules
  441. withFoundCount := 0
  442. for _, found := range expectedWithModules {
  443. if found {
  444. withFoundCount++
  445. }
  446. }
  447. if withFoundCount < 3 { // At least stream, http_ssl_module, etc should be found
  448. t.Errorf("Too few --with- modules found: %d", withFoundCount)
  449. }
  450. }
  451. func TestAddModuleRegexParsing(t *testing.T) {
  452. testCases := []struct {
  453. name string
  454. input string
  455. expected []string // expected module names
  456. }{
  457. {
  458. name: "single add-module with version",
  459. input: "--add-module=../ngx_devel_kit-0.3.3",
  460. expected: []string{"ngx_devel_kit"},
  461. },
  462. {
  463. name: "add-module with nginx in name",
  464. input: "--add-module=../echo-nginx-module-0.63",
  465. expected: []string{"echo_nginx_module"},
  466. },
  467. {
  468. name: "multiple add-modules",
  469. input: "--add-module=../ngx_lua-0.10.26 --add-module=../headers-more-nginx-module-0.37",
  470. expected: []string{"ngx_lua", "headers_more_nginx_module"},
  471. },
  472. {
  473. name: "add-module with different separators",
  474. input: "--add-module=../set-misc-nginx-module-0.33 --add-module=../ngx_coolkit-0.2",
  475. expected: []string{"set_misc_nginx_module", "ngx_coolkit"},
  476. },
  477. }
  478. // Regex to parse --add-module arguments
  479. addModuleRe := regexp.MustCompile(`--add-module=(?:[^/\s]+/)?([^/\s-]+(?:-[^/\s-]+)*)-[0-9.]+`)
  480. for _, tc := range testCases {
  481. t.Run(tc.name, func(t *testing.T) {
  482. matches := addModuleRe.FindAllStringSubmatch(tc.input, -1)
  483. if len(matches) != len(tc.expected) {
  484. t.Errorf("Expected %d matches, got %d", len(tc.expected), len(matches))
  485. for i, match := range matches {
  486. if len(match) > 1 {
  487. t.Logf("Match %d: %s", i, match[1])
  488. }
  489. }
  490. return
  491. }
  492. for i, match := range matches {
  493. if len(match) < 2 {
  494. t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match))
  495. continue
  496. }
  497. moduleName := match[1]
  498. // Convert dashes to underscores for consistency
  499. normalizedName := strings.ReplaceAll(moduleName, "-", "_")
  500. expectedModule := tc.expected[i]
  501. if normalizedName != expectedModule {
  502. t.Errorf("Expected module name %s, got %s (normalized from %s)", expectedModule, normalizedName, moduleName)
  503. }
  504. }
  505. })
  506. }
  507. }
  508. func TestNormalizeAddModuleName(t *testing.T) {
  509. testCases := []struct {
  510. name string
  511. addModuleName string
  512. expectedResult string
  513. }{
  514. {
  515. name: "ngx_devel_kit",
  516. addModuleName: "ngx_devel_kit",
  517. expectedResult: "devel_kit",
  518. },
  519. {
  520. name: "echo-nginx-module",
  521. addModuleName: "echo-nginx-module",
  522. expectedResult: "echo_nginx",
  523. },
  524. {
  525. name: "headers-more-nginx-module",
  526. addModuleName: "headers-more-nginx-module",
  527. expectedResult: "headers_more_nginx",
  528. },
  529. {
  530. name: "ngx_lua",
  531. addModuleName: "ngx_lua",
  532. expectedResult: "lua",
  533. },
  534. {
  535. name: "set-misc-nginx-module",
  536. addModuleName: "set-misc-nginx-module",
  537. expectedResult: "set_misc_nginx",
  538. },
  539. {
  540. name: "ngx_stream_lua",
  541. addModuleName: "ngx_stream_lua",
  542. expectedResult: "stream_lua",
  543. },
  544. }
  545. for _, tc := range testCases {
  546. t.Run(tc.name, func(t *testing.T) {
  547. result := normalizeAddModuleName(tc.addModuleName)
  548. if result != tc.expectedResult {
  549. t.Errorf("normalizeAddModuleName(%s) = %s, expected %s",
  550. tc.addModuleName, result, tc.expectedResult)
  551. }
  552. })
  553. }
  554. }
  555. func TestStreamConfigurationParsing(t *testing.T) {
  556. // Test parsing of stream configuration to verify stream module is working
  557. streamConfig := `stream {
  558. log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  559. include /usr/local/openresty/nginx/conf/streams-enabled/*.conf;
  560. default_type application/octet-stream;
  561. upstream sshd_63_stream {
  562. server 192.168.1.63:22;
  563. }
  564. server {
  565. listen 6001;
  566. proxy_pass sshd_63_stream;
  567. }
  568. }`
  569. // Simple test to verify stream block can be detected (word boundary to avoid matching "upstream sshd_63_stream")
  570. streamBlockRe := regexp.MustCompile(`\bstream\s*\{`)
  571. matches := streamBlockRe.FindAllString(streamConfig, -1)
  572. if len(matches) != 1 {
  573. t.Errorf("Expected to find 1 stream block, found %d", len(matches))
  574. }
  575. // Test upstream parsing within stream
  576. upstreamRe := regexp.MustCompile(`upstream\s+([a-zA-Z0-9_]+)\s*\{`)
  577. upstreamMatches := upstreamRe.FindAllStringSubmatch(streamConfig, -1)
  578. if len(upstreamMatches) != 1 {
  579. t.Errorf("Expected to find 1 upstream, found %d", len(upstreamMatches))
  580. } else if upstreamMatches[0][1] != "sshd_63_stream" {
  581. t.Errorf("Expected upstream name 'sshd_63_stream', got '%s'", upstreamMatches[0][1])
  582. }
  583. // Test server block parsing within stream
  584. serverRe := regexp.MustCompile(`server\s*\{[^}]*listen\s+(\d+)`)
  585. serverMatches := serverRe.FindAllStringSubmatch(streamConfig, -1)
  586. if len(serverMatches) != 1 {
  587. t.Errorf("Expected to find 1 server with listen directive, found %d", len(serverMatches))
  588. } else if serverMatches[0][1] != "6001" {
  589. t.Errorf("Expected listen port '6001', got '%s'", serverMatches[0][1])
  590. }
  591. }
  592. func TestIntegratedModuleDetection(t *testing.T) {
  593. // This test simulates the complete flow of module detection for OpenResty
  594. // This would test the integration between --add-module parsing and --with- parsing
  595. // Mock nginx -V output combining both --add-module and --with- parameters
  596. mockNginxV := `nginx version: openresty/1.25.3.1
  597. configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.3 --add-module=../ngx_lua-0.10.26 --with-http_ssl_module --with-stream --with-stream_ssl_module`
  598. // Test both regex patterns work on the same input
  599. withModuleRe := regexp.MustCompile(`--with-([a-zA-Z0-9_-]+)(?:_module)?(?:=([^"'\s]+|"[^"]*"|'[^']*'))?`)
  600. addModuleRe := regexp.MustCompile(`--add-module=(?:[^/\s]+/)?([^/\s-]+(?:-[^/\s-]+)*)-[0-9.]+`)
  601. withMatches := withModuleRe.FindAllStringSubmatch(mockNginxV, -1)
  602. addMatches := addModuleRe.FindAllStringSubmatch(mockNginxV, -1)
  603. t.Logf("Found %d --with- matches and %d --add-module matches", len(withMatches), len(addMatches))
  604. // Verify we can parse both types
  605. if len(withMatches) == 0 {
  606. t.Error("Failed to parse any --with- modules")
  607. }
  608. if len(addMatches) == 0 {
  609. t.Error("Failed to parse any --add-module modules")
  610. }
  611. // Build a combined module list like the actual code should do
  612. allModules := make(map[string]bool)
  613. // Process --with- modules
  614. for _, match := range withMatches {
  615. if len(match) > 1 {
  616. moduleName := match[1]
  617. normalized := normalizeModuleNameFromConfigure(moduleName)
  618. allModules[normalized] = true
  619. t.Logf("--with- module: %s -> %s", moduleName, normalized)
  620. }
  621. }
  622. // Process --add-module modules
  623. for _, match := range addMatches {
  624. if len(match) > 1 {
  625. moduleName := match[1]
  626. normalized := normalizeAddModuleName(moduleName)
  627. allModules[normalized] = true
  628. t.Logf("--add-module: %s -> %s", moduleName, normalized)
  629. }
  630. }
  631. // Verify we have both types of modules
  632. expectedModules := []string{"stream", "http_ssl", "devel_kit", "lua"}
  633. foundCount := 0
  634. for _, expected := range expectedModules {
  635. if allModules[expected] {
  636. foundCount++
  637. t.Logf("✓ Found expected module: %s", expected)
  638. } else {
  639. t.Logf("✗ Missing expected module: %s", expected)
  640. }
  641. }
  642. if foundCount < 2 {
  643. t.Errorf("Expected to find at least 2 modules, found %d", foundCount)
  644. }
  645. }