~openstack-charmers-next/charms/trusty/cisco-vpp/trunk

« back to all changes in this revision

Viewing changes to unit_tests/test_pcidev.py

  • Committer: Liam Young
  • Date: 2015-07-24 08:41:29 UTC
  • Revision ID: liam.young@canonical.com-20150724084129-rsakcktxl7fjd133
Fix misleading pcidev mock method names

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from test_utils import CharmTestCase, patch_open
2
2
from test_pcidev_helper import (
3
3
    check_device,
4
 
    mock_subprocess,
5
 
    mock_filehandle,
6
 
    mock_globs,
7
 
    mock_islink,
8
 
    mock_realpath,
 
4
    mocked_subprocess,
 
5
    mocked_filehandle,
 
6
    mocked_globs,
 
7
    mocked_islink,
 
8
    mocked_realpath,
9
9
)
10
10
from mock import patch, MagicMock
11
11
import PCIDev
45
45
    @patch('os.path.islink')
46
46
    @patch('os.path.realpath')
47
47
    def eth_int(self, pci_address, _osrealpath, _osislink, subproc_map=None):
48
 
        self.glob.glob.side_effect = mock_globs
49
 
        _osislink.side_effect = mock_islink
50
 
        _osrealpath.side_effect = mock_realpath
51
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
48
        self.glob.glob.side_effect = mocked_globs
 
49
        _osislink.side_effect = mocked_islink
 
50
        _osrealpath.side_effect = mocked_realpath
 
51
        self.subprocess.check_output.side_effect = mocked_subprocess(
52
52
            subproc_map=subproc_map)
53
53
 
54
54
        with patch_open() as (_open, _file):
55
 
            super_fh = mock_filehandle()
 
55
            super_fh = mocked_filehandle()
56
56
            _file.readlines = MagicMock()
57
57
            _open.side_effect = super_fh._setfilename
58
58
            _file.read.side_effect = super_fh._getfilecontents_read
102
102
    @patch('PCIDev.PCINetDevice.update_attributes')
103
103
    def test_get_loaded_kmod(self, _update):
104
104
        device = PCIDev.PCINetDevice('0000:06:00.0')
105
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
105
        self.subprocess.check_output.side_effect = mocked_subprocess()
106
106
        device.get_loaded_kmod()
107
107
        self.assertEqual(device.loaded_kmod, 'igb_uio')
108
108
 
109
109
    @patch('PCIDev.PCINetDevice.update_attributes')
110
110
    def test_get_loaded_kmod_notloaded(self, _update):
111
111
        device = PCIDev.PCINetDevice('0000:07:00.0')
112
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
112
        self.subprocess.check_output.side_effect = mocked_subprocess(
113
113
            subproc_map=pci_responses.NET_SETUP_ORPHAN)
114
114
        device.get_loaded_kmod()
115
115
        self.assertEqual(device.loaded_kmod, None)
117
117
    @patch('PCIDev.PCINetDevice.update_attributes')
118
118
    def test_get_modalias_kmod(self, _update):
119
119
        device = PCIDev.PCINetDevice('0000:07:00.0')
120
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
120
        self.subprocess.check_output.side_effect = mocked_subprocess()
121
121
        with patch_open() as (_open, _file):
122
 
            super_fh = mock_filehandle()
 
122
            super_fh = mocked_filehandle()
123
123
            _file.readlines = MagicMock()
124
124
            _open.side_effect = super_fh._setfilename
125
125
            _file.read.side_effect = super_fh._getfilecontents_read
132
132
    @patch('PCIDev.PCINetDevice.update_attributes')
133
133
    def test_get_interface_info_vpe(self, _update, _vpe_info, _eth_info):
134
134
        dev6 = PCIDev.PCINetDevice('0000:06:00.0')
135
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
135
        self.subprocess.check_output.side_effect = mocked_subprocess()
136
136
        dev6.get_loaded_kmod()
137
137
        dev6.get_interface_info()
138
138
        _vpe_info.assert_called_with()
143
143
    @patch('PCIDev.PCINetDevice.update_attributes')
144
144
    def test_get_interface_info_eth(self, _update, _vpe_info, _eth_info):
145
145
        dev = PCIDev.PCINetDevice('0000:10:00.0')
146
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
146
        self.subprocess.check_output.side_effect = mocked_subprocess()
147
147
        dev.get_loaded_kmod()
148
148
        dev.get_interface_info()
149
149
        _eth_info.assert_called_with()
154
154
    @patch('PCIDev.PCINetDevice.update_attributes')
155
155
    def test_get_interface_info_orphan(self, _update, _vpe_info, _eth_info):
156
156
        dev = PCIDev.PCINetDevice('0000:07:00.0')
157
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
157
        self.subprocess.check_output.side_effect = mocked_subprocess(
158
158
            subproc_map=pci_responses.NET_SETUP_ORPHAN)
159
159
        dev.get_loaded_kmod()
160
160
        dev.get_interface_info()
182
182
    @patch('PCIDev.PCINetDevice.update_attributes')
183
183
    def test_bind(self, _update, _pci_rescan):
184
184
        dev = PCIDev.PCINetDevice('0000:07:00.0')
185
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
185
        self.subprocess.check_output.side_effect = mocked_subprocess()
186
186
        with patch_open() as (_open, _file):
187
187
            dev.bind('enic')
188
188
            _open.assert_called_with('/sys/bus/pci/drivers/enic/bind', 'w')
193
193
    @patch('PCIDev.PCINetDevice.pci_rescan')
194
194
    @patch('PCIDev.PCINetDevice.update_attributes')
195
195
    def test_unbind(self, _update, _pci_rescan):
196
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
196
        self.subprocess.check_output.side_effect = mocked_subprocess()
197
197
        dev = PCIDev.PCINetDevice('0000:07:00.0')
198
198
        dev.get_loaded_kmod()
199
199
        with patch_open() as (_open, _file):
207
207
    @patch('PCIDev.PCINetDevice.pci_rescan')
208
208
    @patch('PCIDev.PCINetDevice.update_attributes')
209
209
    def test_unbind_nokmod(self, _update, _pci_rescan):
210
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
210
        self.subprocess.check_output.side_effect = mocked_subprocess(
211
211
            subproc_map=pci_responses.NET_SETUP_ORPHAN)
212
212
        dev = PCIDev.PCINetDevice('0000:07:00.0')
213
213
        dev.get_loaded_kmod()
218
218
 
219
219
    @patch('PCIDev.PCINetDevice.update_attributes')
220
220
    def test_get_vpe_interface_info(self, _update):
221
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
221
        self.subprocess.check_output.side_effect = mocked_subprocess()
222
222
        dev = PCIDev.PCINetDevice('0000:07:00.0')
223
223
        dev.get_vpe_interface_info()
224
224
        self.assertEqual('TenGigabitEthernet7/0/0', dev.interface_name)
227
227
 
228
228
    @patch('PCIDev.PCINetDevice.update_attributes')
229
229
    def test_get_vpe_interface_info_orphan(self, _update):
230
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
230
        self.subprocess.check_output.side_effect = mocked_subprocess(
231
231
            subproc_map=pci_responses.NET_SETUP_ORPHAN)
232
232
        dev = PCIDev.PCINetDevice('0000:07:00.0')
233
233
        dev.get_vpe_interface_info()
237
237
 
238
238
    @patch('PCIDev.PCINetDevice.update_attributes')
239
239
    def test_get_vpe_cli_out(self, _update):
240
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
240
        self.subprocess.check_output.side_effect = mocked_subprocess()
241
241
        dev = PCIDev.PCINetDevice('0000:07:00.0')
242
242
        self.assertTrue('local0' in dev.get_vpe_cli_out())
243
243
 
244
244
    @patch('PCIDev.PCINetDevice.get_vpe_cli_out')
245
245
    @patch('PCIDev.PCINetDevice.update_attributes')
246
246
    def test_get_vpe_interfaces_and_macs(self, _update, _get_vpe_cli_out):
247
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
247
        self.subprocess.check_output.side_effect = mocked_subprocess()
248
248
        _get_vpe_cli_out.return_value = pci_responses.CONFD_CLI
249
249
        dev = PCIDev.PCINetDevice('0000:07:00.0')
250
250
        vpe_devs = dev.get_vpe_interfaces_and_macs()
267
267
    @patch('subprocess.CalledProcessError')
268
268
    def test_get_vpe_interfaces_and_macs_invalid_cli(self, _excpt, _update,
269
269
                                                     _get_vpe_cli_out):
270
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
270
        self.subprocess.check_output.side_effect = mocked_subprocess()
271
271
        dev = PCIDev.PCINetDevice('0000:07:00.0')
272
272
        _get_vpe_cli_out.return_value = pci_responses.CONFD_CLI_NOLOCAL
273
273
        with nose.tools.assert_raises(Exception):
277
277
    @patch('PCIDev.PCINetDevice.update_attributes')
278
278
    def test_get_vpe_interfaces_and_macs_invmac(self, _update,
279
279
                                                _get_vpe_cli_out):
280
 
        self.subprocess.check_output.side_effect = mock_subprocess()
 
280
        self.subprocess.check_output.side_effect = mocked_subprocess()
281
281
        _get_vpe_cli_out.return_value = pci_responses.CONFD_CLI_INVMAC
282
282
        dev = PCIDev.PCINetDevice('0000:07:00.0')
283
283
        vpe_devs = dev.get_vpe_interfaces_and_macs()
373
373
    def test_get_sysnet_mac(self, _update):
374
374
        device = PCIDev.PCINetDevice('0000:10:00.1')
375
375
        with patch_open() as (_open, _file):
376
 
            super_fh = mock_filehandle()
 
376
            super_fh = mocked_filehandle()
377
377
            _file.readlines = MagicMock()
378
378
            _open.side_effect = super_fh._setfilename
379
379
            _file.read.side_effect = super_fh._getfilecontents_read
384
384
    def test_get_sysnet_device_state(self, _update):
385
385
        device = PCIDev.PCINetDevice('0000:10:00.1')
386
386
        with patch_open() as (_open, _file):
387
 
            super_fh = mock_filehandle()
 
387
            super_fh = mocked_filehandle()
388
388
            _file.readlines = MagicMock()
389
389
            _open.side_effect = super_fh._setfilename
390
390
            _file.read.side_effect = super_fh._getfilecontents_read
408
408
 
409
409
    @patch('os.path.islink')
410
410
    def pci_devs(self, _osislink, subproc_map=None):
411
 
        self.glob.glob.side_effect = mock_globs
 
411
        self.glob.glob.side_effect = mocked_globs
412
412
        rp_patcher = patch('os.path.realpath')
413
413
        rp_mock = rp_patcher.start()
414
 
        rp_mock.side_effect = mock_realpath
415
 
        _osislink.side_effect = mock_islink
416
 
        self.subprocess.check_output.side_effect = mock_subprocess(
 
414
        rp_mock.side_effect = mocked_realpath
 
415
        _osislink.side_effect = mocked_islink
 
416
        self.subprocess.check_output.side_effect = mocked_subprocess(
417
417
            subproc_map=subproc_map)
418
418
 
419
419
        with patch_open() as (_open, _file):
420
 
            super_fh = mock_filehandle()
 
420
            super_fh = mocked_filehandle()
421
421
            _file.readlines = MagicMock()
422
422
            _open.side_effect = super_fh._setfilename
423
423
            _file.read.side_effect = super_fh._getfilecontents_read