miaopanel
1package webserver
2
3import (
4 "strings"
5 "testing"
6)
7
8const sampleMainConf = `user www;
9worker_processes 1;
10
11events {
12 worker_connections 768;
13 multi_accept on;
14}
15
16http {
17 include /etc/nginx/mime.types;
18 keepalive_timeout 65;
19 gzip on;
20 gzip_min_length 1000;
21 gzip_comp_level 2;
22 # a comment with a brace } must not confuse the parser
23 server {
24 listen 80;
25 location / {
26 root /www/wwwroot/demo;
27 }
28 }
29}
30`
31
32func TestPatchPerformanceUpdatesAndKeepsStructure(t *testing.T) {
33 p := Performance{
34 WorkerProcesses: "auto",
35 WorkerConnections: 51200,
36 KeepaliveTimeout: 60,
37 Gzip: true,
38 GzipMinLengthKB: 1,
39 GzipCompLevel: 5,
40 ClientMaxBodyMB: 50,
41 ServerNamesHashBucket: 512,
42 ClientHeaderBufferKB: 32,
43 ClientBodyBufferKB: 512,
44 }
45 out, err := patchPerformance(sampleMainConf, p)
46 if err != nil {
47 t.Fatal(err)
48 }
49 for _, want := range []string{
50 "worker_processes auto;",
51 "worker_connections 51200;",
52 "keepalive_timeout 60;",
53 "gzip on;",
54 "gzip_min_length 1k;",
55 "gzip_comp_level 5;",
56 "client_max_body_size 50m;",
57 "server_names_hash_bucket_size 512;",
58 "client_header_buffer_size 32k;",
59 "client_body_buffer_size 512k;",
60 "multi_accept on;",
61 "location / {",
62 } {
63 if !strings.Contains(out, want) {
64 t.Errorf("patched config missing %q\n%s", want, out)
65 }
66 }
67 if strings.Count(out, "worker_connections") != 1 {
68 t.Errorf("worker_connections duplicated:\n%s", out)
69 }
70 if strings.Count(out, "gzip on;") != 1 {
71 t.Errorf("gzip duplicated:\n%s", out)
72 }
73}
74
75func TestPatchPerformanceInsertsMissingDirectives(t *testing.T) {
76 src := `worker_processes 1;
77events { worker_connections 128; }
78http {
79 include /etc/nginx/mime.types;
80}
81`
82 p := Performance{
83 WorkerProcesses: "2",
84 WorkerConnections: 4096,
85 KeepaliveTimeout: 30,
86 Gzip: false,
87 GzipMinLengthKB: 10,
88 GzipCompLevel: 3,
89 ClientMaxBodyMB: 20,
90 ServerNamesHashBucket: 64,
91 ClientHeaderBufferKB: 8,
92 ClientBodyBufferKB: 64,
93 }
94 out, err := patchPerformance(src, p)
95 if err != nil {
96 t.Fatal(err)
97 }
98 for _, want := range []string{
99 "worker_processes 2;",
100 "worker_connections 4096;",
101 "keepalive_timeout 30;",
102 "gzip off;",
103 "client_max_body_size 20m;",
104 "include /etc/nginx/mime.types;",
105 } {
106 if !strings.Contains(out, want) {
107 t.Errorf("missing %q\n%s", want, out)
108 }
109 }
110}
111
112func TestPresentationValidate(t *testing.T) {
113 ok := Performance{
114 WorkerProcesses: "auto", WorkerConnections: 1024, KeepaliveTimeout: 60,
115 GzipMinLengthKB: 1, GzipCompLevel: 5, ClientMaxBodyMB: 50,
116 ServerNamesHashBucket: 64, ClientHeaderBufferKB: 8, ClientBodyBufferKB: 16,
117 }
118 if err := ok.validate(); err != nil {
119 t.Fatalf("valid config rejected: %v", err)
120 }
121 bad := ok
122 bad.ServerNamesHashBucket = 100
123 if err := bad.validate(); err == nil {
124 t.Fatal("non power-of-two bucket accepted")
125 }
126 bad = ok
127 bad.WorkerProcesses = "many"
128 if err := bad.validate(); err == nil {
129 t.Fatal("invalid worker_processes accepted")
130 }
131}