Exemple #1
0
    def test__render(self):
        attrs = dict(attr1=xmlutil.ConstantSelector(1),
                     attr2=xmlutil.ConstantSelector(2),
                     attr3=xmlutil.ConstantSelector(3))

        # Create a master template element
        master_elem = xmlutil.TemplateElement('test', attr1=attrs['attr1'])

        # Create a couple of slave template element
        slave_elems = [
            xmlutil.TemplateElement('test', attr2=attrs['attr2']),
            xmlutil.TemplateElement('test', attr3=attrs['attr3']),
        ]

        # Try the render
        elem = master_elem._render(None, None, slave_elems, None)

        # Verify the particulars of the render
        self.assertEqual(elem.tag, 'test')
        self.assertEqual(len(elem.nsmap), 0)
        for k, v in elem.items():
            self.assertEqual(str(attrs[k].value), v)

        # Create a parent for the element to be rendered
        parent = etree.Element('parent')

        # Try the render again...
        elem = master_elem._render(parent, None, slave_elems, dict(a='foo'))

        # Verify the particulars of the render
        self.assertEqual(len(parent), 1)
        self.assertEqual(parent[0], elem)
        self.assertEqual(len(elem.nsmap), 1)
        self.assertEqual(elem.nsmap['a'], 'foo')
Exemple #2
0
    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)
Exemple #3
0
    def test_render(self):
        # Create a template element
        tmpl_elem = xmlutil.TemplateElement('test')
        tmpl_elem.text = xmlutil.Selector()

        # Create the object we're going to render
        obj = ['elem1', 'elem2', 'elem3', 'elem4']

        # Try a render with no object
        elems = tmpl_elem.render(None, None)
        self.assertEqual(len(elems), 0)

        # Try a render with one object
        elems = tmpl_elem.render(None, 'foo')
        self.assertEqual(len(elems), 1)
        self.assertEqual(elems[0][0].text, 'foo')
        self.assertEqual(elems[0][1], 'foo')

        # Now, try rendering an object with multiple entries
        parent = etree.Element('parent')
        elems = tmpl_elem.render(parent, obj)
        self.assertEqual(len(elems), 4)

        # Check the results
        for idx in range(len(obj)):
            self.assertEqual(elems[idx][0].text, obj[idx])
            self.assertEqual(elems[idx][1], obj[idx])
Exemple #4
0
    def test_element_text(self):
        # Create an element
        elem = xmlutil.TemplateElement('test')

        # Ensure that it has no text
        self.assertEqual(elem.text, None)

        # Try setting it to a string and ensure it becomes a selector
        elem.text = 'test'
        self.assertEqual(hasattr(elem.text, 'chain'), True)
        self.assertEqual(len(elem.text.chain), 1)
        self.assertEqual(elem.text.chain[0], 'test')

        # Try resetting the text to None
        elem.text = None
        self.assertEqual(elem.text, None)

        # Now make up a selector and try setting the text to that
        sel = xmlutil.Selector()
        elem.text = sel
        self.assertEqual(elem.text, sel)

        # Finally, try deleting the text and see what happens
        del elem.text
        self.assertEqual(elem.text, None)
Exemple #5
0
    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)
Exemple #6
0
 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)
Exemple #7
0
 def construct(self):
     root = xmlutil.TemplateElement('volume_types')
     elem = xmlutil.SubTemplateElement(root,
                                       'volume_type',
                                       selector='volume_types')
     make_voltype(elem)
     return xmlutil.MasterTemplate(root, 1)
Exemple #8
0
    def test_element_subselector(self):
        sel = xmlutil.Selector('a', 'b')

        # Create a template element with an explicit subselector
        elem = xmlutil.TemplateElement('test', subselector=sel)

        self.assertEqual(elem.subselector, sel)
Exemple #9
0
 def construct(self):
     root = xmlutil.TemplateElement('volumes')
     elem = xmlutil.SubTemplateElement(root, 'volume', selector='volumes')
     make_volume(elem)
     alias = Volume_tenant_attribute.alias
     namespace = Volume_tenant_attribute.namespace
     return xmlutil.SlaveTemplate(root, 1, nsmap={alias: namespace})
Exemple #10
0
 def construct(self):
     root = xmlutil.TemplateElement('snapshots')
     elem = xmlutil.SubTemplateElement(root,
                                       'snapshot',
                                       selector='snapshots')
     make_snapshot(elem)
     return xmlutil.MasterTemplate(root, 1)
    def test_subordinate_apply(self):
        # Construct a main template
        elem = xmlutil.TemplateElement('test')
        main = xmlutil.MainTemplate(elem, 3)

        # Construct a subordinate template with applicable minimum version
        subordinate = xmlutil.SubordinateTemplate(elem, 2)
        self.assertEqual(subordinate.apply(main), True)

        # Construct a subordinate template with equal minimum version
        subordinate = xmlutil.SubordinateTemplate(elem, 3)
        self.assertEqual(subordinate.apply(main), True)

        # Construct a subordinate template with inapplicable minimum version
        subordinate = xmlutil.SubordinateTemplate(elem, 4)
        self.assertEqual(subordinate.apply(main), False)

        # Construct a subordinate template with applicable version range
        subordinate = xmlutil.SubordinateTemplate(elem, 2, 4)
        self.assertEqual(subordinate.apply(main), True)

        # Construct a subordinate template with low version range
        subordinate = xmlutil.SubordinateTemplate(elem, 1, 2)
        self.assertEqual(subordinate.apply(main), False)

        # Construct a subordinate template with high version range
        subordinate = xmlutil.SubordinateTemplate(elem, 4, 5)
        self.assertEqual(subordinate.apply(main), False)

        # Construct a subordinate template with matching version range
        subordinate = xmlutil.SubordinateTemplate(elem, 3, 3)
        self.assertEqual(subordinate.apply(main), True)
Exemple #12
0
    def construct(self):
        root = xmlutil.TemplateElement('host')
        elem = xmlutil.make_flat_dict('resource',
                                      selector='host',
                                      subselector='resource')
        root.append(elem)

        return xmlutil.MasterTemplate(root, 1)
Exemple #13
0
    def test_element_get_attributes(self):
        expected = dict(a=1, b=2, c=3)

        # Create a template element with some attributes
        elem = xmlutil.TemplateElement('test', attrib=expected)

        # Verify that get() retrieves the attributes
        for k, v in expected.items():
            self.assertEqual(elem.get(k).chain[0], v)
Exemple #14
0
    def test__siblings(self):
        # Set up a basic template
        elem = xmlutil.TemplateElement('test')
        tmpl = xmlutil.Template(elem)

        # Check that we get the right siblings
        siblings = tmpl._siblings()
        self.assertEqual(len(siblings), 1)
        self.assertEqual(siblings[0], elem)
Exemple #15
0
    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(set(elem.keys()), expected)
 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})
Exemple #17
0
    def construct(self):
        root = xmlutil.TemplateElement('quota_class_set',
                                       selector='quota_class_set')
        root.set('id')

        for resource in QUOTAS.resources:
            elem = xmlutil.SubTemplateElement(root, resource)
            elem.text = resource

        return xmlutil.MasterTemplate(root, 1)
Exemple #18
0
    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')
Exemple #19
0
    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')
        elem.set('topic')

        return xmlutil.MasterTemplate(root, 1)
Exemple #20
0
    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)
Exemple #21
0
    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])
Exemple #22
0
    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)
Exemple #23
0
    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')
Exemple #24
0
    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)
Exemple #25
0
    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.assertEqual('child2' in elem, False)

        # Ensure the child cannot be retrieved by name
        def get_key(elem, key):
            return elem[key]

        self.assertRaises(KeyError, get_key, elem, 'child2')
Exemple #26
0
    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)
Exemple #27
0
    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)
Exemple #28
0
 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)
Exemple #29
0
    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])
Exemple #30
0
    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)