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 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)
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())
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')