def test_lxc_container(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) customize_log = self.makeFile() c = LXCContainer(DEFAULT_CONTAINER, "dsa...", "precise", "ppa", customize_log=customize_log) running = yield c.is_running() self.assertFalse(running) self.assertFalse(c.is_constructed()) # verify we can't run a non-constructed container failure = c.run() yield self.assertFailure(failure, LXCError) yield c.create() self.assertFalse(running) self.assertTrue(c.is_constructed()) yield c.run() running = yield c.is_running() self.assertTrue(running) self.assertTrue(c.is_constructed()) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) # Verify we have a path into the container self.assertTrue(os.path.exists(c.rootfs)) self.assertTrue(c.is_constructed()) self.verify_container(c, "dsa...", "precise", "ppa") # Verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[DEFAULT_CONTAINER], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(DEFAULT_CONTAINER, containers) # Verify the customize log file. self.assertTrue(os.path.exists(customize_log)) # and its gone output = _lxc_ls() self.assertNotIn(DEFAULT_CONTAINER, output)
def test_lxc_container(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) customize_log = self.makeFile() c = LXCContainer( DEFAULT_CONTAINER, "dsa...", "ppa", customize_log=customize_log) running = yield c.is_running() self.assertFalse(running) self.assertFalse(c.is_constructed()) # verify we can't run a non-constructed container failure = c.run() yield self.assertFailure(failure, LXCError) yield c.create() self.assertFalse(running) self.assertTrue(c.is_constructed()) yield c.run() running = yield c.is_running() self.assertTrue(running) self.assertTrue(c.is_constructed()) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) # verify we have a path into the container self.assertTrue(os.path.exists(c.rootfs)) self.assertTrue(c.is_constructed()) self.verify_container(c, "dsa...", "ppa") # verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[DEFAULT_CONTAINER], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(DEFAULT_CONTAINER, containers) # Verify the customize log file. self.assertTrue(os.path.exists(customize_log)) # and its gone output = _lxc_ls() self.assertNotIn(DEFAULT_CONTAINER, output)
def test_container_clone(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) self.addCleanup(self.clean_container, DEFAULT_CONTAINER + "_child") master_container = LXCContainer(DEFAULT_CONTAINER, origin="ppa", public_key="dsa...", series="oneiric") # verify that we cannot clone an unconstructed container failure = master_container.clone("test_lxc_fail") yield self.assertFailure(failure, LXCError) yield master_container.create() # Clone a child container from the template child_name = DEFAULT_CONTAINER + "_child" c = yield master_container.clone(child_name) self.assertEqual(c.container_name, child_name) running = yield c.is_running() self.assertFalse(running) yield c.run() running = yield c.is_running() self.assertTrue(running) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) self.verify_container(c, "dsa...", "oneiric", "ppa") # verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[child_name], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(child_name, containers) # and its gone output = _lxc_ls() self.assertNotIn(child_name, output) yield master_container.destroy()
def test_container_clone(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) self.addCleanup(self.clean_container, DEFAULT_CONTAINER + "_child") master_container = LXCContainer(DEFAULT_CONTAINER, origin="ppa", public_key="dsa...") # verify that we cannot clone an unconstructed container failure = master_container.clone("test_lxc_fail") yield self.assertFailure(failure, LXCError) yield master_container.create() # Clone a child container from the template child_name = DEFAULT_CONTAINER + "_child" c = yield master_container.clone(child_name) self.assertEqual(c.container_name, child_name) running = yield c.is_running() self.assertFalse(running) yield c.run() running = yield c.is_running() self.assertTrue(running) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) self.verify_container(c, "dsa...", "ppa") # verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[child_name], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(child_name, containers) # and its gone output = _lxc_ls() self.assertNotIn(child_name, output) yield master_container.destroy()
def _destroy_containers(self): container_map = yield get_containers(self._qualified_name) for container_name in container_map: container = LXCContainer(container_name, None, None, None) if container_map[container.container_name]: yield container.stop() yield container.destroy()
def is_running(self): """Is the unit container running?""" # TODO: container running may not imply agent running. # query zookeeper for the unit agent presence node? if not self.container: returnValue(False) container_map = yield get_containers( prefix=self.container.container_name) returnValue(container_map.get(self.container.container_name, False))
def test_get_containers_with_prefix(self): lxc_ls_mock = self.mocker.mock() self.patch(lxc, "_cmd", lxc_ls_mock) lxc_ls_mock(["lxc-ls"]) self.mocker.result((0, lxc_output_sample)) self.mocker.replay() container_map = yield get_containers("ca") self.assertEqual( dict(calendarserver=True, caprica=False), container_map)
def test_get_containers(self): lxc_ls_mock = self.mocker.mock() self.patch(lxc, "_cmd", lxc_ls_mock) lxc_ls_mock(["lxc-ls"]) self.mocker.result((0, lxc_output_sample)) self.mocker.replay() container_map = yield get_containers(None) self.assertEqual( dict(caprica=False, gemini=False, reconnoiter=False, virgo=False, calendarserver=True), container_map)
def is_running(self): """Is the unit container running. """ # TODO: container running may not imply agent running. the # pid file has the pid from the container, we need a container # pid -> host pid mapping to query status from the machine agent. # alternatively querying zookeeper for the unit agent presence # node. if not self.container: returnValue(False) container_map = yield get_containers( prefix=self.container.container_name) returnValue(container_map.get(self.container.container_name, False))