def testPreStartWithCreate(self): """Verifies that pre-start creates mounted dirs. Checks that missing mount points are created when force_create is enabled. """ with lxc_utils.TempDir() as tmpdir: src_dir = os.path.join(tmpdir, 'foobar') config = [{ 'mount': True, 'source': src_dir, 'target': '/target0', 'readonly': True, 'force_create': True }] with ConfigFile(config) as test_cfg, MockContainer() as container: manager = lxc_config.DeployConfigManager(container, test_cfg) # Pre-condition: the path doesn't exist. self.assertFalse(lxc_utils.path_exists(src_dir)) # After calling deploy_pre_start, the path should exist and the # mount should be created in the container. manager.deploy_pre_start() self.assertTrue(lxc_utils.path_exists(src_dir)) self.assertEqual(len(config), len(container.mounts)) for c in config: self.assertTrue(container.has_mount(c))
def install_ssp(self, ssp_url): """Downloads and installs the given server package. @param ssp_url: The URL of the ssp to download and install. """ # The host dir is mounted directly on /usr/local/autotest within the # container. The SSP structure assumes it gets untarred into the # /usr/local directory of the container's rootfs. In order to unpack # with the correct directory structure, create a tmpdir, mount the # container's host dir as ./autotest, and unpack the SSP. if not self.is_running(): super(Zygote, self).install_ssp(ssp_url) return usr_local_path = os.path.join(self.host_path, 'usr', 'local') os.makedirs(usr_local_path) with lxc_utils.TempDir(dir=usr_local_path) as tmpdir: download_tmp = os.path.join(tmpdir, 'autotest_server_package.tar.bz2') lxc.download_extract(ssp_url, download_tmp, usr_local_path) container_ssp_path = os.path.join( constants.CONTAINER_HOST_DIR, constants.CONTAINER_AUTOTEST_DIR.lstrip(os.path.sep)) self.attach_run('mkdir -p %s && mount --bind %s %s' % (constants.CONTAINER_AUTOTEST_DIR, container_ssp_path, constants.CONTAINER_AUTOTEST_DIR))
def testPreStart(self): """Verifies that pre-start works correctly. Checks that mounts are correctly created in the container. """ with lxc_utils.TempDir() as tmpdir: config = [ { 'mount': True, 'source': tempfile.mkdtemp(dir=tmpdir), 'target': '/target0', 'readonly': True, 'force_create': False }, { 'mount': True, 'source': tempfile.mkdtemp(dir=tmpdir), 'target': '/target1', 'readonly': False, 'force_create': False }, ] with ConfigFile(config) as test_cfg, MockContainer() as container: manager = lxc_config.DeployConfigManager(container, test_cfg) manager.deploy_pre_start() self.assertEqual(len(config), len(container.mounts)) for c in config: self.assertTrue(container.has_mount(c))
def testMountDirectoryRelativePath(self): """Verifies that relative-path mounts work.""" with lxc_utils.TempDir() as tmpdir, self.createZygote() as zygote: dst = 'testMountDirectoryRelativePath/testMount' zygote.start(wait_for_network=False) zygote.mount_dir(tmpdir, dst, readonly=True) # Verify that the mount points is correctly bound.. self.verifyBindMount(zygote, dst, tmpdir)
def testMountDirectory(self): """Verifies that read-write mounts work.""" with lxc_utils.TempDir() as tmpdir, self.createZygote() as zygote: dst = '/testMountDirectory/testMount' zygote.start(wait_for_network=False) zygote.mount_dir(tmpdir, dst, readonly=False) # Verify that the mount point is correctly bound, and is read-write. self.verifyBindMount(zygote, dst, tmpdir) zygote.attach_run('test -r {0} -a -w {0}'.format(dst))
def testMountDirectoryReadOnly(self): """Verifies that read-only mounts work.""" with lxc_utils.TempDir() as tmpdir, self.createContainer() as container: dst = '/testMountDirectoryReadOnly/testMount' container.mount_dir(tmpdir, dst, readonly=True) container.start(wait_for_network=False) # Verify that the mount point is correctly bound, and is read-only. self.verifyBindMount(container, dst, tmpdir) container.attach_run('test -r %s -a ! -w %s' % (dst, dst))
def testMountDirectoryReadOnly(self): """Verifies that read-only mounts are mounted, and read-only.""" with lxc_utils.TempDir() as tmpdir, self.createZygote() as zygote: dst = '/testMountDirectoryReadOnly/testMount' zygote.start(wait_for_network=False) zygote.mount_dir(tmpdir, dst, readonly=True) # Verify that the mount point is correctly bound, and is read-only. self.verifyBindMount(zygote, dst, tmpdir) try: zygote.attach_run('test -r {0} -a ! -w {0}'.format(dst)) except error.CmdError: self.fail('Bind mount is not read-only')
def testDetectExistingMounts(self): """Verifies that host mounts are properly reconstructed. When a Zygote is instantiated on top of an already-running container, any previously-created bind mounts have to be detected. This enables proper cleanup later. """ with lxc_utils.TempDir() as tmpdir, self.createZygote() as zygote0: zygote0.start(wait_for_network=False) # Create a bind mounted directory. zygote0.mount_dir(tmpdir, 'foo') # Create another zygote on top of the existing container. zygote1 = lxc.Zygote(container_path=zygote0.container_path, name=zygote0.name, attribute_values={}) # Verify that the new zygote contains the same bind mounts. self.assertEqual(zygote0.mounts, zygote1.mounts)
def testCopyDirectory(self): """Verifies that directories are correctly copied into the container.""" control_string = 'pack my box with five dozen liquor jugs' with lxc_utils.TempDir() as tmpdir: fd, tmpfile = tempfile.mkstemp(dir=tmpdir) f = os.fdopen(fd, 'w') f.write(control_string) f.close() with self.createContainer() as container: dst = os.path.join(constants.CONTAINER_AUTOTEST_DIR, os.path.basename(tmpdir)) container.copy(tmpdir, dst) container.start(wait_for_network=False) # Verify the file content. test_file = os.path.join(dst, os.path.basename(tmpfile)) test_string = container.attach_run('cat %s' % test_file).stdout self.assertEquals(control_string, test_string)