def testUnnecessaryCleanup(self): data = { 'title': '', 'actions': [ { 'generate_resource_id': { 'reference': 'my-device', 'prefix': 'iot-device', 'requires_cleanup': False } }, { 'execute_command': { 'command': '', 'cleanup_for': 'my-device', 'events': [] } }, ] } validator = schema.Validator(data) with self.assertRaisesRegex( schema.ValidationError, r'cleanup_for reference \[my-device\] was marked as not requiring ' r'cleanup'): validator.Validate()
def testMissingCleanup(self): data = { 'title': '', 'actions': [{ 'generate_resource_id': { 'reference': 'my-device', 'prefix': 'iot-device' } }, { 'generate_resource_id': { 'reference': 'my-device2', 'prefix': 'iot-device' } }, { 'execute_command': { 'command': '', 'events': [] } }] } validator = schema.Validator(data) with self.assertRaisesRegex( schema.ValidationError, r'No cleanup_for rules found for generate_resource_id action: ' r'\[my-device\]\n' r'No cleanup_for rules found for generate_resource_id action: ' r'\[my-device2\]\n'): validator.Validate()
def testDuplicateCleanup(self): data = { 'title': '', 'actions': [ { 'generate_resource_id': { 'reference': 'my-device', 'prefix': 'iot-device' } }, { 'execute_command': { 'command': '', 'cleanup_for': 'my-device', 'events': [] } }, { 'execute_command': { 'command': '', 'cleanup_for': 'my-device', 'events': [] } }, ] } validator = schema.Validator(data) with self.assertRaisesRegex( schema.ValidationError, r'Duplicate cleanup_for reference found: \[my-device\]'): validator.Validate()
def testValidateTemplate(self): val = pkg_resources.GetResourceFromFile(SCENARIO_TEMPLATE_PATH) template = yaml.load(val, version=yaml.VERSION_1_2) validator = scenario_schema.Validator(template) try: validator.Validate() except scenario_schema.ValidationError as e: self.fail(e)
def testValidateCreateExample(self): example = yaml.load( pkg_resources.GetResourceFromFile(CREATE_EXAMPLE_PATH), version=yaml.VERSION_1_2) validator = scenario_schema.Validator(example) try: validator.Validate() except scenario_schema.ValidationError as e: self.fail(e)
def testMissingMissingGenerated(self): data = { 'title': '', 'actions': [{ 'execute_command': { 'command': '', 'cleanup_for': 'asdf', 'events': [] } }] } validator = schema.Validator(data) with self.assertRaisesRegex( schema.ValidationError, r'cleanup_for reference \[asdf\] was not found in a ' r'generate_resource_id action'): validator.Validate()
def RunScenario(self, spec_path, track, execution_mode, update_modes, debug=False): full_spec_path = sdk_test_base.SdkBase.Resource(spec_path) spec_data = yaml.load_path( full_spec_path, round_trip=True, version=yaml.VERSION_1_2) validator = schema.Validator(spec_data) try: validator.Validate() except schema.ValidationError as e: self.fail(e) spec = schema.Scenario.FromData(spec_data) self.track = track stream_mocker = CreateStreamMocker(self) scenario_context = schema.ScenarioContext( spec_path, full_spec_path, spec_data, track, execution_mode, update_modes, stream_mocker, self.Run, debug) self.StartPatch( 'googlecloudsdk.core.console.console_io.CanPrompt', return_value=True) if execution_mode == session.ExecutionMode.LOCAL: self.StartObjectPatch(retry, '_SleepMs') actions = spec.LoadActions() for a in actions: try: a.Execute(scenario_context) except assertions.Error: if execution_mode == session.ExecutionMode.REMOTE: for c in actions: # Iterates over the remaining actions because it is a generator. c.ExecuteCleanup(scenario_context) # Continue raising the original error. raise if update_modes: spec.UpdateSummary(scenario_context)
def testBadSchema(self): data = {'title': '', 'actions': [], 'foo': 'bar'} validator = schema.Validator(data) with self.assertRaises(schema.ValidationError): validator.Validate()