From cd8de886f5509621b964d3488afcfb3076a36e11 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 29 Feb 2020 13:20:49 +0100
Subject: [PATCH 2/4] python3: celery now handle bytes (#40288)

---
 hobo/agent/common/management/commands/hobo_deploy.py | 3 ++-
 hobo/agent/worker/services.py                        | 3 ++-
 tests_multitenant/test_agent_worker.py               | 6 ++++--
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/hobo/agent/common/management/commands/hobo_deploy.py b/hobo/agent/common/management/commands/hobo_deploy.py
index c6eb0e7..aec0719 100644
--- a/hobo/agent/common/management/commands/hobo_deploy.py
+++ b/hobo/agent/common/management/commands/hobo_deploy.py
@@ -5,33 +5,34 @@ import os
 import requests
 import subprocess
 import sys
 import tempfile
 
 from django.conf import settings
 from django.core.management.base import BaseCommand, CommandError
 from django.core.management import call_command, get_commands
+from django.utils.encoding import force_text
 from django.utils.six.moves.urllib import parse as urlparse
 
 from tenant_schemas.utils import tenant_context
 from hobo.multitenant.middleware import TenantMiddleware, TenantNotFound
 from hobo.theme.utils import get_theme
 
 # TODO: move this to settings
 KEY_SIZE = 1024
 DAYS = 3652
 
 
 def replace_file(path, content):
     dirname = os.path.dirname(path)
     fd, temp = tempfile.mkstemp(dir=dirname,
             prefix='.tmp-'+os.path.basename(path)+'-')
     f = os.fdopen(fd, 'w')
-    f.write(content)
+    f.write(force_text(content))
     f.flush()
     os.fsync(f.fileno())
     f.close()
     os.rename(temp, path)
 
 
 class Command(BaseCommand):
     early_secondary_exit = True
diff --git a/hobo/agent/worker/services.py b/hobo/agent/worker/services.py
index 7fa8f7d..3ffa147 100644
--- a/hobo/agent/worker/services.py
+++ b/hobo/agent/worker/services.py
@@ -18,16 +18,17 @@ from __future__ import print_function
 import fnmatch
 import json
 import multiprocessing
 import multiprocessing.pool
 import os
 import subprocess
 import sys
 
+from django.utils.encoding import force_bytes
 from django.utils.six.moves.urllib import parse as urlparse
 
 from . import settings
 
 
 class BaseService(object):
     tenants_dir = None
 
@@ -75,17 +76,17 @@ class BaseService(object):
         return False
 
     def execute(self, environment):
         if not os.path.exists(self.service_manage_try_cmd):
             return
         cmd = self.service_manage_cmd + ' hobo_deploy ' + self.base_url + ' -'
         cmd_process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        stdout, stderr = cmd_process.communicate(input=json.dumps(environment))
+        stdout, stderr = cmd_process.communicate(input=force_bytes(json.dumps(environment)))
         if cmd_process.returncode != 0:
             raise RuntimeError('command "%s" failed: %r %r' % (cmd, stdout, stderr))
 
     @classmethod
     def notify(cls, data):
         if not os.path.exists(cls.service_manage_try_cmd):
             return
         tenants = cls.get_actual_tenants()
diff --git a/tests_multitenant/test_agent_worker.py b/tests_multitenant/test_agent_worker.py
index 84aad37..f70ece6 100644
--- a/tests_multitenant/test_agent_worker.py
+++ b/tests_multitenant/test_agent_worker.py
@@ -1,16 +1,18 @@
 # -*- coding: utf-8 -*-
 import importlib
 import json
 import mock
 import os
 import pytest
 import six
 
+from django.utils.encoding import force_text
+
 from hobo.agent.worker.services import deploy, notify
 from hobo.agent.worker import settings
 
 
 ENVIRONMENT = {
     u'timestamp': '2022-02-22',
     u'services': [
         {
@@ -97,17 +99,17 @@ def test_deploy(mocked_subprocess, mocked_exists):
     assert '/usr/bin/wcsctl hobo_deploy https://wcs.dev.publik.love/ -' in mock_calls
     assert '/usr/lib/combo/manage.py hobo_deploy https://combo.dev.publik.love/ -' in mock_calls
     assert '/usr/bin/hobo-manage hobo_deploy https://hobo2.dev.publik.love/ -' in mock_calls
 
     # environment sent
     mock_calls = mocked_communicate.mock_calls
     assert len(mock_calls) == 4
     for i in range(0, len(mock_calls)):
-        assert json.loads(mock_calls[0][2]['input']) == ENVIRONMENT
+        assert json.loads(force_text(mock_calls[0][2]['input'])) == ENVIRONMENT
 
     mocked_opened.returncode = 1
     with pytest.raises(RuntimeError, match='failed: '):
         deploy(ENVIRONMENT)
 
 @mock.patch('hobo.agent.worker.services.os.path.exists', return_value=True)
 @mock.patch('hobo.agent.worker.services.subprocess')
 def test_deploy_host_with_agent_patterns(mocked_subprocess, mocked_exists, local_settings):
@@ -142,17 +144,17 @@ def test_notify(mocked_subprocess, mocked_listdir, mocked_exists):
 
     # process called
     mock_calls = set(x[1][0] for x in mocked_subprocess.Popen.mock_calls)
     assert mock_calls == {'/usr/lib/combo/manage.py hobo_notify -'}
 
     # notification sent
     mock_calls = mocked_communicate.mock_calls
     assert len(mock_calls) == 1
-    assert json.loads(mock_calls[0][2]['input'])['objects']['data'][0]['uuid'] == '12345'
+    assert json.loads(force_text(mock_calls[0][2]['input']))['objects']['data'][0]['uuid'] == '12345'
 
     mocked_opened.returncode = 1
     with pytest.raises(RuntimeError, match='failed: '):
         notify(NOTIFICATION)
 
     # ignore subprocess error
     mocked_communicate.reset_mock()
     mocked_subprocess.Popen.side_effect = OSError
-- 
2.25.1

