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
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()
def test_get_config_dir_when_path_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) expected_result = "/test/dir" # pylint: disable=protected-access resources.__CONFIG_DIR = expected_result mocked_exists = mockbase.mock_os_any(mocker, "path.exists", True) # run actual_result = resources.get_config_dir() # assert mocked_exists.assert_called_once_with(expected_result) assert actual_result == expected_result
def test_get_scripts_file_when_path_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) mocked_scripts_dir = _mock_get_scripts_dir(mocker, "/test/dir") expected_result = "/test/dir/openvpn_up_down.bash" mocked_exists = mockbase.mock_os_any(mocker, "path.exists", True) # run actual_result = resources.get_scripts_file() # assert mocked_scripts_dir.assert_called_once() mocked_exists.assert_called_once_with(expected_result) assert actual_result == expected_result
def test_get_scripts_dir_when_path_exists(mocker): # setup _setup() # pylint: disable=protected-access resources.__SCRIPTS_DIR = "/test/dir" mockbase = MockBase("resources") mocked_exists = mockbase.mock_os_any(mocker, "path.exists", True) expected_result = "/test/dir" # run actual_result = resources.get_scripts_dir() # assert mocked_exists.assert_called_once_with(expected_result) assert actual_result == expected_result
def test_get_config_file_when_path_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) mocked_exists = mockbase.mock_os_any(mocker, "path.exists", True) expected_result = test_dir + "/config.yml" # run actual_result = resources.get_config_file() # assert mocked_config_dir.assert_called_once() mocked_exists.assert_called_once_with(expected_result) assert actual_result == expected_result
def test_list_config_dir_when_filetype_is_set_and_list_has_no_files(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) dir_list = list() mocked_listdir = mockbase.mock_os_any(mocker, "listdir", dir_list) # run actual_list = resources.list_config_dir(filetype=".txt") # assert mocked_config_dir.assert_called_once() mocked_listdir.assert_called_once_with("/test/dir") assert not actual_list
def test_get_config_dir_when_path_not_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) expected_result = "/test/other/dir/config" # pylint: disable=protected-access resources.__CONFIG_DIR = "/test/dir" mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False) mocked_filename = mockbase.mock_resource_filename(mocker, expected_result) # run actual_result = resources.get_config_dir() # assert mocked_exists.assert_called_once_with("/test/dir") mocked_filename.assert_called_once() assert actual_result == expected_result
def test_get_config_file_when_path_not_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False) expected_result = test_dir + "/config.yml" # run and assert try: resources.get_config_file() assert False except resources.ResourceNotFoundError as error: assert error.resource_file == expected_result mocked_config_dir.assert_called_once() mocked_exists.assert_called_once_with(expected_result)
def test_list_config_dir_when_filetype_is_none_and_list_has_files(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) test_dir = "/test/dir" mocked_config_dir = _mock_get_config_dir(mocker, test_dir) # pylint: disable=protected-access dir_list = ["some", "other"] mocked_listdir = mockbase.mock_os_any(mocker, "listdir", dir_list) expected_list = [test_dir + "/" + _file for _file in dir_list] # run actual_list = resources.list_config_dir() # assert mocked_config_dir.assert_called_once() mocked_listdir.assert_called_once_with("/test/dir") assert actual_list == expected_list
def test_get_scripts_file_when_path_not_exists(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) mocked_scripts_dir = _mock_get_scripts_dir(mocker, "/test/dir") expected_result = "/test/dir/openvpn_up_down.bash" mocked_exists = mockbase.mock_os_any(mocker, "path.exists", False) # run try: resources.get_scripts_file() assert False except resources.ResourceNotFoundError as error: assert error.resource_file == expected_result # assert mocked_scripts_dir.assert_called_once() mocked_exists.assert_called_once_with(expected_result)
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
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()