def test_resource_not_found_error_without_message(mocker): # setup test_file = "test_file" mocked_zip_path = mocker.patch("connord.resources.get_zip_path") mocked_zip_path.side_effect = resources.ResourceNotFoundError(test_file) # run and assert try: resources.get_zip_path() assert False except resources.ResourceNotFoundError as error: assert error.resource_file == test_file assert str(error) == "Resource does not exist: {!r}".format(test_file)
def test_resource_not_found_error_when_resource_file_is_none(mocker): # setup test_file = None test_message = "test_message" mocked_zip_path = mocker.patch("connord.resources.get_zip_path") mocked_zip_path.side_effect = resources.ResourceNotFoundError( test_file, test_message) # run and assert try: resources.get_zip_path() assert False except resources.ResourceNotFoundError as error: assert error.resource_file == test_file assert str(error) == test_message
def test_update_orig_when_zip_file_not_exists(mocker): zippath = "/etc/openvpn/client/nordvpn/ovpn.zip" set_up(mocker) import importlib importlib.reload(update) mocked_shutil = mocker.patch("connord.update.move") mocked_zip_file = mocker.patch("connord.update.resources.get_zip_file") from connord import resources mocked_zip_file.side_effect = resources.ResourceNotFoundError(zippath) try: update.update_orig("ovpn.zip") except resources.ResourceNotFoundError: assert False mocked_shutil.assert_not_called()
def _forge_scripts(self, scripts): """Adds the formatted script paths to the resulting command-line :param scripts: list of scripts served as dictionaries """ for script in scripts: name = script["name"] flag = self._format_flag(name) path = script["path"] file_ = script["creates"] if name in ("up", "down"): arg = self._format_script_arg("openvpn_up_down.bash", path, file_) elif name == "ipchange": arg = self._format_script_arg("openvpn_ipchange.bash", path, file_) else: if path == "built-in": raise resources.ResourceNotFoundError( path, "No built-in found for {!r}.".format(name) ) arg = self._format_script_arg(name, path, file_) self._add_openvpn_cmd_option(flag, arg)
def get_config_path(table, fallback=False, ipv6=False): """Return rules path of the table""" if fallback: if ipv6: table_regex = re.compile(r"[0-9]*[-]?{}6.fallback".format(table)) else: table_regex = re.compile(r"[0-9]*[-]?{}.fallback".format(table)) file_list = resources.list_config_dir(filetype="fallback") else: if ipv6: table_regex = re.compile(r"[0-9]*[-]?{}6.rules".format(table)) else: table_regex = re.compile(r"[0-9]*[-]?{}.rules".format(table)) file_list = resources.list_config_dir(filetype="rules") configs_found = [] for file_ in file_list: result = table_regex.search(file_) if result: configs_found.append(file_) if not configs_found: if fallback: error_message = ( "Could not find a fallback configuration for '{}' " "table.").format(table) else: error_message = "Could not find a configuration for '{}' table.".format( table) raise resources.ResourceNotFoundError(table, error_message) elif len(configs_found) > 1: raise IptablesError("Ambiguous file paths: {}.".format(configs_found)) return configs_found[0]