66
71
self.mocker.result(signalmock)
68
73
proxymock.Get(ARGS, KWARGS)
75
self.expect(self.dbusmock.exceptions.DBusException)
76
self.mocker.result(exc)
69
77
signalmock.remove()
71
78
self.mocker.replay()
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)
86
def assertOffline(self, state):
87
"""Check that the state given is OFFLINE."""
88
self.assertEquals(state, OFFLINE)
90
def assertUnknown(self, state):
91
"""Check that the state was UNKNOWN."""
92
self.assertEquals(state, UNKNOWN)
93
# pylint: enable=C0103
95
def get_nms(self, callback):
96
"""Get the NetworkManagerState object."""
97
nms = NetworkManagerState(callback, self.dbusmock)
98
nms.find_online_state()
101
def check_nm_error(self, callback, error):
102
"""Check that the error handling is correct."""
103
nms = self.get_nms(callback)
106
def check_nm_state(self, callback, state):
107
"""Check the state handling is correct."""
108
nms = self.get_nms(callback)
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)
118
class NetworkManagerStateTestCase(NetworkManagerBaseTestCase):
119
"""Test NetworkManager state retrieval code."""
122
"""Setup the mocker dbus object tree."""
123
super(NetworkManagerStateTestCase, self).setUp()
73
126
def test_nm_online(self):
74
127
"""Check the connected case."""
76
def got_state_cb(state):
77
"""State was given."""
78
self.assertEquals(state, ONLINE)
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)
84
130
def test_nm_offline(self):
85
131
"""Check the disconnected case."""
87
def got_state_cb(state):
88
"""State was given."""
89
self.assertEquals(state, OFFLINE)
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)
95
134
def test_nm_connecting_then_online(self):
96
135
"""Check the waiting for connection case."""
98
def got_state_cb(state):
99
"""State was given."""
100
self.assertEquals(state, ONLINE)
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)
107
139
def test_nm_connecting_then_offline(self):
108
140
"""Check the waiting but fail case."""
110
def got_state_cb(state):
111
"""State was given."""
112
self.assertEquals(state, OFFLINE)
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)
120
class NetworkManagerStateErrorsTestCase(MockerTestCase):
141
self.check_nm_state_change(self.assertOffline,
142
NM_STATE_CONNECTING, NM_STATE_DISCONNECTED)
145
class NetworkManagerStateErrorsTestCase(NetworkManagerBaseTestCase):
121
146
"""Test NetworkManager state retrieval code."""
123
# Statement seems to have no effect
124
# pylint: disable=W0104
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)
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)
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)
146
self.dbusmock.Interface(proxymock, ANY)
147
ifmock = self.mocker.mock()
148
self.mocker.result(ifmock)
150
ifmock.connect_to_signal(ARGS, KWARGS)
151
signalmock = self.mocker.mock()
152
self.mocker.result(signalmock)
154
proxymock.Get(ARGS, KWARGS)
155
self.dbusmock.exceptions.DBusException
156
self.mocker.result(exc)
151
self.expect(self.dbusmock.exceptions.DBusException)
152
self.mocker.result(exc)
158
153
self.mocker.replay()
160
155
def test_nm_not_running(self):
161
156
"""Check the case when NM is not running."""
163
def got_state_cb(state):
164
"""State was given."""
165
self.assertEquals(state, UNKNOWN)
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())
172
160
def test_dbus_problem(self):
173
161
"""Check the case when DBus throws some other exception."""
175
def got_state_cb(state):
176
"""State was given."""
177
self.assertEquals(state, UNKNOWN)
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)