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 _format_script_arg(script_name, path, file_): """Format the argument for the scripts key to combine the script path and the path to the environment file in one string. Handles the special value 'built-in' to resolve to the built-in path of the corresponding script. :param script_name: name of the script :param path: either 'built-in' or a user-defined path :param file_: The name for the file created by the script :returns: the formatted argument. """ if path == "built-in": script_path = resources.get_scripts_file(script_name=script_name) env_dir = resources.get_stats_dir() env_file = "{}/{}".format(env_dir, file_) else: script_path = path env_file = file_ return "'{}' {}".format(script_path, env_file)
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 forge(self): """The high-level command to assemble the openvpn cmd. Adds the --config and the --writepid flag flag if not present yet.""" self._forge_command_line() self._forge_config() if "--config" not in self.cmd: try: self.forge_ovpn_config() except resources.ResourceNotFoundError: update.update( force=True ) # give updating a try else let the error happen self.forge_ovpn_config() else: index = self.cmd.index("--config") config_file = self.cmd[index + 1] self.cmd.remove("--config") self.cmd.remove(config_file) self.forge_ovpn_config(config_file=config_file) if not self.has_flag("--writepid"): pid_dir = resources.get_stats_dir(create=True) pid_file = pid_dir + "/openvpn.pid" self._add_openvpn_cmd_option("--writepid", pid_file)