def construct(self): root = xmlutil.TemplateElement('transfers') elem = xmlutil.SubTemplateElement(root, 'transfer', selector='transfers') make_transfer(elem) alias = Volume_transfer.alias namespace = Volume_transfer.namespace return xmlutil.MasterTemplate(root, 1, nsmap={alias: namespace})
def test_element_attribute_keys(self): attrs = dict(a=1, b=2, c=3, d=4) expected = set(attrs.keys()) # Create a template element with some attributes elem = xmlutil.TemplateElement('test', attrib=attrs) # Now verify keys self.assertEqual(expected, set(elem.keys()))
def construct(self): root = xmlutil.TemplateElement('volumes') elem = xmlutil.SubTemplateElement(root, 'volume', selector='volume') elem.append(VolumeImageMetadataMetadataTemplate()) alias = Volume_image_metadata.alias namespace = Volume_image_metadata.namespace return xmlutil.SlaveTemplate(root, 1, nsmap={alias: namespace})
def construct(self): root = xmlutil.TemplateElement('volume_image_metadata', selector='volume_image_metadata') elem = xmlutil.SubTemplateElement(root, 'meta', selector=xmlutil.get_items) elem.set('key', 0) elem.text = 1 return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('quota_set', selector='quota_set') root.set('id') for resource in QUOTAS.resources: elem = xmlutil.SubTemplateElement(root, resource) elem.text = resource return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('cgsnapshots') elem = xmlutil.SubTemplateElement(root, 'cgsnapshot', selector='cgsnapshots') make_cgsnapshot(elem) alias = Cgsnapshots.alias namespace = Cgsnapshots.namespace return xmlutil.MasterTemplate(root, 1, nsmap={alias: namespace})
def construct(self): root = xmlutil.TemplateElement('snapshots') elem = xmlutil.SubTemplateElement(root, 'snapshot', selector='snapshots') make_snapshot(elem) alias = Extended_snapshot_attributes.alias namespace = Extended_snapshot_attributes.namespace return xmlutil.SlaveTemplate(root, 1, nsmap={alias: namespace})
def test__nsmap(self): # Set up a basic template elem = xmlutil.TemplateElement('test') tmpl = xmlutil.Template(elem, nsmap=dict(a="foo")) # Check out that we get the right namespace dictionary nsmap = tmpl._nsmap() self.assertNotEqual(id(nsmap), id(tmpl.nsmap)) self.assertEqual(len(nsmap), 1) self.assertEqual(nsmap['a'], 'foo')
def test_master_copy(self): # Construct a master template elem = xmlutil.TemplateElement('test') tmpl = xmlutil.MasterTemplate(elem, 1, nsmap=dict(a='foo')) # Give it a slave slave = xmlutil.TemplateElement('test') tmpl.attach(slave) # Construct a copy copy = tmpl.copy() # Check to see if we actually managed a copy self.assertNotEqual(tmpl, copy) self.assertEqual(tmpl.root, copy.root) self.assertEqual(tmpl.version, copy.version) self.assertEqual(id(tmpl.nsmap), id(copy.nsmap)) self.assertNotEqual(id(tmpl.slaves), id(copy.slaves)) self.assertEqual(len(tmpl.slaves), len(copy.slaves)) self.assertEqual(tmpl.slaves[0], copy.slaves[0])
def construct(self): root = xmlutil.TemplateElement('hosts') elem = xmlutil.SubTemplateElement(root, 'host', selector='hosts') elem.set('service-status') elem.set('service') elem.set('zone') elem.set('service-state') elem.set('host_name') elem.set('last-update') return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('services') elem = xmlutil.SubTemplateElement(root, 'service', selector='services') elem.set('binary') elem.set('host') elem.set('zone') elem.set('status') elem.set('state') elem.set('update_at') return xmlutil.MasterTemplate(root, 1)
def construct(self): tagname = xmlutil.Selector('key') def extraspec_sel(obj, do_raise=False): # Have to extract the key and value for later use... key, value = obj.items()[0] return dict(key=key, value=value) root = xmlutil.TemplateElement(tagname, selector=extraspec_sel) root.text = 'value' return xmlutil.MasterTemplate(root, 1)
def test_element_initial_attributes(self): # Create a template element with some attributes elem = xmlutil.TemplateElement('test', attrib=dict(a=1, b=2, c=3), c=4, d=5, e=6) # Verify all the attributes are as expected expected = dict(a=1, b=2, c=4, d=5, e=6) for k, v in expected.items(): self.assertEqual(elem.attrib[k].chain[0], v)
def test_element_insert_child(self): # Create an element elem = xmlutil.TemplateElement('test') # Make sure the element starts off empty self.assertEqual(len(elem), 0) # Create a few children children = [xmlutil.TemplateElement('child1'), xmlutil.TemplateElement('child2'), xmlutil.TemplateElement('child3'), ] # Extend the parent by those children elem.extend(children) # Create a child to insert child = xmlutil.TemplateElement('child4') # Insert it elem.insert(1, child) # Ensure the child was inserted in the right place self.assertEqual(len(elem), 4) children.insert(1, child) for idx in range(len(elem)): self.assertEqual(children[idx], elem[idx]) self.assertEqual(children[idx].tag in elem, True) self.assertEqual(elem[children[idx].tag], children[idx]) # Ensure that multiple children of the same name are rejected child2 = xmlutil.TemplateElement('child2') self.assertRaises(KeyError, elem.insert, 2, child2)
def test_element_extend_children(self): # Create an element elem = xmlutil.TemplateElement('test') # Make sure the element starts off empty self.assertEqual(len(elem), 0) # Create a few children children = [xmlutil.TemplateElement('child1'), xmlutil.TemplateElement('child2'), xmlutil.TemplateElement('child3'), ] # Extend the parent by those children elem.extend(children) # Verify that the children were added self.assertEqual(len(elem), 3) for idx in range(len(elem)): self.assertEqual(children[idx], elem[idx]) self.assertEqual(children[idx].tag in elem, True) self.assertEqual(elem[children[idx].tag], children[idx]) # Ensure that multiple children of the same name are rejected children2 = [xmlutil.TemplateElement('child4'), xmlutil.TemplateElement('child1'), ] self.assertRaises(KeyError, elem.extend, children2) # Also ensure that child4 was not added self.assertEqual(len(elem), 3) self.assertEqual(elem[-1].tag, 'child3')
def test_element_remove_child(self): # Create an element elem = xmlutil.TemplateElement('test') # Make sure the element starts off empty self.assertEqual(len(elem), 0) # Create a few children children = [ xmlutil.TemplateElement('child1'), xmlutil.TemplateElement('child2'), xmlutil.TemplateElement('child3'), ] # Extend the parent by those children elem.extend(children) # Create a test child to remove child = xmlutil.TemplateElement('child2') # Try to remove it self.assertRaises(ValueError, elem.remove, child) # Ensure that no child was removed self.assertEqual(len(elem), 3) # Now remove a legitimate child elem.remove(children[1]) # Ensure that the child was removed self.assertEqual(len(elem), 2) self.assertEqual(elem[0], children[0]) self.assertEqual(elem[1], children[2]) self.assertNotIn('child2', elem) # Ensure the child cannot be retrieved by name def get_key(elem, key): return elem[key] self.assertRaises(KeyError, get_key, elem, 'child2')
def test_apply_text(self): # Create a template element tmpl_elem = xmlutil.TemplateElement('test') tmpl_elem.text = xmlutil.ConstantSelector(1) # Create an etree element elem = etree.Element('test') # Apply the template to the element tmpl_elem.apply(elem, None) # Now, verify the text was set self.assertEqual(str(tmpl_elem.text.value), elem.text)
def test_element_append_child(self): # Create an element elem = xmlutil.TemplateElement('test') # Make sure the element starts off empty self.assertEqual(len(elem), 0) # Create a child element child = xmlutil.TemplateElement('child') # Append the child to the parent elem.append(child) # Verify that the child was added self.assertEqual(len(elem), 1) self.assertEqual(elem[0], child) self.assertEqual('child' in elem, True) self.assertEqual(elem['child'], child) # Ensure that multiple children of the same name are rejected child2 = xmlutil.TemplateElement('child') self.assertRaises(KeyError, elem.append, child2)
def construct(self): # TODO(uni): template elements of 'host', 'service' and 'disabled' # should be deprecated to make ServicesUpdateTemplate consistent # with ServicesIndexTemplate. Still keeping it here for API # compatibility sake. root = xmlutil.TemplateElement('host') root.set('host') root.set('service') root.set('disabled') root.set('binary') root.set('status') return xmlutil.MasterTemplate(root, 1)
def test_dyntag(self): obj = ['a', 'b', 'c'] # Create a template element with a dynamic tag tmpl_elem = xmlutil.TemplateElement(xmlutil.Selector()) # Try the render parent = etree.Element('parent') elems = tmpl_elem.render(parent, obj) # Verify the particulars of the render self.assertEqual(len(elems), len(obj)) for idx in range(len(obj)): self.assertEqual(elems[idx][0].tag, obj[idx])
def construct(self): root = xmlutil.TemplateElement('os-volume_upload_image', selector='os-volume_upload_image') root.set('id') root.set('updated_at') root.set('status') root.set('display_description') root.set('size') root.set('volume_type') root.set('image_id') root.set('container_format') root.set('disk_format') root.set('image_name') return xmlutil.MasterTemplate(root, 1)
def test_apply_attrs(self): # Create a template element attrs = dict(attr1=xmlutil.ConstantSelector(1), attr2=xmlutil.ConstantSelector(2)) tmpl_elem = xmlutil.TemplateElement('test', attrib=attrs) # Create an etree element elem = etree.Element('test') # Apply the template to the element tmpl_elem.apply(elem, None) # Now, verify the correct attributes were set for k, v in elem.items(): self.assertEqual(str(attrs[k].value), v)
def construct(self): root = xmlutil.TemplateElement('services') elem = xmlutil.SubTemplateElement(root, 'service', selector='services') elem.set('binary') elem.set('host') elem.set('zone') elem.set('status') elem.set('state') elem.set('update_at') elem.set('disabled_reason') elem.set('replication_status') elem.set('active_backend_id') elem.set('frozen') return xmlutil.MasterTemplate(root, 1)
def test_element_attribute_items(self): expected = dict(a=xmlutil.Selector(1), b=xmlutil.Selector(2), c=xmlutil.Selector(3)) keys = set(expected.keys()) # Create a template element with some attributes elem = xmlutil.TemplateElement('test', attrib=expected) # Now verify items for k, v in elem.items(): self.assertEqual(expected[k], v) keys.remove(k) # Did we visit all keys? self.assertEqual(len(keys), 0)
def test_element_set_attributes(self): attrs = dict(a=None, b='foo', c=xmlutil.Selector('foo', 'bar')) # Create a bare template element with no attributes elem = xmlutil.TemplateElement('test') # Set the attribute values for k, v in attrs.items(): elem.set(k, v) # Now verify what got set self.assertEqual(len(elem.attrib['a'].chain), 1) self.assertEqual(elem.attrib['a'].chain[0], 'a') self.assertEqual(len(elem.attrib['b'].chain), 1) self.assertEqual(elem.attrib['b'].chain[0], 'foo') self.assertEqual(elem.attrib['c'], attrs['c'])
def construct(self): root = xmlutil.TemplateElement('os-volume_upload_image', selector='os-volume_upload_image') root.set('id') root.set('updated_at') root.set('status') root.set('display_description') root.set('size') root.set('volume_type') root.set('image_id') root.set('container_format') root.set('disk_format') root.set('image_name') root.set('protected') if CONF.glance_api_version == 2: root.set('visibility') else: root.set('is_public') return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('limits', selector='limits') rates = xmlutil.SubTemplateElement(root, 'rates') rate = xmlutil.SubTemplateElement(rates, 'rate', selector='rate') rate.set('uri', 'uri') rate.set('regex', 'regex') limit = xmlutil.SubTemplateElement(rate, 'limit', selector='limit') limit.set('value', 'value') limit.set('verb', 'verb') limit.set('remaining', 'remaining') limit.set('unit', 'unit') limit.set('next-available', 'next-available') absolute = xmlutil.SubTemplateElement(root, 'absolute', selector='absolute') limit = xmlutil.SubTemplateElement(absolute, 'limit', selector=xmlutil.get_items) limit.set('name', 0) limit.set('value', 1) return xmlutil.MasterTemplate(root, 1, nsmap=limits_nsmap)
def construct(self): sel = xmlutil.Selector('meta', xmlutil.get_items, 0) root = xmlutil.TemplateElement('meta', selector=sel) root.set('key', 0) root.text = 1 return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
def construct(self): root = xmlutil.TemplateElement('snapshot', selector='snapshot') make_snapshot(root) return xmlutil.MasterTemplate(root, 1)
def construct(self): elem = xmlutil.TemplateElement('test') return xmlutil.SlaveTemplate(elem, 1)