modules_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 TestOpenRestyModuleParsing(t *testing.T) {
  335. // Test case based on real OpenResty nginx -V output
  336. openRestyOutput := `nginx version: openresty/1.25.3.1
  337. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  338. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  339. TLS SNI support enabled
  340. 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`
  341. // Test parsing --add-module arguments
  342. addModuleRe := regexp.MustCompile(`--add-module=([^/\s]+/)([^/\s-]+)-([0-9.]+)`)
  343. matches := addModuleRe.FindAllStringSubmatch(openRestyOutput, -1)
  344. expectedModules := map[string]bool{
  345. "ngx_devel_kit": false,
  346. "echo_nginx_module": false,
  347. "xss_nginx_module": false,
  348. "ngx_coolkit": false,
  349. "set_misc_nginx_module": false,
  350. "form_input_nginx_module": false,
  351. "encrypted_session_nginx_module": false,
  352. "srcache_nginx_module": false,
  353. "ngx_lua": false,
  354. "ngx_lua_upstream": false,
  355. "headers_more_nginx_module": false,
  356. "array_var_nginx_module": false,
  357. "memc_nginx_module": false,
  358. "redis2_nginx_module": false,
  359. "redis_nginx_module": false,
  360. "rds_json_nginx_module": false,
  361. "rds_csv_nginx_module": false,
  362. "ngx_stream_lua": false,
  363. }
  364. t.Logf("Found %d --add-module matches", len(matches))
  365. for _, match := range matches {
  366. if len(match) > 2 {
  367. moduleName := match[2]
  368. t.Logf("Found add-module: %s", moduleName)
  369. if _, expected := expectedModules[moduleName]; expected {
  370. expectedModules[moduleName] = true
  371. } else {
  372. // This might be a valid module we didn't expect
  373. t.Logf("Unexpected add-module found: %s", moduleName)
  374. }
  375. }
  376. }
  377. // Check that we found most expected modules
  378. foundCount := 0
  379. for moduleName, found := range expectedModules {
  380. if found {
  381. foundCount++
  382. } else {
  383. t.Logf("Expected add-module %s was not found", moduleName)
  384. }
  385. }
  386. if foundCount == 0 {
  387. t.Error("No add-modules were parsed successfully")
  388. }
  389. // Test parsing --with- arguments as well
  390. withModuleRe := regexp.MustCompile(`--with-([a-zA-Z0-9_-]+)(?:_module)?(?:=([^"'\s]+|"[^"]*"|'[^']*'))?`)
  391. withMatches := withModuleRe.FindAllStringSubmatch(openRestyOutput, -1)
  392. expectedWithModules := map[string]bool{
  393. "cc-opt": false,
  394. "ld-opt": false,
  395. "http_ssl_module": false,
  396. "http_v2_module": false,
  397. "http_realip_module": false,
  398. "stream": false,
  399. "stream_ssl_module": false,
  400. "stream_ssl_preread_module": false,
  401. }
  402. t.Logf("Found %d --with- matches", len(withMatches))
  403. for _, match := range withMatches {
  404. if len(match) > 1 {
  405. moduleName := match[1]
  406. t.Logf("Found with-module: %s", moduleName)
  407. if _, expected := expectedWithModules[moduleName]; expected {
  408. expectedWithModules[moduleName] = true
  409. }
  410. }
  411. }
  412. // Verify we found the key --with- modules
  413. withFoundCount := 0
  414. for _, found := range expectedWithModules {
  415. if found {
  416. withFoundCount++
  417. }
  418. }
  419. if withFoundCount < 3 { // At least stream, http_ssl_module, etc should be found
  420. t.Errorf("Too few --with- modules found: %d", withFoundCount)
  421. }
  422. }
  423. func TestAddModuleRegexParsing(t *testing.T) {
  424. testCases := []struct {
  425. name string
  426. input string
  427. expected []string // expected module names
  428. }{
  429. {
  430. name: "single add-module with version",
  431. input: "--add-module=../ngx_devel_kit-0.3.3",
  432. expected: []string{"ngx_devel_kit"},
  433. },
  434. {
  435. name: "add-module with nginx in name",
  436. input: "--add-module=../echo-nginx-module-0.63",
  437. expected: []string{"echo_nginx_module"},
  438. },
  439. {
  440. name: "multiple add-modules",
  441. input: "--add-module=../ngx_lua-0.10.26 --add-module=../headers-more-nginx-module-0.37",
  442. expected: []string{"ngx_lua", "headers_more_nginx_module"},
  443. },
  444. {
  445. name: "add-module with different separators",
  446. input: "--add-module=../set-misc-nginx-module-0.33 --add-module=../ngx_coolkit-0.2",
  447. expected: []string{"set_misc_nginx_module", "ngx_coolkit"},
  448. },
  449. }
  450. // Regex to parse --add-module arguments
  451. addModuleRe := regexp.MustCompile(`--add-module=(?:[^/\s]+/)?([^/\s-]+(?:-[^/\s-]+)*)-[0-9.]+`)
  452. for _, tc := range testCases {
  453. t.Run(tc.name, func(t *testing.T) {
  454. matches := addModuleRe.FindAllStringSubmatch(tc.input, -1)
  455. if len(matches) != len(tc.expected) {
  456. t.Errorf("Expected %d matches, got %d", len(tc.expected), len(matches))
  457. for i, match := range matches {
  458. if len(match) > 1 {
  459. t.Logf("Match %d: %s", i, match[1])
  460. }
  461. }
  462. return
  463. }
  464. for i, match := range matches {
  465. if len(match) < 2 {
  466. t.Errorf("Match %d should have at least 2 groups, got %d", i, len(match))
  467. continue
  468. }
  469. moduleName := match[1]
  470. // Convert dashes to underscores for consistency
  471. normalizedName := strings.ReplaceAll(moduleName, "-", "_")
  472. expectedModule := tc.expected[i]
  473. if normalizedName != expectedModule {
  474. t.Errorf("Expected module name %s, got %s (normalized from %s)", expectedModule, normalizedName, moduleName)
  475. }
  476. }
  477. })
  478. }
  479. }
  480. func TestNormalizeAddModuleName(t *testing.T) {
  481. testCases := []struct {
  482. name string
  483. addModuleName string
  484. expectedResult string
  485. }{
  486. {
  487. name: "ngx_devel_kit",
  488. addModuleName: "ngx_devel_kit",
  489. expectedResult: "devel_kit",
  490. },
  491. {
  492. name: "echo-nginx-module",
  493. addModuleName: "echo-nginx-module",
  494. expectedResult: "echo_nginx",
  495. },
  496. {
  497. name: "headers-more-nginx-module",
  498. addModuleName: "headers-more-nginx-module",
  499. expectedResult: "headers_more_nginx",
  500. },
  501. {
  502. name: "ngx_lua",
  503. addModuleName: "ngx_lua",
  504. expectedResult: "lua",
  505. },
  506. {
  507. name: "set-misc-nginx-module",
  508. addModuleName: "set-misc-nginx-module",
  509. expectedResult: "set_misc_nginx",
  510. },
  511. {
  512. name: "ngx_stream_lua",
  513. addModuleName: "ngx_stream_lua",
  514. expectedResult: "stream_lua",
  515. },
  516. }
  517. for _, tc := range testCases {
  518. t.Run(tc.name, func(t *testing.T) {
  519. result := normalizeAddModuleName(tc.addModuleName)
  520. if result != tc.expectedResult {
  521. t.Errorf("normalizeAddModuleName(%s) = %s, expected %s",
  522. tc.addModuleName, result, tc.expectedResult)
  523. }
  524. })
  525. }
  526. }
  527. func TestStreamConfigurationParsing(t *testing.T) {
  528. // Test parsing of stream configuration to verify stream module is working
  529. streamConfig := `stream {
  530. 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';
  531. include /usr/local/openresty/nginx/conf/streams-enabled/*.conf;
  532. default_type application/octet-stream;
  533. upstream sshd_63_stream {
  534. server 192.168.1.63:22;
  535. }
  536. server {
  537. listen 6001;
  538. proxy_pass sshd_63_stream;
  539. }
  540. }`
  541. // Simple test to verify stream block can be detected (word boundary to avoid matching "upstream sshd_63_stream")
  542. streamBlockRe := regexp.MustCompile(`\bstream\s*\{`)
  543. matches := streamBlockRe.FindAllString(streamConfig, -1)
  544. if len(matches) != 1 {
  545. t.Errorf("Expected to find 1 stream block, found %d", len(matches))
  546. }
  547. // Test upstream parsing within stream
  548. upstreamRe := regexp.MustCompile(`upstream\s+([a-zA-Z0-9_]+)\s*\{`)
  549. upstreamMatches := upstreamRe.FindAllStringSubmatch(streamConfig, -1)
  550. if len(upstreamMatches) != 1 {
  551. t.Errorf("Expected to find 1 upstream, found %d", len(upstreamMatches))
  552. } else if upstreamMatches[0][1] != "sshd_63_stream" {
  553. t.Errorf("Expected upstream name 'sshd_63_stream', got '%s'", upstreamMatches[0][1])
  554. }
  555. // Test server block parsing within stream
  556. serverRe := regexp.MustCompile(`server\s*\{[^}]*listen\s+(\d+)`)
  557. serverMatches := serverRe.FindAllStringSubmatch(streamConfig, -1)
  558. if len(serverMatches) != 1 {
  559. t.Errorf("Expected to find 1 server with listen directive, found %d", len(serverMatches))
  560. } else if serverMatches[0][1] != "6001" {
  561. t.Errorf("Expected listen port '6001', got '%s'", serverMatches[0][1])
  562. }
  563. }
  564. func TestIntegratedModuleDetection(t *testing.T) {
  565. // This test simulates the complete flow of module detection for OpenResty
  566. // This would test the integration between --add-module parsing and --with- parsing
  567. // Mock nginx -V output combining both --add-module and --with- parameters
  568. mockNginxV := `nginx version: openresty/1.25.3.1
  569. 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`
  570. // Test both regex patterns work on the same input
  571. withModuleRe := regexp.MustCompile(`--with-([a-zA-Z0-9_-]+)(?:_module)?(?:=([^"'\s]+|"[^"]*"|'[^']*'))?`)
  572. addModuleRe := regexp.MustCompile(`--add-module=(?:[^/\s]+/)?([^/\s-]+(?:-[^/\s-]+)*)-[0-9.]+`)
  573. withMatches := withModuleRe.FindAllStringSubmatch(mockNginxV, -1)
  574. addMatches := addModuleRe.FindAllStringSubmatch(mockNginxV, -1)
  575. t.Logf("Found %d --with- matches and %d --add-module matches", len(withMatches), len(addMatches))
  576. // Verify we can parse both types
  577. if len(withMatches) == 0 {
  578. t.Error("Failed to parse any --with- modules")
  579. }
  580. if len(addMatches) == 0 {
  581. t.Error("Failed to parse any --add-module modules")
  582. }
  583. // Build a combined module list like the actual code should do
  584. allModules := make(map[string]bool)
  585. // Process --with- modules
  586. for _, match := range withMatches {
  587. if len(match) > 1 {
  588. moduleName := match[1]
  589. normalized := normalizeModuleNameFromConfigure(moduleName)
  590. allModules[normalized] = true
  591. t.Logf("--with- module: %s -> %s", moduleName, normalized)
  592. }
  593. }
  594. // Process --add-module modules
  595. for _, match := range addMatches {
  596. if len(match) > 1 {
  597. moduleName := match[1]
  598. normalized := normalizeAddModuleName(moduleName)
  599. allModules[normalized] = true
  600. t.Logf("--add-module: %s -> %s", moduleName, normalized)
  601. }
  602. }
  603. // Verify we have both types of modules
  604. expectedModules := []string{"stream", "http_ssl", "devel_kit", "lua"}
  605. foundCount := 0
  606. for _, expected := range expectedModules {
  607. if allModules[expected] {
  608. foundCount++
  609. t.Logf("✓ Found expected module: %s", expected)
  610. } else {
  611. t.Logf("✗ Missing expected module: %s", expected)
  612. }
  613. }
  614. if foundCount < 2 {
  615. t.Errorf("Expected to find at least 2 modules, found %d", foundCount)
  616. }
  617. }