def run(self): """High-level command to run openvpn with the assembled command-line. Shuts down openvpn after a timeout. Waits this time for openvpn to startup and writes the environment files from the scripts. As soon as they are present iptables rules are applied. If something goes wrong call the panic method :returns: True if everything went fine or running in daemon mode. """ config_dict = resources.get_config()["openvpn"] self.cleanup() printer = Printer() printer.info("Running openvpn with '{}'".format(self.cmd)) with subprocess.Popen(self.cmd) as ovpn: # give openvpn a maximum of 60 seconds to startup. A lower value is bad if # asked for username/password. # pylint: disable=unused-variable for i in range(300): try: if self.is_running(ovpn): # delay initialization of iptables until resource files are # created. If none are created the delay still applies as normal # timeout time.sleep(0.2) for script in config_dict["scripts"]: stage = script["stage"] if stage in ("up", "always"): resources.get_stats_file( stats_name=script["creates"], create=False ) else: self.panic(ovpn, "Openvpn process stopped unexpected.") if iptables.apply_config_dir(self.server, self.protocol): resources.write_stats(self.server, stats_name="server") stats_dict = resources.get_stats() stats_dict["last_server"] = {} stats_dict["last_server"]["domain"] = self.domain stats_dict["last_server"]["protocol"] = self.protocol resources.write_stats(stats_dict) else: self.panic(ovpn, "Applying iptables failed.") break except resources.ResourceNotFoundError: pass ### for else: self.panic(ovpn, "Timeout reached.") if self.is_running(ovpn): ovpn.wait() return True
def _forge_config(self): """Add the openvpn options from 'config.yml' to the openvpn command""" openvpn_config = resources.get_config()["openvpn"] for k, v in openvpn_config.items(): if isinstance(v, bool) or v in ("true", "True", "false", "False"): self._forge_bool(k, v) elif isinstance(v, list): self._forge_list(k, v) elif isinstance(v, (int, float)): self._forge_number(k, v) else: self._forge_string(k, v)
def test_get_config_when_invalid_yaml(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) yaml_fixture = "tests/fixtures/config_invalid_yaml_fixture.yml" mocked_config_file = _mock_get_config_file(mocker, yaml_fixture) # run and assert try: resources.get_config() assert False except resources.MalformedResourceError as error: assert error.resource_file == yaml_fixture 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' ) mocked_config_file.assert_called_once()
def test_get_config_when_valid_yaml(mocker): # setup mockbase = MockBase("resources") mockbase.setup(resources) fixture = "tests/fixtures/config_valid_yaml_fixture.yml" import yaml with open(fixture, "r") as fix_fd: expected_result = yaml.safe_load(fix_fd) mocked_config_file = _mock_get_config_file( mocker, "tests/fixtures/config_valid_yaml_fixture.yml") # run actual_result = resources.get_config() # assert mocked_config_file.assert_called_once() assert actual_result == expected_result
def test_it_get_config(): assert isinstance(resources.get_config(), dict)