1 # Copyright (c) 2017 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """Unit tests for server/cros/network/rf_switch/scpi.py.""" 6 7 import socket 8 import unittest 9 10 from autotest_lib.client.common_lib.test_utils import mock 11 from autotest_lib.server.cros.network.rf_switch import rf_mocks 12 from autotest_lib.server.cros.network.rf_switch import scpi 13 14 15 class ScpiTestBase(unittest.TestCase): 16 """Base class for scpi unit test cases.""" 17 18 _HOST = '1.2.3.4' 19 _PORT = 5015 20 21 def setUp(self): 22 self.god = mock.mock_god(debug=False) 23 24 def tearDown(self): 25 self.god.unstub_all() 26 27 28 class ScpiInitTest(ScpiTestBase): 29 """Unit test for the scpi initializing.""" 30 31 def test_scpi_init(self): 32 """Verify socket connection creations for SCPI.""" 33 self.god.stub_function(socket, 'socket') 34 sock = rf_mocks.SocketMock(socket, socket) 35 socket.socket.expect_call().and_return(sock) 36 37 self.god.stub_function(sock, 'connect') 38 sock.connect.expect_call((self._HOST, self._PORT)) 39 mock_scpi = rf_mocks.ScpiMock(self._HOST, self._PORT) 40 self.god.check_playback() 41 42 self.assertEquals(mock_scpi.host, self._HOST) 43 self.assertEquals(mock_scpi.port, self._PORT) 44 45 46 class ScpiMethodTests(ScpiTestBase): 47 """Unit tests for scpi Methods.""" 48 49 def setUp(self): 50 """Define mock god and mock scpi.""" 51 super(ScpiMethodTests, self).setUp() 52 self.mock_scpi = rf_mocks.ScpiMock( 53 self._HOST, self._PORT, test_interface=True) 54 55 def test_write_method(self): 56 """Verify write method sends correct command.""" 57 test_command = 'this is a command' 58 59 # monitor send for correct command. 60 self.god.stub_function(self.mock_scpi.socket, 'send') 61 self.mock_scpi.socket.send.expect_call('%s' % test_command) 62 63 # call reset and see correct command is send to socket. 64 self.mock_scpi.write(test_command) 65 self.god.check_playback() 66 67 def test_read_method(self): 68 """Verify read method.""" 69 test_buffer = 'This is a response.' 70 71 # Mock socket receive to send our test buffer. 72 self.god.stub_function_to_return( 73 self.mock_scpi.socket, 'recv', test_buffer) 74 75 response = self.mock_scpi.read() 76 77 # check we got correct information back 78 self.assertEqual(test_buffer, response, 'Read response did not match') 79 80 def test_query_method(self): 81 """Verify Query command send and receives response back.""" 82 test_command = 'this is a command' 83 test_buffer = 'This is a response.' 84 85 # Mock socket receive to send our test buffer. 86 self.god.stub_function_to_return( 87 self.mock_scpi.socket, 'recv', test_buffer) 88 89 # monitor send for correct command. 90 self.god.stub_function(self.mock_scpi.socket, 'send') 91 self.mock_scpi.socket.send.expect_call('%s' % test_command) 92 93 # call reset and see correct command is send to socket. 94 response = self.mock_scpi.query(test_command) 95 self.god.check_playback() 96 97 # check we got correct information back 98 self.assertEqual(test_buffer, response, 'Read response did not match') 99 100 def test_error_query_method(self): 101 """Verify error query returns correct error and message.""" 102 code = 101 103 msg = 'Error Message' 104 105 self.god.stub_function_to_return( 106 self.mock_scpi.socket, 'recv', '%d, "%s"' % (code, msg)) 107 108 # monitor send for correct command. 109 self.god.stub_function(self.mock_scpi.socket, 'send') 110 self.mock_scpi.socket.send.expect_call('%s\n' % 111 scpi.Scpi.CMD_ERROR_CHECK) 112 113 # call info and see correct command is send to socket. 114 code_recv, msg_recv = self.mock_scpi.error_query() 115 self.god.check_playback() 116 117 # check we got right information back 118 self.assertEqual(code, code_recv, 'Error code did not match') 119 self.assertEqual(msg, msg_recv, 'Error message did not match') 120 121 def test_info_method(self): 122 """Verify info method returns correct information.""" 123 info = { 124 'Manufacturer': 'Company Name', 125 'Model': 'Model Name', 126 'Serial': '1234567890', 127 'Version': '1.2.3.4.5' 128 } 129 self.god.stub_function_to_return( 130 self.mock_scpi.socket, 'recv', '%s,%s,%s,%s' % ( 131 info['Manufacturer'], info['Model'], info['Serial'], 132 info['Version'])) 133 134 # monitor send for correct command. 135 self.god.stub_function(self.mock_scpi.socket, 'send') 136 self.mock_scpi.socket.send.expect_call('%s\n' % scpi.Scpi.CMD_IDENTITY) 137 138 # call info and see correct command is send to socket. 139 info_recv = self.mock_scpi.info() 140 self.god.check_playback() 141 142 # check we got right information back 143 self.assertDictEqual(info, info_recv, 'Info returned did not match') 144 145 def test_reset_method(self): 146 """Verify reset method.""" 147 # monitor send for correct command. 148 self.god.stub_function(self.mock_scpi.socket, 'send') 149 self.mock_scpi.socket.send.expect_call('%s\n' % scpi.Scpi.CMD_RESET) 150 151 # call reset and see correct command is send to socket. 152 self.mock_scpi.reset() 153 self.god.check_playback() 154 155 def test_status_method(self): 156 """Verify status method.""" 157 status = 1 158 159 self.god.stub_function_to_return(self.mock_scpi.socket, 'recv', status) 160 161 # monitor send for correct command. 162 self.god.stub_function(self.mock_scpi.socket, 'send') 163 self.mock_scpi.socket.send.expect_call('%s\n' % 164 scpi.Scpi.CMD_STATUS) 165 166 # call info and see correct command is send to socket. 167 status_recv = self.mock_scpi.status() 168 self.god.check_playback() 169 170 # check we got right information back 171 self.assertEqual(status, status_recv, 'returned status did not match') 172 173 if __name__ == '__main__': 174 unittest.main() 175