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)
changes/57/613057/1
Sam Morrison 9 months ago
parent
commit
5610ebd10d

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

@@ -1736,18 +1736,29 @@ class ShellTest(utils.TestCase):
1736 1736
                           cmd % (FAKE_UUID_1, testfile), api_version='2.57')
1737 1737
 
1738 1738
     def test_rebuild_change_user_data(self):
1739
-        self.run_command('rebuild sample-server %s --user-data test' %
1740
-                         FAKE_UUID_1, api_version='2.57')
1741
-        user_data = servers.ServerManager.transform_userdata('test')
1739
+        testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
1740
+        with open(testfile) as testfile_fd:
1741
+            data = testfile_fd.read().encode('utf-8')
1742
+        expected_file_data = servers.ServerManager.transform_userdata(data)
1743
+        self.run_command('rebuild sample-server %s --user-data %s' %
1744
+                         (FAKE_UUID_1, testfile), api_version='2.57')
1742 1745
         self.assert_called('GET', '/servers?name=sample-server', pos=0)
1743 1746
         self.assert_called('GET', '/servers/1234', pos=1)
1744 1747
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_1, pos=2)
1745 1748
         self.assert_called('POST', '/servers/1234/action',
1746 1749
                            {'rebuild': {'imageRef': FAKE_UUID_1,
1747
-                                        'user_data': user_data,
1750
+                                        'user_data': expected_file_data,
1748 1751
                                         'description': None}}, pos=3)
1749 1752
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_2, pos=4)
1750 1753
 
1754
+    def test_rebuild_invalid_user_data(self):
1755
+        invalid_file = os.path.join(os.path.dirname(__file__),
1756
+                                    'no_such_file')
1757
+        cmd = ('rebuild sample-server %s --user-data %s'
1758
+               % (FAKE_UUID_1, invalid_file))
1759
+        self.assertRaises(exceptions.CommandError, self.run_command, cmd,
1760
+                          api_version='2.57')
1761
+
1751 1762
     def test_rebuild_unset_user_data(self):
1752 1763
         self.run_command('rebuild sample-server %s --user-data-unset' %
1753 1764
                          FAKE_UUID_1, api_version='2.57')

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

@@ -1894,8 +1894,15 @@ def do_rebuild(cs, args):
1894 1894
                     _("Cannot specify '--user-data-unset' with "
1895 1895
                       "'--user-data'."))
1896 1896
         elif args.user_data:
1897
-            kwargs['userdata'] = args.user_data
1898
-
1897
+            try:
1898
+                kwargs['userdata'] = open(args.user_data)
1899
+            except IOError as e:
1900
+                raise exceptions.CommandError(
1901
+                    _("Can't open '%(user_data)s': %(exc)s") % {
1902
+                        'user_data': args.user_data,
1903
+                        'exc': e,
1904
+                    }
1905
+                )
1899 1906
     if cs.api_version >= api_versions.APIVersion('2.54'):
1900 1907
         if args.key_unset:
1901 1908
             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