def testOutsideChrootOutbound(self, _): """Tests FromChroot() calls from outside the chroot.""" self.PatchObject(os, 'getcwd', return_value=self.FakeCwd(FAKE_SOURCE_PATH)) self.SetChrootPath(constants.SOURCE_ROOT) resolver = path_util.ChrootPathResolver() # Case: source root path. self.assertEqual( os.path.join(constants.SOURCE_ROOT, 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'))) # Case: cyclic source/chroot sub-path elimination. self.assertEqual( os.path.join(constants.SOURCE_ROOT, 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR, constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep), constants.DEFAULT_CHROOT_DIR, constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep), 'some/path'))) # Case: path inside the cache directory. self.assertEqual( os.path.join(path_util.GetCacheDir(), 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path'))) # Case: non-rooted chroot paths. self.assertEqual(os.path.join(self.chroot_path, 'some/path'), resolver.FromChroot('/some/path'))
def testOutsideChrootInbound(self, _): """Tests ToChroot() calls from outside the chroot.""" for source_path, source_from_path_repo in itertools.product( (None, CUSTOM_SOURCE_PATH), (False, True)): if source_from_path_repo: actual_source_path = FAKE_REPO_PATH else: actual_source_path = source_path or constants.SOURCE_ROOT fake_cwd = self.FakeCwd(actual_source_path) self.PatchObject(os, 'getcwd', return_value=fake_cwd) self.SetChrootPath(actual_source_path) resolver = path_util.ChrootPathResolver( source_path=source_path, source_from_path_repo=source_from_path_repo) self.PatchObject(resolver, '_ReadChrootLink', return_value=None) source_rel_cwd = os.path.relpath(fake_cwd, actual_source_path) # Case: path inside the chroot space. self.assertEqual( '/some/path', resolver.ToChroot(os.path.join(self.chroot_path, 'some/path'))) # Case: path inside the cache directory. self.assertEqual( os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path'), resolver.ToChroot( os.path.join(path_util.GetCacheDir(), 'some/path'))) # Case: absolute path inside the source tree. if source_from_path_repo: self.assertEqual( os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'), resolver.ToChroot(os.path.join(FAKE_REPO_PATH, 'some/path'))) else: self.assertEqual( os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'), resolver.ToChroot( os.path.join(actual_source_path, 'some/path'))) # Case: relative path inside the source tree. if source_from_path_repo: self.assertEqual( os.path.join(constants.CHROOT_SOURCE_ROOT, source_rel_cwd, 'some/path'), resolver.ToChroot('some/path')) else: self.assertEqual( os.path.join(constants.CHROOT_SOURCE_ROOT, source_rel_cwd, 'some/path'), resolver.ToChroot('some/path')) # Case: unreachable, path with improper source root prefix. with self.assertRaises(ValueError): resolver.ToChroot( os.path.join(actual_source_path + '-foo', 'some/path')) # Case: unreachable (random). with self.assertRaises(ValueError): resolver.ToChroot('/some/path')
def ResolvePathFromChroot(self, path_inside_chroot): """Resolves path from chroot. Args: path_inside_chroot: path inside chroot. Returns: Path outside chroot which points to the given path inside chroot. """ return path_util.ChrootPathResolver(source_path=self.cros_dir).FromChroot( path_inside_chroot)
def testInsideChroot(self, _): """Tests {To,From}Chroot() call from inside the chroot.""" self.SetChrootPath(constants.SOURCE_ROOT) resolver = path_util.ChrootPathResolver() self.assertEqual(os.path.realpath('some/path'), resolver.ToChroot('some/path')) self.assertEqual(os.path.realpath('/some/path'), resolver.ToChroot('/some/path')) self.assertEqual(os.path.realpath('some/path'), resolver.FromChroot('some/path')) self.assertEqual(os.path.realpath('/some/path'), resolver.FromChroot('/some/path'))
def run(self): """Kicks off devserver in a separate process and waits for it to finish.""" # Truncate the log file if it already exists. if os.path.exists(self.log_file): osutils.SafeUnlink(self.log_file, sudo=True) path_resolver = path_util.ChrootPathResolver() port = self.port if self.port else 0 cmd = [ self.devserver_bin, '--pidfile', path_resolver.ToChroot(self._pid_file), '--logfile', path_resolver.ToChroot(self.log_file), '--port=%d' % port, '--static_dir=%s' % path_resolver.ToChroot(self.static_dir) ] if not self.port: cmd.append('--portfile=%s' % path_resolver.ToChroot(self.port_file)) if self.src_image: cmd.append('--src_image=%s' % path_resolver.ToChroot(self.src_image)) if self.board: cmd.append('--board=%s' % self.board) chroot_args = ['--no-ns-pid'] # The chromite bin directory is needed for cros_generate_update_payload. extra_env = { 'PATH': '%s:%s' % (os.environ['PATH'], path_resolver.ToChroot(constants.CHROMITE_BIN_DIR)) } result = self._RunCommand(cmd, enter_chroot=True, chroot_args=chroot_args, cwd=constants.SOURCE_ROOT, extra_env=extra_env, check=False, stdout=True, stderr=subprocess.STDOUT, encoding='utf-8') if result.returncode != 0: msg = ('Devserver failed to start!\n' '--- Start output from the devserver startup command ---\n' '%s' '--- End output from the devserver startup command ---' % result.output) logging.error(msg)
def testSymlinkedChroot(self, _): self.SetChrootPath(constants.SOURCE_ROOT) resolver = path_util.ChrootPathResolver() self.PatchObject(resolver, '_ReadChrootLink', return_value='/another/path') # Should still resolve paths from the chroot to the default location. self.assertEqual( os.path.join(constants.SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR, 'some/path'), resolver.FromChroot('/some/path')) # Should be able to handle translating the linked location to a chroot path. self.assertEqual('/some/path', resolver.ToChroot('/another/path/some/path'))
def run(self): """Kicks off devserver in a separate process and waits for it to finish.""" # Truncate the log file if it already exists. if os.path.exists(self.log_file): osutils.SafeUnlink(self.log_file, sudo=True) path_resolver = path_util.ChrootPathResolver() port = self.port if self.port else 0 cmd = [ self.devserver_bin, '--pidfile', path_resolver.ToChroot(self._pid_file), '--logfile', path_resolver.ToChroot(self.log_file), '--port=%d' % port, '--critical_update' ] if not self.port: cmd.append('--portfile=%s' % path_resolver.ToChroot(self.port_file)) if self.static_dir: cmd.append('--static_dir=%s' % path_resolver.ToChroot(self.static_dir)) if self.src_image: cmd.append('--src_image=%s' % path_resolver.ToChroot(self.src_image)) if self.board: cmd.append('--board=%s' % self.board) chroot_args = ['--no-ns-pid'] result = self._RunCommand(cmd, enter_chroot=True, chroot_args=chroot_args, cwd=constants.SOURCE_ROOT, error_code_ok=True, redirect_stdout=True, combine_stdout_stderr=True) if result.returncode != 0: msg = (('Devserver failed to start!\n' '--- Start output from the devserver startup command ---\n' '%s' '--- End output from the devserver startup command ---') % (result.output)) logging.error(msg)
def testOutsideChrootOutbound(self, _): """Tests FromChroot() calls from outside the chroot.""" self.PatchObject(os, 'getcwd', return_value=self.FakeCwd(FAKE_SOURCE_PATH)) self.SetChrootPath(constants.SOURCE_ROOT) resolver = path_util.ChrootPathResolver() # These two patches are only necessary or have any affect on the test when # the test is run inside of a symlinked chroot. The _ReadChrootLink patch # ensures it runs as if it is not in a symlinked chroot. The realpath # patch is necessary to make it actually behave as if that's the case. # In both instances the effective return value are as if it was not in a # symlinked chroot. # TODO(saklein) Rewrite these tests so this isn't necessary. self.PatchObject(resolver, '_ReadChrootLink', return_value=None) self.PatchObject(os.path, 'realpath', side_effect=lambda x: x) # Case: source root path. self.assertEqual( os.path.join(constants.SOURCE_ROOT, 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_SOURCE_ROOT, 'some/path'))) # Case: cyclic source/chroot sub-path elimination. self.assertEqual( os.path.join(constants.SOURCE_ROOT, 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_SOURCE_ROOT, constants.DEFAULT_CHROOT_DIR, constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep), constants.DEFAULT_CHROOT_DIR, constants.CHROOT_SOURCE_ROOT.lstrip(os.path.sep), 'some/path'))) # Case: path inside the cache directory. self.assertEqual( os.path.join(path_util.GetCacheDir(), 'some/path'), resolver.FromChroot( os.path.join(constants.CHROOT_CACHE_ROOT, 'some/path'))) # Case: non-rooted chroot paths. self.assertEqual(os.path.join(self.chroot_path, 'some/path'), resolver.FromChroot('/some/path'))