Tests: Ruby hooks.
This commit is contained in:
7
test/ruby/hooks/config.ru
Normal file
7
test/ruby/hooks/config.ru
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
app = Proc.new do |env|
|
||||||
|
['200', {
|
||||||
|
'Content-Length' => '0'
|
||||||
|
}, ['']]
|
||||||
|
end
|
||||||
|
|
||||||
|
run app
|
||||||
3
test/ruby/hooks/eval.rb
Normal file
3
test/ruby/hooks/eval.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
File.write("./cookie_eval.#{SecureRandom.hex}", "evaluated")
|
||||||
9
test/ruby/hooks/multiple.rb
Normal file
9
test/ruby/hooks/multiple.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
on_worker_boot do
|
||||||
|
File.write("./cookie_worker_boot.#{SecureRandom.hex}", "worker booted")
|
||||||
|
end
|
||||||
|
|
||||||
|
on_thread_boot do
|
||||||
|
File.write("./cookie_thread_boot.#{SecureRandom.hex}", "thread booted")
|
||||||
|
end
|
||||||
5
test/ruby/hooks/on_thread_boot.rb
Normal file
5
test/ruby/hooks/on_thread_boot.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
on_thread_boot do
|
||||||
|
File.write("./cookie_thread_boot.#{SecureRandom.hex}", "booted")
|
||||||
|
end
|
||||||
5
test/ruby/hooks/on_thread_shutdown.rb
Normal file
5
test/ruby/hooks/on_thread_shutdown.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
on_thread_shutdown do
|
||||||
|
File.write("./cookie_thread_shutdown.#{SecureRandom.hex}", "shutdown")
|
||||||
|
end
|
||||||
5
test/ruby/hooks/on_worker_boot.rb
Normal file
5
test/ruby/hooks/on_worker_boot.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
on_worker_boot do
|
||||||
|
File.write("./cookie_worker_boot.#{SecureRandom.hex}", "booted")
|
||||||
|
end
|
||||||
5
test/ruby/hooks/on_worker_shutdown.rb
Normal file
5
test/ruby/hooks/on_worker_shutdown.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require 'securerandom'
|
||||||
|
|
||||||
|
on_worker_shutdown do
|
||||||
|
File.write("./cookie_worker_shutdown.#{SecureRandom.hex}", "shutdown")
|
||||||
|
end
|
||||||
98
test/test_ruby_hooks.py
Normal file
98
test/test_ruby_hooks.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import os
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from conftest import unit_stop
|
||||||
|
from unit.applications.lang.ruby import TestApplicationRuby
|
||||||
|
from unit.option import option
|
||||||
|
from unit.utils import waitforglob
|
||||||
|
|
||||||
|
|
||||||
|
class TestRubyHooks(TestApplicationRuby):
|
||||||
|
prerequisites = {'modules': {'ruby': 'all'}}
|
||||||
|
|
||||||
|
def _wait_cookie(self, pattern, count):
|
||||||
|
return waitforglob(
|
||||||
|
option.temp_dir + '/ruby/hooks/cookie_' + pattern, count
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_ruby_hooks_eval(self):
|
||||||
|
processes = 2
|
||||||
|
|
||||||
|
self.load('hooks', processes=processes, hooks='eval.rb')
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('eval.*', processes)
|
||||||
|
|
||||||
|
assert hooked, 'hooks evaluated'
|
||||||
|
|
||||||
|
def test_ruby_hooks_on_worker_boot(self):
|
||||||
|
processes = 2
|
||||||
|
|
||||||
|
self.load('hooks', processes=processes, hooks='on_worker_boot.rb')
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('worker_boot.*', processes)
|
||||||
|
|
||||||
|
assert hooked, 'on_worker_boot called'
|
||||||
|
|
||||||
|
def test_ruby_hooks_on_worker_shutdown(self):
|
||||||
|
processes = 2
|
||||||
|
|
||||||
|
self.load('hooks', processes=processes, hooks='on_worker_shutdown.rb')
|
||||||
|
|
||||||
|
assert self.get()['status'] == 200, 'app response'
|
||||||
|
|
||||||
|
self.load('empty')
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('worker_shutdown.*', processes)
|
||||||
|
|
||||||
|
assert hooked, 'on_worker_shutdown called'
|
||||||
|
|
||||||
|
def test_ruby_hooks_on_thread_boot(self):
|
||||||
|
processes = 1
|
||||||
|
threads = 2
|
||||||
|
|
||||||
|
self.load(
|
||||||
|
'hooks',
|
||||||
|
processes=processes,
|
||||||
|
threads=threads,
|
||||||
|
hooks='on_thread_boot.rb',
|
||||||
|
)
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('thread_boot.*', processes * threads)
|
||||||
|
|
||||||
|
assert hooked, 'on_thread_boot called'
|
||||||
|
|
||||||
|
def test_ruby_hooks_on_thread_shutdown(self):
|
||||||
|
processes = 1
|
||||||
|
threads = 2
|
||||||
|
|
||||||
|
self.load(
|
||||||
|
'hooks',
|
||||||
|
processes=processes,
|
||||||
|
threads=threads,
|
||||||
|
hooks='on_thread_shutdown.rb',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert self.get()['status'] == 200, 'app response'
|
||||||
|
|
||||||
|
self.load('empty')
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('thread_shutdown.*', processes * threads)
|
||||||
|
|
||||||
|
assert hooked, 'on_thread_shutdown called'
|
||||||
|
|
||||||
|
def test_ruby_hooks_multiple(self):
|
||||||
|
processes = 1
|
||||||
|
threads = 1
|
||||||
|
|
||||||
|
self.load(
|
||||||
|
'hooks', processes=processes, threads=threads, hooks='multiple.rb',
|
||||||
|
)
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('worker_boot.*', processes)
|
||||||
|
assert hooked, 'on_worker_boot called'
|
||||||
|
|
||||||
|
hooked = self._wait_cookie('thread_boot.*', threads)
|
||||||
|
assert hooked, 'on_thread_boot called'
|
||||||
@@ -12,7 +12,7 @@ class TestApplicationRuby(TestApplicationProto):
|
|||||||
def prepare_env(self, script):
|
def prepare_env(self, script):
|
||||||
shutil.copytree(
|
shutil.copytree(
|
||||||
option.test_dir + '/ruby/' + script,
|
option.test_dir + '/ruby/' + script,
|
||||||
option.temp_dir + '/ruby/' + script
|
option.temp_dir + '/ruby/' + script,
|
||||||
)
|
)
|
||||||
|
|
||||||
public_dir(option.temp_dir + '/ruby/' + script)
|
public_dir(option.temp_dir + '/ruby/' + script)
|
||||||
@@ -22,17 +22,23 @@ class TestApplicationRuby(TestApplicationProto):
|
|||||||
|
|
||||||
script_path = option.temp_dir + '/ruby/' + script
|
script_path = option.temp_dir + '/ruby/' + script
|
||||||
|
|
||||||
|
app = {
|
||||||
|
"type": self.get_application_type(),
|
||||||
|
"processes": {"spare": 0},
|
||||||
|
"working_directory": script_path,
|
||||||
|
"script": script_path + '/' + name,
|
||||||
|
}
|
||||||
|
|
||||||
|
for key in [
|
||||||
|
'hooks',
|
||||||
|
]:
|
||||||
|
if key in kwargs:
|
||||||
|
app[key] = kwargs[key]
|
||||||
|
|
||||||
self._load_conf(
|
self._load_conf(
|
||||||
{
|
{
|
||||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||||
"applications": {
|
"applications": {script: app},
|
||||||
script: {
|
|
||||||
"type": self.get_application_type(),
|
|
||||||
"processes": {"spare": 0},
|
|
||||||
"working_directory": script_path,
|
|
||||||
"script": script_path + '/' + name,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -47,13 +47,15 @@ class TestApplicationProto(TestControl):
|
|||||||
if 'applications' in conf:
|
if 'applications' in conf:
|
||||||
for app in conf['applications'].keys():
|
for app in conf['applications'].keys():
|
||||||
app_conf = conf['applications'][app]
|
app_conf = conf['applications'][app]
|
||||||
if 'user' in kwargs:
|
|
||||||
app_conf['user'] = kwargs['user']
|
|
||||||
|
|
||||||
if 'group' in kwargs:
|
for key in [
|
||||||
app_conf['group'] = kwargs['group']
|
'user',
|
||||||
|
'group',
|
||||||
if 'isolation' in kwargs:
|
'isolation',
|
||||||
app_conf['isolation'] = kwargs['isolation']
|
'processes',
|
||||||
|
'threads',
|
||||||
|
]:
|
||||||
|
if key in kwargs:
|
||||||
|
app_conf[key] = kwargs[key]
|
||||||
|
|
||||||
assert 'success' in self.conf(conf), 'load application configuration'
|
assert 'success' in self.conf(conf), 'load application configuration'
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import glob
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -16,8 +17,8 @@ def public_dir(path):
|
|||||||
os.chmod(os.path.join(root, f), 0o777)
|
os.chmod(os.path.join(root, f), 0o777)
|
||||||
|
|
||||||
|
|
||||||
def waitforfiles(*files):
|
def waitforfiles(*files, timeout=50):
|
||||||
for i in range(50):
|
for i in range(timeout):
|
||||||
wait = False
|
wait = False
|
||||||
|
|
||||||
for f in files:
|
for f in files:
|
||||||
@@ -33,6 +34,21 @@ def waitforfiles(*files):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def waitforglob(pattern, count=1, timeout=50):
|
||||||
|
for i in range(timeout):
|
||||||
|
n = 0
|
||||||
|
|
||||||
|
for f in glob.glob(pattern):
|
||||||
|
n += 1
|
||||||
|
|
||||||
|
if n == count:
|
||||||
|
return True
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def waitforsocket(port):
|
def waitforsocket(port):
|
||||||
for i in range(50):
|
for i in range(50):
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||||
@@ -72,8 +88,8 @@ def sysctl():
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def waitformount(template, wait=50):
|
def waitformount(template, timeout=50):
|
||||||
for i in range(wait):
|
for i in range(timeout):
|
||||||
if findmnt().find(template) != -1:
|
if findmnt().find(template) != -1:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -82,8 +98,8 @@ def waitformount(template, wait=50):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def waitforunmount(template, wait=50):
|
def waitforunmount(template, timeout=50):
|
||||||
for i in range(wait):
|
for i in range(timeout):
|
||||||
if findmnt().find(template) == -1:
|
if findmnt().find(template) == -1:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user