def Create(arguments): """Create or replace the chroot. Args: arguments (CreateArguments): The various arguments to create a chroot. Returns: int - The version of the resulting chroot. """ cros_build_lib.AssertOutsideChroot() cmd = [os.path.join(constants.CHROMITE_BIN_DIR, 'cros_sdk')] cmd.extend(arguments.GetArgList()) cros_build_lib.run(cmd) version = GetChrootVersion(arguments.chroot_path) if not arguments.replace: # Force replace scenarios. Only needed when we're not already replacing it. if not version: # Force replace when we can't get a version for a chroot that exists, # since something must have gone wrong. logging.notice('Replacing broken chroot.') arguments.replace = True return Create(arguments) elif not cros_sdk_lib.IsChrootVersionValid(arguments.chroot_path): # Force replace when the version is not valid, i.e. ahead of the chroot # version hooks. logging.notice('Replacing chroot ahead of current checkout.') arguments.replace = True return Create(arguments) elif not cros_sdk_lib.IsChrootDirValid(arguments.chroot_path): # Force replace when the permissions or owner are not correct. logging.notice('Replacing chroot with invalid permissions.') arguments.replace = True return Create(arguments) return GetChrootVersion(arguments.chroot_path)
def testInvalidVersion(self): """Test version higher than latest.""" osutils.WriteFile(self.version_file, str(self.latest_version + 1)) self.assertFalse( cros_sdk_lib.IsChrootVersionValid(self.chroot_path, self.hooks_dir))
def testLatestVersionValid(self): """Test latest version.""" osutils.WriteFile(self.version_file, str(self.latest_version)) self.assertTrue( cros_sdk_lib.IsChrootVersionValid(self.chroot_path, self.hooks_dir))
def testLowerVersionValid(self): """Lower versions are considered valid.""" osutils.WriteFile(self.version_file, str(self.latest_version - 1)) self.assertTrue( cros_sdk_lib.IsChrootVersionValid(self.chroot_path, self.hooks_dir))