self, method, url, body, headers): # When the user is the common one for all the tests ('son, 'goku') # it creates this basic auth and we return the datacenters value if headers['Authorization'] == 'Basic Z286dHJ1bmtz': expected_response = self.fixtures.load("not_found_error.xml") return (httplib.NOT_FOUND, expected_response, {}, '') elif headers['Authorization'] != 'Basic c29uOmdvaGFu': return (httplib.OK, self.fixtures.load('ent_1_dcreps.xml'), {}, '') else: # son:gohan user: forbidden error expected_response = self.fixtures.load("privilege_errors.html") return (httplib.FORBIDDEN, expected_response, {}, '') def _api_admin_enterprises_1_datacenterrepositories_2( self, method, url, body, headers): return (httplib.OK, self.fixtures.load('ent_1_dcrep_2.xml'), {}, '') def _api_admin_enterprises_1_datacenterrepositories_2_virtualmachinetemplates( self, method, url, body, headers): return (httplib.OK, self.fixtures.load('ent_1_dcrep_2_templates.xml'), {}, '') def _api_admin_enterprises_1_datacenterrepositories_2_virtualmachinetemplates_11( self, method, url, body, headers): return (httplib.OK, self.fixtures.load('ent_1_dcrep_2_template_11.xml'), {}, '') if __name__ == '__main__': sys.exit(unittest.main())
side_effect=ssl.SSLError(TRANSIENT_SSL_ERROR)) self.assertRaises(ssl.SSLError, conn.request, '/') self.assertGreater(connection.request.call_count, 1) def test_rate_limit_error(self): sock = Mock() con = Connection() try: with patch('libcloud.utils.py3.httplib.HTTPResponse.getheaders', MagicMock(return_value=CONFLICT_RESPONSE_STATUS)): with patch( 'libcloud.utils.py3.httplib.HTTPResponse._read_status', MagicMock(return_value=SIMPLE_RESPONSE_STATUS)): with tempfile.TemporaryFile(mode='w+b') as f: f.write('HTTP/1.1 429 CONFLICT\n'.encode()) f.flush() sock.makefile = Mock(return_value=f) mock_obj = httplib.HTTPResponse(sock) mock_obj.begin() Response(mock_obj, con) except RateLimitReachedError: pass except Exception: self.fail('Failed to raise Rate Limit exception') if __name__ == '__main__': unittest.main()
container = self.driver.get_container("1i31") started = container.start() self.assertEqual(started.id, "1i31") self.assertEqual(started.name, "newcontainer") self.assertEqual(started.state, "pending") self.assertEqual(started.extra['state'], "starting") def test_stop_container(self): container = self.driver.get_container("1i31") stopped = container.stop() self.assertEqual(stopped.id, "1i31") self.assertEqual(stopped.name, "newcontainer") self.assertEqual(stopped.state, "pending") self.assertEqual(stopped.extra['state'], "stopping") def test_ex_search_containers(self): containers = self.driver.ex_search_containers({"state": "running"}) self.assertEqual(len(containers), 1) def test_destroy_container(self): container = self.driver.get_container("1i31") destroyed = container.destroy() self.assertEqual(destroyed.id, "1i31") self.assertEqual(destroyed.name, "newcontainer") self.assertEqual(destroyed.state, "pending") self.assertEqual(destroyed.extra['state'], "stopping") if __name__ == '__main__': sys.exit(unittest.main())
raise socket.gaierror('') def test_retry_connection(self): con = Connection(timeout=1, retry_delay=0.1) con.connection = Mock() connect_method = 'libcloud.common.base.Connection.request' with patch(connect_method) as mock_connect: try: mock_connect.side_effect = socket.gaierror('') con.request('/') except socket.gaierror: pass except Exception: self.fail('Failed to raise socket exception') def test_retry_connection_ssl_error(self): conn = Connection(timeout=1, retry_delay=0.1) with patch.object(conn, 'connect', Mock()): with patch.object(conn, 'connection') as connection: connection.request = MagicMock( __name__='request', side_effect=ssl.SSLError(TRANSIENT_SSL_ERROR)) self.assertRaises(ssl.SSLError, conn.request, '/') self.assertGreater(connection.request.call_count, 1) if __name__ == '__main__': unittest.main()