deploy_test.go (1822 bytes)
1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 "testing" 7 ) 8 9 // TestIncludeAWK checks deploy/include.awk. deploy.sh runs it on the nginx 10 // site file of the server, so a mistake in it breaks the site. The test 11 // needs awk. 12 func TestIncludeAWK(t *testing.T) { 13 awk, err := exec.LookPath("awk") 14 if err != nil { 15 t.Skip("no awk:", err) 16 } 17 const inc = "include snippets/sbm-webpublish.conf;" 18 certbot := `server { 19 server_name a.com; 20 root /var/www/a; 21 location / { 22 try_files $uri $uri/ =404; 23 } 24 listen [::]:443 ssl ipv6only=on; # managed by Certbot 25 listen 443 ssl; # managed by Certbot 26 } 27 server { 28 if ($host = a.com) { 29 return 301 https://$host$request_uri; 30 } # managed by Certbot 31 listen 80; 32 server_name a.com; 33 return 404; # managed by Certbot 34 } 35 ` 36 plain := "server {\n listen 80;\n listen 8443 ssl;\n # listen 443 ssl;\n server_name a.com;\n}\n" 37 for _, c := range []struct{ name, in, want string }{ 38 // One line in the HTTPS block, above its first listen line. The 39 // block for port 80 stays the same. 40 {"certbot", certbot, strings.Replace(certbot, " listen [::]:443", " "+inc+"\n listen [::]:443", 1)}, 41 // Each HTTPS block gets the line, with the indent of its listen line. 42 {"two blocks", "server\n{\n\tlisten 127.0.0.1:443 ssl;\n}\nserver {\n listen 443;\n}\n", 43 "server\n{\n\t" + inc + "\n\tlisten 127.0.0.1:443 ssl;\n}\nserver {\n " + inc + "\n listen 443;\n}\n"}, 44 // Port 8443 and a comment are not HTTPS on port 443. 45 {"no https", plain, plain}, 46 } { 47 cmd := exec.Command(awk, "-f", "deploy/include.awk") 48 cmd.Stdin = strings.NewReader(c.in) 49 out, err := cmd.Output() 50 if err != nil { 51 t.Fatalf("%s: %v", c.name, err) 52 } 53 if string(out) != c.want { 54 t.Errorf("%s: got\n%s\nwant\n%s", c.name, out, c.want) 55 } 56 } 57 }