Commit 4ff78798 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

some refactoring

parent 91e67bbe
......@@ -32,6 +32,34 @@ log = logging.getLogger("test_controller")
BAD_SANDBOX_NAMES = "", ".", "..", "foo/bar", "../blah", "/root", "foo/"
def wait_until(func, *, period=.05, timeout=1, msg=None):
limit = time.time() + timeout
while time.time() < limit:
if func():
return
time.sleep(period)
txt = "timeout expired"
if msg:
txt += " (%s)" % msg
raise AssertionError(txt)
@contextmanager
def preamble():
try:
yield
except Exception as e:
log.exception("test preamble failed")
raise
@contextmanager
def part(descr):
try:
yield
except Exception as e:
log.exception("test part %r failed", descr)
raise
@contextmanager
def lock_loop(loop, *, timeout=1):
cond = threading.Condition()
......@@ -192,36 +220,34 @@ class ControllerTestCase(unittest.TestCase):
@contextmanager
def check_sandbox_transition(self, app, prev_state, next_state, *, timeout=10, msg=None):
self.session.refresh(app)
self.assertEqual(S(app.sandbox_state), prev_state, msg=msg)
def check_sandbox_transition(self, app, prev_state, next_state, *, timeout=10):
def get_state(app):
self.session.refresh(app)
return S(app.sandbox_state)
self.assertEqual(get_state(app), prev_state)
yield
limit = time.time() + timeout
while time.time() < limit:
self.session.refresh(app)
if app.sandbox_state != prev_state:
self.assertEqual(S(app.sandbox_state), next_state, msg=msg)
return
time.sleep(.1)
self.fail("timeout expired (expected state change to %r)" % next_state)
wait_until(lambda: get_state(app) != prev_state,
timeout=timeout, msg = "expected state change to %r" % next_state)
self.assertEqual(get_state(app), next_state)
@contextmanager
def check_version_transition(self, ver, prev_state, next_state, *, ignore_state=_UNSET, timeout=10, msg=None):
self.session.refresh(ver)
cur_state = V(ver.state)
self.assertEqual(cur_state, prev_state, msg=msg)
def check_version_transition(self, ver, prev_state, next_state, *, ignore_state=_UNSET, timeout=10):
def get_state(ver):
self.session.refresh(ver)
return V(ver.state)
self.assertEqual(get_state(ver), prev_state)
yield
limit = time.time() + timeout
while time.time() < limit:
self.session.refresh(ver)
cur_state = ver.state
if cur_state not in (prev_state, ignore_state):
self.assertEqual(cur_state, next_state, msg=msg)
return
time.sleep(.1)
self.fail("timeout expired (expected state change to %r)" % next_state)
wait_until(lambda: get_state(ver) not in (prev_state, ignore_state),
timeout=timeout, msg = "expected state change to %r)" % next_state)
self.assertEqual(get_state(ver), next_state)
def check_sandbox_running(self, dct):
for app, image in dct.items():
......@@ -359,15 +385,16 @@ class ControllerTestCase(unittest.TestCase):
def test_sandbox_start_bad_parameters(self, ses, app):
# bad docker_name
for docker_name in BAD_SANDBOX_NAMES:
with ses.begin():
w = Webapp(docker_name=docker_name, sandbox_state = S.starting, docker_os=app.docker_os)
ses.add(w)
with part(docker_name):
with ses.begin():
w = Webapp(docker_name=docker_name, sandbox_state = S.starting, docker_os=app.docker_os)
ses.add(w)
with self.check_log("error", "malformatted docker_name"), \
self.check_sandbox_transition(w, S.starting, S.start_error, msg=repr(w.docker_name)):
self.notify()
with self.check_log("error", "malformatted docker_name"), \
self.check_sandbox_transition(w, S.starting, S.start_error):
self.notify()
self.check_sandbox_running({w: None})
self.check_sandbox_running({w: None})
# bad id
with ses.begin():
......@@ -431,8 +458,9 @@ class ControllerTestCase(unittest.TestCase):
@with_db
def test_sandbox_commit(self, ses, app):
self.start_sandbox(app)
self.check_sandbox_running({app: "factory/test-busybox:latest"})
with preamble():
self.start_sandbox(app)
self.check_sandbox_running({app: "factory/test-busybox:latest"})
self.commit_sandbox(app, "1.0", stop=True)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment