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 @@
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
import StringIO
import contextlib
import datetime
import httplib
import ipaddress
import mock
import multiprocessing
import os
import requests
import requests.exceptions
import shutil
import signal
import StringIO
import sys
import tempfile
import time
......@@ -1136,3 +1137,75 @@ class KedifaUpdaterMappingTest(KedifaMixin, unittest.TestCase):
u = Updater(1, self.mapping, None, None, None, None, True)
u.updateMapping()
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):
except IOError:
current = ''
if not(certificate) and not current:
if master_content is not None:
if not(certificate):
if not current and master_content is not None:
url = self.master_certificate_file
certificate = master_content
else:
return False
if current != certificate:
with open(certificate_file, 'w') as fh:
fh.write(certificate)
......
......@@ -23,6 +23,7 @@ import versioneer
tests_require = [
'urllib3 >= 1.18', # https://github.com/urllib3/urllib3/issues/258
'ipaddress',
'mock',
]
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