def test_notifies_queues(self): cg = self._launch_container(EXITS_WITH_TERM_1) job_q = queue.Queue() activity_q = queue.Queue() restart(10, cg, job_q, activity_q) self.assertHasMessageForCg(job_q, RestartCompleteMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartCgroupMessage, cg.path) self.assertHasNoMessages(activity_q)
def test_restart_wipes_fs(self): q = queue.Queue() cg = self._launch_container(EXITS_IF_FILE) time.sleep(2) restart(docker_wipe_fs, 1, cg, q, q) time.sleep(2) self.assertTrue(docker_json(cg)["State"]["Running"])
def test_restart_kills_processes(self): q = queue.Queue() cg = self._launch_container(NEVER_EXITS) time.sleep(2) restart(null, 1, cg, q, q) time.sleep(2) self.assertFalse(docker_json(cg)["State"]["Running"])
def test_notifies_queues_timeout(self): cg = self._launch_container(NEVER_EXITS) job_q = queue.Queue() activity_q = queue.Queue() restart(3, cg, job_q, activity_q) self.assertHasMessageForCg(job_q, RestartCompleteMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartCgroupMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartTimeoutMessage, cg.path, grace_period=3)
def test_restarts_with_ports(self): host_port = random_free_port() options = ["-p", "{0}:80".format(host_port)] + WELL_BEHAVED cg = self._launch_container(options) restart(queue.Queue(), 10, cg) binding = docker_json(cg)["NetworkSettings"]["Ports"]["80/tcp"][0] port = int(binding["HostPort"]) self.assertEqual(host_port, port)
def test_notifies_queues(self): cg = self._launch_container(EXITS_WITH_TERM_1) self._wait_pids(cg, 2) job_q = queue.Queue() activity_q = queue.Queue() restart(docker, 10, cg, job_q, activity_q) self.assertHasMessageForCg(job_q, RestartCompleteMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartCgroupMessage, cg.path) self.assertHasNoMessages(activity_q)
def test_restarts_with_ports(self): host_port = random_free_port() options = ["-p", "{0}:80".format(host_port)] + EXITS_WITH_TERM_1 cg = self._launch_container(options) q = queue.Queue() restart(docker, 10, cg, q, q) binding = docker_json(cg)["NetworkSettings"]["Ports"]["80/tcp"][0] port = int(binding["HostPort"]) self.assertEqual(host_port, port)
def test_notifies_queues_timeout(self): cg = self._launch_container(NEVER_EXITS) self._wait_pids(cg, 1) job_q = queue.Queue() activity_q = queue.Queue() restart(docker, 3, cg, job_q, activity_q) self.assertHasMessageForCg(job_q, RestartCompleteMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartCgroupMessage, cg.path) self.assertHasMessageForCg(activity_q, RestartTimeoutMessage, cg.path, grace_period=3)
def test_restarts_misbehaved_container(self): cg = self._launch_container(MIS_BEHAVED) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() restart(queue.Queue(), 3, cg) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertGreater(time_after - time_before, 2)
def test_restarts_well_behaved_container(self): cg = self._launch_container(WELL_BEHAVED) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() restart(queue.Queue(), 10, cg) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertLess(time_after - time_before, 5)
def test_restart_container_with_term_all(self): cg = self._launch_container(EXITS_WITH_TERM_ALL) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() q = queue.Queue() restart(10, cg, q, q) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertLess(time_after - time_before, 5)
def test_restarts_misbehaved_container(self): cg = self._launch_container(NEVER_EXITS) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() q = queue.Queue() restart(3, cg, q, q) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertGreater(time_after - time_before, 2)
def test_restarts_misbehaved_container(self): cg = self._launch_container(NEVER_EXITS) self._wait_pids(cg, 1) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() q = queue.Queue() restart(docker, 3, cg, q, q) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertGreater(time_after - time_before, 2)
def restart_one(root_cg, grace_period, container_id): q = queue.Queue() cg = Cgroup(os.path.join(root_cg, container_id)) try: restart(grace_period, cg, q, q) except IOError: logger.error("%s: container does not exist", cg.name()) return 1 finally: while not q.empty(): m = q.get() logger.debug("%s: received %s", cg.name(), m.__class__.__name__) return 0
def test_restart_container_with_term_all(self): cg = self._launch_container(EXITS_WITH_TERM_ALL) self._wait_pids(cg, 2) pid_before = docker_json(cg)["State"]["Pid"] time_before = time.time() q = queue.Queue() restart(docker, 10, cg, q, q) time_after = time.time() pid_after = docker_json(cg)["State"]["Pid"] self.assertNotEqual(pid_before, pid_after) self.assertLess(time_after - time_before, 5)
def restart_one(root_cg_path, restart_adapter, restart_grace_period, container_id): q = queue.Queue() cg = Cgroup(os.path.join(root_cg_path, container_id)) try: restart(restart_adapter, restart_grace_period, cg, q, q) except IOError: logger.error("%s: container does not exist", cg.name()) return 1 finally: while not q.empty(): m = q.get() logger.debug("%s: received %s", cg.name(), m.__class__.__name__) return 0
def test_restart_with_memory_limit(self): options = ["--memory", "10mb"] + WELL_BEHAVED cg = self._launch_container(options) restart(queue.Queue(), 10, cg)
def test_restart_with_memory_limit(self): options = ["--memory", "10mb"] + EXITS_WITH_TERM_1 cg = self._launch_container(options) q = queue.Queue() restart(docker, 10, cg, q, q)