def get_validations_data(validation, path=constants.ANSIBLE_VALIDATION_DIR): """Return validation data with format: ID, Name, Description, Groups, Parameters :param validation: Name of the validation without the `yaml` extension. Defaults to `constants.ANSIBLE_VALIDATION_DIR` :type validation: `string` :param path: The path to the validations directory :type path: `string` :return: The validation data with the format (ID, Name, Description, Groups, Parameters) :rtype: `dict` :Example: >>> validation = 'check-something' >>> get_validations_data(validation) {'Description': 'Verify that the server has enough something', 'Groups': ['group1', 'group2'], 'ID': 'check-something', 'Name': 'Verify the server fits the something requirements', 'Parameters': {'param1': 24}} """ if not isinstance(validation, six.string_types): raise TypeError("The input data should be a String") data = {} val_path = "{}/{}.yaml".format(path, validation) if os.path.exists(val_path): val = Validation(val_path) data.update(val.get_formated_data) data.update({'Parameters': val.get_vars}) return data
def get_validations_parameters(validations_data, validation_name=[], groups=[]): """Return parameters for a list of validations :param validations_data: A list of absolute validations playbooks path :type validations_data: `list` :param validation_name: A list of validation name :type validation_name: `list` :param groups: A list of validation groups :type groups: `list` :return: a dictionary containing the current parameters for each `validation_name` or `groups` :rtype: `dict` :Example: >>> validations_data = ['/foo/bar/check-ram.yaml', '/foo/bar/check-cpu.yaml'] >>> validation_name = ['check-ram', 'check-cpu'] >>> get_validations_parameters(validations_data, validation_name) {'check-cpu': {'parameters': {'minimal_cpu_count': 8}}, 'check-ram': {'parameters': {'minimal_ram_gb': 24}}} """ params = {} for val in validations_data: v = Validation(val) if v.id in validation_name or set(groups).intersection(v.groups): params[v.id] = { 'parameters': v.get_vars } return params
def get_validations_playbook(path, validation_id=None, groups=None): """Get a list of validations playbooks paths either by their names or their groups :param path: Path of the validations playbooks :type path: `string` :param validation_id: List of validation name :type validation_id: `list` or a `string` of comma-separated validations :param groups: List of validation group :type groups: `list` or a `string` of comma-separated groups :return: A list of absolute validations playbooks path :rtype: `list` :Example: >>> path = '/usr/share/validation-playbooks' >>> validation_id = ['512e','check-cpu'] >>> groups = None >>> get_validations_playbook(path, validation_id, groups) ['/usr/share/ansible/validation-playbooks/512e.yaml', '/usr/share/ansible/validation-playbooks/check-cpu.yaml',] """ if not validation_id: validation_id = [] else: validation_id = convert_data(validation_id) if not groups: groups = [] else: groups = convert_data(groups) pl = [] for f in os.listdir(path): pl_path = join(path, f) if os.path.isfile(pl_path): if validation_id: if os.path.splitext(f)[0] in validation_id or \ os.path.basename(f) in validation_id: pl.append(pl_path) if groups: val = Validation(pl_path) if set(groups).intersection(val.groups): pl.append(pl_path) return pl
def parse_all_validations_on_disk(path, groups=None): """Return a list of validations metadata which can be sorted by Groups :param path: The absolute path of the validations directory :type path: `string` :param groups: Groups of validations. Could be a `list` or a comma-separated `string` of groups :type groups: `list` or `string` :return: A list of validations metadata. :rtype: `list` :Example: >>> path = '/foo/bar' >>> parse_all_validations_on_disk(path) [{'description': 'Detect whether the node disks use Advanced Format.', 'groups': ['prep', 'pre-deployment'], 'id': '512e', 'name': 'Advanced Format 512e Support'}, {'description': 'Make sure that the server has enough CPU cores.', 'groups': ['prep', 'pre-introspection'], 'id': 'check-cpu', 'name': 'Verify if the server fits the CPU core requirements'}] """ results = [] if not groups: groups = [] else: groups = convert_data(groups) validations_abspath = glob.glob("{path}/*.yaml".format(path=path)) for playbook in validations_abspath: val = Validation(playbook) if not groups or set(groups).intersection(val.groups): results.append(val.get_metadata) return results
def get_validation_parameters(validation): """Return dictionary of parameters""" return Validation(validation).get_vars
def test_groups_with_no_metadata(self, mock_open, mock_yaml): with self.assertRaises(NameError) as exc_mgr: Validation('/tmp/foo').groups self.assertEqual('No metadata found in validation foo', str(exc_mgr.exception))
def test_groups_with_no_existing_groups(self, mock_open, mock_yaml): val = Validation('/tmp/foo') groups = val.groups self.assertEqual(groups, [])
def test_get_id(self, mock_open, mock_yaml): val = Validation('/tmp/foo') id = val.id get_id = val.get_id self.assertEqual(id, 'foo') self.assertEqual(get_id, 'foo')
def test_groups(self, mock_open, mock_yaml): val = Validation('/tmp/foo') groups = val.groups self.assertEqual(groups, ['prep', 'pre-deployment'])
def test_get_vars(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_vars self.assertEqual(data, fakes.FAKE_VARS)
def test_get_vars_no_vars(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_vars self.assertEqual(data, {})
def test_get_metadata_wrong_playbook(self, mock_open, mock_yaml): with self.assertRaises(NameError) as exc_mgr: Validation('/tmp/foo').get_metadata self.assertEqual('No metadata found in validation foo', str(exc_mgr.exception))
def test_get_metadata(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_metadata self.assertEqual(data, fakes.FAKE_METADATA)
def test_get_data(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_data self.assertEqual(data, fakes.FAKE_PLAYBOOK[0])
def test_get_formated_data(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_formated_data self.assertEqual(data, fakes.FORMATED_DATA)
def test_get_ordered_dict(self, mock_open, mock_yaml): val = Validation('/tmp/foo') data = val.get_ordered_dict self.assertEquals(data, fakes.FAKE_PLAYBOOK[0])