From ef055110f501689bbf42af8b80729cc388ce0aaa Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Wed, 26 Jul 2017 12:41:13 -0400 Subject: [PATCH] handle unicode host names in fact cache --- awx/main/models/jobs.py | 4 ++-- awx/main/tests/unit/models/test_jobs.py | 14 ++++++++++++++ awx/plugins/fact_caching/tower.py | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 1dee17bd56..049df6f2a5 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -716,10 +716,10 @@ class Job(UnifiedJob, JobOptions, SurveyJobMixin, JobNotificationMixin, TaskMana return '{}'.format(self.inventory.id) def memcached_fact_host_key(self, host_name): - return '{}-{}'.format(self.inventory.id, base64.b64encode(host_name)) + return '{}-{}'.format(self.inventory.id, base64.b64encode(host_name.encode('utf-8'))) def memcached_fact_modified_key(self, host_name): - return '{}-{}-modified'.format(self.inventory.id, base64.b64encode(host_name)) + return '{}-{}-modified'.format(self.inventory.id, base64.b64encode(host_name.encode('utf-8'))) def _get_inventory_hosts(self, only=['name', 'ansible_facts', 'modified',]): return self.inventory.hosts.only(*only) diff --git a/awx/main/tests/unit/models/test_jobs.py b/awx/main/tests/unit/models/test_jobs.py index 0e9113cf7a..e60b775066 100644 --- a/awx/main/tests/unit/models/test_jobs.py +++ b/awx/main/tests/unit/models/test_jobs.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import pytest from awx.main.models import ( @@ -111,6 +113,18 @@ def test_start_job_fact_cache_existing_host(hosts, hosts2, job, job2, inventory, assert ansible_facts_cached == json.dumps(hosts[1].ansible_facts) +def test_memcached_fact_host_key_unicode(job): + host_name = u'Iñtërnâtiônàlizætiøn' + host_key = job.memcached_fact_host_key(host_name) + assert host_key == '5-ScOxdMOrcm7DonRpw7Ruw6BsaXrDpnRpw7hu' + + +def test_memcached_fact_modified_key_unicode(job): + host_name = u'Iñtërnâtiônàlizætiøn' + host_key = job.memcached_fact_modified_key(host_name) + assert host_key == '5-ScOxdMOrcm7DonRpw7Ruw6BsaXrDpnRpw7hu-modified' + + def test_finish_job_fact_cache(job, hosts, inventory, mocker, new_time): job.start_job_fact_cache() diff --git a/awx/plugins/fact_caching/tower.py b/awx/plugins/fact_caching/tower.py index 86624f75da..8267a0d498 100755 --- a/awx/plugins/fact_caching/tower.py +++ b/awx/plugins/fact_caching/tower.py @@ -57,10 +57,10 @@ class CacheModule(BaseCacheModule): return '{}'.format(self._inventory_id) def translate_host_key(self, host_name): - return '{}-{}'.format(self._inventory_id, base64.b64encode(host_name)) + return '{}-{}'.format(self._inventory_id, base64.b64encode(host_name.encode('utf-8'))) def translate_modified_key(self, host_name): - return '{}-{}-modified'.format(self._inventory_id, base64.b64encode(host_name)) + return '{}-{}-modified'.format(self._inventory_id, base64.b64encode(host_name.encode('utf-8'))) def get(self, key): host_key = self.translate_host_key(key)