Commit 53e99f68 authored by Łukasz Nowak's avatar Łukasz Nowak Committed by Łukasz Nowak

updater: Cover updateCertificate

Fix one condition.
parent 2cd28eac
...@@ -17,17 +17,18 @@ ...@@ -17,17 +17,18 @@
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
import StringIO
import contextlib import contextlib
import datetime import datetime
import httplib import httplib
import ipaddress import ipaddress
import mock
import multiprocessing import multiprocessing
import os import os
import requests import requests
import requests.exceptions import requests.exceptions
import shutil import shutil
import signal import signal
import StringIO
import sys import sys
import tempfile import tempfile
import time import time
...@@ -1136,3 +1137,75 @@ class KedifaUpdaterMappingTest(KedifaMixin, unittest.TestCase): ...@@ -1136,3 +1137,75 @@ class KedifaUpdaterMappingTest(KedifaMixin, unittest.TestCase):
u = Updater(1, self.mapping, None, None, None, None, True) u = Updater(1, self.mapping, None, None, None, None, True)
u.updateMapping() u.updateMapping()
self.assertEqual(u.mapping, {'file': 'url'}) self.assertEqual(u.mapping, {'file': 'url'})
class KedifaUpdaterUpdateCertificateTest(KedifaMixin, unittest.TestCase):
def setupMapping(self, mapping_content=''):
mapping = tempfile.NamedTemporaryFile(dir=self.testdir, delete=False)
mapping.write(mapping_content)
mapping.close()
self.mapping = mapping.name
def _update(self, certificate, fetch, master_content):
certificate_file = tempfile.NamedTemporaryFile(
dir=self.testdir, delete=False)
certificate_file.write(certificate)
certificate_file.close()
self.setupMapping('http://example.com %s' % (certificate_file.name,))
u = Updater(
1, self.mapping, '/master/certificate/file', None, None, None, True)
u.updateMapping()
with mock.patch.object(
Updater, 'fetchCertificate', return_value=fetch):
result = u.updateCertificate(certificate_file.name, master_content)
return open(certificate_file.name, 'r').read(), result
def test_nocert_nofetch_nomaster(self):
certificate, update = self._update(
certificate='', fetch='', master_content=None)
self.assertEqual('', certificate)
self.assertFalse(update)
def test_cert_nofetch_nomaster(self):
certificate, update = self._update(
certificate='old content', fetch='', master_content=None)
self.assertEqual('old content', certificate)
self.assertFalse(update)
def test_nocert_fetch_nomaster(self):
certificate, update = self._update(
certificate='', fetch='content', master_content=None)
self.assertEqual('content', certificate)
self.assertTrue(update)
def test_cert_fetch_nomaster(self):
certificate, update = self._update(
certificate='old content', fetch='content', master_content=None)
self.assertEqual('content', certificate)
self.assertTrue(update)
def test_nocert_nofetch_master(self):
certificate, update = self._update(
certificate='', fetch='', master_content='master')
self.assertEqual('master', certificate)
self.assertTrue(update)
def test_cert_nofetch_master(self):
# This is important feature. Master certifcate does not override existing
# certificate, so it can use provided outside of KeDiFa
certificate, update = self._update(
certificate='old content', fetch='', master_content='master')
self.assertEqual('old content', certificate)
self.assertFalse(update)
def test_nocert_fetch_master(self):
certificate, update = self._update(
certificate='', fetch='content', master_content='master')
self.assertEqual('content', certificate)
self.assertTrue(update)
def test_cert_fetch_master(self):
certificate, update = self._update(
certificate='old content', fetch='content', master_content='master')
self.assertEqual('content', certificate)
self.assertTrue(update)
...@@ -60,13 +60,12 @@ class Updater(object): ...@@ -60,13 +60,12 @@ class Updater(object):
except IOError: except IOError:
current = '' current = ''
if not(certificate) and not current: if not(certificate):
if master_content is not None: if not current and master_content is not None:
url = self.master_certificate_file url = self.master_certificate_file
certificate = master_content certificate = master_content
else: else:
return False return False
if current != certificate: if current != certificate:
with open(certificate_file, 'w') as fh: with open(certificate_file, 'w') as fh:
fh.write(certificate) fh.write(certificate)
......
...@@ -23,6 +23,7 @@ import versioneer ...@@ -23,6 +23,7 @@ import versioneer
tests_require = [ tests_require = [
'urllib3 >= 1.18', # https://github.com/urllib3/urllib3/issues/258 'urllib3 >= 1.18', # https://github.com/urllib3/urllib3/issues/258
'ipaddress', 'ipaddress',
'mock',
] ]
setup( setup(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment