Tests: Go application tests.
This commit is contained in:
6
test/go/404/404.html
Normal file
6
test/go/404/404.html
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<html>
|
||||||
|
<head><title>404 Not Found</title></head>
|
||||||
|
<body bgcolor="white">
|
||||||
|
<center><h1>404 Not Found</h1></center>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
22
test/go/404/app.go
Normal file
22
test/go/404/app.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
b, e := ioutil.ReadFile("404.html")
|
||||||
|
|
||||||
|
if e == nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
io.WriteString(w, string(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
23
test/go/command_line_arguments/app.go
Normal file
23
test/go/command_line_arguments/app.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
args := strings.Join(os.Args[1:], ",")
|
||||||
|
|
||||||
|
w.Header().Add("X-Arg-0", fmt.Sprintf("%v", os.Args[0]))
|
||||||
|
w.Header().Add("Content-Length", fmt.Sprintf("%v", len(args)))
|
||||||
|
io.WriteString(w, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
19
test/go/cookies/app.go
Normal file
19
test/go/cookies/app.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cookie1, _ := r.Cookie("var1")
|
||||||
|
cookie2, _ := r.Cookie("var2")
|
||||||
|
|
||||||
|
w.Header().Set("X-Cookie-1", cookie1.Value)
|
||||||
|
w.Header().Set("X-Cookie-2", cookie2.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
13
test/go/empty/app.go
Normal file
13
test/go/empty/app.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
17
test/go/get_variables/app.go
Normal file
17
test/go/get_variables/app.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("X-Var-1", r.URL.Query().Get("var1"))
|
||||||
|
w.Header().Set("X-Var-2", r.URL.Query().Get("var2"))
|
||||||
|
w.Header().Set("X-Var-3", r.URL.Query().Get("var3"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
21
test/go/mirror/app.go
Normal file
21
test/go/mirror/app.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var buf [32768]byte;
|
||||||
|
len, _ := r.Body.Read(buf[:])
|
||||||
|
|
||||||
|
w.Header().Add("Content-Length", fmt.Sprintf("%v", len))
|
||||||
|
io.WriteString(w, string(buf[:len]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
19
test/go/post_variables/app.go
Normal file
19
test/go/post_variables/app.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseForm()
|
||||||
|
|
||||||
|
w.Header().Set("X-Var-1", r.Form.Get("var1"))
|
||||||
|
w.Header().Set("X-Var-2", r.Form.Get("var2"))
|
||||||
|
w.Header().Set("X-Var-3", r.Form.Get("var3"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
30
test/go/variables/app.go
Normal file
30
test/go/variables/app.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"nginx/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var buf [4096]byte;
|
||||||
|
len, _ := r.Body.Read(buf[:])
|
||||||
|
|
||||||
|
w.Header().Set("Request-Method", r.Method)
|
||||||
|
w.Header().Set("Request-Uri", r.RequestURI)
|
||||||
|
w.Header().Set("Server-Protocol", r.Proto)
|
||||||
|
w.Header().Set("Server-Protocol-Major", fmt.Sprintf("%v", r.ProtoMajor))
|
||||||
|
w.Header().Set("Server-Protocol-Minor", fmt.Sprintf("%v", r.ProtoMinor))
|
||||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%v", len))
|
||||||
|
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
|
||||||
|
w.Header().Set("Custom-Header", r.Header.Get("Custom-Header"))
|
||||||
|
w.Header().Set("Http-Host", r.Header.Get("Host"))
|
||||||
|
|
||||||
|
io.WriteString(w, string(buf[:len]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
unit.ListenAndServe(":7080", nil)
|
||||||
|
}
|
||||||
154
test/test_go_application.py
Normal file
154
test/test_go_application.py
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
import unittest
|
||||||
|
import unit
|
||||||
|
|
||||||
|
class TestUnitGoApplication(unit.TestUnitApplicationGo):
|
||||||
|
|
||||||
|
def setUpClass():
|
||||||
|
u = unit.TestUnit()
|
||||||
|
|
||||||
|
if u.architecture == '32bit':
|
||||||
|
raise unittest.SkipTest('Skip Go tests for x86')
|
||||||
|
|
||||||
|
u.check_modules('go')
|
||||||
|
|
||||||
|
def test_go_application_variables(self):
|
||||||
|
self.load('variables')
|
||||||
|
|
||||||
|
body = 'Test body string.'
|
||||||
|
|
||||||
|
resp = self.post(headers={
|
||||||
|
'Host': 'localhost',
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
'Custom-Header': 'blah'
|
||||||
|
}, body=body)
|
||||||
|
|
||||||
|
self.assertEqual(resp['status'], 200, 'status')
|
||||||
|
headers = resp['headers']
|
||||||
|
header_server = headers.pop('Server')
|
||||||
|
self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header')
|
||||||
|
|
||||||
|
date = headers.pop('Date')
|
||||||
|
self.assertEqual(date[-4:], ' GMT', 'date header timezone')
|
||||||
|
self.assertLess(abs(self.date_to_sec_epoch(date) - self.sec_epoch()), 5,
|
||||||
|
'date header')
|
||||||
|
|
||||||
|
self.assertDictEqual(headers, {
|
||||||
|
'Content-Length': str(len(body)),
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
'Request-Method': 'POST',
|
||||||
|
'Request-Uri': '/',
|
||||||
|
'Http-Host': 'localhost',
|
||||||
|
'Server-Protocol': 'HTTP/1.1',
|
||||||
|
'Server-Protocol-Major': '1',
|
||||||
|
'Server-Protocol-Minor': '1',
|
||||||
|
'Custom-Header': 'blah'
|
||||||
|
}, 'headers')
|
||||||
|
self.assertEqual(resp['body'], body, 'body')
|
||||||
|
|
||||||
|
def test_go_application_get_variables(self):
|
||||||
|
self.load('get_variables')
|
||||||
|
|
||||||
|
resp = self.get(url='/?var1=val1&var2=&var3')
|
||||||
|
self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables')
|
||||||
|
self.assertEqual(resp['headers']['X-Var-2'], '', 'GET variables 2')
|
||||||
|
self.assertEqual(resp['headers']['X-Var-3'], '', 'GET variables 3')
|
||||||
|
|
||||||
|
def test_go_application_post_variables(self):
|
||||||
|
self.load('post_variables')
|
||||||
|
|
||||||
|
resp = self.post(headers={
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
'Host': 'localhost',
|
||||||
|
'Connection': 'close'
|
||||||
|
}, body='var1=val1&var2=&var3')
|
||||||
|
|
||||||
|
self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables')
|
||||||
|
self.assertEqual(resp['headers']['X-Var-2'], '', 'POST variables 2')
|
||||||
|
self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3')
|
||||||
|
|
||||||
|
def test_go_application_404(self):
|
||||||
|
self.load('404')
|
||||||
|
|
||||||
|
resp = self.get()
|
||||||
|
|
||||||
|
self.assertEqual(resp['status'], 404, '404 status')
|
||||||
|
self.assertRegex(resp['body'], r'<title>404 Not Found</title>',
|
||||||
|
'404 body')
|
||||||
|
|
||||||
|
def test_go_keepalive_body(self):
|
||||||
|
self.load('mirror')
|
||||||
|
|
||||||
|
(resp, sock) = self.post(headers={
|
||||||
|
'Connection': 'keep-alive',
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
'Host': 'localhost'
|
||||||
|
}, start=True, body='0123456789' * 500)
|
||||||
|
|
||||||
|
self.assertEqual(resp['body'], '0123456789' * 500, 'keep-alive 1')
|
||||||
|
|
||||||
|
resp = self.post(headers={
|
||||||
|
'Connection': 'close',
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
'Host': 'localhost'
|
||||||
|
}, sock=sock, body='0123456789')
|
||||||
|
|
||||||
|
self.assertEqual(resp['body'], '0123456789', 'keep-alive 2')
|
||||||
|
|
||||||
|
def test_go_application_cookies(self):
|
||||||
|
self.load('cookies')
|
||||||
|
|
||||||
|
resp = self.get(headers={
|
||||||
|
'Cookie': 'var1=val1; var2=val2',
|
||||||
|
'Host': 'localhost',
|
||||||
|
'Connection': 'close'
|
||||||
|
})
|
||||||
|
|
||||||
|
self.assertEqual(resp['headers']['X-Cookie-1'], 'val1', 'cookie 1')
|
||||||
|
self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie 2')
|
||||||
|
|
||||||
|
def test_go_application_command_line_arguments_type(self):
|
||||||
|
self.load('command_line_arguments')
|
||||||
|
|
||||||
|
self.assertIn('error', self.conf(''"a b c",
|
||||||
|
'/applications/command_line_arguments/arguments'), 'arguments type')
|
||||||
|
|
||||||
|
def test_go_application_command_line_arguments_0(self):
|
||||||
|
self.load('command_line_arguments')
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['headers']['X-Arg-0'],
|
||||||
|
self.conf_get('/applications/command_line_arguments/executable'),
|
||||||
|
'argument 0')
|
||||||
|
|
||||||
|
def test_go_application_command_line_arguments(self):
|
||||||
|
self.load('command_line_arguments')
|
||||||
|
|
||||||
|
arg1 = '--cc=gcc-7.2.0'
|
||||||
|
arg2 = '--cc-opt=\'-O0 -DNXT_DEBUG_MEMORY=1 -fsanitize=address\''
|
||||||
|
arg3 = '--debug'
|
||||||
|
|
||||||
|
self.conf('["' + arg1 + '", "' + arg2 + '", "' + arg3 + '"]',
|
||||||
|
'/applications/command_line_arguments/arguments')
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['body'], arg1 + ',' + arg2 + ',' + arg3,
|
||||||
|
'arguments')
|
||||||
|
|
||||||
|
def test_go_application_command_line_arguments_change(self):
|
||||||
|
self.load('command_line_arguments')
|
||||||
|
|
||||||
|
args_path = '/applications/command_line_arguments/arguments'
|
||||||
|
|
||||||
|
self.conf('["0", "a", "$", ""]', args_path)
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['body'], '0,a,$,', 'arguments')
|
||||||
|
|
||||||
|
self.conf('["-1", "b", "%"]', args_path)
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['body'], '-1,b,%', 'arguments change')
|
||||||
|
|
||||||
|
self.conf('[]', args_path)
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['headers']['Content-Length'], '0',
|
||||||
|
'arguments empty')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
58
test/unit.py
58
test/unit.py
@@ -9,7 +9,7 @@ import select
|
|||||||
import platform
|
import platform
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from subprocess import call
|
import subprocess
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
|
|
||||||
class TestUnit(unittest.TestCase):
|
class TestUnit(unittest.TestCase):
|
||||||
@@ -48,16 +48,34 @@ class TestUnit(unittest.TestCase):
|
|||||||
self.stop()
|
self.stop()
|
||||||
exit("Unit is writing log too long")
|
exit("Unit is writing log too long")
|
||||||
|
|
||||||
self._check_alerts(log)
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
missed_module = ''
|
missed_module = ''
|
||||||
for module in modules:
|
for module in modules:
|
||||||
m = re.search('module: ' + module, log)
|
if module == 'go':
|
||||||
|
env = os.environ.copy()
|
||||||
|
env['GOPATH'] = self.pardir + '/go'
|
||||||
|
|
||||||
|
try:
|
||||||
|
process = subprocess.Popen(['go', 'build', '-o',
|
||||||
|
self.testdir + '/go/check_module',
|
||||||
|
current_dir + '/go/empty/app.go'], env=env)
|
||||||
|
process.communicate()
|
||||||
|
|
||||||
|
m = module if process.returncode == 0 else None
|
||||||
|
|
||||||
|
except:
|
||||||
|
m = None
|
||||||
|
|
||||||
|
else:
|
||||||
|
m = re.search('module: ' + module, log)
|
||||||
|
|
||||||
if m is None:
|
if m is None:
|
||||||
missed_module = module
|
missed_module = module
|
||||||
break
|
break
|
||||||
|
|
||||||
self.stop()
|
self.stop()
|
||||||
|
self._check_alerts(log)
|
||||||
shutil.rmtree(self.testdir)
|
shutil.rmtree(self.testdir)
|
||||||
|
|
||||||
if missed_module:
|
if missed_module:
|
||||||
@@ -75,7 +93,7 @@ class TestUnit(unittest.TestCase):
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
def _run_unit():
|
def _run_unit():
|
||||||
call([self.pardir + '/build/unitd',
|
subprocess.call([self.pardir + '/build/unitd',
|
||||||
'--no-daemon',
|
'--no-daemon',
|
||||||
'--modules', self.pardir + '/build',
|
'--modules', self.pardir + '/build',
|
||||||
'--state', self.testdir + '/state',
|
'--state', self.testdir + '/state',
|
||||||
@@ -99,7 +117,7 @@ class TestUnit(unittest.TestCase):
|
|||||||
with open(self.testdir + '/unit.pid', 'r') as f:
|
with open(self.testdir + '/unit.pid', 'r') as f:
|
||||||
pid = f.read().rstrip()
|
pid = f.read().rstrip()
|
||||||
|
|
||||||
call(['kill', '-s', 'QUIT', pid])
|
subprocess.call(['kill', '-s', 'QUIT', pid])
|
||||||
|
|
||||||
for i in range(50):
|
for i in range(50):
|
||||||
if not os.path.exists(self.testdir + '/unit.pid'):
|
if not os.path.exists(self.testdir + '/unit.pid'):
|
||||||
@@ -401,6 +419,36 @@ class TestUnitApplicationPHP(TestUnitApplicationProto):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
class TestUnitApplicationGo(TestUnitApplicationProto):
|
||||||
|
def load(self, script, name='app'):
|
||||||
|
|
||||||
|
if not os.path.isdir(self.testdir + '/go'):
|
||||||
|
os.mkdir(self.testdir + '/go')
|
||||||
|
|
||||||
|
env = os.environ.copy()
|
||||||
|
env['GOPATH'] = self.pardir + '/go'
|
||||||
|
process = subprocess.Popen(['go', 'build', '-o',
|
||||||
|
self.testdir + '/go/' + name,
|
||||||
|
self.current_dir + '/go/' + script + '/' + name + '.go'],
|
||||||
|
env=env)
|
||||||
|
process.communicate()
|
||||||
|
|
||||||
|
self.conf({
|
||||||
|
"listeners": {
|
||||||
|
"*:7080": {
|
||||||
|
"application": script
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"applications": {
|
||||||
|
script: {
|
||||||
|
"type": "go",
|
||||||
|
"processes": { "spare": 0 },
|
||||||
|
"working_directory": self.current_dir + '/go/' + script,
|
||||||
|
"executable": self.testdir + '/go/' + name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
class TestUnitApplicationPerl(TestUnitApplicationProto):
|
class TestUnitApplicationPerl(TestUnitApplicationProto):
|
||||||
def load(self, script, name='psgi.pl'):
|
def load(self, script, name='psgi.pl'):
|
||||||
self.conf({
|
self.conf({
|
||||||
|
|||||||
Reference in New Issue
Block a user