class TestTunnelConf(TestStartingSingleTunnel):
    """
    Run the same tests as TestStartingSingleTunnel with a tunnel
    loaded from the configuration file. Also adds a few extra tests.
    """

    def setUp(self):
        self.executable = 'cal_run_forever'

        conf = SafeConfigParser()
        sec1 = 'tunnel:test'
        conf.add_section(sec1)
        conf.set(sec1, 'tunnel_type', 'base')
        conf.set(sec1, 'cmd', self.executable)
        conf.set(sec1, 'executable', self.executable)

        self.tm = TunnelManager()
        self.tm.load_tunnels(conf)
        self.tm.start_tunnels()

        self.t = self.tm.tunnels[0]

    def test_already_loaded(self):
        conf = SafeConfigParser()
        self.assertRaises(TunnelsAlreadyLoadedException, self.tm.load_tunnels, *[conf])
    def setUp(self):
        self.executable = 'cal_run_forever'
        self.cmd = [self.executable]

        self.t = TunnelBase(self.cmd, self.executable)

        self.tm = TunnelManager()
        self.tm.tunnels = [self.t]
    def test_minimums(self):
        self.conf = SafeConfigParser()
        self.sec1 = 'tunnel:testvpnc'
        self.conf.add_section(self.sec1)
        self.conf.set(self.sec1, 'tunnel_type', 'vpnc')
        self.conf.set(self.sec1, 'conf_file', '/path/to/conf.conf')

        tm = TunnelManager()
        tm.load_tunnels(self.conf)

        self.assertEqual(len(tm.tunnels), 1)
    def setUp(self):
        self.executable = 'cal_run_forever'

        self.conf = SafeConfigParser()
        self.sec1 = 'tunnel:test'
        self.conf.add_section(self.sec1)
        self.conf.set(self.sec1, 'tunnel_type', 'base')
        self.conf.set(self.sec1, 'cmd', self.executable)
        self.conf.set(self.sec1, 'executable', self.executable)

        self.tm = TunnelManager()
    def setUp(self):
        self.conf = SafeConfigParser()
        self.sec1 = 'tunnel:testvpnc'
        self.conf.add_section(self.sec1)
        self.conf.set(self.sec1, 'tunnel_type', 'vpnc')
        self.conf.set(self.sec1, 'conf_file', '/path/to/conf.conf')
        self.conf.set(self.sec1, 'ips', '10.10.10.10, 5.5.5.5')

        self.vpnc_sec = 'vpnc'
        self.conf.add_section(self.vpnc_sec)
        self.conf.set(self.vpnc_sec, 'bin', '/path/to/vpnc.bin')
        self.conf.set(self.vpnc_sec, 'base_conn_script', '/path/to/vpnc-conn-script')

        self.tm = TunnelManager()
    def setUp(self):
        self.executable = 'cal_run_forever'

        conf = SafeConfigParser()
        sec1 = 'tunnel:test'
        conf.add_section(sec1)
        conf.set(sec1, 'tunnel_type', 'base')
        conf.set(sec1, 'cmd', self.executable)
        conf.set(sec1, 'executable', self.executable)

        self.tm = TunnelManager()
        self.tm.load_tunnels(conf)
        self.tm.start_tunnels()

        self.t = self.tm.tunnels[0]
Exemplo n.º 7
0
def run_tunnels(configfile='/etc/calabar/calabar.conf'):
    """Run the configured VPN/SSH tunnels and keep them running"""
    config = SafeConfigParser()
    config.read(configfile)

    tm = TunnelManager()
    _run_tunnels(tm, config)
class TestInvalidTunnels(unittest.TestCase):

    def setUp(self):
        self.tunnel = TunnelBase(['ls'], 'DOESNOTEXIST')

        self.tm = TunnelManager()
        self.tm.tunnels.append(self.tunnel)

    def test_start_bad_exec(self):
        self.tm.start_tunnels()
        # No error was raised
        self.assertFalse(self.tunnel.is_running())

    def test_continue_bad_exec(self):
        self.tm.continue_tunnels()
        # No error was raised
        self.assertFalse(self.tunnel.is_running())
    def test_invalid_tunnel_type_conf(self):
        conf = SafeConfigParser()
        sec1 = 'tunnel:test'
        conf.add_section(sec1)
        conf.set(sec1, 'tunnel_type', 'INVALID')

        tm = TunnelManager()
        self.assertRaises(NotImplementedError, tm.load_tunnels, *[conf])
Exemplo n.º 10
0
class TestLongRun(TearDownRunForever):

    def setUp(self):
        self.executable = 'cal_run_forever'
        self.cmd = [self.executable]

        self.t = TunnelBase(self.cmd, self.executable)

        self.tm = TunnelManager()
        self.tm.tunnels = [self.t]

    def tearDown(self):
        signal.signal(signal.SIGCHLD, signal.SIG_IGN)
        try:
            subprocess.call("ps auxww | grep %s | awk '{print $2}' | xargs kill" % self.executable, shell=True)
        except OSError:
            pass

    def test_single_run(self):
        self.tm.start_tunnels()
        self.tm.continue_tunnels()
        time.sleep(1)

        self.assertTrue(is_really_running(self.t))

    def test_close_running_externally_handled(self):
        self.tm.start_tunnels()

        proc = self.t.proc
        os.kill(proc.pid, signal.SIGTERM)
        # Wait for the process to close
        _wait_for_condition(lambda : not self.t.is_running(), "Process not closed")

        self.assertFalse(self.t.is_running())

    def test_continue_after_kill(self):
        self.tm.start_tunnels()

        self.t.close(wait=False)

        _wait_for_condition(lambda: not is_really_running(self.t), "Process not stopped")
        self.tm.continue_tunnels()
        _wait_for_condition(lambda: is_really_running(self.t), "Process not started")

        self.assertTrue(is_really_running(self.t))
Exemplo n.º 11
0
class TestVpncParser(unittest.TestCase):

    def setUp(self):
        self.conf = SafeConfigParser()
        self.sec1 = 'tunnel:testvpnc'
        self.conf.add_section(self.sec1)
        self.conf.set(self.sec1, 'tunnel_type', 'vpnc')
        self.conf.set(self.sec1, 'conf_file', '/path/to/conf.conf')
        self.conf.set(self.sec1, 'ips', '10.10.10.10, 5.5.5.5')

        self.vpnc_sec = 'vpnc'
        self.conf.add_section(self.vpnc_sec)
        self.conf.set(self.vpnc_sec, 'bin', '/path/to/vpnc.bin')
        self.conf.set(self.vpnc_sec, 'base_conn_script', '/path/to/vpnc-conn-script')

        self.tm = TunnelManager()

    def test_base_parse(self):
        self.tm.load_tunnels(self.conf)
        self.assertEqual(len(self.tm.tunnels), 1)

    def test_name(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.assertEqual(t.name, 'testvpnc')

    def test_cmd(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        expected_cmd = ['calabar_vpnc', '/path/to/conf.conf', '--no-detach',
                        '--local-port', '0', '--non-inter', '--script']
        self.assertEqual(len(t.cmd), len(expected_cmd)+1) # +1 for the actual script

        for expected_arg in expected_cmd:
            self.assertTrue(expected_arg in t.cmd)

        # Now check the last arg and make sure it's to /tmp/...
        self.assertTrue(t.cmd[-1].startswith('/tmp/'))


    def test_executable(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.assertEqual(t.executable, '/path/to/vpnc.bin')
Exemplo n.º 12
0
    def setUp(self):
        self.tunnel = TunnelBase(['ls'], 'DOESNOTEXIST')

        self.tm = TunnelManager()
        self.tm.tunnels.append(self.tunnel)
Exemplo n.º 13
0
    def test_invalid_load_tunnel_type(self):
        tun_conf_d = {'tunnel_type': 'INVALID'}

        tm = TunnelManager()
        self.assertRaises(NotImplementedError, tm._load_tunnel, *['test', tun_conf_d])
Exemplo n.º 14
0
class TestStartingSingleTunnel(TearDownRunForever):

    def setUp(self):
        self.executable = 'cal_run_forever'
        self.cmd = [self.executable]

        self.t = TunnelBase(self.cmd, self.executable)

        self.tm = TunnelManager()
        self.tm.tunnels = [self.t]

    def test_start_success(self):
        self.tm.start_tunnels()

        self.assertTrue(self.t.is_running())

    def test_continue_not_started(self):
        """
        Calling continue before starting shouldn't error out
        """
        self.tm.continue_tunnels()

    def test_continue_post_start(self):
        self.tm.start_tunnels()

        self.tm.continue_tunnels()
        self.assertTrue(self.t.is_running())

    def test_vanilla_continue(self):
        self.tm.start_tunnels()
        self.tm.continue_tunnels()

        self.assertTrue(self.t.is_running())

    def test_close(self):
        self.tm.start_tunnels()

        self.t.close(wait=False)
Exemplo n.º 15
0
class TestBaseParser(TearDownRunForever):

    def setUp(self):
        self.executable = 'cal_run_forever'

        self.conf = SafeConfigParser()
        self.sec1 = 'tunnel:test'
        self.conf.add_section(self.sec1)
        self.conf.set(self.sec1, 'tunnel_type', 'base')
        self.conf.set(self.sec1, 'cmd', self.executable)
        self.conf.set(self.sec1, 'executable', self.executable)

        self.tm = TunnelManager()

    def test_base_parse(self):
        self.tm.load_tunnels(self.conf)
        self.tm.start_tunnels()

    def test_name(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.assertEqual(t.name, 'test')

    def test_cmd(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.assertEqual(t.cmd, [self.executable])

    def test_executable(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.assertEqual(t.executable, self.executable)

    def test_runs(self):
        self.tm.load_tunnels(self.conf)
        t = self.tm.tunnels[0]

        self.tm.start_tunnels()

        self.assertTrue(t.is_running())