def test_sendall_passing_to_socket(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "close") self.m.StubOutWithMock(self.ais, "_sendall") # rest _unicode = str if sys.version_info[0] >= 3 else unicode self.ais._connected = True for line in [ "test", "test\r\n", _unicode("test"), _unicode("test\r\n"), ]: # setup self.ais.sock = mox.MockAnything() self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais._sendall(b"%c" + line.rstrip('\r\n').encode('ascii') + b'\r\n').AndReturn(None) mox.Replay(self.ais.sock) self.ais.sendall(line) mox.Verify(self.ais.sock)
def __init__(self): self.conductor_api = mox.MockAnything() self.db = mox.MockAnything() self._events = [] self.instance_events = mock.MagicMock() self.instance_events.prepare_for_instance_event.side_effect = \ self._prepare_for_instance_event
def test_copy_image_to_volume(self): """resize_image common case usage.""" mox = self.mox drv = self._driver TEST_IMG_SOURCE = 'foo.img' volume = {'size': self.TEST_SIZE_IN_GB, 'name': TEST_IMG_SOURCE} def fake_local_path(volume): return volume['name'] self.stubs.Set(drv, 'local_path', fake_local_path) mox.StubOutWithMock(image_utils, 'fetch_to_raw') image_utils.fetch_to_raw(None, None, None, TEST_IMG_SOURCE, mox_lib.IgnoreArg(), size=self.TEST_SIZE_IN_GB, run_as_root=True) mox.StubOutWithMock(image_utils, 'resize_image') image_utils.resize_image(TEST_IMG_SOURCE, self.TEST_SIZE_IN_GB, run_as_root=True) mox.StubOutWithMock(image_utils, 'qemu_img_info') data = mox_lib.MockAnything() data.virtual_size = 1 * units.Gi image_utils.qemu_img_info(TEST_IMG_SOURCE, run_as_root=True).AndReturn(data) mox.ReplayAll() drv.copy_image_to_volume(None, volume, None, None) mox.VerifyAll()
def test_socket_readlines(self): fdr, fdw = os.pipe() f = os.fdopen(fdw, 'w') f.write("something") f.close() class BreakBlocking(Exception): pass self.m.ReplayAll() self.ais.sock = mox.MockAnything() # part 1 - conn drop before setblocking self.ais.sock.setblocking(0).AndRaise(socket.error) # part 2 - conn drop trying to recv self.ais.sock.setblocking(0) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b'') # part 3 - nothing to read self.ais.sock.setblocking(0) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndRaise( socket.error("Resource temporarily unavailable")) # part 4 - yield 3 lines (blocking False) self.ais.sock.setblocking(0) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"a\r\n" * 3) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndRaise( socket.error("Resource temporarily unavailable")) # part 5 - yield 3 lines 2 times (blocking True) self.ais.sock.setblocking(0) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"b\r\n" * 3) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"b\r\n" * 3) self.ais.sock.fileno().AndReturn(fdr) self.ais.sock.recv(mox.IgnoreArg()).AndRaise(BreakBlocking) mox.Replay(self.ais.sock) next_method = '__next__' if sys.version_info[0] >= 3 else 'next' # part 1 with self.assertRaises(aprslib.exceptions.ConnectionDrop): getattr(self.ais._socket_readlines(), next_method)() # part 2 with self.assertRaises(aprslib.exceptions.ConnectionDrop): getattr(self.ais._socket_readlines(), next_method)() # part 3 with self.assertRaises(StopIteration): getattr(self.ais._socket_readlines(), next_method)() # part 4 for line in self.ais._socket_readlines(): self.assertEqual(line, b'a') # part 5 with self.assertRaises(BreakBlocking): for line in self.ais._socket_readlines(blocking=True): self.assertEqual(line, b'b') mox.Verify(self.ais.sock)
def test_send_login(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "close") self.m.StubOutWithMock(self.ais, "_sendall") # part 1 - raises self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"invalidreply") self.ais.close() # part 2 - raises (empty callsign) self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp verified, xx") self.ais.close() # part 3 - raises (callsign doesn't match self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp NOMATCH verified, xx") self.ais.close() # part 4 - raises (unverified, but pass is not -1) self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx") self.ais.close() # part 5 - normal, receive only self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx") # part 6 - normal, correct pass self.ais._sendall(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL verified, xx") mox.Replay(self.ais.sock) self.m.ReplayAll() # part 1 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 2 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 3 self.ais.set_login("CALL", "-1") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 4 self.ais.set_login("CALL", "99999") self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login) # part 5 self.ais.set_login("CALL", "-1") self.ais._send_login() # part 6 self.ais.set_login("CALL", "99999") self.ais._send_login() mox.Verify(self.ais.sock) self.m.VerifyAll()
def test_close(self): self.ais._connected = True self.ais.sock = mox.MockAnything() self.ais.sock.close() mox.Replay(self.ais.sock) self.ais.close() mox.Verify(self.ais.sock) self.assertFalse(self.ais._connected) self.assertEqual(self.ais.buf, b'')
def test_filter(self): testFilter = 'x/CALLSIGN' self.ais._connected = True self.ais.sock = mox.MockAnything() self.ais.sock.sendall(b'#filter ' + testFilter.encode('ascii') + b'\r\n') mox.Replay(self.ais.sock) self.ais.set_filter(testFilter) self.assertEqual(self.ais.filter, testFilter) mox.Verify(self.ais.sock)
def test_connect(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "_open_socket") self.m.StubOutWithMock(self.ais, "close") # part 1 - socket creation errors self.ais._open_socket().AndRaise(socket.timeout("timed out")) self.ais.close() self.ais._open_socket().AndRaise(socket.error('any')) self.ais.close() # part 2 - invalid banner from server self.ais._open_socket() self.ais.sock.getpeername().AndReturn((1, 2)) self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"junk") self.ais.close() # part 3 - everything going well self.ais._open_socket() self.ais.sock.getpeername().AndReturn((1, 2)) self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()) self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# server banner") mox.Replay(self.ais.sock) self.m.ReplayAll() # part 1 self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) # part 2 self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect) self.assertFalse(self.ais._connected) # part 3 self.ais._connect() self.assertTrue(self.ais._connected) mox.Verify(self.ais.sock) self.m.VerifyAll()
def test_delete_volume_thinlvm_snap(self, _mock_create_export): self.configuration.volume_clear = 'zero' self.configuration.volume_clear_size = 0 self.configuration.lvm_type = 'thin' self.configuration.iscsi_helper = 'tgtadm' lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration, vg_obj=mox.MockAnything(), db=db) # Ensures that copy_volume is not called for ThinLVM self.mox.StubOutWithMock(volutils, 'copy_volume') self.mox.StubOutWithMock(volutils, 'clear_volume') self.mox.StubOutWithMock(lvm_driver, '_execute') self.mox.ReplayAll() uuid = '00000000-0000-0000-0000-c3aa7ee01536' fake_snapshot = {'name': 'volume-' + uuid, 'id': uuid, 'size': 123} lvm_driver._delete_volume(fake_snapshot, is_snapshot=True)
def test_sendall_socketerror(self): self.ais.sock = mox.MockAnything() self.m.StubOutWithMock(self.ais, "close") # setup self.ais.sock.setblocking(mox.IgnoreArg()) self.ais.sock.settimeout(mox.IgnoreArg()) self.ais.sock.sendall(mox.IgnoreArg()).AndRaise(socket.error) self.ais.close() mox.Replay(self.ais.sock) self.m.ReplayAll() # test self.ais._connected = True with self.assertRaises(aprslib.ConnectionError): self.ais.sendall("test") # verify mox.Verify(self.ais.sock) self.m.VerifyAll()
def setUp(self): super(TestWSGIService, self).setUp() self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything())
def setUp(self): super(TestLauncher, self).setUp() self.stubs.Set(wsgi.Loader, "load_app", mox.MockAnything()) self.service = service.WSGIService("test_service")