Replace assertDictContainsSubset in tests

This method has been removed from Python [1]. Replace it using the <=
operator to check if a dict is a subset of another.

[1] https://github.com/python/cpython/pull/28268

Change-Id: I39c87ff7e12d182bb73c782dc77d672e3194a32d
This commit is contained in:
Benjamin Schanzel
2025-03-21 14:28:28 +01:00
parent 454f1edb39
commit fa75c998ae
2 changed files with 44 additions and 41 deletions

View File

@@ -604,13 +604,13 @@ class TestDriverKubernetes(tests.DBTestCase):
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
# We don't check the value of 'locked' because we may get a # We don't check the value of 'locked' because we may get a
# cached value returned. # cached value returned.
self.assertDictContainsSubset({'id': '0000000000', self.assertTrue({'id': '0000000000',
'ipv6': None, 'ipv6': None,
'label': ['kubernetes-namespace'], 'label': ['kubernetes-namespace'],
'provider': 'kubespray', 'provider': 'kubespray',
'public_ipv4': None, 'public_ipv4': None,
'connection_port': 'redacted', 'connection_port': 'redacted',
'state': 'ready'}, objs[0]) 'state': 'ready'}.items() <= objs[0].items())
def test_kubernetes_custom(self): def test_kubernetes_custom(self):
# Test a pod with a custom spec # Test a pod with a custom spec

View File

@@ -116,10 +116,10 @@ class TestWebApp(tests.DBTestCase):
'application/json') 'application/json')
data = f.read() data = f.read()
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
self.assertDictContainsSubset({'id': image.build_id, self.assertTrue({'id': image.build_id,
'image': 'fake-image', 'image': 'fake-image',
'provider': 'fake-provider', 'provider': 'fake-provider',
'state': 'ready'}, objs[0]) 'state': 'ready'}.items() <= objs[0].items())
def test_dib_image_list_json(self): def test_dib_image_list_json(self):
configfile = self.setup_config('node.yaml') configfile = self.setup_config('node.yaml')
@@ -146,9 +146,9 @@ class TestWebApp(tests.DBTestCase):
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
# make sure this is valid json and has some of the # make sure this is valid json and has some of the
# non-changing keys # non-changing keys
self.assertDictContainsSubset({'id': f'fake-image-{image.build_id}', self.assertTrue({'id': f'fake-image-{image.build_id}',
'formats': ['qcow2'], 'formats': ['qcow2'],
'state': 'ready'}, objs[0]) 'state': 'ready'}.items() <= objs[0].items())
def test_image_status_json(self): def test_image_status_json(self):
configfile = self.setup_config("node.yaml") configfile = self.setup_config("node.yaml")
@@ -178,9 +178,9 @@ class TestWebApp(tests.DBTestCase):
data = f.read() data = f.read()
objs = json.loads(data.decode("utf8")) objs = json.loads(data.decode("utf8"))
self.assertDictContainsSubset({"image": "fake-image", self.assertTrue({"image": "fake-image",
"paused": False, "paused": False,
"build_request": None}, objs[0]) "build_request": None}.items() <= objs[0].items())
self.zk.submitBuildRequest("fake-image") self.zk.submitBuildRequest("fake-image")
@@ -188,9 +188,11 @@ class TestWebApp(tests.DBTestCase):
f = request.urlopen(req) f = request.urlopen(req)
data = f.read() data = f.read()
objs = json.loads(data.decode("utf8")) objs = json.loads(data.decode("utf8"))
self.assertDictContainsSubset({"image": "fake-image", self.assertTrue({
"image": "fake-image",
"paused": False, "paused": False,
"build_request": "pending"}, objs[0]) "build_request": "pending"
}.items() <= objs[0].items())
builder._janitor._emitBuildRequestStats() builder._janitor._emitBuildRequestStats()
self.assertReportedStat('nodepool.image_build_requests', '1', 'g') self.assertReportedStat('nodepool.image_build_requests', '1', 'g')
@@ -201,9 +203,11 @@ class TestWebApp(tests.DBTestCase):
data = f.read() data = f.read()
objs = json.loads(data.decode("utf8")) objs = json.loads(data.decode("utf8"))
self.assertDictContainsSubset({"image": "fake-image", self.assertTrue({
"image": "fake-image",
"paused": False, "paused": False,
"build_request": "building"}, objs[0]) "build_request": "building"
}.items() <= objs[0].items())
def test_node_list_json(self): def test_node_list_json(self):
configfile = self.setup_config('node.yaml') configfile = self.setup_config('node.yaml')
@@ -227,13 +231,13 @@ class TestWebApp(tests.DBTestCase):
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
# We don't check the value of 'locked' because we may get a # We don't check the value of 'locked' because we may get a
# cached value returned. # cached value returned.
self.assertDictContainsSubset({'id': '0000000000', self.assertTrue({'id': '0000000000',
'ipv6': '', 'ipv6': '',
'label': ['fake-label'], 'label': ['fake-label'],
'provider': 'fake-provider', 'provider': 'fake-provider',
'public_ipv4': 'fake', 'public_ipv4': 'fake',
'connection_port': 22, 'connection_port': 22,
'state': 'ready'}, objs[0]) 'state': 'ready'}.items() <= objs[0].items())
self.assertTrue('locked' in objs[0]) self.assertTrue('locked' in objs[0])
# specify valid node_id # specify valid node_id
req = request.Request( req = request.Request(
@@ -247,13 +251,13 @@ class TestWebApp(tests.DBTestCase):
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
# We don't check the value of 'locked' because we may get a # We don't check the value of 'locked' because we may get a
# cached value returned. # cached value returned.
self.assertDictContainsSubset({'id': '0000000000', self.assertTrue({'id': '0000000000',
'ipv6': '', 'ipv6': '',
'label': ['fake-label'], 'label': ['fake-label'],
'provider': 'fake-provider', 'provider': 'fake-provider',
'public_ipv4': 'fake', 'public_ipv4': 'fake',
'connection_port': 22, 'connection_port': 22,
'state': 'ready'}, objs[0]) 'state': 'ready'}.items() <= objs[0].items())
self.assertTrue('locked' in objs[0]) self.assertTrue('locked' in objs[0])
# node_id not found # node_id not found
req = request.Request( req = request.Request(
@@ -299,10 +303,9 @@ class TestWebApp(tests.DBTestCase):
data = f.read() data = f.read()
objs = json.loads(data.decode('utf8')) objs = json.loads(data.decode('utf8'))
objs = [o for o in objs if 'min-ready' not in o['requestor']] objs = [o for o in objs if 'min-ready' not in o['requestor']]
self.assertDictContainsSubset({'node_types': ['fake-label'], self.assertTrue({'node_types': ['fake-label'],
'requestor': 'test_request_list', 'requestor': 'test_request_list',
'event_id': req.event_id, }, 'event_id': req.event_id}.items() <= objs[0].items())
objs[0])
def test_label_list_json(self): def test_label_list_json(self):
configfile = self.setup_config('node.yaml') configfile = self.setup_config('node.yaml')