def test_make_flat_dict(self): expected_xml = ("<?xml version='1.0' encoding='UTF-8'?>\n" '<wrapper><a>foo</a><b>bar</b></wrapper>') root = xmlutil.make_flat_dict('wrapper') tmpl = xmlutil.MasterTemplate(root, 1) result = tmpl.serialize(dict(wrapper=dict(a='foo', b='bar'))) self.assertEqual(result, expected_xml)
def construct(self): root = xmlutil.TemplateElement('floating_ip_pools') elem = xmlutil.SubTemplateElement(root, 'floating_ip_pool', selector='floating_ip_pools') make_float_ip(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('diagnostics') elem = xmlutil.SubTemplateElement(root, xmlutil.Selector(0), selector=xmlutil.get_items) elem.text = 1 return xmlutil.MasterTemplate(root, 1, nsmap=sd_nsmap)
def construct(self): root = xmlutil.TemplateElement('consoles') console = xmlutil.SubTemplateElement(root, 'console', selector='consoles') console.append(ConsoleTemplate()) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('keypairs') elem = xmlutil.make_flat_dict('keypair', selector='keypairs', subselector='keypair') root.append(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('tenant_usages') elem = xmlutil.SubTemplateElement(root, 'tenant_usage', selector='tenant_usages') make_usage(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('volumeAttachments') elem = xmlutil.SubTemplateElement(root, 'volumeAttachment', selector='volumeAttachments') make_attachment(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('snapshots') elem = xmlutil.SubTemplateElement(root, 'snapshot', selector='snapshots') make_snapshot(elem) return xmlutil.MasterTemplate(root, 1)
def test_master_attach(self): # Set up a master template elem = xmlutil.TemplateElement('test') tmpl = xmlutil.MasterTemplate(elem, 1) # Make sure it has a root but no slaves self.assertEqual(tmpl.root, elem) self.assertEqual(len(tmpl.slaves), 0) # Try to attach an invalid slave bad_elem = xmlutil.TemplateElement('test2') self.assertRaises(ValueError, tmpl.attach, bad_elem) self.assertEqual(len(tmpl.slaves), 0) # Try to attach an invalid and a valid slave good_elem = xmlutil.TemplateElement('test') self.assertRaises(ValueError, tmpl.attach, good_elem, bad_elem) self.assertEqual(len(tmpl.slaves), 0) # Try to attach an inapplicable template class InapplicableTemplate(xmlutil.Template): def apply(self, master): return False inapp_tmpl = InapplicableTemplate(good_elem) tmpl.attach(inapp_tmpl) self.assertEqual(len(tmpl.slaves), 0) # Now try attaching an applicable template tmpl.attach(good_elem) self.assertEqual(len(tmpl.slaves), 1) self.assertEqual(tmpl.slaves[0].root, good_elem)
def test_slave_apply(self): # Construct a master template elem = xmlutil.TemplateElement('test') master = xmlutil.MasterTemplate(elem, 3) # Construct a slave template with applicable minimum version slave = xmlutil.SlaveTemplate(elem, 2) self.assertEqual(slave.apply(master), True) # Construct a slave template with equal minimum version slave = xmlutil.SlaveTemplate(elem, 3) self.assertEqual(slave.apply(master), True) # Construct a slave template with inapplicable minimum version slave = xmlutil.SlaveTemplate(elem, 4) self.assertEqual(slave.apply(master), False) # Construct a slave template with applicable version range slave = xmlutil.SlaveTemplate(elem, 2, 4) self.assertEqual(slave.apply(master), True) # Construct a slave template with low version range slave = xmlutil.SlaveTemplate(elem, 1, 2) self.assertEqual(slave.apply(master), False) # Construct a slave template with high version range slave = xmlutil.SlaveTemplate(elem, 4, 5) self.assertEqual(slave.apply(master), False) # Construct a slave template with matching version range slave = xmlutil.SlaveTemplate(elem, 3, 3) self.assertEqual(slave.apply(master), True)
def construct(self): root = xmlutil.TemplateElement('extensions') elem = xmlutil.SubTemplateElement(root, 'extension', selector='extensions') make_ext(elem) return xmlutil.MasterTemplate(root, 1, nsmap=ext_nsmap)
def construct(self): root = xmlutil.TemplateElement('actions') elem = xmlutil.SubTemplateElement(root, 'action', selector='actions') elem.set('created_at') elem.set('action') elem.set('error') return xmlutil.MasterTemplate(root, 1, nsmap=sa_nsmap)
def construct(self): root = xmlutil.TemplateElement('virtual_interfaces') elem = xmlutil.SubTemplateElement(root, 'virtual_interface', selector='virtual_interfaces') elem.set('id') elem.set('mac_address') return xmlutil.MasterTemplate(root, 1, nsmap=vif_nsmap)
def construct(self): root = MetadataTemplateElement('metadata', selector='metadata') elem = xmlutil.SubTemplateElement(root, 'meta', selector=xmlutil.get_items) elem.set('key', 0) elem.text = 1 return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
def construct(self): root = xmlutil.TemplateElement('weights') weight = xmlutil.SubTemplateElement(root, 'weight', selector='weights') blob = xmlutil.SubTemplateElement(weight, 'blob') blob.text = 'blob' inner_weight = xmlutil.SubTemplateElement(weight, 'weight') inner_weight.text = 'weight' return xmlutil.MasterTemplate(root, 1, nsmap=zone_nsmap)
def construct(self): root = xmlutil.TemplateElement('account', selector='account') root.set('id', 'id') root.set('name', 'name') root.set('description', 'description') root.set('manager', 'manager') return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('quota_set', selector='quota_set') root.set('id') for resource in quota_resources: elem = xmlutil.SubTemplateElement(root, resource) elem.text = resource 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 construct(self): def shimmer(obj, do_raise=False): # A bare list is passed in; we need to wrap it in a dict return dict(hosts=obj) root = xmlutil.TemplateElement('hosts', selector=shimmer) elem = xmlutil.SubTemplateElement(root, 'host', selector='hosts') elem.set('host_name') elem.set('service') return xmlutil.MasterTemplate(root, 1)
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('console', selector='console') id_elem = xmlutil.SubTemplateElement(root, 'id', selector='id') id_elem.text = xmlutil.Selector() port_elem = xmlutil.SubTemplateElement(root, 'port', selector='port') port_elem.text = xmlutil.Selector() host_elem = xmlutil.SubTemplateElement(root, 'host', selector='host') host_elem.text = xmlutil.Selector() passwd_elem = xmlutil.SubTemplateElement(root, 'password', selector='password') passwd_elem.text = xmlutil.Selector() constype_elem = xmlutil.SubTemplateElement(root, 'console_type', selector='console_type') constype_elem.text = xmlutil.Selector() 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): elem = xmlutil.TemplateElement('test') return xmlutil.MasterTemplate(elem, 1)
def test__serialize(self): # Our test object to serialize obj = { 'test': { 'name': 'foobar', 'values': [1, 2, 3, 4], 'attrs': { 'a': 1, 'b': 2, 'c': 3, 'd': 4, }, 'image': { 'name': 'image_foobar', 'id': 42, }, }, } # Set up our master template root = xmlutil.TemplateElement('test', selector='test', name='name') value = xmlutil.SubTemplateElement(root, 'value', selector='values') value.text = xmlutil.Selector() attrs = xmlutil.SubTemplateElement(root, 'attrs', selector='attrs') xmlutil.SubTemplateElement(attrs, 'attr', selector=xmlutil.get_items, key=0, value=1) master = xmlutil.MasterTemplate(root, 1, nsmap=dict(f='foo')) # Set up our slave template root_slave = xmlutil.TemplateElement('test', selector='test') image = xmlutil.SubTemplateElement(root_slave, 'image', selector='image', id='id') image.text = xmlutil.Selector('name') slave = xmlutil.SlaveTemplate(root_slave, 1, nsmap=dict(b='bar')) # Attach the slave to the master... master.attach(slave) # Try serializing our object siblings = master._siblings() nsmap = master._nsmap() result = master._serialize(None, obj, siblings, nsmap) # Now we get to manually walk the element tree... self.assertEqual(result.tag, 'test') self.assertEqual(len(result.nsmap), 2) self.assertEqual(result.nsmap['f'], 'foo') self.assertEqual(result.nsmap['b'], 'bar') self.assertEqual(result.get('name'), obj['test']['name']) for idx, val in enumerate(obj['test']['values']): self.assertEqual(result[idx].tag, 'value') self.assertEqual(result[idx].text, str(val)) idx += 1 self.assertEqual(result[idx].tag, 'attrs') for attr in result[idx]: self.assertEqual(attr.tag, 'attr') self.assertEqual(attr.get('value'), str(obj['test']['attrs'][attr.get('key')])) idx += 1 self.assertEqual(result[idx].tag, 'image') self.assertEqual(result[idx].get('id'), str(obj['test']['image']['id'])) self.assertEqual(result[idx].text, obj['test']['image']['name'])
def construct(self): root = xmlutil.TemplateElement('servers') elem = xmlutil.SubTemplateElement(root, 'server', selector='servers') make_server(elem, detailed=True) return xmlutil.MasterTemplate(root, 1, nsmap=server_nsmap)
def construct(self): root = xmlutil.TemplateElement('servers') elem = xmlutil.SubTemplateElement(root, 'server', selector='servers') make_server(elem) xmlutil.make_links(root, 'servers_links') return xmlutil.MasterTemplate(root, 1, nsmap=server_nsmap)
def construct(self): return xmlutil.MasterTemplate(xmlutil.make_flat_dict('keypair'), 1)
def test(self): root = xmlutil.TemplateElement('servers') a = xmlutil.SubTemplateElement(root, 'a', selector='servers') a.text = xmlutil.Selector('a') return xmlutil.MasterTemplate(root, 1, nsmap={None: "asdf"})
def construct(self): root = xmlutil.TemplateElement('host') root.set('host') root.set('status') return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('host') root.set('host') root.set('power_action') return xmlutil.MasterTemplate(root, 1)