~shanepatrickfagan/ubuntu-sso-client/nm-state-bug-fix

« back to all changes in this revision

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

  • Committer: Shane Fagan
  • Date: 2011-06-16 14:09:01 UTC
  • Revision ID: shanepatrickfagan@ubuntu.com-20110616140901-s5wp07etv58fahtu
fixed nearly all of the tests and added methods for everything

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
"""Tests for the network state detection code."""
21
21
 
 
22
from twisted.trial.unittest import TestCase
 
23
 
22
24
from ubuntu_sso.networkstate import (NetworkManagerState,
23
25
                                     ONLINE, OFFLINE, UNKNOWN)
24
26
from ubuntu_sso.networkstate.linux import  (DBUS_UNKNOWN_SERVICE)
25
27
 
26
28
from mocker import ARGS, KWARGS, ANY, MockerTestCase
27
29
 
28
 
UNKNOWN = 0
29
 
 
30
30
 
31
31
class TestException(Exception):
32
32
    """An exception to test error conditions."""
46
46
    """Test NetworkManager state retrieval code."""
47
47
 
48
48
    def setUp(self):
49
 
        """Setup the mocker dbus object tree."""
50
 
        self.dbusmock = self.mocker.mock()
51
 
        self.dbusmock.SystemBus()
52
 
        sysbusmock = self.mocker.mock()
53
 
        self.mocker.result(sysbusmock)
54
 
 
55
 
        sysbusmock.get_object(ARGS, KWARGS)
56
 
        proxymock = self.mocker.mock()
57
 
        self.mocker.result(proxymock)
58
 
 
59
 
        self.dbusmock.Interface(proxymock, ANY)
60
 
        ifmock = self.mocker.mock()
61
 
        self.mocker.result(ifmock)
62
 
 
63
 
        ifmock.connect_to_signal(ARGS, KWARGS)
64
 
        signalmock = self.mocker.mock()
65
 
        self.mocker.result(signalmock)
66
 
 
67
 
        proxymock.Get(ARGS, KWARGS)
68
 
        signalmock.remove()
69
 
 
70
 
        self.mocker.replay()
 
49
         """Setup the mocker dbus object tree."""
 
50
         self.dbusmock = self.mocker.mock()
 
51
         self.dbusmock.SystemBus()
 
52
         sysbusmock = self.mocker.mock()
 
53
         self.mocker.result(sysbusmock)
 
54
 
 
55
         sysbusmock.get_object(ARGS, KWARGS)
 
56
         proxymock = self.mocker.mock()
 
57
         self.mocker.result(proxymock)
 
58
 
 
59
         self.dbusmock.Interface(proxymock, ANY)
 
60
         ifmock = self.mocker.mock()
 
61
         self.mocker.result(ifmock)
 
62
 
 
63
         ifmock.connect_to_signal(ARGS, KWARGS)
 
64
         signalmock = self.mocker.mock()
 
65
         self.mocker.result(signalmock)
 
66
 
 
67
         proxymock.Get(ARGS, KWARGS)
 
68
         signalmock.remove()
 
69
 
 
70
         self.mocker.replay()
71
71
 
72
72
    def test_nm_online(self):
73
73
        """Check the connected case."""
77
77
            self.assertEquals(state, ONLINE)
78
78
 
79
79
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
80
 
        nms.find_online_state()
81
 
        nms.got_state(NM_STATE_CONNECTED)
 
80
        nms.set_state_values(True)
 
81
        nms.got_state(nms.nm_state_connected)
82
82
 
83
83
    def test_nm_offline(self):
84
84
        """Check the disconnected case."""
88
88
            self.assertEquals(state, OFFLINE)
89
89
 
90
90
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
91
 
        nms.find_online_state()
92
 
        nms.got_state(NM_STATE_DISCONNECTED)
 
91
        nms.set_state_values(True)
 
92
        nms.got_state(nms.nm_state_disconnected)
93
93
 
94
94
    def test_nm_connecting_then_online(self):
95
95
        """Check the waiting for connection case."""
99
99
            self.assertEquals(state, ONLINE)
100
100
 
101
101
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
102
 
        nms.find_online_state()
103
 
        nms.got_state(NM_STATE_CONNECTING)
104
 
        nms.state_changed(NM_STATE_CONNECTED)
 
102
        nms.set_state_values(True)
 
103
        nms.got_state(nms.nm_state_connecting)
 
104
        nms.state_changed(nms.nm_state_connected)
105
105
 
106
106
    def test_nm_connecting_then_offline(self):
107
107
        """Check the waiting but fail case."""
111
111
            self.assertEquals(state, OFFLINE)
112
112
 
113
113
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
114
 
        nms.find_online_state()
115
 
        nms.got_state(NM_STATE_CONNECTING)
116
 
        nms.state_changed(NM_STATE_DISCONNECTED)
117
 
 
 
114
        nms.set_state_values(True)
 
115
        nms.got_state(nms.nm_state_connecting)
 
116
        nms.state_changed(nms.nm_state_disconnected)
 
117
 
 
118
 
 
119
class CheckVersionTestCase(TestCase):
 
120
    """Check the version tests."""
 
121
 
 
122
    def test_got_version(self):
 
123
        """Check that got version checks for the right version."""
 
124
        self.new_state_version = None
 
125
 
 
126
        def check_return_value(state_version):
 
127
            """Test true return."""
 
128
            self.new_state_version = state_version
 
129
 
 
130
        def check_got_version(version, expected):
 
131
            nms.got_version(version)
 
132
            self.assertEquals(self.new_state_version, expected)
 
133
 
 
134
        nms = NetworkManagerState(None, None)
 
135
        self.patch(nms, "set_state_values", check_return_value)
 
136
        versions = ["0.8.9997", "0.9", "1.0", "1.12.4215"]
 
137
        for x in versions:
 
138
            check_got_version(x, True)
 
139
        versions = ["0.8.8713", "0.3.2141", "0.1.3124", "0.5.5125"]
 
140
        for x in versions:
 
141
            check_got_version(x, False)
 
142
 
 
143
class CheckSetValuesTestCase(TestCase):
 
144
    """Check that the values are set."""
 
145
 
 
146
    def test_set_state(self):
 
147
        """Test that the states are actually set."""
 
148
        def check_set_values(nms):
 
149
            if nms.nm_state_asleep == 10 and \
 
150
                     nms.nm_state_disconnected == 30 and \
 
151
                     nms.nm_state_connecting == 40 and \
 
152
                     nms.nm_state_connected == 70 and \
 
153
                     nms.nm_state_unknown == 0:
 
154
                return True
 
155
            else:
 
156
                return False
 
157
        nms = NetworkManagerState(None, None)
 
158
        nms.set_state_values(True)
 
159
        self.assertEquals(check_set_values(nms), True)
 
160
        nms.set_state_values(False)
 
161
        self.assertEquals(check_set_values(nms), False)
 
162
 
 
163
 
 
164
class CheckDetectOnlineStateMethodCall(TestCase):
 
165
    """Check detect online state method call."""
 
166
 
 
167
    def test_detect_online_state_call(self):
 
168
        self.is_called = False
 
169
        def check_method_call():
 
170
            self.is_called = True
 
171
        nms = NetworkManagerState(None, None)
 
172
        self.patch(nms, "detect_online_state", check_method_call)
 
173
        nms.detect_online_state()
 
174
        self.assertEquals(self.is_called, True)
118
175
 
119
176
class NetworkManagerStateErrorsTestCase(MockerTestCase):
120
177
    """Test NetworkManager state retrieval code."""
161
218
 
162
219
        def got_state_cb(state):
163
220
            """State was given."""
164
 
            self.assertEquals(state, UNKNOWN)
 
221
            self.assertEquals(state, nms.nm_state_unknown)
165
222
 
166
223
        self.mock_dbus_error_while_getting_state(TestNmNotAvailableException)
167
224
        nms = NetworkManagerState(got_state_cb, self.dbusmock)
168
 
        nms.find_online_state()
 
225
        nms.set_state_values(True)
169
226
        nms.got_error(TestNmNotAvailableException())
170
227
 
171
228
    def test_dbus_problem(self):