From 3757845a2744dcb275f7b1ff5f8fbdb78a565c77 Mon Sep 17 00:00:00 2001 From: Mikhail Gordeev Date: Mon, 20 Apr 2020 19:58:00 +0300 Subject: [PATCH] Add error handling tests --- tests/minimal_config.yaml | 15 ++++++++++ tests/test_errors.py | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/minimal_config.yaml create mode 100644 tests/test_errors.py diff --git a/tests/minimal_config.yaml b/tests/minimal_config.yaml new file mode 100644 index 0000000..b0b64b5 --- /dev/null +++ b/tests/minimal_config.yaml @@ -0,0 +1,15 @@ +--- +remote: '/var/empty' +key: 0x00000000 + +images: + rootfs-minimal: + target: ve/docker + kinds: + - tar.xz + +branches: + Sisyphus: + arches: + x86_64: +... diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 0000000..105acc3 --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,59 @@ +from unittest import TestCase + +import shutil +import tempfile + +import yaml + +from cloud_build import CB +from cloud_build import Error + + +def update(old_dict, kwargs): + new_dict = old_dict.copy() + for key, value in kwargs.items(): + if value is None: + del new_dict[key] + else: + new_dict[key] = value + return new_dict + + +class TestErrors(TestCase): + def setUp(self): + kwargs = {} + kwargs['data_dir'] = tempfile.mkdtemp(prefix='cloud_build') + self.kwargs = kwargs + + def tearDown(self): + shutil.rmtree(self.kwargs['data_dir']) + + def test_read_config_not_found(self): + regex = 'config file.*No such file or directory' + self.kwargs.update(config='/var/empty/cb_conf.yaml') + self.assertRaisesRegex(Error, regex, CB, **self.kwargs) + + def test_read_config_permission_denied(self): + regex = 'config file.*Permission denied' + self.kwargs.update(config='/root/cb_conf.yaml') + self.assertRaisesRegex(Error, regex, CB, **self.kwargs) + + def test_required_parameters_in_config(self): + config = tempfile.mktemp(prefix='cb_conf') + with open('tests/minimal_config.yaml') as f: + cfg = yaml.safe_load(f) + + parameter = 'key' + for parameter in ['remote', 'key', 'images', 'branches']: + with open(config, 'w') as f: + yaml.safe_dump(update(cfg, {parameter: None}), f) + + regex = f'parameter.*{parameter}' + self.kwargs.update(config=config) + self.assertRaisesRegex(Error, regex, CB, **self.kwargs) + + def test_run_already_running(self): + self.kwargs.update(config='tests/minimal_config.yaml') + cb = CB(**self.kwargs) # noqa F841 + regex = 'already running' + self.assertRaisesRegex(Error, regex, CB, **self.kwargs)