~mvo/ubuntu-sso-client/strawman-lp711413

« back to all changes in this revision

Viewing changes to ubuntu_sso/networkstate/tests/test_linux.py

  • Committer: Manuel de la Pena
  • Date: 2011-06-16 17:05:26 UTC
  • mfrom: (719 ubuntu-sso-client)
  • mto: This revision was merged to the branch mainline in revision 721.
  • Revision ID: mandel@themacaque.com-20110616170526-pvdvfhin2t2ppbxl
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        return DBUS_UNKNOWN_SERVICE
44
44
 
45
45
 
46
 
class NetworkManagerStateTestCase(MockerTestCase):
47
 
    """Test NetworkManager state retrieval code."""
 
46
class NetworkManagerBaseTestCase(MockerTestCase):
 
47
    """Base test case for NM state tests."""
48
48
 
49
49
    def setUp(self):
50
50
        """Setup the mocker dbus object tree."""
 
51
        super(NetworkManagerBaseTestCase, self).setUp()
 
52
 
51
53
        self.dbusmock = self.mocker.mock()
52
54
        self.dbusmock.SystemBus()
53
55
        sysbusmock = self.mocker.mock()
54
56
        self.mocker.result(sysbusmock)
55
57
 
56
58
        sysbusmock.get_object(ARGS, KWARGS)
 
59
 
 
60
    def connect_proxy(self, exc=None):
 
61
        """Get a proxy mock object, and allow failing with specified exc."""
57
62
        proxymock = self.mocker.mock()
58
63
        self.mocker.result(proxymock)
59
64
 
66
71
        self.mocker.result(signalmock)
67
72
 
68
73
        proxymock.Get(ARGS, KWARGS)
 
74
        if exc is not None:
 
75
            self.expect(self.dbusmock.exceptions.DBusException)
 
76
            self.mocker.result(exc)
69
77
        signalmock.remove()
70
 
 
71
78
        self.mocker.replay()
72
79
 
 
80
    # Invalid name "assert[A-Z].*"
 
81
    # pylint: disable=C0103
 
82
    def assertOnline(self, state):
 
83
        """Check that the state given is ONLINE."""
 
84
        self.assertEquals(state, ONLINE)
 
85
 
 
86
    def assertOffline(self, state):
 
87
        """Check that the state given is OFFLINE."""
 
88
        self.assertEquals(state, OFFLINE)
 
89
 
 
90
    def assertUnknown(self, state):
 
91
        """Check that the state was UNKNOWN."""
 
92
        self.assertEquals(state, UNKNOWN)
 
93
    # pylint: enable=C0103
 
94
 
 
95
    def get_nms(self, callback):
 
96
        """Get the NetworkManagerState object."""
 
97
        nms = NetworkManagerState(callback, self.dbusmock)
 
98
        nms.find_online_state()
 
99
        return nms
 
100
 
 
101
    def check_nm_error(self, callback, error):
 
102
        """Check that the error handling is correct."""
 
103
        nms = self.get_nms(callback)
 
104
        nms.got_error(error)
 
105
 
 
106
    def check_nm_state(self, callback, state):
 
107
        """Check the state handling is correct."""
 
108
        nms = self.get_nms(callback)
 
109
        nms.got_state(state)
 
110
 
 
111
    def check_nm_state_change(self, callback, fmstate, tostate):
 
112
        """Check the state change handling is correct."""
 
113
        nms = self.get_nms(callback)
 
114
        nms.got_state(fmstate)
 
115
        nms.state_changed(tostate)
 
116
 
 
117
 
 
118
class NetworkManagerStateTestCase(NetworkManagerBaseTestCase):
 
119
    """Test NetworkManager state retrieval code."""
 
120
 
 
121
    def setUp(self):
 
122
        """Setup the mocker dbus object tree."""
 
123
        super(NetworkManagerStateTestCase, self).setUp()
 
124
        self.connect_proxy()
 
125
 
73
126
    def test_nm_online(self):
74
127
        """Check the connected case."""
75
 
 
76
 
        def got_state_cb(state):
77
 
            """State was given."""
78
 
            self.assertEquals(state, ONLINE)
79
 
 
80
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
81
 
        nms.find_online_state()
82
 
        nms.got_state(NM_STATE_CONNECTED)
 
128
        self.check_nm_state(self.assertOnline, NM_STATE_CONNECTED)
83
129
 
84
130
    def test_nm_offline(self):
85
131
        """Check the disconnected case."""
86
 
 
87
 
        def got_state_cb(state):
88
 
            """State was given."""
89
 
            self.assertEquals(state, OFFLINE)
90
 
 
91
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
92
 
        nms.find_online_state()
93
 
        nms.got_state(NM_STATE_DISCONNECTED)
 
132
        self.check_nm_state(self.assertOffline, NM_STATE_DISCONNECTED)
94
133
 
95
134
    def test_nm_connecting_then_online(self):
96
135
        """Check the waiting for connection case."""
97
 
 
98
 
        def got_state_cb(state):
99
 
            """State was given."""
100
 
            self.assertEquals(state, ONLINE)
101
 
 
102
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
103
 
        nms.find_online_state()
104
 
        nms.got_state(NM_STATE_CONNECTING)
105
 
        nms.state_changed(NM_STATE_CONNECTED)
 
136
        self.check_nm_state_change(self.assertOnline,
 
137
                                   NM_STATE_CONNECTING, NM_STATE_CONNECTED)
106
138
 
107
139
    def test_nm_connecting_then_offline(self):
108
140
        """Check the waiting but fail case."""
109
 
 
110
 
        def got_state_cb(state):
111
 
            """State was given."""
112
 
            self.assertEquals(state, OFFLINE)
113
 
 
114
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
115
 
        nms.find_online_state()
116
 
        nms.got_state(NM_STATE_CONNECTING)
117
 
        nms.state_changed(NM_STATE_DISCONNECTED)
118
 
 
119
 
 
120
 
class NetworkManagerStateErrorsTestCase(MockerTestCase):
 
141
        self.check_nm_state_change(self.assertOffline,
 
142
                                   NM_STATE_CONNECTING, NM_STATE_DISCONNECTED)
 
143
 
 
144
 
 
145
class NetworkManagerStateErrorsTestCase(NetworkManagerBaseTestCase):
121
146
    """Test NetworkManager state retrieval code."""
122
147
 
123
 
    # Statement seems to have no effect
124
 
    # pylint: disable=W0104
125
 
 
126
 
    def setUp(self):
127
 
        """Setup the mocker dbus object tree."""
128
 
        self.dbusmock = self.mocker.mock()
129
 
        self.dbusmock.SystemBus()
130
 
        self.sysbusmock = self.mocker.mock()
131
 
        self.mocker.result(self.sysbusmock)
132
 
        self.sysbusmock.get_object(ARGS, KWARGS)
133
 
 
134
148
    def mock_except_while_getting_proxy(self, exc):
135
149
        """Simulate an exception while getting the DBus proxy object."""
136
150
        self.mocker.throw(exc)
137
 
        self.dbusmock.exceptions.DBusException
138
 
        self.mocker.result(exc)
139
 
        self.mocker.replay()
140
 
 
141
 
    def mock_dbus_error_while_getting_state(self, exc):
142
 
        """Simulate an exception while getting the State."""
143
 
        proxymock = self.mocker.mock()
144
 
        self.mocker.result(proxymock)
145
 
 
146
 
        self.dbusmock.Interface(proxymock, ANY)
147
 
        ifmock = self.mocker.mock()
148
 
        self.mocker.result(ifmock)
149
 
 
150
 
        ifmock.connect_to_signal(ARGS, KWARGS)
151
 
        signalmock = self.mocker.mock()
152
 
        self.mocker.result(signalmock)
153
 
 
154
 
        proxymock.Get(ARGS, KWARGS)
155
 
        self.dbusmock.exceptions.DBusException
156
 
        self.mocker.result(exc)
157
 
        signalmock.remove()
 
151
        self.expect(self.dbusmock.exceptions.DBusException)
 
152
        self.mocker.result(exc)
158
153
        self.mocker.replay()
159
154
 
160
155
    def test_nm_not_running(self):
161
156
        """Check the case when NM is not running."""
162
 
 
163
 
        def got_state_cb(state):
164
 
            """State was given."""
165
 
            self.assertEquals(state, UNKNOWN)
166
 
 
167
 
        self.mock_dbus_error_while_getting_state(TestNmNotAvailableException)
168
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
169
 
        nms.find_online_state()
170
 
        nms.got_error(TestNmNotAvailableException())
 
157
        self.connect_proxy(TestNmNotAvailableException)
 
158
        self.check_nm_error(self.assertUnknown, TestNmNotAvailableException())
171
159
 
172
160
    def test_dbus_problem(self):
173
161
        """Check the case when DBus throws some other exception."""
174
 
 
175
 
        def got_state_cb(state):
176
 
            """State was given."""
177
 
            self.assertEquals(state, UNKNOWN)
178
 
 
179
162
        self.mock_except_while_getting_proxy(TestException)
180
 
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
181
 
        nms.find_online_state()
 
163
        self.get_nms(self.assertUnknown)