summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorAndrey Zelenkov <zelenkov@nginx.com>2018-06-08 18:32:55 +0300
committerAndrey Zelenkov <zelenkov@nginx.com>2018-06-08 18:32:55 +0300
commitf2a856aa3b0ea0e0d892a9a3865c54de83c71a74 (patch)
treebcc68a2f5790bb6c46ec1fa56bac2bee9ba00114 /test
parent2b39ed5dfb56fa01e38ee8540973f4eb42a1ff82 (diff)
downloadunit-f2a856aa3b0ea0e0d892a9a3865c54de83c71a74.tar.gz
unit-f2a856aa3b0ea0e0d892a9a3865c54de83c71a74.tar.bz2
Tests: Go application tests.
Diffstat (limited to '')
-rw-r--r--test/go/404/404.html6
-rw-r--r--test/go/404/app.go22
-rw-r--r--test/go/command_line_arguments/app.go23
-rw-r--r--test/go/cookies/app.go19
-rw-r--r--test/go/empty/app.go13
-rw-r--r--test/go/get_variables/app.go17
-rw-r--r--test/go/mirror/app.go21
-rw-r--r--test/go/post_variables/app.go19
-rw-r--r--test/go/variables/app.go30
-rw-r--r--test/test_go_application.py154
-rw-r--r--test/unit.py58
11 files changed, 377 insertions, 5 deletions
diff --git a/test/go/404/404.html b/test/go/404/404.html
new file mode 100644
index 00000000..6d0c635a
--- /dev/null
+++ b/test/go/404/404.html
@@ -0,0 +1,6 @@
+<html>
+<head><title>404 Not Found</title></head>
+<body bgcolor="white">
+<center><h1>404 Not Found</h1></center>
+</body>
+</html>
diff --git a/test/go/404/app.go b/test/go/404/app.go
new file mode 100644
index 00000000..abb33066
--- /dev/null
+++ b/test/go/404/app.go
@@ -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)
+}
diff --git a/test/go/command_line_arguments/app.go b/test/go/command_line_arguments/app.go
new file mode 100644
index 00000000..228e07c0
--- /dev/null
+++ b/test/go/command_line_arguments/app.go
@@ -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)
+}
diff --git a/test/go/cookies/app.go b/test/go/cookies/app.go
new file mode 100644
index 00000000..6fb9def0
--- /dev/null
+++ b/test/go/cookies/app.go
@@ -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)
+}
diff --git a/test/go/empty/app.go b/test/go/empty/app.go
new file mode 100644
index 00000000..2e07405f
--- /dev/null
+++ b/test/go/empty/app.go
@@ -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)
+}
diff --git a/test/go/get_variables/app.go b/test/go/get_variables/app.go
new file mode 100644
index 00000000..563febc8
--- /dev/null
+++ b/test/go/get_variables/app.go
@@ -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)
+}
diff --git a/test/go/mirror/app.go b/test/go/mirror/app.go
new file mode 100644
index 00000000..82b1c92d
--- /dev/null
+++ b/test/go/mirror/app.go
@@ -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)
+}
diff --git a/test/go/post_variables/app.go b/test/go/post_variables/app.go
new file mode 100644
index 00000000..433afc62
--- /dev/null
+++ b/test/go/post_variables/app.go
@@ -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)
+}
diff --git a/test/go/variables/app.go b/test/go/variables/app.go
new file mode 100644
index 00000000..5db4ac67
--- /dev/null
+++ b/test/go/variables/app.go
@@ -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)
+}
diff --git a/test/test_go_application.py b/test/test_go_application.py
new file mode 100644
index 00000000..abc2c4c1
--- /dev/null
+++ b/test/test_go_application.py
@@ -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()
diff --git a/test/unit.py b/test/unit.py
index 06ee2d76..f9716366 100644
--- a/test/unit.py
+++ b/test/unit.py
@@ -9,7 +9,7 @@ import select
import platform
import tempfile
import unittest
-from subprocess import call
+import subprocess
from multiprocessing import Process
class TestUnit(unittest.TestCase):
@@ -48,16 +48,34 @@ class TestUnit(unittest.TestCase):
self.stop()
exit("Unit is writing log too long")
- self._check_alerts(log)
+ current_dir = os.path.dirname(os.path.abspath(__file__))
missed_module = ''
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:
missed_module = module
break
self.stop()
+ self._check_alerts(log)
shutil.rmtree(self.testdir)
if missed_module:
@@ -75,7 +93,7 @@ class TestUnit(unittest.TestCase):
print()
def _run_unit():
- call([self.pardir + '/build/unitd',
+ subprocess.call([self.pardir + '/build/unitd',
'--no-daemon',
'--modules', self.pardir + '/build',
'--state', self.testdir + '/state',
@@ -99,7 +117,7 @@ class TestUnit(unittest.TestCase):
with open(self.testdir + '/unit.pid', 'r') as f:
pid = f.read().rstrip()
- call(['kill', '-s', 'QUIT', pid])
+ subprocess.call(['kill', '-s', 'QUIT', pid])
for i in range(50):
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):
def load(self, script, name='psgi.pl'):
self.conf({