From 4542aca5fcb10b2c71912bbcce05ee0ccb36c4ed Mon Sep 17 00:00:00 2001 From: Mikhail Durnosvistov Date: Fri, 27 Dec 2013 05:50:46 -0500 Subject: [PATCH] Get rid object model `dict` methods part 4 Currently unified object model supports dict-like behavior of objects. This behavior will be removed in the future so it's necessary to change Ironic's code to use object attributes as attributes instead of dictionary keys. This patch refactors usage of objects in `db` unit tests. Change-Id: Id7b3a35e7bec67f1b874df13d6e88660991c586f Partial-Bug: #1256260 --- ironic/tests/db/test_chassis.py | 6 ++-- ironic/tests/db/test_conductor.py | 16 +++++------ ironic/tests/db/test_nodes.py | 28 +++++++++--------- ironic/tests/db/test_ports.py | 48 +++++++++++++++---------------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/ironic/tests/db/test_chassis.py b/ironic/tests/db/test_chassis.py index 3efe49d1a5..3e86172c9c 100644 --- a/ironic/tests/db/test_chassis.py +++ b/ironic/tests/db/test_chassis.py @@ -56,13 +56,13 @@ class DbChassisTestCase(base.DbTestCase): ch = self._create_test_chassis() chassis = self.dbapi.get_chassis(ch['id']) - self.assertEqual(chassis['uuid'], ch['uuid']) + self.assertEqual(chassis.uuid, ch['uuid']) def test_get_chassis_by_uuid(self): ch = self._create_test_chassis() chassis = self.dbapi.get_chassis(ch['uuid']) - self.assertEqual(chassis['id'], ch['id']) + self.assertEqual(chassis.id, ch['id']) def test_get_chassis_that_does_not_exist(self): self.assertRaises(exception.ChassisNotFound, @@ -75,7 +75,7 @@ class DbChassisTestCase(base.DbTestCase): ch['uuid'] = new_uuid res = self.dbapi.update_chassis(ch['id'], {'uuid': new_uuid}) - self.assertEqual(res['uuid'], new_uuid) + self.assertEqual(res.uuid, new_uuid) def test_update_chassis_that_does_not_exist(self): new_uuid = ironic_utils.generate_uuid() diff --git a/ironic/tests/db/test_conductor.py b/ironic/tests/db/test_conductor.py index aa345b12ba..9ef5e95a8d 100644 --- a/ironic/tests/db/test_conductor.py +++ b/ironic/tests/db/test_conductor.py @@ -47,8 +47,8 @@ class DbConductorTestCase(base.DbTestCase): def test_get_conductor(self): c1 = self._create_test_cdr() - c2 = self.dbapi.get_conductor(c1['hostname']) - self.assertEqual(c1['id'], c2['id']) + c2 = self.dbapi.get_conductor(c1.hostname) + self.assertEqual(c1.id, c2.id) def test_get_conductor_not_found(self): self._create_test_cdr() @@ -59,24 +59,24 @@ class DbConductorTestCase(base.DbTestCase): def test_unregister_conductor(self): c = self._create_test_cdr() - self.dbapi.unregister_conductor(c['hostname']) + self.dbapi.unregister_conductor(c.hostname) self.assertRaises( exception.ConductorNotFound, self.dbapi.unregister_conductor, - c['hostname']) + c.hostname) @mock.patch.object(timeutils, 'utcnow') def test_touch_conductor(self, mock_utcnow): test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time c = self._create_test_cdr(updated_at=test_time) - self.assertEqual(test_time, timeutils.normalize_time(c['updated_at'])) + self.assertEqual(test_time, timeutils.normalize_time(c.updated_at)) test_time = datetime.datetime(2000, 1, 1, 0, 1) mock_utcnow.return_value = test_time - self.dbapi.touch_conductor(c['hostname']) - c = self.dbapi.get_conductor(c['hostname']) - self.assertEqual(test_time, timeutils.normalize_time(c['updated_at'])) + self.dbapi.touch_conductor(c.hostname) + c = self.dbapi.get_conductor(c.hostname) + self.assertEqual(test_time, timeutils.normalize_time(c.updated_at)) def test_touch_conductor_not_found(self): self._create_test_cdr() diff --git a/ironic/tests/db/test_nodes.py b/ironic/tests/db/test_nodes.py index ba6ca1e07e..c2739e267e 100644 --- a/ironic/tests/db/test_nodes.py +++ b/ironic/tests/db/test_nodes.py @@ -79,12 +79,12 @@ class DbNodeTestCase(base.DbTestCase): def test_get_node_by_id(self): n = self._create_test_node() res = self.dbapi.get_node(n['id']) - self.assertEqual(n['uuid'], res['uuid']) + self.assertEqual(n['uuid'], res.uuid) def test_get_node_by_uuid(self): n = self._create_test_node() res = self.dbapi.get_node(n['uuid']) - self.assertEqual(n['id'], res['id']) + self.assertEqual(n['id'], res.id) def test_get_node_that_does_not_exist(self): self.assertRaises(exception.NodeNotFound, @@ -219,7 +219,7 @@ class DbNodeTestCase(base.DbTestCase): instance_uuid='12345678-9999-0000-aaaa-123456789012') res = self.dbapi.get_node_by_instance(n['instance_uuid']) - self.assertEqual(n['uuid'], res['uuid']) + self.assertEqual(n['uuid'], res.uuid) def test_get_node_by_instance_wrong_uuid(self): self._create_test_node( @@ -274,7 +274,7 @@ class DbNodeTestCase(base.DbTestCase): self.dbapi.destroy_node(node_id) - self.assertRaises(exception.PortNotFound, self.dbapi.get_port, p['id']) + self.assertRaises(exception.PortNotFound, self.dbapi.get_port, p.id) def test_ports_get_destroyed_after_destroying_a_node_by_uuid(self): n = self._create_test_node() @@ -285,7 +285,7 @@ class DbNodeTestCase(base.DbTestCase): self.dbapi.destroy_node(n['uuid']) - self.assertRaises(exception.PortNotFound, self.dbapi.get_port, p['id']) + self.assertRaises(exception.PortNotFound, self.dbapi.get_port, p.id) def test_update_node(self): n = self._create_test_node() @@ -295,7 +295,7 @@ class DbNodeTestCase(base.DbTestCase): self.assertNotEqual(old_extra, new_extra) res = self.dbapi.update_node(n['id'], {'extra': new_extra}) - self.assertEqual(new_extra, res['extra']) + self.assertEqual(new_extra, res.extra) def test_update_node_not_found(self): node_uuid = ironic_utils.generate_uuid() @@ -307,9 +307,9 @@ class DbNodeTestCase(base.DbTestCase): n = self._create_test_node() new_i_uuid = ironic_utils.generate_uuid() res = self.dbapi.update_node(n['id'], {'instance_uuid': new_i_uuid}) - self.assertEqual(new_i_uuid, res['instance_uuid']) + self.assertEqual(new_i_uuid, res.instance_uuid) res = self.dbapi.update_node(n['id'], {'instance_uuid': None}) - self.assertIsNone(res['instance_uuid']) + self.assertIsNone(res.instance_uuid) def test_update_node_already_assosicated(self): n = self._create_test_node() @@ -332,7 +332,7 @@ class DbNodeTestCase(base.DbTestCase): # check reservation res = self.dbapi.get_node(uuid) - self.assertEqual(r1, res['reservation']) + self.assertEqual(r1, res.reservation) def test_release_reservation(self): n = self._create_test_node() @@ -344,7 +344,7 @@ class DbNodeTestCase(base.DbTestCase): # release reservation self.dbapi.release_nodes(r1, [uuid]) res = self.dbapi.get_node(uuid) - self.assertIsNone(res['reservation']) + self.assertIsNone(res.reservation) def test_reservation_of_reserved_node_fails(self): n = self._create_test_node() @@ -377,7 +377,7 @@ class DbNodeTestCase(base.DbTestCase): # another host succeeds self.dbapi.reserve_nodes(r2, [uuid]) res = self.dbapi.get_node(uuid) - self.assertEqual(r2, res['reservation']) + self.assertEqual(r2, res.reservation) def test_reserve_many_nodes(self): uuids = self._create_many_test_nodes() @@ -387,7 +387,7 @@ class DbNodeTestCase(base.DbTestCase): for uuid in uuids: res = self.dbapi.get_node(uuid) - self.assertEqual(r1, res['reservation']) + self.assertEqual(r1, res.reservation) def test_reserve_overlaping_ranges_fails(self): uuids = self._create_many_test_nodes() @@ -417,7 +417,7 @@ class DbNodeTestCase(base.DbTestCase): res = self.dbapi.get_node(uuids[i]) reservation = r1 if i < 3 else r2 - self.assertEqual(reservation, res['reservation']) + self.assertEqual(reservation, res.reservation) def test_reserve_empty(self): self.assertRaises(exception.InvalidIdentity, @@ -461,4 +461,4 @@ class DbNodeTestCase(base.DbTestCase): for uuid in uuids: res = self.dbapi.get_node(uuid) - self.assertIsNone(res['reservation']) + self.assertIsNone(res.reservation) diff --git a/ironic/tests/db/test_ports.py b/ironic/tests/db/test_ports.py index 6a4f63c846..40299e52f4 100644 --- a/ironic/tests/db/test_ports.py +++ b/ironic/tests/db/test_ports.py @@ -24,7 +24,7 @@ from ironic.common import utils as ironic_utils from ironic.db import api as dbapi from ironic.tests.db import base -from ironic.tests.db import utils +from ironic.tests.db import utils as db_utils class DbPortTestCase(base.DbTestCase): @@ -34,24 +34,24 @@ class DbPortTestCase(base.DbTestCase): # replaces a test for creating a port. super(DbPortTestCase, self).setUp() self.dbapi = dbapi.get_instance() - ndict = utils.get_test_node() + ndict = db_utils.get_test_node() self.n = self.dbapi.create_node(ndict) - self.p = utils.get_test_port() + self.p = db_utils.get_test_port() def test_get_port_by_id(self): self.dbapi.create_port(self.p) res = self.dbapi.get_port(self.p['id']) - self.assertEqual(self.p['address'], res['address']) + self.assertEqual(self.p['address'], res.address) def test_get_port_by_uuid(self): self.dbapi.create_port(self.p) res = self.dbapi.get_port(self.p['uuid']) - self.assertEqual(self.p['id'], res['id']) + self.assertEqual(self.p['id'], res.id) def test_get_port_list(self): uuids = [] for i in range(1, 6): - n = utils.get_test_port(id=i, uuid=ironic_utils.generate_uuid(), + n = db_utils.get_test_port(id=i, uuid=ironic_utils.generate_uuid(), address='52:54:00:cf:2d:3%s' % i) self.dbapi.create_port(n) uuids.append(six.text_type(n['uuid'])) @@ -63,7 +63,7 @@ class DbPortTestCase(base.DbTestCase): self.dbapi.create_port(self.p) res = self.dbapi.get_port(self.p['address']) - self.assertEqual(self.p['id'], res['id']) + self.assertEqual(self.p['id'], res.id) self.assertRaises(exception.PortNotFound, self.dbapi.get_port, 99) @@ -73,16 +73,16 @@ class DbPortTestCase(base.DbTestCase): self.dbapi.get_port, 'not-a-mac') def test_get_ports_by_node_id(self): - p = utils.get_test_port(node_id=self.n['id']) + p = db_utils.get_test_port(node_id=self.n.id) self.dbapi.create_port(p) - res = self.dbapi.get_ports_by_node(self.n['id']) - self.assertEqual(self.p['address'], res[0]['address']) + res = self.dbapi.get_ports_by_node(self.n.id) + self.assertEqual(self.p['address'], res[0].address) def test_get_ports_by_node_uuid(self): - p = utils.get_test_port(node_id=self.n['id']) + p = db_utils.get_test_port(node_id=self.n.id) self.dbapi.create_port(p) - res = self.dbapi.get_ports_by_node(self.n['uuid']) - self.assertEqual(self.p['address'], res[0]['address']) + res = self.dbapi.get_ports_by_node(self.n.uuid) + self.assertEqual(self.p['address'], res[0].address) def test_get_ports_by_node_that_does_not_exist(self): self.dbapi.create_port(self.p) @@ -107,30 +107,30 @@ class DbPortTestCase(base.DbTestCase): self.assertNotEqual(old_address, new_address) res = self.dbapi.update_port(self.p['id'], {'address': new_address}) - self.assertEqual(new_address, res['address']) + self.assertEqual(new_address, res.address) def test_destroy_port_on_reserved_node(self): - p = self.dbapi.create_port(utils.get_test_port(node_id=self.n['id'])) - uuid = self.n['uuid'] + p = self.dbapi.create_port(db_utils.get_test_port(node_id=self.n.id)) + uuid = self.n.uuid self.dbapi.reserve_nodes('fake-reservation', [uuid]) self.assertRaises(exception.NodeLocked, - self.dbapi.destroy_port, p['id']) + self.dbapi.destroy_port, p.id) def test_update_port_on_reserved_node(self): - p = self.dbapi.create_port(utils.get_test_port(node_id=self.n['id'])) - uuid = self.n['uuid'] + p = self.dbapi.create_port(db_utils.get_test_port(node_id=self.n.id)) + uuid = self.n.uuid self.dbapi.reserve_nodes('fake-reservation', [uuid]) new_address = 'ff.ee.dd.cc.bb.aa' self.assertRaises(exception.NodeLocked, - self.dbapi.update_port, p['id'], + self.dbapi.update_port, p.id, {'address': new_address}) def test_update_port_duplicated_address(self): self.dbapi.create_port(self.p) address1 = self.p['address'] address2 = 'aa-bb-cc-11-22-33' - p2 = utils.get_test_port(id=123, uuid=ironic_utils.generate_uuid(), - node_id=self.n['id'], address=address2) + p2 = db_utils.get_test_port(id=123, uuid=ironic_utils.generate_uuid(), + node_id=self.n.id, address=address2) self.dbapi.create_port(p2) self.assertRaises(exception.MACAlreadyExists, self.dbapi.update_port, p2['id'], @@ -139,7 +139,7 @@ class DbPortTestCase(base.DbTestCase): def test_create_port_duplicated_address(self): self.dbapi.create_port(self.p) dup_address = self.p['address'] - p2 = utils.get_test_port(id=123, uuid=ironic_utils.generate_uuid(), - node_id=self.n['id'], address=dup_address) + p2 = db_utils.get_test_port(id=123, uuid=ironic_utils.generate_uuid(), + node_id=self.n.id, address=dup_address) self.assertRaises(exception.MACAlreadyExists, self.dbapi.create_port, p2)