Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import contextlib
import tempfile
from kibitzr.conf import ReloadableSettings
from kibitzr.stash import Stash
from .compat import mock
class SettingsMock(ReloadableSettings):
def __init__(self):
self.checks = []
self.notifiers = {}
self.creds = {'pass': 'password'}
@classmethod
def instance(cls):
ReloadableSettings._instance = cls()
return ReloadableSettings._instance
@staticmethod
def dispose():
ReloadableSettings._instance = None
def test_unnamed_check():
conf = "checks: [{period: 1}]"
with patch_conf(conf):
conf = ReloadableSettings('::')
assert conf.checks == [{
'name': 'Unnamed check 1',
'schedule': [TimelineRule(interval=1, unit='seconds', at=None)]
}]
def test_reread():
conf1 = (
"checks:\n"
" - name: Name\n"
" url: URL\n"
)
conf2 = conf1 + " period: 60\n"
with patch_creds(""):
with patch_conf(conf1):
conf = ReloadableSettings('::')
# Config didn't change:
assert not conf.reread()
with patch_conf(conf2):
# Config changed:
assert conf.reread()
assert conf.checks == [{
'name': 'Name',
'url': 'URL',
'schedule': [TimelineRule(interval=60, unit='seconds', at=None)]
}]
def test_name_from_url_population():
conf = "checks: [{url: 'http://example.com/page_name'}]"
with patch_conf(conf):
conf = ReloadableSettings('::')
assert conf.checks[0]['name'] == 'http-example-com-page_name'
def test_complex_conf_sample():
with patch_conf(sample_conf):
with patch_creds(sample_creds):
conf = ReloadableSettings('::')
assert conf.checks == checks
assert conf.creds['mailgun']['url'] == 'ZZZ'
def settings():
"""
Returns singleton instance of settings
"""
return ReloadableSettings.instance()