def test_image_valid_on_flavor(self, mock__get_validated_flavor, mock__get_validated_image): image = { "id": "fake_id", "min_ram": None, "size": 2, "min_disk": 0 } flavor = mock.MagicMock() success = validation.ValidationResult(True) fail = validation.ValidationResult(False) mock__get_validated_flavor.return_value = (success, flavor) mock__get_validated_image.return_value = (fail, image) validator = self._unwrap_validator(validation.image_valid_on_flavor, "flavor", "image") result = validator(None, None, None) self.assertFalse(result.is_valid, result.msg) mock__get_validated_image.return_value = (success, image) # test flavor.disk None validator = self._unwrap_validator(validation.image_valid_on_flavor, "flavor", "image") flavor.disk = None flavor.ram = 2 image["min_ram"] = 4 result = validator(None, None, None) self.assertFalse(result.is_valid, result.msg) image["min_ram"] = 1 result = validator(None, None, None) self.assertTrue(result.is_valid, result.msg) # test validate_disk false validator = self._unwrap_validator(validation.image_valid_on_flavor, "flavor", "image", False) flavor.disk = 1 flavor.ram = 2 image["min_ram"] = 4 result = validator(None, None, None) self.assertFalse(result.is_valid, result.msg) image["min_ram"] = 1 result = validator(None, None, None) self.assertTrue(result.is_valid, result.msg) # test validate_disk true and flavor.disk not None validator = self._unwrap_validator(validation.image_valid_on_flavor, "flavor", "image") image["size"] = 2 image["min_disk"] = 0 flavor.disk = 5.0 / (1024 ** 3) result = validator(None, None, None) self.assertTrue(result.is_valid, result.msg) image["min_disk"] = flavor.disk * 2 result = validator(None, None, None) self.assertFalse(result.is_valid, result.msg) image["min_disk"] = flavor.disk / 4 image["size"] = 1000 result = validator(None, None, None) self.assertFalse(result.is_valid, result.msg)
def test__validate_helper__no_valid(self): validators = [ mock.MagicMock(return_value=validation.ValidationResult(True)), mock.MagicMock(return_value=validation.ValidationResult( is_valid=False)) ] clients = mock.MagicMock() args = {"a": 1, "b": 2} self.assertRaises(exceptions.InvalidScenarioArgument, scenario.Scenario._validate_helper, validators, clients, args, "fake_uuid")
def test_validation_result(self): self.assertEqual("validation success", str(validation.ValidationResult(True))) self.assertEqual("my msg", str(validation.ValidationResult(False, "my msg"))) self.assertEqual( "---------- Exception in validator ----------\ntb\n", str( validation.ValidationResult(False, "my msg", etype=Exception, etraceback="tb\n")))
def test__validate_helper(self): validators = [ mock.MagicMock(return_value=validation.ValidationResult(True)), mock.MagicMock(return_value=validation.ValidationResult(True)) ] clients = mock.MagicMock() config = {"a": 1, "b": 2} deployment = mock.MagicMock() scenario.Scenario._validate_helper(validators, clients, config, deployment) for validator in validators: validator.assert_called_with(config, clients=clients, deployment=deployment)
def test_image_valid_on_flavor_context( self, mock__get_validated_image, mock_flavor_transform): clients = mock.MagicMock() clients.nova().flavors.get.side_effect = nova_exc.NotFound("") image = {"min_ram": 24, "id": "fake_id"} success = validation.ValidationResult(True) mock__get_validated_image.return_value = (success, image) validator = self._unwrap_validator(validation.image_valid_on_flavor, "flavor", "image") config = { "args": {"flavor": {"name": "test"}}, "context": { "flavors": [{ "name": "test", "ram": 32, }] } } # test ram image["min_ram"] = 64 result = validator(config, clients, None) self.assertFalse(result.is_valid, result.msg)
def test_validator(self): failure = validation.ValidationResult(False) func = lambda *args, **kv: kv scenario = lambda: None # Check arguments passed to validator wrap = validation.validator(func) wrap_args = ["foo", "bar"] wrap_kwargs = {"foo": "spam"} wrap_scenario = wrap(*wrap_args, **wrap_kwargs) wrap_validator = wrap_scenario(scenario) validators = wrap_validator.validators self.assertEqual(1, len(validators)) validator, = validators self.assertEqual(wrap_kwargs, validator(None, None, None)) self.assertEqual(wrap_validator, scenario) # Default result func_success = lambda *a, **kv: None validator, = self._get_scenario_validators(func_success, scenario) self.assertTrue(validator(None, None, None).is_valid) # Failure result func_failure = lambda *a, **kv: failure validator, = self._get_scenario_validators(func_failure, scenario) self.assertFalse(validator(None, None, None).is_valid)
def test_old_validator_users_error(self): validator_func = mock.Mock() validator_func.return_value = validation.ValidationResult(False) validator = validation.validator(validator_func) self.assertEqual(self.Plugin, validator("a", "b", "c", d=1)(self.Plugin)) self.assertEqual(1, len(self.Plugin._meta_get("validators"))) vname, args, kwargs = self.Plugin._meta_get("validators")[0] validator_cls = common_validation.Validator.get(vname) validator_inst = validator_cls(*args, **kwargs) fake_admin = fakes.fake_credential() fake_users1 = fakes.fake_credential() fake_users2 = fakes.fake_credential() users = [{"credential": fake_users1}, {"credential": fake_users2}] ctx = {"admin": {"credential": fake_admin}, "users": users} self.assertRaises(common_validation.ValidationError, validator_inst.validate, ctx, {}, None, None) fake_users1.clients.assert_called_once_with() fake_users2.clients.assert_called_once_with() validator_func.assert_called_once_with( {}, fake_users1.clients.return_value, mock.ANY, "a", "b", "c", d=1) deployment = validator_func.call_args[0][2] self.assertEqual( { "admin": fake_admin, "users": [fake_users1, fake_users2] }, deployment.get_credentials_for("openstack"))
def test_valid_command_unreadable_script_file(self, mock__file_access_ok): mock__file_access_ok.return_value = validation.ValidationResult(False) validator = self._unwrap_validator(validation.valid_command, param_name="p") command = {"script_file": "foobar", "interpreter": "foo"} result = validator({"args": {"p": command}}, None, None) self.assertFalse(result.is_valid, result.msg)
def test_valid_command(self, mock__file_access_ok): validator = self._unwrap_validator(validation.valid_command, param_name="p") mock__file_access_ok.return_value = validation.ValidationResult(True) command = {"script_file": "foobar", "interpreter": "foo"} result = validator({"args": {"p": command}}, None, None) self.assertTrue(result.is_valid, result.msg) mock__file_access_ok.assert_called_once_with( filename="foobar", mode=os.R_OK, param_name="p.script_file", required=True)
def test_valid_command_local_path(self, mock__file_access_ok): mock__file_access_ok.return_value = validation.ValidationResult(False) validator = self._unwrap_validator(validation.valid_command, param_name="p") command = {"remote_path": "bar", "local_path": "foobar"} result = validator({"args": {"p": command}}, None, None) self.assertFalse(result.is_valid, result.msg) mock__file_access_ok.assert_called_once_with( filename="foobar", mode=os.R_OK, param_name="p.local_path", required=True)