Esempio n. 1
0
def test_get_stats_when_stats_dict_is_none(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_file = "tests/fixtures/config_valid_yaml_fixture.yml"
    mocked_stats_file = _mock_get_stats_file(mocker, stats_file)
    mocked_safe_load = mocker.patch("connord.resources.yaml.safe_load",
                                    return_value=None)
    expected_result = dict()

    # run
    actual_result = resources.get_stats()

    # assert
    mocked_stats_file.assert_called_once()
    mocked_safe_load.assert_called_once()
    assert actual_result == expected_result
Esempio n. 2
0
def test_get_stats_when_valid_yaml(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_file = "tests/fixtures/config_valid_yaml_fixture.yml"
    mocked_stats_file = _mock_get_stats_file(mocker, stats_file)
    import yaml

    with open(stats_file) as stats_fd:
        expected_result = yaml.safe_load(stats_fd)

    # run
    actual_result = resources.get_stats()

    # assert
    mocked_stats_file.assert_called_once()
    assert actual_result == expected_result
Esempio n. 3
0
def test_write_stats_when_valid_dict(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources, create_tmpdir=True)

    stats_file = mockbase.get_tmpdir() + "/stats"
    stats = {"connord": ["value", "too", {"test": "value"}]}
    mocked_stats_file = _mock_get_stats_file(mocker, stats_file)

    # run
    resources.write_stats(stats)

    # assert
    import os

    mocked_stats_file.assert_called_once()
    assert os.path.exists(stats_file)

    # tear down
    mockbase.tear_down()
Esempio n. 4
0
def test_get_stats_dir_when_default_and_path_not_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_dir = "/run/dir"
    # pylint: disable=protected-access
    resources.__RUN_DIR = stats_dir

    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False)
    mocked_makedirs = mockbase.mock_os_any(mocker, "makedirs", stats_dir)
    expected_result = stats_dir

    # run
    actual_result = resources.get_stats_dir()

    # assert
    mocked_exists.assert_called_once_with(stats_dir)
    mocked_makedirs.assert_called_once_with(stats_dir, mode=0o750)

    assert actual_result == expected_result
Esempio n. 5
0
def test_write_stats_when_invalid_dict(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources, create_tmpdir=True)

    stats_file = "/test/stats"
    stats = list()
    mocked_stats_file = _mock_get_stats_file(mocker, stats_file)

    # run
    try:
        resources.write_stats(stats)
    except TypeError as error:
        assert str(
            error
            # pylint: disable=line-too-long
        ) == "Could not write to {!r}: Invalid type: Found <class 'list'> but expected <class 'dict'>.".format(
            stats_file)

    # assert
    mocked_stats_file.assert_called_once()
Esempio n. 6
0
def test_get_stats_file_when_default_and_path_not_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_dir = "/run/dir"

    mocked_stats_dir = _mock_get_stats_dir(mocker, stats_dir)
    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False)
    mocked_open = mockbase.mock_open(mocker)
    expected_result = stats_dir + "/stats"

    # run
    actual_result = resources.get_stats_file()

    # assert
    mocked_stats_dir.assert_called_once_with(True)
    mocked_exists.assert_called_once_with(expected_result)
    mocked_open.assert_called_once_with(expected_result, "w")

    assert actual_result == expected_result
Esempio n. 7
0
def test_get_stats_dir_when_create_is_false_and_path_not_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_dir = "/run/dir"
    # pylint: disable=protected-access
    resources.__RUN_DIR = stats_dir

    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False)
    mocked_makedirs = mockbase.mock_os_any(mocker, "makedirs", stats_dir)
    expected_result = stats_dir

    # run
    try:
        resources.get_stats_dir(create=False)
        assert False
    except resources.ResourceNotFoundError as error:
        assert error.resource_file == expected_result

    # assert
    mocked_exists.assert_called_once_with(stats_dir)
    mocked_makedirs.assert_not_called()
Esempio n. 8
0
def test_get_stats_when_invalid_yaml(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_file = "tests/fixtures/config_invalid_yaml_fixture.yml"
    mocked_stats_file = _mock_get_stats_file(mocker, stats_file)

    # run
    try:
        resources.get_stats()
    except resources.MalformedResourceError as error:
        assert error.resource_file == stats_file
        assert error.problem == "expected <block end>, but found '-'"
        assert (
            error.problem_mark
            # pylint: disable=line-too-long
            ==
            '  in "tests/fixtures/config_invalid_yaml_fixture.yml", line 4, column 3'
        )

    # assert
    mocked_stats_file.assert_called_once()
Esempio n. 9
0
def test_get_stats_file_when_create_is_false_and_path_not_exists(mocker):
    # setup
    mockbase = MockBase("resources")
    mockbase.mock_user_is_root(mocker, True)
    mockbase.setup(resources)

    stats_dir = "/run/dir"

    mocked_stats_dir = _mock_get_stats_dir(mocker, stats_dir)
    mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False)
    mocked_open = mockbase.mock_open(mocker)
    expected_result = stats_dir + "/stats"

    # run
    try:
        resources.get_stats_file(create=False)
        assert False
    except resources.ResourceNotFoundError as error:
        assert error.resource_file == expected_result

    # assert
    mocked_stats_dir.assert_called_once_with(False)
    mocked_exists.assert_called_once_with(expected_result)
    mocked_open.assert_not_called()