Always generate fresh uuid in test cases
not doing so introduces implicit (ordering) dependency issues between test cases. For instance, in python client functional test[1] renaming `TestSimplePythonAPI.test_introspect_get_status` to `TestSimplePythonAPI.test_01_introspect_get_status` makes `TestSimplePythonAPI.test_api_version` fail. Notes: I use this command to check for the cases where fixed uuids are being used: `git grep -En "\<[^\s,\.(]*uuid[^\s,\.]*\s*=\s*['\"]"` Links: [1] https://github.com/openstack/python-ironic-inspector-client/blob/ master/ironic_inspector_client/test/functional.py#L137 Change-Id: Icd4a2a25824c6372d10fc40d67d8f275016b201c
This commit is contained in:
parent
003a321566
commit
61d0b85ed6
@ -17,6 +17,7 @@ import mock
|
||||
from oslo_concurrency import lockutils
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
from ironic_inspector.common import i18n
|
||||
# Import configuration options
|
||||
@ -84,7 +85,7 @@ class BaseTest(unittest.TestCase):
|
||||
class NodeTest(BaseTest):
|
||||
def setUp(self):
|
||||
super(NodeTest, self).setUp()
|
||||
self.uuid = '1a1a1a1a-2b2b-3c3c-4d4d-5e5e5e5e5e5e'
|
||||
self.uuid = uuidutils.generate_uuid()
|
||||
self.bmc_address = '1.2.3.4'
|
||||
self.macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']
|
||||
self.node = mock.Mock(driver='pxe_ipmitool',
|
||||
|
@ -104,7 +104,7 @@ class TestApiIntrospect(BaseAPITest):
|
||||
|
||||
@mock.patch.object(introspect, 'introspect', autospec=True)
|
||||
def test_introspect_invalid_uuid(self, introspect_mock):
|
||||
uuid_dummy = 'uuid1'
|
||||
uuid_dummy = 'invalid-uuid'
|
||||
res = self.app.post('/v1/introspection/%s' % uuid_dummy)
|
||||
self.assertEqual(400, res.status_code)
|
||||
|
||||
|
@ -18,6 +18,7 @@ import unittest
|
||||
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
from ironic_inspector import db
|
||||
from ironic_inspector import node_cache
|
||||
@ -30,26 +31,26 @@ CONF = cfg.CONF
|
||||
class TestNodeCache(test_base.NodeTest):
|
||||
def test_add_node(self):
|
||||
# Ensure previous node information is cleared
|
||||
uuid2 = uuidutils.generate_uuid()
|
||||
session = db.get_session()
|
||||
with session.begin():
|
||||
db.Node(uuid=self.node.uuid).save(session)
|
||||
db.Node(uuid='uuid2').save(session)
|
||||
db.Node(uuid=uuid2).save(session)
|
||||
db.Attribute(name='mac',
|
||||
value='11:22:11:22:11:22',
|
||||
uuid=self.uuid).save(session)
|
||||
|
||||
res = node_cache.add_node(self.node.uuid, mac=self.macs,
|
||||
bmc_address='1.2.3.4', foo=None)
|
||||
self.assertEqual(self.uuid, res.uuid)
|
||||
self.assertTrue(time.time() - 60 < res.started_at < time.time() + 60)
|
||||
self.assertFalse(res._locked)
|
||||
node = node_cache.add_node(self.node.uuid, mac=self.macs,
|
||||
bmc_address='1.2.3.4', foo=None)
|
||||
self.assertEqual(self.uuid, node.uuid)
|
||||
self.assertTrue(time.time() - 60 < node.started_at < time.time() + 60)
|
||||
self.assertFalse(node._locked)
|
||||
|
||||
res = (db.model_query(db.Node.uuid,
|
||||
db.Node.started_at).order_by(db.Node.uuid).all())
|
||||
self.assertEqual(['1a1a1a1a-2b2b-3c3c-4d4d-5e5e5e5e5e5e',
|
||||
'uuid2'], [t.uuid for t in res])
|
||||
self.assertTrue(time.time() - 60 < res[0].started_at <
|
||||
time.time() + 60)
|
||||
res = set(db.model_query(db.Node.uuid,
|
||||
db.Node.started_at).all())
|
||||
|
||||
expected = {(node.uuid, node.started_at), (uuid2, None)}
|
||||
self.assertEqual(expected, res)
|
||||
|
||||
res = (db.model_query(db.Attribute.name,
|
||||
db.Attribute.value, db.Attribute.uuid).
|
||||
@ -88,7 +89,7 @@ class TestNodeCache(test_base.NodeTest):
|
||||
def test_delete_nodes_not_in_list(self, mock__delete_node,
|
||||
mock__list_node_uuids,
|
||||
mock__get_lock_ctx):
|
||||
uuid2 = 'uuid2'
|
||||
uuid2 = uuidutils.generate_uuid()
|
||||
uuids = {self.uuid}
|
||||
mock__list_node_uuids.return_value = {self.uuid, uuid2}
|
||||
session = db.get_session()
|
||||
@ -100,10 +101,11 @@ class TestNodeCache(test_base.NodeTest):
|
||||
|
||||
def test_add_node_duplicate_mac(self):
|
||||
session = db.get_session()
|
||||
uuid = uuidutils.generate_uuid()
|
||||
with session.begin():
|
||||
db.Node(uuid='another-uuid').save(session)
|
||||
db.Node(uuid=uuid).save(session)
|
||||
db.Attribute(name='mac', value='11:22:11:22:11:22',
|
||||
uuid='another-uuid').save(session)
|
||||
uuid=uuid).save(session)
|
||||
self.assertRaises(utils.Error,
|
||||
node_cache.add_node,
|
||||
self.node.uuid, mac=['11:22:11:22:11:22'])
|
||||
@ -122,12 +124,13 @@ class TestNodeCache(test_base.NodeTest):
|
||||
|
||||
def test__list_node_uuids(self):
|
||||
session = db.get_session()
|
||||
uuid2 = uuidutils.generate_uuid()
|
||||
with session.begin():
|
||||
db.Node(uuid=self.node.uuid).save(session)
|
||||
db.Node(uuid='uuid2').save(session)
|
||||
db.Node(uuid=uuid2).save(session)
|
||||
|
||||
node_uuid_list = node_cache._list_node_uuids()
|
||||
self.assertEqual({self.uuid, 'uuid2'}, node_uuid_list)
|
||||
self.assertEqual({self.uuid, uuid2}, node_uuid_list)
|
||||
|
||||
def test_add_attribute(self):
|
||||
session = db.get_session()
|
||||
@ -424,41 +427,42 @@ class TestNodeCacheIronicObjects(unittest.TestCase):
|
||||
super(TestNodeCacheIronicObjects, self).setUp()
|
||||
self.ports = {'mac1': mock.Mock(address='mac1', spec=['address']),
|
||||
'mac2': mock.Mock(address='mac2', spec=['address'])}
|
||||
self.uuid = uuidutils.generate_uuid()
|
||||
|
||||
def test_node_provided(self, mock_ironic):
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0,
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
node=mock.sentinel.node)
|
||||
self.assertIs(mock.sentinel.node, node_info.node())
|
||||
self.assertFalse(mock_ironic.called)
|
||||
|
||||
def test_node_not_provided(self, mock_ironic):
|
||||
mock_ironic.return_value.node.get.return_value = mock.sentinel.node
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0)
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0)
|
||||
|
||||
self.assertIs(mock.sentinel.node, node_info.node())
|
||||
self.assertIs(node_info.node(), node_info.node())
|
||||
|
||||
mock_ironic.assert_called_once_with()
|
||||
mock_ironic.return_value.node.get.assert_called_once_with('uuid')
|
||||
mock_ironic.return_value.node.get.assert_called_once_with(self.uuid)
|
||||
|
||||
def test_node_ironic_preset(self, mock_ironic):
|
||||
mock_ironic2 = mock.Mock()
|
||||
mock_ironic2.node.get.return_value = mock.sentinel.node
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0,
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
ironic=mock_ironic2)
|
||||
self.assertIs(mock.sentinel.node, node_info.node())
|
||||
|
||||
self.assertFalse(mock_ironic.called)
|
||||
mock_ironic2.node.get.assert_called_once_with('uuid')
|
||||
mock_ironic2.node.get.assert_called_once_with(self.uuid)
|
||||
|
||||
def test_ports_provided(self, mock_ironic):
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0,
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
ports=self.ports)
|
||||
self.assertIs(self.ports, node_info.ports())
|
||||
self.assertFalse(mock_ironic.called)
|
||||
|
||||
def test_ports_provided_list(self, mock_ironic):
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0,
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
ports=list(self.ports.values()))
|
||||
self.assertEqual(self.ports, node_info.ports())
|
||||
self.assertFalse(mock_ironic.called)
|
||||
@ -466,26 +470,26 @@ class TestNodeCacheIronicObjects(unittest.TestCase):
|
||||
def test_ports_not_provided(self, mock_ironic):
|
||||
mock_ironic.return_value.node.list_ports.return_value = list(
|
||||
self.ports.values())
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0)
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0)
|
||||
|
||||
self.assertEqual(self.ports, node_info.ports())
|
||||
self.assertIs(node_info.ports(), node_info.ports())
|
||||
|
||||
mock_ironic.assert_called_once_with()
|
||||
mock_ironic.return_value.node.list_ports.assert_called_once_with(
|
||||
'uuid', limit=0)
|
||||
self.uuid, limit=0)
|
||||
|
||||
def test_ports_ironic_preset(self, mock_ironic):
|
||||
mock_ironic2 = mock.Mock()
|
||||
mock_ironic2.node.list_ports.return_value = list(
|
||||
self.ports.values())
|
||||
node_info = node_cache.NodeInfo(uuid='uuid', started_at=0,
|
||||
node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=0,
|
||||
ironic=mock_ironic2)
|
||||
self.assertEqual(self.ports, node_info.ports())
|
||||
|
||||
self.assertFalse(mock_ironic.called)
|
||||
mock_ironic2.node.list_ports.assert_called_once_with(
|
||||
'uuid', limit=0)
|
||||
self.uuid, limit=0)
|
||||
|
||||
|
||||
class TestUpdate(test_base.NodeTest):
|
||||
|
@ -19,6 +19,7 @@ import eventlet
|
||||
from ironicclient import exceptions
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
from ironic_inspector import firewall
|
||||
from ironic_inspector import node_cache
|
||||
@ -49,10 +50,8 @@ class BaseTest(test_base.NodeTest):
|
||||
},
|
||||
'boot_interface': '01-' + self.pxe_mac.replace(':', '-'),
|
||||
}
|
||||
self.all_ports = [
|
||||
mock.Mock(uuid='port_uuid%d' % i, address=mac)
|
||||
for i, mac in enumerate(self.macs)
|
||||
]
|
||||
self.all_ports = [mock.Mock(uuid=uuidutils.generate_uuid(),
|
||||
address=mac) for mac in self.macs]
|
||||
self.ports = [self.all_ports[1]]
|
||||
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
"""Tests for introspection rules."""
|
||||
|
||||
import mock
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
from ironic_inspector import db
|
||||
from ironic_inspector import node_cache
|
||||
@ -27,7 +28,7 @@ from ironic_inspector import utils
|
||||
class BaseTest(test_base.BaseTest):
|
||||
def setUp(self):
|
||||
super(BaseTest, self).setUp()
|
||||
self.uuid = 'uuid'
|
||||
self.uuid = uuidutils.generate_uuid()
|
||||
self.conditions_json = [
|
||||
{'op': 'eq', 'field': 'memory_mb', 'value': 1024},
|
||||
{'op': 'eq', 'field': 'local_gb', 'value': 60},
|
||||
@ -139,7 +140,7 @@ class TestGetRule(BaseTest):
|
||||
def test_get(self):
|
||||
rule_json = rules.get(self.uuid).as_dict()
|
||||
|
||||
self.assertTrue(rule_json.pop(self.uuid))
|
||||
self.assertTrue(rule_json.pop('uuid'))
|
||||
self.assertEqual({'description': None,
|
||||
'conditions': self.conditions_json,
|
||||
'actions': self.actions_json},
|
||||
@ -149,15 +150,16 @@ class TestGetRule(BaseTest):
|
||||
self.assertRaises(utils.Error, rules.get, 'foobar')
|
||||
|
||||
def test_get_all(self):
|
||||
rules.create(self.conditions_json, self.actions_json, uuid='uuid2')
|
||||
self.assertEqual([self.uuid, 'uuid2'],
|
||||
[r.as_dict()['uuid'] for r in rules.get_all()])
|
||||
uuid2 = uuidutils.generate_uuid()
|
||||
rules.create(self.conditions_json, self.actions_json, uuid=uuid2)
|
||||
self.assertEqual({self.uuid, uuid2},
|
||||
{r.as_dict()['uuid'] for r in rules.get_all()})
|
||||
|
||||
|
||||
class TestDeleteRule(BaseTest):
|
||||
def setUp(self):
|
||||
super(TestDeleteRule, self).setUp()
|
||||
self.uuid2 = self.uuid + '-2'
|
||||
self.uuid2 = uuidutils.generate_uuid()
|
||||
rules.create(self.conditions_json, self.actions_json, uuid=self.uuid)
|
||||
rules.create(self.conditions_json, self.actions_json, uuid=self.uuid2)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user