| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright New York University and the in-toto contributors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
""" |
| 7 |
<Program Name> |
| 8 |
test_in_toto_mock.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Shikher Verma <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
June 12, 2017 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test in_toto_mock command line tool. |
| 21 |
|
| 22 |
""" |
| 23 |
import logging |
| 24 |
import os |
| 25 |
import unittest |
| 26 |
from pathlib import Path |
| 27 |
|
| 28 |
from in_toto.in_toto_mock import main as in_toto_mock_main |
| 29 |
from tests.common import CliTestCase, TmpDirMixin |
| 30 |
|
| 31 |
# Required to cache and restore default log level |
| 32 |
logger = logging.getLogger("in_toto") |
| 33 |
|
| 34 |
|
| 35 |
class TestInTotoMockTool(CliTestCase, TmpDirMixin): |
| 36 |
"""Test in_toto_mock's main() - requires sys.argv patching; and |
| 37 |
in_toto_mock- calls runlib and error logs/exits on Exception.""" |
| 38 |
|
| 39 |
cli_main_func = staticmethod(in_toto_mock_main) |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def setUpClass(self): |
| 43 |
"""Create and change into temporary directory, |
| 44 |
dummy artifact and base arguments.""" |
| 45 |
self.set_up_test_dir() |
| 46 |
|
| 47 |
# Below tests override the base logger ('in_toto') log level to |
| 48 |
# `logging.INFO`. We cache the original log level before running the tests |
| 49 |
# to restore it afterwards. |
| 50 |
self._base_log_level = logger.level |
| 51 |
|
| 52 |
self.test_step = "test_step" |
| 53 |
self.test_link = self.test_step + ".link" |
| 54 |
self.test_artifact = "test_artifact" |
| 55 |
Path(self.test_artifact).touch() |
| 56 |
|
| 57 |
@classmethod |
| 58 |
def tearDownClass(self): |
| 59 |
self.tear_down_test_dir() |
| 60 |
|
| 61 |
# Restore log level to what it was before running in-toto-mock |
| 62 |
logger.setLevel(self._base_log_level) |
| 63 |
|
| 64 |
def tearDown(self): |
| 65 |
try: |
| 66 |
os.remove(self.test_link) |
| 67 |
except OSError: |
| 68 |
pass |
| 69 |
|
| 70 |
def test_main_required_args(self): |
| 71 |
"""Test CLI command with required arguments.""" |
| 72 |
|
| 73 |
args = ["--name", self.test_step, "--", "python3", "--version"] |
| 74 |
self.assert_cli_sys_exit(args, 0) |
| 75 |
|
| 76 |
self.assertTrue(os.path.exists(self.test_link)) |
| 77 |
|
| 78 |
def test_main_wrong_args(self): |
| 79 |
"""Test CLI command with missing arguments.""" |
| 80 |
|
| 81 |
wrong_args_list = [[], ["--name", "test-step"], ["--", "echo", "blub"]] |
| 82 |
|
| 83 |
for wrong_args in wrong_args_list: |
| 84 |
self.assert_cli_sys_exit(wrong_args, 2) |
| 85 |
self.assertFalse(os.path.exists(self.test_link)) |
| 86 |
|
| 87 |
def test_main_bad_cmd(self): |
| 88 |
"""Test CLI command with non-existing command.""" |
| 89 |
# TODO: Is it safe to assume this command does not exist, or should we |
| 90 |
# assert for it? |
| 91 |
args = ["-n", "bad-command", "--", "ggadsfljasdhlasdfljvzxc"] |
| 92 |
self.assert_cli_sys_exit(args, 1) |
| 93 |
|
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
unittest.main() |