From 3c01d90e5ce55c92dee6057a5e9cede81a4dcc86 Mon Sep 17 00:00:00 2001
From: Lukasz Nowak <luke@nexedi.com>
Date: Thu, 20 Dec 2018 12:22:58 +0100
Subject: [PATCH] slapos: Encode unicode to UTF-8

/reviewed-on https://lab.nexedi.com/nexedi/slapos/merge_requests/480
---
 slapos/recipe/request.py                       | 7 +++++--
 slapos/recipe/slapconfiguration.py             | 2 ++
 slapos/test/recipe/test_request.py             | 1 +
 slapos/test/recipe/test_slaposconfiguration.py | 3 ++-
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/slapos/recipe/request.py b/slapos/recipe/request.py
index 0c57043b7..7b3a22a95 100644
--- a/slapos/recipe/request.py
+++ b/slapos/recipe/request.py
@@ -190,12 +190,15 @@ class Recipe(object):
 
     # Then try to get all the parameters. In case of problem, put empty string.
     for param in return_parameters:
-      options['connection-%s' % param] = ''
+      value = ''
       try:
-        options['connection-%s' % param] = return_parameter_dict[param]
+        value = return_parameter_dict[param]
       except KeyError:
         if self.failed is None:
           self.failed = param
+      if isinstance(value, unicode):
+        value = value.encode('UTF-8')
+      options['connection-%s' % param] = value
 
   def _filterForStorage(self, partition_parameter_kw):
     return partition_parameter_kw
diff --git a/slapos/recipe/slapconfiguration.py b/slapos/recipe/slapconfiguration.py
index b70e861ae..1a2bf833f 100644
--- a/slapos/recipe/slapconfiguration.py
+++ b/slapos/recipe/slapconfiguration.py
@@ -264,6 +264,8 @@ class Recipe(object):
           # be very careful with overriding master's information
           for key, value in flatten_dict(partition_params).items():
             if key not in options:
+              if isinstance(value, unicode):
+                value = value.encode('UTF-8')
               options[key] = value
       # print out augmented options to see what we are passing
       logger.debug(str(options))
diff --git a/slapos/test/recipe/test_request.py b/slapos/test/recipe/test_request.py
index 0e64a8107..aa7e3987c 100644
--- a/slapos/test/recipe/test_request.py
+++ b/slapos/test/recipe/test_request.py
@@ -83,6 +83,7 @@ class RecipeTestMixin(object):
     result = recipe.install()
     self.assertEqual([], result)
     self.assertEqual(options['connection-anything'], 'done')
+    self.assertIsInstance(options['connection-anything'], str)
     self.request_instance.assert_called_with(
       '', 'RootSoftwareInstance', '', filter_kw={},
       partition_parameter_kw=self.called_partition_parameter_kw,
diff --git a/slapos/test/recipe/test_slaposconfiguration.py b/slapos/test/recipe/test_slaposconfiguration.py
index 9235acd39..0e6c36a5d 100644
--- a/slapos/test/recipe/test_slaposconfiguration.py
+++ b/slapos/test/recipe/test_slaposconfiguration.py
@@ -18,7 +18,7 @@ class SlapConfigurationTest(unittest.TestCase):
     self.resource_file = os.path.join(self.instance_root, slapformat.Partition.resource_file)
     self.resource = {
       "tun": {
-        "ipv4": "192.168.0.1"
+        "ipv4": u"192.168.0.1"
       },
       "address_list": [
         10, 20
@@ -48,6 +48,7 @@ class SlapConfigurationTest(unittest.TestCase):
 
     self.assertEqual(options['tun-ipv4'], "192.168.0.1",
       "Folded attrs should be separated by -")
+    self.assertIsInstance(options['tun-ipv4'], str)
 
     self.assertEqual(options['address-list'], [10, 20],
       "All underscores should be replaced with -")
-- 
2.30.9