class TestParamFileVisitor(unittest.TestCase): def setUp(self): self.model = yaml.safe_load(open(os.path.join(MODEL_DIR, 'service.yaml'))) self.service_model = ServiceModel(self.model, 'servicename') self.resolver = ShapeResolver(self.model['definitions']) self.files = FileCreator() def tearDown(self): self.files.remove_all() def test_visitor(self): contents = 'This is a test' filename = self.files.create_file('jobOne.hql', contents) # We have modified our test model to mark jobXml with x-no-paramfile. params = {'clusterName': u'foo', 'jobs': [{'hiveJob': {'script': 'file://' + filename, 'jobXml': 'file://' + filename}}]} shape = self.resolver.get_shape_by_name( 'submit-jobs-request', 'SubmitJobsRequest') visited = ParamFileVisitor().visit(params, shape) params['jobs'][0]['hiveJob']['script'] = contents self.assertEqual(params, visited) def test_ref_map_visitor(self): contents = 'This is a test' filename = self.files.create_file('jobOne.hql', contents) # We have modified our test model to mark jobXml with x-no-paramfile. params = {'jobs': {'job1': {'hiveJob': {'script': 'file://' + filename, 'jobXml': 'file://' + filename}}}} shape = self.resolver.get_shape_by_name( 'map-paramfile-test', 'RefMapParamFileTest') visited = ParamFileVisitor().visit(params, shape) params['jobs']['job1']['hiveJob']['script'] = contents self.assertEqual(params, visited) def test_explicit_map_visitor(self): contents = 'This is a test' filename = self.files.create_file('jobOne.hql', contents) # We have modified our test model to mark jobXml with x-no-paramfile. params = {'jobs': {'job1': {'script': 'file://' + filename, 'jobXml': 'file://' + filename}}} shape = self.resolver.get_shape_by_name( 'map-paramfile-test', 'ExplicitMapParamFileTest') visited = ParamFileVisitor().visit(params, shape) params['jobs']['job1']['script'] = contents self.assertEqual(params, visited) def test_blob_visitor(self): contents = b'This is a test' filename = self.files.create_file('jobOne.hql', contents, mode='wb') # We have modified our test model to mark jobXml with x-no-paramfile. params = {'jobs': {'job1': {'script': 'fileb://' + filename, 'jobXml': 'fileb://' + filename}}} shape = self.resolver.get_shape_by_name( 'blob-test', 'BlobParamFileTest') visited = ParamFileVisitor().visit(params, shape) params['jobs']['job1']['script'] = contents self.assertEqual(params, visited)
def create_argument_model_from_schema(schema): # Given a JSON schema (described in schema.py), convert it # to a shape object from `cdp.model.Shape` that can be # used as the argument_model for the Argument classes below. transformer = SchemaTransformer() shapes_map = transformer.transform(schema) shape_resolver = ShapeResolver(shapes_map) # The SchemaTransformer guarantees that the top level shape # will always be named 'InputShape'. arg_shape = shape_resolver.get_shape_by_name('InputShape', 'InputShape') return arg_shape
def test_documents_enum_values(self): shape_map = { 'EnumArg': { 'type': 'string', 'enum': ['FOO', 'BAZ'] } } shape = StringShape('EnumArg', shape_map['EnumArg'], ShapeResolver(shape_map)) arg_table = {'arg-name': mock.Mock(argument_model=shape, _UNDOCUMENTED=False)} help_command = mock.Mock() help_command.doc = ReSTDocument() help_command.arg_table = arg_table operation_model = mock.Mock() operation_model.service_model.operation_names = [] help_command.obj = operation_model operation_generator = OperationDocumentGenerator(help_command) operation_generator.doc_option('arg-name', help_command) rendered = help_command.doc.getvalue().decode('utf-8') self.assertIn('Possible values', rendered) self.assertIn('FOO', rendered) self.assertIn('BAZ', rendered)
def setUp(self): model = yaml.safe_load(open(os.path.join(MODEL_DIR, 'service.yaml'))) self.resolver = ShapeResolver(model['definitions'])
class TestShapeResolver(unittest.TestCase): def setUp(self): model = yaml.safe_load(open(os.path.join(MODEL_DIR, 'service.yaml'))) self.resolver = ShapeResolver(model['definitions']) def _check_endpoint_shape(self, shape): self.assertEqual(shape.name, 'endpoint') self.assertEqual(shape.type_name, 'object') self.assertEqual(len(shape.required_members), 2) self.assertTrue('address' in shape.required_members) self.assertTrue('port' in shape.required_members) self.assertEqual(shape.members['address'].name, 'address') self.assertEqual(shape.members['address'].type_name, 'string') self.assertEqual(shape.members['port'].name, 'port') self.assertEqual(shape.members['port'].type_name, 'integer') def test_get_shape_by_name(self): shape = self.resolver.get_shape_by_name('endpoint', 'Endpoint') self._check_endpoint_shape(shape) def test_resolve_shape_reference(self): shape = self.resolver.resolve_shape_ref('endpoint', '#/definitions/Endpoint') self._check_endpoint_shape(shape) def test_min_and_max_length(self): shape = self.resolver.get_shape_by_name('shape', 'MinAndMaxLengthTest') self.assertEqual(shape.type_name, 'object') self.assertEqual(shape.members['stringparam'].min_length, 10) self.assertEqual(shape.members['stringparam'].max_length, 20) def test_minimum_and_maximum(self): shape = self.resolver.get_shape_by_name('shape', 'MinimumAndMaximumTest') self.assertEqual(shape.type_name, 'object') self.assertEqual(shape.members['intparam'].minimum, 15) self.assertEqual(shape.members['intparam'].maximum, 30) def test_list_shape(self): shape = self.resolver.get_shape_by_name('shape', 'DescribeDirectorsRequest') self.assertEqual(shape.members['names'].name, 'names') self.assertEqual(shape.members['names'].type_name, 'array') self.assertEqual(shape.members['names'].member.name, 'items') self.assertEqual(shape.members['names'].member.type_name, 'string') def test_string_shape_with_enum(self): shape = self.resolver.get_shape_by_name('shape', 'Director') self.assertEqual(shape.members['status'].name, 'status') self.assertEqual(shape.members['status'].type_name, 'string') self.assertEqual(shape.members['status'].enum, [ 'UNAVAILABLE', 'CREATING', 'STOPPED', 'STARTING', 'RUNNING', 'STOPPING', 'DESTROYING', 'UNKNOWN' ]) def test_no_paramfile(self): shape = self.resolver.get_shape_by_name('shape', 'NoParamfileTest') self.assertEqual(shape.type_name, 'object') self.assertEqual(shape.members['stringparam1'].is_no_paramfile, True) self.assertEqual(shape.members['stringparam2'].is_no_paramfile, False) def test_unknown_shape(self): with self.assertRaises(NoShapeFoundError): self.resolver.get_shape_by_name('unknown', 'Unknown') def test_invalid_shape_type(self): with self.assertRaises(InvalidShapeError): self.resolver.get_shape_by_name('invalid', 'MissingShapeType') with self.assertRaises(InvalidShapeError): self.resolver.get_shape_by_name('invalid', 'InvalidShapeType') def test_undocumented_param(self): shape = self.resolver.get_shape_by_name('shape', 'UndocumentedParmTest') self.assertEqual(shape.members['undocumentedParam'].is_undocumented, True) self.assertEqual(shape.members['documentedParam'].is_undocumented, False) def test_referenced_map(self): shape = self.resolver.get_shape_by_name('shape', 'ReferencedMapTest') self.assertEqual(shape.members['endpoints'].type_name, 'map') self.assertEqual(shape.members['endpoints'].key.type_name, 'string') self.assertEqual(shape.members['endpoints'].value.type_name, 'object') self.assertTrue( self._compare_lists( list(shape.members['endpoints'].value.members.keys()), ['port', 'address'])) def test_explicit_map(self): shape = self.resolver.get_shape_by_name('shape', 'ExplicitTypeMapTest') self.assertEqual(shape.members['endpoints'].type_name, 'map') self.assertEqual(shape.members['endpoints'].key.type_name, 'string') self.assertEqual(shape.members['endpoints'].value.type_name, 'object') self.assertTrue( self._compare_lists( list(shape.members['endpoints'].value.members.keys()), ['port', 'address'])) def test_no_freeform_map_dict_shape(self): with self.assertRaises(InvalidShapeError): shape = self.resolver.get_shape_by_name('invalid', 'NoFreeformMapDictTest') shape.members['endpoints'].value def test_no_freeform_map_true_shape(self): with self.assertRaises(InvalidShapeError): shape = self.resolver.get_shape_by_name('invalid', 'NoFreeformMapTrueTest') shape.members['endpoints'].value def test_blob_shape(self): shape = self.resolver.get_shape_by_name('shape', 'BlobTest') self.assertEqual(shape.members['blobparam'].type_name, 'blob') def _compare_lists(self, x, y): return collections.Counter(x) == collections.Counter(y)
def setUp(self): self.model = yaml.safe_load(open(os.path.join(MODEL_DIR, 'service.yaml'))) self.service_model = ServiceModel(self.model, 'servicename') self.resolver = ShapeResolver(self.model['definitions']) self.files = FileCreator()