def _detach(cfg: config.UAConfig, assume_yes: bool) -> int: """Detach the machine from the active Ubuntu Advantage subscription, :param cfg: a ``config.UAConfig`` instance :param assume_yes: Assume a yes answer to any prompts requested. In this case, it means automatically disable any service during detach. @return: 0 on success, 1 otherwise """ to_disable = [] for ent_cls in entitlements.ENTITLEMENT_CLASSES: ent = ent_cls(cfg) if ent.can_disable(silent=True): to_disable.append(ent) if to_disable: suffix = "s" if len(to_disable) > 1 else "" print("Detach will disable the following service{}:".format(suffix)) for ent in to_disable: print(" {}".format(ent.name)) if not util.prompt_for_confirmation(assume_yes=assume_yes): return 1 for ent in to_disable: ent.disable(silent=True) contract_client = contract.UAContractClient(cfg) machine_token = cfg.machine_token["machineToken"] contract_id = cfg.machine_token["machineTokenInfo"]["contractInfo"]["id"] contract_client.detach_machine_from_contract(machine_token, contract_id) cfg.delete_cache() print(ua_status.MESSAGE_DETACH_SUCCESS) return 0
def test_delete_cache_unsets_entitlements(self, tmpdir): """The delete_cache unsets any cached entitlements content.""" cfg = UAConfig({"data_dir": tmpdir.strpath}) token = { "machineTokenInfo": { "contractInfo": { "resourceEntitlements": [{ "type": "entitlement1", "entitled": True }] } } } cfg.write_cache("machine-token", token) previous_entitlements = { "entitlement1": { "entitlement": { "type": "entitlement1", "entitled": True } } } assert previous_entitlements == cfg.entitlements cfg.delete_cache() assert {} == cfg.entitlements
def test_delete_cache_unsets_entitlements(self): """The delete_cache unsets any cached entitlements content.""" tmp_dir = self.tmp_dir() cfg = UAConfig({'data_dir': tmp_dir}) token = { 'machineTokenInfo': { 'contractInfo': { 'resourceEntitlements': [{ 'type': 'entitlement1', 'entitled': True }] } } } cfg.write_cache('machine-token', token) previous_entitlements = { 'entitlement1': { 'entitlement': { 'type': 'entitlement1', 'entitled': True } } } assert previous_entitlements == cfg.entitlements cfg.delete_cache() assert {} == cfg.entitlements
def test_delete_cache_ignores_files_not_defined_in_data_paths(self): """Any files in data_dir undefined in cfg.data_paths will remain.""" tmp_dir = self.tmp_dir() cfg = UAConfig({'data_dir': tmp_dir}) t_file = self.tmp_path('otherfile', tmp_dir) with open(t_file, 'w') as f: f.write('content') assert [os.path.basename(t_file)] == os.listdir(tmp_dir) cfg.delete_cache() assert [os.path.basename(t_file)] == os.listdir(tmp_dir)
def test_delete_cache_ignores_files_not_defined_in_data_paths( self, tmpdir): """Any files in data_dir undefined in cfg.data_paths will remain.""" cfg = UAConfig({'data_dir': tmpdir.strpath}) t_file = tmpdir.join('otherfile') with open(t_file.strpath, 'w') as f: f.write('content') assert [os.path.basename(t_file.strpath)] == os.listdir(tmpdir.strpath) cfg.delete_cache() assert [os.path.basename(t_file.strpath)] == os.listdir(tmpdir.strpath)
def test_delete_cache_removes_any_cached_data_path_files(self, tmpdir): """Any cached files defined in cfg.data_paths will be removed.""" cfg = UAConfig({'data_dir': tmpdir.strpath}) # Create half of the cached files, but not all odd_keys = list(cfg.data_paths.keys())[::2] for odd_key in odd_keys: cfg.write_cache(odd_key, odd_key) assert len(odd_keys) == len(os.listdir(tmpdir.strpath)) cfg.delete_cache() dirty_files = os.listdir(tmpdir.strpath) assert 0 == len(dirty_files), '%d files not deleted' % len(dirty_files)
def test_delete_cache_ignores_files_not_defined_in_data_paths( self, tmpdir): """Any files in data_dir undefined in cfg.data_paths will remain.""" cfg = UAConfig({"data_dir": tmpdir.strpath}) t_file = tmpdir.join(PRIVATE_SUBDIR, "otherfile") os.makedirs(os.path.dirname(t_file.strpath)) with open(t_file.strpath, "w") as f: f.write("content") assert [os.path.basename(t_file.strpath) ] == os.listdir(tmpdir.join(PRIVATE_SUBDIR).strpath) cfg.delete_cache() assert [os.path.basename(t_file.strpath) ] == os.listdir(tmpdir.join(PRIVATE_SUBDIR).strpath)
def test_delete_cache_removes_any_cached_data_path_files(self, tmpdir): """Any cached files defined in cfg.data_paths will be removed.""" cfg = UAConfig({"data_dir": tmpdir.strpath}) # Create half of the cached files, but not all odd_keys = list(cfg.data_paths.keys())[::2] for odd_key in odd_keys: cfg.write_cache(odd_key, odd_key) present_files = list( itertools.chain( *[walk_entry[2] for walk_entry in os.walk(tmpdir.strpath)])) assert len(odd_keys) == len(present_files) cfg.delete_cache() dirty_files = list( itertools.chain( *[walk_entry[2] for walk_entry in os.walk(tmpdir.strpath)])) assert 0 == len(dirty_files), "{} files not deleted".format( len(dirty_files))
def test_delete_cache_properly_clears_all_caches_simple( self, tmpdir, property_name, data_path_name, expected_null_value): """ Ensure that delete_cache clears the cache for simple attributes (Simple in this context means those that are simply read from the filesystem and returned.) """ property_value = 'our-value' cfg = UAConfig({'data_dir': tmpdir.strpath}) data_path = cfg.data_path(data_path_name) with open(data_path, 'w') as f: f.write(property_value) before_prop_value = getattr(cfg, property_name) assert before_prop_value == property_value cfg.delete_cache() after_prop_value = getattr(cfg, property_name) assert expected_null_value == after_prop_value