mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Some rework stratosphere ssp plugin
* add missing command entry in commands.properties * migrate httpclient 3.x to 4.x * fix the broken SspClient * add webapp session checking in mock ssp server
This commit is contained in:
parent
b32b49e853
commit
cb048f4108
@ -593,6 +593,11 @@ addBigSwitchVnsDevice=1
|
||||
deleteBigSwitchVnsDevice=1
|
||||
listBigSwitchVnsDevices=1
|
||||
|
||||
#### stratosphere ssp commands
|
||||
|
||||
addStratosphereSsp=1
|
||||
deleteStratoshereSsp=1
|
||||
|
||||
#### host simulator commands
|
||||
|
||||
configureSimulator=1
|
||||
|
||||
@ -17,26 +17,33 @@
|
||||
package org.apache.cloudstack.network.element;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpConnectionManager;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||
import org.apache.commons.httpclient.URIException;
|
||||
import org.apache.commons.httpclient.cookie.CookiePolicy;
|
||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
||||
import org.apache.commons.httpclient.params.HttpClientParams;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.client.params.CookiePolicy;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.params.CoreConnectionPNames;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
@ -44,117 +51,75 @@ import com.google.gson.annotations.SerializedName;
|
||||
*/
|
||||
public class SspClient {
|
||||
private static final Logger s_logger = Logger.getLogger(SspClient.class);
|
||||
private static final HttpConnectionManager s_httpclient_manager = new MultiThreadedHttpConnectionManager();
|
||||
private static final HttpClientParams s_httpclient_params = new HttpClientParams();
|
||||
private static final HttpClient s_client = new DefaultHttpClient(
|
||||
new PoolingClientConnectionManager());
|
||||
static {
|
||||
s_httpclient_params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
|
||||
s_client.getParams()
|
||||
.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY)
|
||||
.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
|
||||
}
|
||||
|
||||
private final String apiUrl;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
protected HttpClient client;
|
||||
protected PostMethod postMethod;
|
||||
protected DeleteMethod deleteMethod;
|
||||
protected PutMethod putMethod;
|
||||
|
||||
public SspClient(String apiUrl, String username, String password) {
|
||||
super();
|
||||
this.apiUrl = apiUrl;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
client = new HttpClient(s_httpclient_params, s_httpclient_manager);
|
||||
postMethod = new PostMethod(apiUrl);
|
||||
deleteMethod = new DeleteMethod(apiUrl);
|
||||
putMethod = new PutMethod(apiUrl);
|
||||
}
|
||||
|
||||
protected HttpClient getHttpClient() { // for mock test
|
||||
return s_client;
|
||||
}
|
||||
|
||||
private HttpResponse innerExecuteMethod(HttpRequestBase req, String path) {
|
||||
try {
|
||||
URI base = new URI(apiUrl);
|
||||
req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(),
|
||||
base.getPort(), path, null, null));
|
||||
} catch (URISyntaxException e) {
|
||||
s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
|
||||
return null;
|
||||
}
|
||||
HttpResponse res = null;
|
||||
try {
|
||||
res = getHttpClient().execute(req);
|
||||
s_logger.info("ssp api call:" + req + " status=" + res.getStatusLine());
|
||||
} catch (IOException e) {
|
||||
s_logger.error("ssp api call failed: " + req, e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private HttpResponse executeMethod(HttpRequestBase req, String path) {
|
||||
HttpResponse res = innerExecuteMethod(req, path);
|
||||
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
|
||||
req.reset();
|
||||
res = innerExecuteMethod(req, path);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public boolean login() {
|
||||
PostMethod method = postMethod;
|
||||
method.setPath("/ws.v1/login"); // NOTE: /ws.v1/login is correct
|
||||
method.addParameter("username", username);
|
||||
method.addParameter("password", password);
|
||||
HttpPost method = new HttpPost();
|
||||
try {
|
||||
method.setEntity(new UrlEncodedFormEntity(Arrays.asList(
|
||||
new BasicNameValuePair("username", username),
|
||||
new BasicNameValuePair("password", password))));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
s_logger.error("invalid username or password", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
client.executeMethod(method);
|
||||
} catch (HttpException e) {
|
||||
s_logger.info("Login " + username + " to " + apiUrl + " failed", e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
s_logger.info("Login " + username + " to " + apiUrl + " failed", e);
|
||||
return false;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
}
|
||||
String apiCallPath = null;
|
||||
try {
|
||||
apiCallPath = method.getName() + " " + method.getURI().toString();
|
||||
} catch (URIException e) {
|
||||
s_logger.error("method getURI failed", e);
|
||||
}
|
||||
s_logger.info("ssp api call:" + apiCallPath + " user=" + username + " status=" + method.getStatusLine());
|
||||
if (method.getStatusCode() == HttpStatus.SC_OK) {
|
||||
HttpResponse res = this.innerExecuteMethod(method, "/ws.v1/login");
|
||||
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String executeMethod(HttpMethod method) {
|
||||
String apiCallPath = null;
|
||||
try {
|
||||
apiCallPath = method.getName() + " " + method.getURI().toString();
|
||||
} catch (URIException e) {
|
||||
s_logger.error("method getURI failed", e);
|
||||
}
|
||||
|
||||
String response = null;
|
||||
try {
|
||||
client.executeMethod(method);
|
||||
response = method.getResponseBodyAsString();
|
||||
} catch (HttpException e) {
|
||||
s_logger.error("ssp api call failed " + apiCallPath, e);
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
s_logger.error("ssp api call failed " + apiCallPath, e);
|
||||
return null;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
}
|
||||
|
||||
if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
|
||||
if (!login()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
client.executeMethod(method);
|
||||
response = method.getResponseBodyAsString();
|
||||
} catch (HttpException e) {
|
||||
s_logger.error("ssp api call failed " + apiCallPath, e);
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
s_logger.error("ssp api call failed " + apiCallPath, e);
|
||||
return null;
|
||||
} finally {
|
||||
method.releaseConnection();
|
||||
}
|
||||
}
|
||||
s_logger.info("ssp api call:" + apiCallPath + " user=" + username + " status=" + method.getStatusLine());
|
||||
if (method instanceof EntityEnclosingMethod) {
|
||||
EntityEnclosingMethod emethod = (EntityEnclosingMethod)method;
|
||||
RequestEntity reqEntity = emethod.getRequestEntity();
|
||||
if (reqEntity instanceof StringRequestEntity) {
|
||||
StringRequestEntity strReqEntity = (StringRequestEntity)reqEntity;
|
||||
s_logger.debug("ssp api request body:" + strReqEntity.getContent());
|
||||
} else {
|
||||
s_logger.debug("ssp api request body:" + emethod.getRequestEntity());
|
||||
}
|
||||
}
|
||||
s_logger.debug("ssp api response body:" + response);
|
||||
return response;
|
||||
}
|
||||
|
||||
public class TenantNetwork {
|
||||
public String uuid;
|
||||
public String name;
|
||||
@ -167,30 +132,31 @@ public class SspClient {
|
||||
req.name = networkName;
|
||||
req.tenantUuid = tenantUuid;
|
||||
|
||||
PostMethod method = postMethod;
|
||||
method.setPath("/ssp.v1/tenant-networks");
|
||||
StringRequestEntity entity = null;
|
||||
try {
|
||||
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
s_logger.error("failed creating http request body", e);
|
||||
HttpPost method = new HttpPost();
|
||||
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
|
||||
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks");
|
||||
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
|
||||
return null;
|
||||
}
|
||||
method.setRequestEntity(entity);
|
||||
|
||||
String response = executeMethod(method);
|
||||
if (response != null && method.getStatusCode() == HttpStatus.SC_CREATED) {
|
||||
return new Gson().fromJson(response, TenantNetwork.class);
|
||||
try {
|
||||
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
|
||||
TenantNetwork.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (JsonIOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IllegalStateException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean deleteTenantNetwork(String tenantNetworkUuid) {
|
||||
DeleteMethod method = deleteMethod;
|
||||
method.setPath("/ssp.v1/tenant-networks/" + tenantNetworkUuid);
|
||||
|
||||
executeMethod(method);
|
||||
if (method.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
|
||||
HttpDelete method = new HttpDelete();
|
||||
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks/" + tenantNetworkUuid);
|
||||
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -214,30 +180,33 @@ public class SspClient {
|
||||
req.networkUuid = tenantNetworkUuid;
|
||||
req.attachmentType = "NoAttachment";
|
||||
|
||||
PostMethod method = postMethod;
|
||||
method.setPath("/ssp.v1/tenant-ports");
|
||||
StringRequestEntity entity = null;
|
||||
try {
|
||||
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
s_logger.error("failed creating http request body", e);
|
||||
HttpPost method = new HttpPost();
|
||||
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
|
||||
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports");
|
||||
|
||||
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
|
||||
return null;
|
||||
}
|
||||
method.setRequestEntity(entity);
|
||||
|
||||
String response = executeMethod(method);
|
||||
if (response != null && method.getStatusCode() == HttpStatus.SC_CREATED) {
|
||||
return new Gson().fromJson(response, TenantPort.class);
|
||||
try {
|
||||
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
|
||||
TenantPort.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (JsonIOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IllegalStateException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean deleteTenantPort(String tenantPortUuid) {
|
||||
DeleteMethod method = deleteMethod;
|
||||
method.setPath("/ssp.v1/tenant-ports/" + tenantPortUuid);
|
||||
HttpDelete method = new HttpDelete();
|
||||
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + tenantPortUuid);
|
||||
|
||||
executeMethod(method);
|
||||
if (method.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
|
||||
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -252,20 +221,23 @@ public class SspClient {
|
||||
req.attachmentType = "NoAttachment";
|
||||
}
|
||||
|
||||
PutMethod method = putMethod;
|
||||
method.setPath("/ssp.v1/tenant-ports/" + portUuid);
|
||||
StringRequestEntity entity = null;
|
||||
try {
|
||||
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
s_logger.error("failed creating http request body", e);
|
||||
HttpPut method = new HttpPut();
|
||||
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
|
||||
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid);
|
||||
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
||||
return null;
|
||||
}
|
||||
method.setRequestEntity(entity);
|
||||
|
||||
String response = executeMethod(method);
|
||||
if (response != null && method.getStatusCode() == HttpStatus.SC_OK) {
|
||||
return new Gson().fromJson(response, TenantPort.class);
|
||||
try {
|
||||
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
|
||||
TenantPort.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (JsonIOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IllegalStateException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
} catch (IOException e) {
|
||||
s_logger.error("reading response body failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -92,6 +92,7 @@ import com.cloud.vm.dao.NicDao;
|
||||
public class SspElement extends AdapterBase implements ConnectivityProvider, SspManager, SspService, NetworkMigrationResponder {
|
||||
private static final Logger s_logger = Logger.getLogger(SspElement.class);
|
||||
public static final String s_SSP_NAME = "StratosphereSsp";
|
||||
private static final Provider s_ssp_provider = new Provider(s_SSP_NAME, false);
|
||||
|
||||
@Inject
|
||||
NetworkServiceMapDao _ntwkSrvcDao;
|
||||
@ -134,15 +135,7 @@ public class SspElement extends AdapterBase implements ConnectivityProvider, Ssp
|
||||
|
||||
@Override
|
||||
public Provider getProvider() {
|
||||
Provider provider = null;
|
||||
synchronized (s_SSP_NAME) {
|
||||
provider = Provider.getProvider(s_SSP_NAME);
|
||||
if (provider == null) {
|
||||
provider = new Provider(s_SSP_NAME, true);
|
||||
s_logger.debug("registering Network.Provider " + s_SSP_NAME);
|
||||
}
|
||||
}
|
||||
return provider;
|
||||
return s_ssp_provider;
|
||||
}
|
||||
|
||||
private List<SspClient> fetchSspClients(Long physicalNetworkId, Long dataCenterId, boolean enabledOnly) {
|
||||
@ -187,14 +180,10 @@ public class SspElement extends AdapterBase implements ConnectivityProvider, Ssp
|
||||
public boolean isReady(PhysicalNetworkServiceProvider provider) {
|
||||
PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(provider.getPhysicalNetworkId());
|
||||
assert (physicalNetwork != null);
|
||||
if (physicalNetwork != null) {
|
||||
if (fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0) {
|
||||
return true;
|
||||
}
|
||||
s_logger.warn("Ssp api endpoint not found. " + physicalNetwork.toString());
|
||||
} else {
|
||||
s_logger.warn("PhysicalNetwork is NULL.");
|
||||
if (fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0) {
|
||||
return true;
|
||||
}
|
||||
s_logger.warn("Ssp api endpoint not found. " + physicalNetwork.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -17,7 +17,8 @@
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from flask import Flask
|
||||
from flask import Flask,request,make_response
|
||||
from beaker.middleware import SessionMiddleware
|
||||
app = Flask(__name__)
|
||||
|
||||
tenant_networks = []
|
||||
@ -25,50 +26,59 @@ tenant_ports = []
|
||||
|
||||
@app.route("/ws.v1/login", methods=["POST",])
|
||||
def login():
|
||||
response.content_type = "application/json"
|
||||
return ""
|
||||
assert "username" in request.form
|
||||
assert "password" in request.form
|
||||
request.environ["beaker.session"]["login"] = True
|
||||
res = make_response("", 200)
|
||||
res.headers["Content-type"] = "application/json"
|
||||
return res
|
||||
|
||||
@app.route("/ssp.v1/tenant-networks", methods=["POST",])
|
||||
def create_tenant_network():
|
||||
response.content_type = "application/json"
|
||||
response.status = 201
|
||||
if "login" not in request.environ["beaker.session"]:
|
||||
return make_response("", 401)
|
||||
obj = request.json
|
||||
obj["uuid"] = str(uuid.uuid1())
|
||||
tenant_networks.append(obj)
|
||||
return json.dumps(obj)
|
||||
res = make_response(json.dumps(obj), 201)
|
||||
res.headers["Content-type"] = "application/json"
|
||||
return res
|
||||
|
||||
@app.route("/ssp.v1/tenant-networks/<tenant_net_uuid>", methods=["DELETE",])
|
||||
def delete_tenant_network(tenant_net_uuid):
|
||||
if "login" not in request.environ["beaker.session"]:
|
||||
return make_response("", 401)
|
||||
for net in tenant_networks:
|
||||
if net["uuid"] == tenant_net_uuid:
|
||||
tenant_networks.remove(net)
|
||||
response.status = 204
|
||||
return ""
|
||||
response.status = 404
|
||||
return ""
|
||||
return make_response("", 204)
|
||||
return make_response("", 404)
|
||||
|
||||
@app.route("/ssp.v1/tenant-ports", methods=["POST",])
|
||||
def create_tenant_port():
|
||||
response.content_type = "application/json"
|
||||
response.status = 201
|
||||
if "login" not in request.environ["beaker.session"]:
|
||||
return make_response("", 401)
|
||||
obj = request.json
|
||||
obj["uuid"] = str(uuid.uuid1())
|
||||
tenant_ports.append(obj)
|
||||
return json.dumps(obj)
|
||||
res = make_response(json.dumps(obj), 201)
|
||||
res.headers["Content-type"] = "application/json"
|
||||
return res
|
||||
|
||||
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["DELETE",])
|
||||
def delete_tenant_port(tenant_port_uuid):
|
||||
if "login" not in request.environ["beaker.session"]:
|
||||
return make_response("", 401)
|
||||
for port in tenant_ports:
|
||||
if port["uuid"] == tenant_port_uuid:
|
||||
tenant_ports.remove(port)
|
||||
response.status = 204
|
||||
return ""
|
||||
response.status = 404
|
||||
return ""
|
||||
return make_response("", 204)
|
||||
return make_response("", 404)
|
||||
|
||||
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["PUT",])
|
||||
def update_tenant_port(tenant_port_uuid):
|
||||
response.content_type = "application/json"
|
||||
if "login" not in request.environ["beaker.session"]:
|
||||
return make_response("", 401)
|
||||
for port in tenant_ports:
|
||||
if port["uuid"] == tenant_port_uuid:
|
||||
obj = request.json
|
||||
@ -76,10 +86,14 @@ def update_tenant_port(tenant_port_uuid):
|
||||
obj["vlan_id"] = 100
|
||||
tenant_ports.remove(port)
|
||||
tenant_ports.append(obj)
|
||||
response.status = 200
|
||||
return json.dumps(obj)
|
||||
response.status = 404
|
||||
return ""
|
||||
res = make_response(json.dumps(obj), 200)
|
||||
res.headers["Content-type"] = "application/json"
|
||||
return res
|
||||
return make_response("", 404)
|
||||
|
||||
if __name__=="__main__":
|
||||
app.wsgi_app = SessionMiddleware(app.wsgi_app, {
|
||||
"session.auto":True,
|
||||
"session.type":"cookie",
|
||||
"session.validate_key":"hoge"})
|
||||
app.run(host="0.0.0.0", port=9080, debug=True)
|
||||
|
||||
@ -20,46 +20,37 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.URI;
|
||||
import org.apache.commons.httpclient.methods.DeleteMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SspClientTest {
|
||||
HttpClient _client = mock(HttpClient.class);
|
||||
PostMethod _postMethod = mock(PostMethod.class);
|
||||
PutMethod _putMethod = mock(PutMethod.class);
|
||||
DeleteMethod _deleteMethod = mock(DeleteMethod.class);
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
|
||||
String apiUrl = "http://a.example.jp/";
|
||||
String username = "foo";
|
||||
String password = "bar";
|
||||
SspClient sspClient = new SspClient(apiUrl, username, password) {
|
||||
{
|
||||
client = _client;
|
||||
postMethod = _postMethod;
|
||||
putMethod = _putMethod;
|
||||
deleteMethod = _deleteMethod;
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private URI getUri() throws Exception {
|
||||
return new URI(apiUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loginTest() throws Exception {
|
||||
when(_postMethod.getURI()).thenReturn(getUri());
|
||||
when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_OK);
|
||||
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
|
||||
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
|
||||
doReturn(client).when(sspClient).getHttpClient();
|
||||
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
|
||||
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
|
||||
|
||||
assertTrue(sspClient.login());
|
||||
assertTrue(sspClient.login());
|
||||
assertTrue(sspClient.login());
|
||||
@ -69,10 +60,18 @@ public class SspClientTest {
|
||||
public void createNetworkTest() throws Exception {
|
||||
String networkName = "example network 1";
|
||||
String tenant_net_uuid = UUID.randomUUID().toString();
|
||||
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
|
||||
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
|
||||
doReturn(client).when(sspClient).getHttpClient();
|
||||
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
|
||||
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
|
||||
String body = "{\"uuid\":\"" + tenant_net_uuid + "\",\"name\":\"" + networkName
|
||||
+ "\",\"tenant_uuid\":\"" + uuid + "\"}";
|
||||
when(res.getEntity().getContent()).thenReturn(
|
||||
new ByteArrayInputStream(body.getBytes("UTF-8")));
|
||||
|
||||
when(_postMethod.getURI()).thenReturn(getUri());
|
||||
when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
|
||||
when(_postMethod.getResponseBodyAsString()).thenReturn("{\"uuid\":\"" + tenant_net_uuid + "\",\"name\":\"" + networkName + "\",\"tenant_uuid\":\"" + uuid + "\"}");
|
||||
SspClient.TenantNetwork tnet = sspClient.createTenantNetwork(uuid, networkName);
|
||||
assertEquals(tnet.name, networkName);
|
||||
assertEquals(tnet.uuid, tenant_net_uuid);
|
||||
@ -82,9 +81,13 @@ public class SspClientTest {
|
||||
@Test
|
||||
public void deleteNetworkTest() throws Exception {
|
||||
String tenant_net_uuid = UUID.randomUUID().toString();
|
||||
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
|
||||
|
||||
when(_deleteMethod.getURI()).thenReturn(getUri());
|
||||
when(_deleteMethod.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
|
||||
doReturn(client).when(sspClient).getHttpClient();
|
||||
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
|
||||
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
|
||||
|
||||
sspClient.deleteTenantNetwork(tenant_net_uuid);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user