Commit 24693b5b authored by Samuel GAIST's avatar Samuel GAIST

[test][docker] Code cleanup

parent ff19fd18
......@@ -37,14 +37,10 @@
"""Asynchronous process I/O with the Subprocess module"""
import os
import sys
import time
import tempfile
import unittest
import pkg_resources
import time
import requests
import nose
from tempfile import TemporaryDirectory
......@@ -73,7 +69,7 @@ class NoDiscoveryTests(unittest.TestCase):
def tearDown(self):
self.host.teardown()
assert not self.host.containers # All containers are gone
self.assertFalse(self.host.containers) # All containers are gone
class NetworkTest(NoDiscoveryTests):
......@@ -87,7 +83,9 @@ class NetworkTest(NoDiscoveryTests):
try:
self.host.start(container)
status = self.host.wait(container)
except Exception as e:
except Exception:
from . import network
network.remove()
raise
......@@ -261,7 +259,7 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
def tearDown(self):
self.host.teardown()
assert not self.host.containers # All containers are gone
self.assertFalse(self.host.containers) # All containers are gone
@slow
def test_memory_limit(self):
......@@ -291,7 +289,7 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
time.sleep(2)
stats = self.host.statistics(container)
self.host.statistics(container)
status = self.host.wait(container)
......@@ -325,12 +323,14 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
status = self.host.wait(container)
assert stats["memory"]["percent"] > 10, (
"Memory check failed, " "%d%% <= 10%%" % stats["memory"]["percent"]
self.assertTrue(
stats["memory"]["percent"] > 10,
("Memory check failed, " "%d%% <= 10%%" % stats["memory"]["percent"]),
)
assert stats["memory"]["percent"] < 20, (
"Memory check failed, " "%d%% >= 15%%" % stats["memory"]["percent"]
self.assertTrue(
stats["memory"]["percent"] < 20,
("Memory check failed, " "%d%% >= 15%%" % stats["memory"]["percent"]),
)
self.assertEqual(self.host.status(container), "exited")
......@@ -339,14 +339,15 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
def _run_cpulimit(self, processes, max_cpu_percent, sleep_time):
tmp_folder = tempfile.gettempdir()
program = pkg_resources.resource_filename(__name__, "cpu_stress.py")
dst_name = os.path.join("/tmp", os.path.basename(program))
dst_name = os.path.join(tmp_folder, os.path.basename(program))
container = self.host.create_container(
self.test_environment, ["python", dst_name, str(processes)]
)
container.add_volume(program, os.path.join("/tmp", "cpu_stress.py"))
container.add_volume(program, os.path.join(tmp_folder, "cpu_stress.py"))
self.host.start(container, max_cpu_percent=max_cpu_percent)
......@@ -357,9 +358,12 @@ class AsyncWithEnvironmentTest(unittest.TestCase):
self.assertEqual(self.host.status(container), "running")
percent = stats["cpu"]["percent"]
assert percent < (1.1 * max_cpu_percent), (
"%.2f%% is more than 20%% off the expected ceiling at %d%%!"
% (percent, max_cpu_percent)
self.assertTrue(
percent < (1.1 * max_cpu_percent),
(
"%.2f%% is more than 20%% off the expected ceiling at %d%%!"
% (percent, max_cpu_percent)
),
)
# make sure nothing is there anymore
......
Markdown is supported
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