From 4eff70621146d0ddca19b8e22c76d8ee3d043aed Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Wed, 13 Dec 2017 17:14:49 +0100 Subject: [PATCH] Fix possible TypeError in VIF.fixed_ips The VIF['network'] field can be initialized to None and therefore a later call to VIF.fixed_ips() could raise a TypeError. This problem was visible during AttachInterfacesTestJSON tempest test case when nova tried to emit instance.interfacae_attach notification. This patch checks makes sure that if VIF['network'] is None then VIF.fixed_ips() return an empty list instead of raising a TypeError. Change-Id: Ib285d874b19be5bc1dbcd1d2af32e461f67e34cb Closes-Bug: #1737201 (cherry picked from commit 553f2edde596348ca5447588c5a0b06f3b6be286) --- nova/network/model.py | 7 +++++-- nova/tests/unit/network/test_network_info.py | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/nova/network/model.py b/nova/network/model.py index 763e56d68dc4..fa4b13dd94d4 100644 --- a/nova/network/model.py +++ b/nova/network/model.py @@ -409,8 +409,11 @@ class VIF(Model): return not self.__eq__(other) def fixed_ips(self): - return [fixed_ip for subnet in self['network']['subnets'] - for fixed_ip in subnet['ips']] + if self['network']: + return [fixed_ip for subnet in self['network']['subnets'] + for fixed_ip in subnet['ips']] + else: + return [] def floating_ips(self): return [floating_ip for fixed_ip in self.fixed_ips() diff --git a/nova/tests/unit/network/test_network_info.py b/nova/tests/unit/network/test_network_info.py index 7b81ae1b079e..f5f9499704b6 100644 --- a/nova/tests/unit/network/test_network_info.py +++ b/nova/tests/unit/network/test_network_info.py @@ -423,6 +423,11 @@ class VIFTests(test.NoDBTestCase): ] * 2 self.assertEqual(fixed_ips, ips) + def test_vif_get_fixed_ips_network_is_none(self): + vif = model.VIF() + fixed_ips = vif.fixed_ips() + self.assertEqual([], fixed_ips) + def test_vif_get_floating_ips(self): vif = fake_network_cache_model.new_vif() vif['network']['subnets'][0]['ips'][0].add_floating_ip('192.168.1.1')