parent
4d0029e879
commit
9f1c248826
@ -0,0 +1,48 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2011 Citrix Systems
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
def get_view_builder(req):
|
||||
base_url = req.application_url
|
||||
return ViewBuilder(base_url)
|
||||
|
||||
|
||||
class ViewBuilder(object):
|
||||
|
||||
def __init__(self, base_url):
|
||||
"""
|
||||
:param base_url: url of the root wsgi application
|
||||
"""
|
||||
self.base_url = base_url
|
||||
|
||||
def build(self, port_data, is_detail=False):
|
||||
"""Generic method used to generate a port entity."""
|
||||
print "PORT-DATA:%s" %port_data
|
||||
if is_detail:
|
||||
port = self._build_detail(port_data)
|
||||
else:
|
||||
port = self._build_simple(port_data)
|
||||
return port
|
||||
|
||||
def _build_simple(self, port_data):
|
||||
"""Return a simple model of a server."""
|
||||
return dict(port=dict(id=port_data['port-id']))
|
||||
|
||||
def _build_detail(self, port_data):
|
||||
"""Return a simple model of a server."""
|
||||
return dict(port=dict(id=port_data['port-id'],
|
||||
state=port_data['port-state']))
|
@ -0,0 +1,98 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2011 Citrix Systems
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import httplib
|
||||
import socket
|
||||
import urllib
|
||||
|
||||
class MiniClient(object):
|
||||
|
||||
"""A base client class - derived from Glance.BaseClient"""
|
||||
|
||||
action_prefix = '/v0.1/tenants/{tenant_id}'
|
||||
|
||||
def __init__(self, host, port, use_ssl):
|
||||
"""
|
||||
Creates a new client to some service.
|
||||
|
||||
:param host: The host where service resides
|
||||
:param port: The port where service resides
|
||||
:param use_ssl: Should we use HTTPS?
|
||||
"""
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.use_ssl = use_ssl
|
||||
self.connection = None
|
||||
|
||||
def get_connection_type(self):
|
||||
"""
|
||||
Returns the proper connection type
|
||||
"""
|
||||
if self.use_ssl:
|
||||
return httplib.HTTPSConnection
|
||||
else:
|
||||
return httplib.HTTPConnection
|
||||
|
||||
def do_request(self, tenant, method, action, body=None,
|
||||
headers=None, params=None):
|
||||
"""
|
||||
Connects to the server and issues a request.
|
||||
Returns the result data, or raises an appropriate exception if
|
||||
HTTP status code is not 2xx
|
||||
|
||||
:param method: HTTP method ("GET", "POST", "PUT", etc...)
|
||||
:param body: string of data to send, or None (default)
|
||||
:param headers: mapping of key/value pairs to add as headers
|
||||
:param params: dictionary of key/value pairs to add to append
|
||||
to action
|
||||
|
||||
"""
|
||||
action = MiniClient.action_prefix + action
|
||||
action = action.replace('{tenant_id}',tenant)
|
||||
if type(params) is dict:
|
||||
action += '?' + urllib.urlencode(params)
|
||||
|
||||
try:
|
||||
connection_type = self.get_connection_type()
|
||||
headers = headers or {}
|
||||
|
||||
# Open connection and send request
|
||||
c = connection_type(self.host, self.port)
|
||||
c.request(method, action, body, headers)
|
||||
res = c.getresponse()
|
||||
status_code = self.get_status_code(res)
|
||||
if status_code in (httplib.OK,
|
||||
httplib.CREATED,
|
||||
httplib.ACCEPTED,
|
||||
httplib.NO_CONTENT):
|
||||
return res
|
||||
else:
|
||||
raise Exception("Server returned error: %s" % res.read())
|
||||
|
||||
except (socket.error, IOError), e:
|
||||
raise Exception("Unable to connect to "
|
||||
"server. Got error: %s" % e)
|
||||
|
||||
def get_status_code(self, response):
|
||||
"""
|
||||
Returns the integer status code from the response, which
|
||||
can be either a Webob.Response (used in testing) or httplib.Response
|
||||
"""
|
||||
if hasattr(response, 'status_int'):
|
||||
return response.status_int
|
||||
else:
|
||||
return response.status
|
@ -0,0 +1,150 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2011 Citrix Systems
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import gettext
|
||||
|
||||
gettext.install('quantum', unicode=1)
|
||||
|
||||
from miniclient import MiniClient
|
||||
from quantum.common.wsgi import Serializer
|
||||
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 9696
|
||||
USE_SSL = False
|
||||
TENANT_ID = 'totore'
|
||||
|
||||
test_network_data = \
|
||||
{'network': {'network-name': 'test' }}
|
||||
|
||||
def print_response(res):
|
||||
content = res.read()
|
||||
print "Status: %s" %res.status
|
||||
print "Content: %s" %content
|
||||
return content
|
||||
|
||||
def test_list_networks_and_ports(format = 'xml'):
|
||||
client = MiniClient(HOST, PORT, USE_SSL)
|
||||
print "TEST LIST NETWORKS AND PORTS -- FORMAT:%s" %format
|
||||
print "----------------------------"
|
||||
print "--> Step 1 - List All Networks"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks." + format)
|
||||
print_response(res)
|
||||
print "--> Step 2 - Details for Network 001"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001." + format)
|
||||
print_response(res)
|
||||
print "--> Step 3 - Ports for Network 001"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001/ports." + format)
|
||||
print_response(res)
|
||||
print "--> Step 4 - Details for Port 1"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001/ports/1." + format)
|
||||
print_response(res)
|
||||
print "COMPLETED"
|
||||
print "----------------------------"
|
||||
|
||||
def test_create_network(format = 'xml'):
|
||||
client = MiniClient(HOST, PORT, USE_SSL)
|
||||
print "TEST CREATE NETWORK -- FORMAT:%s" %format
|
||||
print "----------------------------"
|
||||
print "--> Step 1 - Create Network"
|
||||
content_type = "application/" + format
|
||||
body = Serializer().serialize(test_network_data, content_type)
|
||||
res = client.do_request(TENANT_ID,'POST', "/networks." + format, body=body)
|
||||
print_response(res)
|
||||
print "--> Step 2 - List All Networks"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks." + format)
|
||||
print_response(res)
|
||||
print "COMPLETED"
|
||||
print "----------------------------"
|
||||
|
||||
def test_rename_network(format = 'xml'):
|
||||
client = MiniClient(HOST, PORT, USE_SSL)
|
||||
content_type = "application/" + format
|
||||
print "TEST RENAME NETWORK -- FORMAT:%s" %format
|
||||
print "----------------------------"
|
||||
print "--> Step 1 - Retrieve network"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001." + format)
|
||||
print_response(res)
|
||||
print "--> Step 2 - Rename network to 'test_renamed'"
|
||||
test_network_data['network']['network-name'] = 'test_renamed'
|
||||
body = Serializer().serialize(test_network_data, content_type)
|
||||
res = client.do_request(TENANT_ID,'PUT', "/networks/001." + format, body=body)
|
||||
print_response(res)
|
||||
print "--> Step 2 - Retrieve network (again)"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001." + format)
|
||||
print_response(res)
|
||||
print "COMPLETED"
|
||||
print "----------------------------"
|
||||
|
||||
def test_delete_network(format = 'xml'):
|
||||
client = MiniClient(HOST, PORT, USE_SSL)
|
||||
content_type = "application/" + format
|
||||
print "TEST DELETE NETWORK -- FORMAT:%s" %format
|
||||
print "----------------------------"
|
||||
print "--> Step 1 - List All Networks"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks." + format)
|
||||
content = print_response(res)
|
||||
network_data = Serializer().deserialize(content, content_type)
|
||||
print network_data
|
||||
net_id = network_data['networks'][0]['id']
|
||||
print "--> Step 2 - Delete network %s" %net_id
|
||||
res = client.do_request(TENANT_ID,'DELETE',
|
||||
"/networks/" + net_id + "." + format)
|
||||
print_response(res)
|
||||
print "--> Step 3 - List All Networks (Again)"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks." + format)
|
||||
print_response(res)
|
||||
print "COMPLETED"
|
||||
print "----------------------------"
|
||||
|
||||
|
||||
def test_create_port(format = 'xml'):
|
||||
client = MiniClient(HOST, PORT, USE_SSL)
|
||||
print "TEST CREATE PORT -- FORMAT:%s" %format
|
||||
print "----------------------------"
|
||||
print "--> Step 1 - List Ports for network 001"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001/ports." + format)
|
||||
print_response(res)
|
||||
print "--> Step 2 - Create Port for network 001"
|
||||
res = client.do_request(TENANT_ID,'POST', "/networks/001/ports." + format)
|
||||
print_response(res)
|
||||
print "--> Step 3 - List Ports for network 001 (again)"
|
||||
res = client.do_request(TENANT_ID,'GET', "/networks/001/ports." + format)
|
||||
print_response(res)
|
||||
print "COMPLETED"
|
||||
print "----------------------------"
|
||||
|
||||
|
||||
def main():
|
||||
test_list_networks_and_ports('xml')
|
||||
test_list_networks_and_ports('json')
|
||||
test_create_network('xml')
|
||||
test_create_network('json')
|
||||
test_rename_network('xml')
|
||||
test_rename_network('json')
|
||||
# NOTE: XML deserializer does not work properly
|
||||
# disabling XML test - this is NOT a server-side issue
|
||||
#test_delete_network('xml')
|
||||
test_delete_network('json')
|
||||
test_create_port('xml')
|
||||
test_create_port('json')
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# Standard boilerplate to call the main() function.
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in new issue