summaryrefslogtreecommitdiff
path: root/src/leap/eip/test_conductor.py
blob: 27460d54f2cc736d540d82e3dc924c3e60a02fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import ConfigParser
import logging
import platform

logging.basicConfig()
logger = logging.getLogger(name=__name__)

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from mock import Mock, patch  # MagicMock

from leap.eip.conductor import EIPConductor

_system = platform.system()


class NotImplementedError(Exception):
    pass


@patch('OpenVPNConnection._get_or_create_config')
@patch('OpenVPNConnection._set_ovpn_command')
class MockedEIPConductor(EIPConductor):
    def _get_or_create_config(self):
        self.config = ConfigParser.ConfigParser()
        self._set_ovpn_command()

    def _set_ovpn_command(self):
        self.command = "mock_command"
        self.args = [1, 2, 3]


class EIPConductorTest(unittest.TestCase):

    __name__ = "eip_conductor_tests"

    def setUp(self):
        self.manager = Mock(
            name="openvpnmanager_mock")

        self.con = MockedEIPConductor(
            manager=self.manager)
        #self.con._connect = Mock(
            #name="_connect")

    def tearDown(self):
        del self.con

    #
    # helpers
    #

    def _missing_test_for_plat(self, do_raise=False):
        if do_raise:
            raise NotImplementedError(
                "This test is not implemented "
                "for the running platform: %s" %
                _system)

    #
    # tests
    #

    def test_manager_was_initialized(self):
        """
        manager init ok during conductor init?
        """
        self.manager.assert_called_once_with()

    def test_vpnconnection_defaults(self):
        """
        default attrs as expected
        """
        con = self.con
        self.assertEqual(con.autostart, True)
        self.assertEqual(con.missing_pkexec, False)
        self.assertEqual(con.missing_vpn_keyfile, False)
        self.assertEqual(con.missing_provider, False)
        self.assertEqual(con.bad_provider, False)

    def test_config_was_init(self):
        """
        is there a config object?
        """
        self.assertTrue(isinstance(self.con.config,
                        ConfigParser.ConfigParser))

    def test_ovpn_command(self):
        """
        set_ovpn_command called
        """
        self.assertEqual(self.con.command,
                         "mock_command")
        self.assertEqual(self.con.args,
                         [1, 2, 3])

    # connect/disconnect calls

    def test_disconnect(self):
        """
        disconnect method calls private and changes status
        """
        self.con._disconnect = Mock(
            name="_disconnect")

        # first we set status to connected
        self.con.status.set_current(self.con.status.CONNECTED)
        self.assertEqual(self.con.status.current,
                         self.con.status.CONNECTED)

        # disconnect
        self.con.disconnect()
        self.con._disconnect.assert_called_once_with()

        # new status should be disconnected
        # XXX this should evolve and check no errors
        # during disconnection
        self.assertEqual(self.con.status.current,
                         self.con.status.DISCONNECTED)

    def test_connect(self):
        """
        connect calls _launch_openvpn private
        """
        self.con._launch_openvpn = Mock()
        self.con.connect()
        self.con._launch_openvpn.assert_called_once_with()

    # XXX tests breaking here ...

    def test_good_poll_connection_state(self):
        """
        """
        #@patch --
        # self.manager.get_connection_state
        state = self.con.poll_connection_state()
        # too whitebox??
        #self.con.status.set_vpn_state.assert_called_with(status_step)
        self.assertEqual(state, 'connected')

    def test_bad_poll_connection_state(self):
        """
        """
        #@patch --
        # self.manager.get_connection_state
        # but raise ConnectionRefusedError
        state = self.con.poll_connection_state()
        self.assertEqual(state, None)


    # XXX more things to test:
    # - called config routinges during initz.
    # - raising proper exceptions with no config
    # - called proper checks on config / permissions


if __name__ == "__main__":
    unittest.main()