def test_invalid_app_names(self): invalid_names = { 'qwe#rty': { 'command': '1' }, 'qwe_rty': { 'command': '1' }, 'que rty': { 'command': '1' }, 'que rty': { 'command': '1' }, } for t in invalid_names: data = self.data.copy() with self.subTest(key=t): data['apps'] = {t: invalid_names[t]} with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(data).validate() expected_message = ( "The 'apps' property does not match the required " "schema: Additional properties are not allowed ('{}' " "was unexpected)").format(t) self.assertEqual(raised.exception.message, expected_message, msg=data)
def test_license_version_without_license_raises_exception(self): self.data['license-version'] = '1.1' with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() expected_message = "'license' is a dependency of 'license-version'" self.assertEqual(raised.exception.message, expected_message, msg=self.data)
def test_summary_too_long(self): self.data['summary'] = 'a' * 80 with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() expected_message = ( "The 'summary' property does not match the required schema: " "'{}' is too long").format(self.data['summary']) self.assertEqual(raised.exception.message, expected_message, msg=self.data)
def test_invalid_part_name_plugin_raises_exception(self): self.data['parts']['plugins'] = {'type': 'go'} with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() expected_message = ("The 'parts' property does not match the " "required schema: Additional properties are not " "allowed ('plugins' was unexpected)") self.assertEqual(raised.exception.message, expected_message, msg=self.data)
def setUp(self): super().setUp() dirs.setup_dirs() patcher = unittest.mock.patch('os.path.exists') self.mock_path_exists = patcher.start() self.mock_path_exists.return_value = True self.addCleanup(patcher.stop) self.part_schema = internal_yaml.Validator().part_schema self.deb_arch = snapcraft.ProjectOptions().deb_arch
def setUp(self): super().setUp() dirs.setup_dirs() patcher = unittest.mock.patch( 'snapcraft.internal.yaml._get_snapcraft_yaml') self.mock_get_yaml = patcher.start() self.mock_get_yaml.return_value = 'snapcraft.yaml' self.addCleanup(patcher.stop) self.part_schema = internal_yaml.Validator().part_schema self.deb_arch = snapcraft.ProjectOptions().deb_arch
def test_valid_types(self): valid_types = [ 'app', 'kernel', 'os', ] for t in valid_types: data = self.data.copy() with self.subTest(key=t): internal_yaml.Validator(data).validate()
def test_apps_required_properties(self): self.data['apps'] = {'service1': {}} with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() expected_message = ("The 'service1' property does not match the " "required schema: 'command' is a required " "property") self.assertEqual(raised.exception.message, expected_message, msg=self.data)
def test_schema_file_not_found(self): mock_the_open = unittest.mock.mock_open() mock_the_open.side_effect = FileNotFoundError() with unittest.mock.patch('snapcraft._schema.open', mock_the_open, create=True): with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() expected_message = ('snapcraft validation file is missing from ' 'installation path') self.assertEqual(raised.exception.message, expected_message)
def test_required_properties(self): for key in self.data: data = self.data.copy() with self.subTest(key=key): del data[key] with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(data).validate() expected_message = '\'{}\' is a required property'.format(key) self.assertEqual(raised.exception.message, expected_message, msg=data)
def test_valid_restart_conditions(self): self.data['apps'] = { 'service1': { 'command': 'binary1', 'daemon': 'simple', } } valid_conditions = ['always', 'on-success', 'on-failure', 'on-abnormal', 'on-abort'] for condition in valid_conditions: with self.subTest(key=condition): self.data['apps']['service1']['restart-condition'] = condition internal_yaml.Validator(self.data).validate()
def test_invalid_restart_condition(self): self.data['apps'] = { 'service1': { 'command': 'binary1', 'daemon': 'simple', 'restart-condition': 'on-watchdog', } } with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(self.data).validate() self.assertEqual( "The 'restart-condition' property does not match the required " "schema: 'on-watchdog' is not one of ['on-success', " "'on-failure', 'on-abnormal', 'on-abort', 'always']", str(raised.exception))
def test_valid_app_daemons(self): self.data['apps'] = { 'service1': {'command': 'binary1 start', 'daemon': 'simple'}, 'service2': { 'command': 'binary2', 'stop-command': 'binary2 --stop', 'daemon': 'simple' }, 'service3': { 'command': 'binary3', 'daemon': 'forking', }, 'service4': { 'command': 'binary4', 'daemon': 'simple', 'restart-condition': 'always', } } internal_yaml.Validator(self.data).validate()
def test_invalid_names(self): invalid_names = [ 'package@awesome', 'something.another', '_hideme', ] for name in invalid_names: data = self.data.copy() with self.subTest(key=name): data['name'] = name with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(data).validate() expected_message = ("The 'name' property does not match the " "required schema: '{}' does not match " "'^[a-z0-9][a-z0-9+-]*$'").format(name) self.assertEqual(raised.exception.message, expected_message, msg=data)
def test_invalid_types(self): invalid_types = [ 'apps', 'framework', 'platform', 'oem', ] for t in invalid_types: data = self.data.copy() with self.subTest(key=t): data['type'] = t with self.assertRaises(SnapcraftSchemaError) as raised: internal_yaml.Validator(data).validate() expected_message = ( "The 'type' property does not match the required " "schema: '{}' is not one of " "['app', 'kernel', 'os']").format(t) self.assertEqual(raised.exception.message, expected_message, msg=data)
def test_license_with_license_version(self): self.data['license'] = 'LICENSE' self.data['license-version'] = '1.0' internal_yaml.Validator(self.data).validate()
def test_full_license_use(self): self.data['license'] = 'LICENSE' self.data['license-agreement'] = 'explicit' self.data['license-version'] = '1.0' internal_yaml.Validator(self.data).validate()
def test_license_hook(self): self.data['license'] = 'LICENSE' internal_yaml.Validator(self.data).validate()
def test_icon_missing_is_valid_yaml(self): self.mock_path_exists.return_value = False internal_yaml.Validator(self.data).validate()