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
tags/11.1.0
Sam Morrison 8 months ago
parent
commit
cac7351b0e

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

@@ -1753,18 +1753,29 @@ class ShellTest(utils.TestCase):
1753 1753
                           cmd % (FAKE_UUID_1, testfile), api_version='2.57')
1754 1754
 
1755 1755
     def test_rebuild_change_user_data(self):
1756
-        self.run_command('rebuild sample-server %s --user-data test' %
1757
-                         FAKE_UUID_1, api_version='2.57')
1758
-        user_data = servers.ServerManager.transform_userdata('test')
1756
+        testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
1757
+        with open(testfile) as testfile_fd:
1758
+            data = testfile_fd.read().encode('utf-8')
1759
+        expected_file_data = servers.ServerManager.transform_userdata(data)
1760
+        self.run_command('rebuild sample-server %s --user-data %s' %
1761
+                         (FAKE_UUID_1, testfile), api_version='2.57')
1759 1762
         self.assert_called('GET', '/servers?name=sample-server', pos=0)
1760 1763
         self.assert_called('GET', '/servers/1234', pos=1)
1761 1764
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_1, pos=2)
1762 1765
         self.assert_called('POST', '/servers/1234/action',
1763 1766
                            {'rebuild': {'imageRef': FAKE_UUID_1,
1764
-                                        'user_data': user_data,
1767
+                                        'user_data': expected_file_data,
1765 1768
                                         'description': None}}, pos=3)
1766 1769
         self.assert_called('GET', '/v2/images/%s' % FAKE_UUID_2, pos=4)
1767 1770
 
1771
+    def test_rebuild_invalid_user_data(self):
1772
+        invalid_file = os.path.join(os.path.dirname(__file__),
1773
+                                    'no_such_file')
1774
+        cmd = ('rebuild sample-server %s --user-data %s'
1775
+               % (FAKE_UUID_1, invalid_file))
1776
+        self.assertRaises(exceptions.CommandError, self.run_command, cmd,
1777
+                          api_version='2.57')
1778
+
1768 1779
     def test_rebuild_unset_user_data(self):
1769 1780
         self.run_command('rebuild sample-server %s --user-data-unset' %
1770 1781
                          FAKE_UUID_1, api_version='2.57')

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

@@ -1915,8 +1915,15 @@ def do_rebuild(cs, args):
1915 1915
                     _("Cannot specify '--user-data-unset' with "
1916 1916
                       "'--user-data'."))
1917 1917
         elif args.user_data:
1918
-            kwargs['userdata'] = args.user_data
1919
-
1918
+            try:
1919
+                kwargs['userdata'] = open(args.user_data)
1920
+            except IOError as e:
1921
+                raise exceptions.CommandError(
1922
+                    _("Can't open '%(user_data)s': %(exc)s") % {
1923
+                        'user_data': args.user_data,
1924
+                        'exc': e,
1925
+                    }
1926
+                )
1920 1927
     if cs.api_version >= api_versions.APIVersion('2.54'):
1921 1928
         if args.key_unset:
1922 1929
             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