def test_setter_na_element(self): """Tests na_element gets appended as child.""" root = NaElement('root') root['e1'] = NaElement('nested') self.assertEqual(len(root.get_children()), 1) e1 = root.get_child_by_name('e1') self.assertIsInstance(e1, NaElement) self.assertIsInstance(e1.get_child_by_name('nested'), NaElement)
def test_translate_struct_tuple(self): """Tests if tuple gets properly converted to NaElements.""" root = NaElement('root') child = ('e1', 'e2') root.translate_struct(child) self.assertEqual(len(root.get_children()), 2) self.assertIsNone(root.get_child_content('e1')) self.assertIsNone(root.get_child_content('e2'))
def test_setter_builtin_types(self): """Tests str, int, float get converted to NaElement.""" root = NaElement('root') root['e1'] = 'v1' root['e2'] = 1 root['e3'] = 2.0 root['e4'] = 8l self.assertEqual(len(root.get_children()), 4) self.assertEqual(root.get_child_content('e1'), 'v1') self.assertEqual(root.get_child_content('e2'), '1') self.assertEqual(root.get_child_content('e3'), '2.0') self.assertEqual(root.get_child_content('e4'), '8')
def test_translate_struct_dict_nonunique_key(self): """Tests if list/dict gets properly converted to NaElements.""" root = NaElement('root') child = [{'e1': 'v1', 'e2': 'v2'}, {'e1': 'v3'}] root.translate_struct(child) self.assertEqual(len(root.get_children()), 3) children = root.get_children() for c in children: if c.get_name() == 'e1': self.assertIn(c.get_content(), ['v1', 'v3']) else: self.assertEqual(c.get_content(), 'v2')
def test_setter_child_list_tuple(self): """Tests list/tuple are appended as child to root.""" root = NaElement('root') root['l'] = ['l1', 'l2'] root['t'] = ('t1', 't2') l = root.get_child_by_name('l') self.assertIsInstance(l, NaElement) t = root.get_child_by_name('t') self.assertIsInstance(t, NaElement) for le in l.get_children(): self.assertIn(le.get_name(), ['l1', 'l2']) for te in t.get_children(): self.assertIn(te.get_name(), ['t1', 't2'])
def test_setter_child_dict(self): """Tests dict is appended as child to root.""" root = NaElement('root') root['d'] = {'e1': 'v1', 'e2': 'v2'} e1 = root.get_child_by_name('d') self.assertIsInstance(e1, NaElement) sub_ch = e1.get_children() self.assertEqual(len(sub_ch), 2) for c in sub_ch: self.assertIn(c.get_name(), ['e1', 'e2']) if c.get_name() == 'e1': self.assertEqual(c.get_content(), 'v1') else: self.assertEqual(c.get_content(), 'v2')
def test_setter_invalid_key(self): """Tests invalid value raises exception.""" root = NaElement('root') try: root[None] = 'value' except Exception as e: if not isinstance(e, KeyError): self.fail(_('Error not a KeyError.'))
def test_setter_invalid_value(self): """Tests invalid value raises exception.""" root = NaElement('root') try: root['k'] = NaServer('localhost') except Exception as e: if not isinstance(e, TypeError): self.fail(_('Error not a TypeError.'))
def test_translate_struct_dict_unique_key(self): """Tests if dict gets properly converted to NaElements.""" root = NaElement('root') child = {'e1': 'v1', 'e2': 'v2', 'e3': 'v3'} root.translate_struct(child) self.assertEqual(len(root.get_children()), 3) self.assertEqual(root.get_child_content('e1'), 'v1') self.assertEqual(root.get_child_content('e2'), 'v2') self.assertEqual(root.get_child_content('e3'), 'v3')
def _create_vs_get(): """Create vs_get API request.""" vs_get = NaElement('vserver-get-iter') vs_get.add_new_child('max-records', '1') query = NaElement('query') query.add_node_with_children('vserver-info', **{'vserver-type': 'node'}) vs_get.add_child_elem(query) desired = NaElement('desired-attributes') desired.add_node_with_children( 'vserver-info', **{'vserver-name': '', 'vserver-type': ''}) vs_get.add_child_elem(desired) return vs_get
def test_setter_no_value(self): """Tests key with None value.""" root = NaElement('root') root['k'] = None self.assertIsNone(root.get_child_content('k'))
def test_translate_invalid_struct(self): """Tests if invalid data structure raises exception.""" root = NaElement('root') child = 'random child element' self.assertRaises(ValueError, root.translate_struct, child)
def _create_ems(netapp_backend, app_version, server_type): """Create ems API request.""" ems_log = NaElement('ems-autosupport-log') host = socket.getfqdn() or 'Cinder_node' if server_type == "cluster": dest = "cluster node" else: dest = "7 mode controller" ems_log.add_new_child('computer-name', host) ems_log.add_new_child('event-id', '0') ems_log.add_new_child('event-source', 'Cinder driver %s' % netapp_backend) ems_log.add_new_child('app-version', app_version) ems_log.add_new_child('category', 'provisioning') ems_log.add_new_child('event-description', 'OpenStack Cinder connected to %s' % dest) ems_log.add_new_child('log-level', '6') ems_log.add_new_child('auto-support', 'false') return ems_log
def _create_vs_get(): """Create vs_get API request.""" vs_get = NaElement('vserver-get-iter') vs_get.add_new_child('max-records', '1') query = NaElement('query') query.add_node_with_children('vserver-info', **{'vserver-type': 'node'}) vs_get.add_child_elem(query) desired = NaElement('desired-attributes') desired.add_node_with_children( 'vserver-info', **{ 'vserver-name': '', 'vserver-type': '' }) vs_get.add_child_elem(desired) return vs_get