Browse Source

Fix up userdata argument to rebuild.

This was using the name of the file as user data as opposed
to the content of the file.

Change-Id: I9752d849aa0e6cf608db0def3ca89565cff4debc
Closes-bug: #1794419
(cherry picked from commit cac7351b0e)
(cherry picked from commit 5610ebd10d)
changes/90/613090/1
Sam Morrison 9 months ago
parent
commit
154b29b7a3

+ 15
- 4
novaclient/tests/unit/v2/test_shell.py View File

@@ -1602,18 +1602,29 @@ class ShellTest(utils.TestCase):
1602 1602
                           cmd % (FAKE_UUID_1, testfile), api_version='2.57')
1603 1603
 
1604 1604
     def test_rebuild_change_user_data(self):
1605
-        self.run_command('rebuild sample-server %s --user-data test' %
1606
-                         FAKE_UUID_1, api_version='2.57')
1607
-        user_data = servers.ServerManager.transform_userdata('test')
1605
+        testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
1606
+        with open(testfile) as testfile_fd:
1607
+            data = testfile_fd.read().encode('utf-8')
1608
+        expected_file_data = servers.ServerManager.transform_userdata(data)
1609
+        self.run_command('rebuild sample-server %s --user-data %s' %
1610
+                         (FAKE_UUID_1, testfile), api_version='2.57')
1608 1611
         self.assert_called('GET', '/servers?name=sample-server', pos=0)
1609 1612
         self.assert_called('GET', '/servers/1234', pos=1)
1610 1613
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_1, pos=2)
1611 1614
         self.assert_called('POST', '/servers/1234/action',
1612 1615
                            {'rebuild': {'imageRef': FAKE_UUID_1,
1613
-                                        'user_data': user_data,
1616
+                                        'user_data': expected_file_data,
1614 1617
                                         'description': None}}, pos=3)
1615 1618
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_2, pos=4)
1616 1619
 
1620
+    def test_rebuild_invalid_user_data(self):
1621
+        invalid_file = os.path.join(os.path.dirname(__file__),
1622
+                                    'no_such_file')
1623
+        cmd = ('rebuild sample-server %s --user-data %s'
1624
+               % (FAKE_UUID_1, invalid_file))
1625
+        self.assertRaises(exceptions.CommandError, self.run_command, cmd,
1626
+                          api_version='2.57')
1627
+
1617 1628
     def test_rebuild_unset_user_data(self):
1618 1629
         self.run_command('rebuild sample-server %s --user-data-unset' %
1619 1630
                          FAKE_UUID_1, api_version='2.57')

+ 9
- 2
novaclient/v2/shell.py View File

@@ -1850,8 +1850,15 @@ def do_rebuild(cs, args):
1850 1850
                     _("Cannot specify '--user-data-unset' with "
1851 1851
                       "'--user-data'."))
1852 1852
         elif args.user_data:
1853
-            kwargs['userdata'] = args.user_data
1854
-
1853
+            try:
1854
+                kwargs['userdata'] = open(args.user_data)
1855
+            except IOError as e:
1856
+                raise exceptions.CommandError(
1857
+                    _("Can't open '%(user_data)s': %(exc)s") % {
1858
+                        'user_data': args.user_data,
1859
+                        'exc': e,
1860
+                    }
1861
+                )
1855 1862
     if cs.api_version >= api_versions.APIVersion('2.54'):
1856 1863
         if args.key_unset:
1857 1864
             kwargs['key_name'] = None

+ 5
- 0
releasenotes/notes/fix-rebuild-userdata-9315e5784feb8ba9.yaml View File

@@ -0,0 +1,5 @@
1
+---
2
+fixes:
3
+  - |
4
+    The user data argument to rebuild was passing the filename as is as userdata.
5
+    Now this passes the contents of the filename as intended.

Loading…
Cancel
Save