class RoleManagerTests(TestCase): def setUp(self): super(RoleManagerTests, self).setUp() self.role_manager = RoleManager() self.template_store = TemplateStore() def test_list_roles(self): # Test self._populate_roles() all_roles = self.role_manager.list_roles() # Verify self.assertEqual(3, len(all_roles)) self.assertTrue(isinstance(all_roles[0], Role)) all_roles.sort(key=lambda x: '%s-%s' % (x.name, x.version)) self.assertEqual('r1', all_roles[0].name) self.assertEqual(1, all_roles[0].version) self.assertEqual('r1', all_roles[1].name) self.assertEqual(2, all_roles[1].version) self.assertEqual('r2', all_roles[2].name) self.assertEqual(1, all_roles[2].version) def test_list_roles_only_latest(self): # Setup list_mock = mock.MagicMock() self.role_manager.template_store.list = list_mock list_mock.return_value = [] # Test self.role_manager.list_roles(only_latest=True) # Verify list_mock.assert_called_once_with(only_latest=True) def test_retrieve_role_by_uuid(self): # Test added_roles = self._populate_roles() found = self.role_manager.retrieve_role_by_uuid(added_roles[0].uuid) # Verify self.assertTrue(found is not None) self.assertTrue(isinstance(found, Role)) self.assertEqual(found.name, 'r1') self.assertEqual(found.version, 2) def test_retrieve_role_by_fake_uuid(self): self.assertRaises(UnknownUUID, self.role_manager.retrieve_role_by_uuid, 'fake') def _populate_roles(self): r1 = self.template_store.create('r1', TEST_TEMPLATE) r1 = self.template_store.update(r1.uuid, TEST_TEMPLATE) r2 = self.template_store.create('r2', TEST_TEMPLATE) return [r1, r2]
def get_all(self): """Returns all roles. An empty list is returned if no roles are present. :return: list of roles; empty list if none are found :rtype: list of tuskar.api.controllers.v2.models.Role """ LOG.debug('Retrieving all roles') manager = RoleManager() all_roles = manager.list_roles(only_latest=False) transfer_roles = [models.Role.from_tuskar_model(r) for r in all_roles] return transfer_roles
def extra_data(self, role_uuid): """Retrieve the extra data files associated with a given role. :param role_uuid: identifies the role :type role_uuid: str :return: a dict where keys are filenames and values are their contents :rtype: dict This method will retrieve all stored role_extra records (these are created at the same time that the Roles are, by using --role-extra parameter to tuskar-load-roles). The internal representation for a given role-extra file encodes the file extension into the name. For instance 'hieradata/compute.yaml' is stored as 'extra_compute_yaml'. The given role's template is searched for 'get_file' directives and then matched against the stored role-extra records (based on their name... e.g. 'extra_controller_yaml' we look for 'controller.yaml' after a get_file directive). This method thus returns all the matched role-extra files for the given role. The keys will include the relative path if one is used in the role template: { "hieradata/common.yaml": "CONTENTS", "hieradata/controller.yaml": "CONTENTS", "hieradata/object.yaml": "CONTENTS" } """ manager = RoleManager() db_role = manager.retrieve_db_role_by_uuid(role_uuid) db_role_extra = manager.retrieve_db_role_extra() role_extra_paths = utils.resolve_template_extra_data( db_role, db_role_extra) return manager.template_extra_data_for_output(role_extra_paths)
def setUp(self): super(RoleManagerTests, self).setUp() self.role_manager = RoleManager() self.template_store = TemplateStore()
def package_templates(self, plan_uuid): """Packages and returns all of the templates related to the given plan. The returned dictionary is keyed by filename and contains the contents of that file (a template or an environment file). :type plan_uuid: str :return: mapping of filename to contents for each file in the plan :rtype: dict :raises tuskar.storage.exceptions.UnknownUUID: if there is no plan with the given UUID """ # Load and parse the plan. db_plan = self.plan_store.retrieve(plan_uuid) master_template = parser.parse_template( db_plan.master_template.contents) environment = parser.parse_environment( db_plan.environment_file.contents) # Compose the plan files and all plan roles and package them into # a single dictionary. plan_contents = composer.compose_template(master_template) env_contents = composer.compose_environment(environment) files_dict = { 'plan.yaml': plan_contents, 'environment.yaml': env_contents, } plan_roles = self._find_roles(environment) manager = RoleManager() for role in plan_roles: contents = composer.compose_template(role.template) filename = name_utils.role_template_filename( role.name, role.version, role.relative_path) files_dict[filename] = contents def _add_template_extra_data_for(templates, template_store): template_extra_data = manager.retrieve_db_role_extra() for template in templates: db_template = template_store.retrieve_by_name(template.name) prefix = os_path.split(db_template.name)[0] template_extra_paths = utils.resolve_template_extra_data( db_template, template_extra_data) extra_data_output = manager.template_extra_data_for_output( template_extra_paths, prefix) files_dict.update(extra_data_output) # also grab any extradata files for the role _add_template_extra_data_for(plan_roles, self.template_store) # in addition to provider roles above, return non-role template files reg_mapping = self.registry_mapping_store.list() for entry in reg_mapping: if os_path.splitext(entry.name)[1] in ('.yaml', '.yml'): # if entry is an alias, don't include it files_dict[entry.name] = entry.contents # similarly, also grab extradata files for the non role templates _add_template_extra_data_for(reg_mapping, self.registry_mapping_store) return files_dict