Merge "baremetal.configdrive: tolerate user_data as a string"

This commit is contained in:
Zuul
2019-08-12 16:24:30 +00:00
committed by Gerrit Code Review
2 changed files with 10 additions and 1 deletions

View File

@@ -52,7 +52,11 @@ def populate_directory(metadata, user_data=None, versions=None,
json.dump(network_data, fp)
if user_data:
with open(os.path.join(subdir, 'user_data'), 'wb') as fp:
# Strictly speaking, user data is binary, but in many cases
# it's actually a text (cloud-init, ignition, etc).
flag = 't' if isinstance(user_data, six.text_type) else 'b'
with open(os.path.join(subdir, 'user_data'),
'w%s' % flag) as fp:
fp.write(user_data)
yield d

View File

@@ -48,6 +48,8 @@ class TestPopulateDirectory(testtools.TestCase):
if user_data is None:
self.assertFalse(os.path.exists(user_data_file))
else:
if isinstance(user_data, six.text_type):
user_data = user_data.encode()
with open(user_data_file, 'rb') as fp:
self.assertEqual(user_data, fp.read())
@@ -60,6 +62,9 @@ class TestPopulateDirectory(testtools.TestCase):
def test_with_user_data(self):
self._check({'foo': 42}, b'I am user data')
def test_with_user_data_as_string(self):
self._check({'foo': 42}, u'I am user data')
def test_with_network_data(self):
self._check({'foo': 42}, network_data={'networks': {}})