sandbox_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package nginx
  2. import (
  3. "path/filepath"
  4. "runtime"
  5. "strings"
  6. "testing"
  7. )
  8. func TestNormalizeIncludeLineRelativeTo(t *testing.T) {
  9. baseDir := "/etc/nginx/sites-available"
  10. if runtime.GOOS == "windows" {
  11. // keep test portable; filepath.Join will use OS-specific separator
  12. baseDir = `C:\nginx\conf\sites-available`
  13. }
  14. sandboxDir := "/tmp/sbx"
  15. tests := []struct {
  16. name string
  17. in string
  18. wantPrefix string
  19. }{
  20. {
  21. name: "relative simple file",
  22. in: " include mime.types;",
  23. wantPrefix: " include ",
  24. },
  25. {
  26. name: "relative path with subdir",
  27. in: "include ../common/snippets/*.conf;",
  28. wantPrefix: "include ",
  29. },
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. out := normalizeIncludeLineRelativeTo(tt.in, baseDir, sandboxDir)
  34. if out == "" {
  35. t.Fatalf("expected non-empty include, got empty")
  36. }
  37. if !strings.HasPrefix(out, tt.wantPrefix) {
  38. t.Fatalf("unexpected prefix: %q, got %q", tt.wantPrefix, out)
  39. }
  40. // if relative input (first two cases), ensure absolute joined path appears
  41. if tt.name == "relative simple file" || tt.name == "relative path with subdir" {
  42. parts := strings.Split(out, "include ")
  43. if len(parts) < 2 {
  44. t.Fatalf("malformed include line: %q", out)
  45. }
  46. pathWithSemi := parts[1]
  47. path := strings.TrimSuffix(pathWithSemi, ";")
  48. if !filepath.IsAbs(path) {
  49. t.Fatalf("expected absolute path, got %q", path)
  50. }
  51. }
  52. })
  53. }
  54. }
  55. func TestReplaceIncludeDirectives(t *testing.T) {
  56. mainConf := `
  57. user nginx;
  58. worker_processes auto;
  59. error_log /var/log/nginx/error.log notice;
  60. pid /var/run/nginx.pid;
  61. events {
  62. worker_connections 1024;
  63. }
  64. http {
  65. include mime.types;
  66. include /etc/nginx/conf.d/*.conf;
  67. include /etc/nginx/sites-enabled/*;
  68. }
  69. stream {
  70. include /etc/nginx/streams-enabled/*;
  71. }
  72. `
  73. siteLines := []string{" include /tmp/sbx/sites-enabled/a.conf;"}
  74. streamLines := []string{" include /tmp/sbx/streams-enabled/s1.conf;"}
  75. out := replaceIncludeDirectives(mainConf, "/tmp/sbx", siteLines, streamLines)
  76. if strings.Contains(out, "/etc/nginx/sites-enabled/*") {
  77. t.Fatal("sites-enabled wildcard should be replaced by sandbox files")
  78. }
  79. if !strings.Contains(out, "/tmp/sbx/sites-enabled/a.conf;") {
  80. t.Fatal("sandbox site include missing")
  81. }
  82. if strings.Contains(out, "/etc/nginx/streams-enabled/*") {
  83. t.Fatal("streams-enabled wildcard should be replaced by sandbox files")
  84. }
  85. if !strings.Contains(out, "/tmp/sbx/streams-enabled/s1.conf;") {
  86. t.Fatal("sandbox stream include missing")
  87. }
  88. // mime.types should be kept (possibly normalized)
  89. if !strings.Contains(strings.ToLower(out), "include") {
  90. t.Fatal("expected include directives to remain")
  91. }
  92. }