Browse Source

Fix flavor keyerror when nova boot vm

When creating a server (the 'nova boot' command),
it calls 'POST /servers' API.

The response does not have 'addresses' and 'flavor' attributes.
When accessing 'networks' attribute in the '_print_server' function,
it calls 'GET /servers/{server_id}' and get the 'addresses' and
'flavor' attributes.

If 'GET /servers/{server_id}' fails, the server object does not have
the 'flavor' attribute, then KeyError is raised when accessing it.

Fix the issue by making it raise a CommandError
when the 'GET /servers/{server_id}' fails.

Co-Authored-By: Takashi Natsume <natsume.takashi@lab.ntt.co.jp>
Change-Id: I3ef096c61b0e05a637ab0c4a1027338fa87e4f09
Closes-Bug: #1781368
tags/12.0.0
zhaolihui 11 months ago
parent
commit
b13ba0138f
3 changed files with 18 additions and 2 deletions
  1. 12
    0
      novaclient/tests/unit/v2/test_shell.py
  2. 1
    1
      novaclient/v2/servers.py
  3. 5
    1
      novaclient/v2/shell.py

+ 12
- 0
novaclient/tests/unit/v2/test_shell.py View File

@@ -1334,6 +1334,18 @@ class ShellTest(utils.TestCase):
1334 1334
             }},
1335 1335
         )
1336 1336
 
1337
+    @mock.patch.object(servers.Server, 'networks',
1338
+                       new_callable=mock.PropertyMock)
1339
+    def test_boot_with_not_found_when_accessing_addresses_attribute(
1340
+            self, mock_networks):
1341
+        mock_networks.side_effect = exceptions.NotFound(
1342
+            404, 'Instance %s could not be found.' % FAKE_UUID_1)
1343
+        ex = self.assertRaises(
1344
+            exceptions.CommandError, self.run_command,
1345
+            'boot --flavor 1 --image %s some-server' % FAKE_UUID_2)
1346
+        self.assertIn('Instance %s could not be found.' % FAKE_UUID_1,
1347
+                      six.text_type(ex))
1348
+
1337 1349
     def test_flavor_list(self):
1338 1350
         out, _ = self.run_command('flavor-list')
1339 1351
         self.assert_called_anytime('GET', '/flavors/detail')

+ 1
- 1
novaclient/v2/servers.py View File

@@ -405,7 +405,7 @@ class Server(base.Resource):
405 405
             for network_label, address_list in self.addresses.items():
406 406
                 networks[network_label] = [a['addr'] for a in address_list]
407 407
             return networks
408
-        except Exception:
408
+        except AttributeError:
409 409
             return {}
410 410
 
411 411
     @api_versions.wraps("2.0", "2.24")

+ 5
- 1
novaclient/v2/shell.py View File

@@ -2368,7 +2368,11 @@ def _print_server(cs, args, server=None, wrap=0):
2368 2368
 
2369 2369
     minimal = getattr(args, "minimal", False)
2370 2370
 
2371
-    networks = server.networks
2371
+    try:
2372
+        networks = server.networks
2373
+    except Exception as e:
2374
+        raise exceptions.CommandError(six.text_type(e))
2375
+
2372 2376
     info = server.to_dict()
2373 2377
     for network_label, address_list in networks.items():
2374 2378
         info['%s network' % network_label] = ', '.join(address_list)

Loading…
Cancel
Save