add unit test case for ecsmetadata and fix other ut

This commit is contained in:
zhangzifa
2019-01-14 18:17:15 +08:00
committed by Jackson Tian
parent 352be1dd14
commit fc29617d7d
11 changed files with 130 additions and 149 deletions

View File

@@ -35,7 +35,7 @@ namespace AlibabaCloud
typedef std::future<CommonResponseOutcome> CommonResponseOutcomeCallable; typedef std::future<CommonResponseOutcome> CommonResponseOutcomeCallable;
typedef std::function<void(const CommonClient*, const CommonRequest&, const CommonResponseOutcome&, const std::shared_ptr<const AsyncCallerContext>&)> CommonResponseAsyncHandler; typedef std::function<void(const CommonClient*, const CommonRequest&, const CommonResponseOutcome&, const std::shared_ptr<const AsyncCallerContext>&)> CommonResponseAsyncHandler;
typedef Outcome<Error, std::string> JsonOutcome; typedef Outcome<Error, std::string> JsonOutcome;
CommonClient(const Credentials &credentials, const ClientConfiguration &configuration); CommonClient(const Credentials &credentials, const ClientConfiguration &configuration);
CommonClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration); CommonClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration);
CommonClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration); CommonClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration);
@@ -51,7 +51,7 @@ namespace AlibabaCloud
HttpRequest buildRoaHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const; HttpRequest buildRoaHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const;
HttpRequest buildRpcHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const; HttpRequest buildRpcHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const;
JsonOutcome makeRequest(const std::string &endpoint, const CommonRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; JsonOutcome makeRequest(const std::string &endpoint, const CommonRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const;
using CoreClient::asyncExecute;
private: private:
std::string canonicalizedQuery(const std::map <std::string, std::string> &params)const; std::string canonicalizedQuery(const std::map <std::string, std::string> &params)const;
std::string canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const; std::string canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const;

View File

@@ -1,12 +1,12 @@
/* /*
* Copyright 2009-2017 Alibaba Cloud All rights reserved. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

View File

@@ -1,12 +1,12 @@
/* /*
* Copyright 2009-2017 Alibaba Cloud All rights reserved. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

View File

@@ -22,13 +22,6 @@
using namespace AlibabaCloud; using namespace AlibabaCloud;
namespace
{
const int DEFAULT_TIMEOUT_IN_MILLISECONDS = 5000;
const char* const METADATA_SERVICE_HOST = "100.100.100.200";
const char* const URL_IN_ECS_METADATA = "/latest/meta-data/ram/security-credentials/";
}
EcsMetadataFetcher::EcsMetadataFetcher() EcsMetadataFetcher::EcsMetadataFetcher()
{ {
} }
@@ -47,13 +40,13 @@ void EcsMetadataFetcher::setRoleName(const std::string & roleName)
roleName_ = roleName; roleName_ = roleName;
} }
std::string EcsMetadataFetcher::getMetadata() std::string EcsMetadataFetcher::getMetadata(const std::string host, const std::string in_path)
{ {
std::stringstream path; std::stringstream path;
path << URL_IN_ECS_METADATA << roleName_; path << in_path << roleName_;
Url credentialUrl; Url credentialUrl;
credentialUrl.setHost(METADATA_SERVICE_HOST); credentialUrl.setHost(host);
credentialUrl.setPath(path.str()); credentialUrl.setPath(path.str());
auto client = std::make_shared<CurlHttpClient>(); auto client = std::make_shared<CurlHttpClient>();

View File

@@ -19,6 +19,12 @@
#include <string> #include <string>
namespace {
const int DEFAULT_TIMEOUT_IN_MILLISECONDS = 5000;
const char* const METADATA_SERVICE_HOST = "100.100.100.200";
const char* const URL_IN_ECS_METADATA = "/latest/meta-data/ram/security-credentials/";
}
namespace AlibabaCloud namespace AlibabaCloud
{ {
class EcsMetadataFetcher class EcsMetadataFetcher
@@ -29,8 +35,7 @@ namespace AlibabaCloud
std::string roleName()const; std::string roleName()const;
void setRoleName(const std::string & roleName); void setRoleName(const std::string & roleName);
std::string getMetadata(const std::string host = METADATA_SERVICE_HOST, const std::string path = URL_IN_ECS_METADATA);
std::string getMetadata();
private: private:
std::string roleName_; std::string roleName_;
}; };

View File

@@ -44,6 +44,7 @@ add_executable(core_ut
commonresponse_ut.cc commonresponse_ut.cc
credentials_ut.cc credentials_ut.cc
curlhttpclient_ut.cc curlhttpclient_ut.cc
ecsmetadatafetcher_ut.cc
endpointprovider_ut.cc endpointprovider_ut.cc
error_ut.cc error_ut.cc
executor_ut.cc executor_ut.cc

View File

@@ -4,11 +4,13 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "../../core/src/Utils.h" #include "../../core/src/Utils.h"
#include "alibabacloud/core/Config.h" #include "alibabacloud/core/Config.h"
#include "alibabacloud/core/AlibabaCloud.h"
#include "alibabacloud/core/CommonClient.h" #include "alibabacloud/core/CommonClient.h"
#include "alibabacloud/core/CommonResponse.h" #include "alibabacloud/core/CommonResponse.h"
#include "alibabacloud/core/CommonRequest.h" #include "alibabacloud/core/CommonRequest.h"
#include "alibabacloud/core/SimpleCredentialsProvider.h" #include "alibabacloud/core/SimpleCredentialsProvider.h"
using namespace std; using namespace std;
using namespace AlibabaCloud; using namespace AlibabaCloud;
@@ -18,23 +20,23 @@ namespace AlibabaCloud {
TestCommonClient(const Credentials &credentials, const ClientConfiguration &configuration): TestCommonClient(const Credentials &credentials, const ClientConfiguration &configuration):
CommonClient(credentials, configuration) CommonClient(credentials, configuration)
{} {}
HttpRequest buildHttp(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) { using CommonClient::buildHttpRequest;
return CommonClient::buildHttpRequest(endpoint, msg, method); using CommonClient::buildRoaHttpRequest;
} using CommonClient::buildRpcHttpRequest;
HttpRequest buildRoa(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) { using CommonClient::makeRequest;
return CommonClient::buildRoaHttpRequest(endpoint, msg, method); using CommonClient::asyncExecute;
}
HttpRequest buildRpc(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::buildRpcHttpRequest(endpoint, msg, method);
}
JsonOutcome buildReq(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::makeRequest(endpoint, msg, method);
}
}; };
} }
static int nbr = 0;
static void task() {
nbr |= 0x01;
}
TEST(CommonClient, basic) { TEST(CommonClient, basic) {
InitializeSdk();
const ClientConfiguration cfg; const ClientConfiguration cfg;
std::string key = "accessKeyId"; std::string key = "accessKeyId";
@@ -50,7 +52,18 @@ TEST(CommonClient, basic) {
CommonRequest cr; CommonRequest cr;
cr.setContent("test-content", 12); cr.setContent("test-content", 12);
CommonClient::CommonResponseOutcome out = cc1.commonResponse(cr); CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr);
EXPECT_TRUE(out1.error().errorCode() == "NetworkError");
EXPECT_TRUE(out1.result().payload() == "");
CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
EXPECT_TRUE(out2.result().payload() == "");
CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
EXPECT_TRUE(out3.result().payload() == "");
/* /*
https://cn-hangzhou/? https://cn-hangzhou/?
AccessKeyId=accessKeyId AccessKeyId=accessKeyId
@@ -66,7 +79,7 @@ https://cn-hangzhou/?
*/ */
TestCommonClient* client = new TestCommonClient(credentials, cfg); TestCommonClient* client = new TestCommonClient(credentials, cfg);
HttpRequest r = client->buildHttp("cn-hangzhou", cr, HttpRequest::Method::Post); HttpRequest r = client->buildHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
EXPECT_TRUE(r.method() == HttpRequest::Method::Post); EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
EXPECT_TRUE(r.url().scheme() == "https"); EXPECT_TRUE(r.url().scheme() == "https");
EXPECT_TRUE(r.url().userName() == ""); EXPECT_TRUE(r.url().userName() == "");
@@ -86,11 +99,10 @@ https://cn-hangzhou/?
EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos); EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
EXPECT_TRUE(r.url().query().find("Version=") != string::npos); EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
cr.setQueryParameter("query_k1", "query_v1"); cr.setQueryParameter("query_k1", "query_v1");
cr.setHeaderParameter("header_k1", "header_v1"); cr.setHeaderParameter("header_k1", "header_v1");
HttpRequest rr = client->buildRoa("cn-shanghai", cr, HttpRequest::Method::Get); HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
EXPECT_TRUE(rr.method() == HttpRequest::Method::Get); EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
EXPECT_TRUE(rr.header("Accept") == "application/json"); EXPECT_TRUE(rr.header("Accept") == "application/json");
EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1"); EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1");
@@ -109,7 +121,14 @@ https://cn-hangzhou/?
// acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc= // acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos); EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
EXPECT_TRUE(rr.header("unknown-header") == ""); EXPECT_TRUE(rr.header("unknown-header") == "");
HttpRequest rrr = client->buildRpc("cn-hangzhou", cr, HttpRequest::Method::Post); HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
cout << "" <<endl; EXPECT_TRUE(client->serviceName() == "Common");
std::function<void()> func(task);
Runnable* rf = new Runnable(func);
EXPECT_TRUE(nbr == 0);
client->asyncExecute(rf);
usleep(10000);
EXPECT_TRUE(nbr == 1);
ShutdownSdk();
} }

View File

@@ -2,12 +2,12 @@
#include <stdio.h> #include <stdio.h>
#include "utils.h" #include "utils.h"
#include "../include/gtest/gtest.h" #include "gtest/gtest.h"
#include "../include/gmock/gmock.h" #include "gmock/gmock.h"
#include "../../core/include/alibabacloud/core/Url.h" #include "alibabacloud/core/Url.h"
#include "../../core/include/alibabacloud/core/HttpRequest.h" #include "alibabacloud/core/HttpRequest.h"
#include "../../core/include/alibabacloud/core/HttpClient.h" #include "alibabacloud/core/HttpClient.h"
#include "../../core/src/CurlHttpClient.h" #include "../src/CurlHttpClient.h"
using namespace std; using namespace std;
using ::testing::Return; using ::testing::Return;

View File

@@ -0,0 +1,34 @@
#include <iostream>
#include <stdio.h>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "alibabacloud/core/Url.h"
#include "alibabacloud/core/HttpRequest.h"
#include "alibabacloud/core/HttpClient.h"
#include "../src/EcsMetadataFetcher.h"
using namespace std;
using namespace AlibabaCloud;
namespace {
TEST(EcsMetadataFetcher, basic) {
EcsMetadataFetcher fetcher;
fetcher.setRoleName("test-role-name");
EXPECT_TRUE(fetcher.roleName() == "test-role-name");
}
TEST(EcsMetadataFetcher, mock) {
HttpRequest request;
string body = "1234567";
HttpResponse response(request);
response.setStatusCode(200);
response.setBody(body.c_str(), body.size());
EcsMetadataFetcher fetcher;
string out = fetcher.getMetadata("a", "b");
EXPECT_TRUE(out == "NetworkError");
out = fetcher.getMetadata("127.0.0.1");
}
}

View File

@@ -40,17 +40,4 @@ TEST(HttpMessage, perRequest) {
EXPECT_TRUE(req.bodySize() == body.size()); EXPECT_TRUE(req.bodySize() == body.size());
EXPECT_TRUE(req.hasBody()); EXPECT_TRUE(req.hasBody());
EXPECT_TRUE(req.body() != nullptr); EXPECT_TRUE(req.body() != nullptr);
} }
class TestHttpMessage: public HttpMessage {
public:
TestHttpMessage(): HttpMessage(){}
};
TEST(HttpMessage, A) {
TestHttpMessage tm;
TestHttpMessage ttm(tm);
}

View File

@@ -4,6 +4,7 @@
#include "alibabacloud/core/SimpleCredentialsProvider.h" #include "alibabacloud/core/SimpleCredentialsProvider.h"
#include "alibabacloud/core/RoaServiceClient.h" #include "alibabacloud/core/RoaServiceClient.h"
#include "alibabacloud/core/HttpMessage.h" #include "alibabacloud/core/HttpMessage.h"
#include "alibabacloud/core/HttpRequest.h"
using namespace std; using namespace std;
using namespace AlibabaCloud; using namespace AlibabaCloud;
@@ -25,58 +26,26 @@ namespace AlibabaCloud {
HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const { HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const {
return RoaServiceClient::buildHttpRequest(endpoint, msg, method); return RoaServiceClient::buildHttpRequest(endpoint, msg, method);
} }
}; };
class TestRoaServiceRequest: public ServiceRequest { class TestRoaServiceRequest: public ServiceRequest {
public: public:
TestRoaServiceRequest(const std::string &product, const std::string &version): TestRoaServiceRequest(const std::string &product, const std::string &version):
ServiceRequest(product, version){} ServiceRequest(product, version){}
using ServiceRequest::addParameter;
void addParameter(const ParameterNameType &name, const ParameterValueType &value) { using ServiceRequest::parameter;
addParameter(name, value); using ServiceRequest::removeParameter;
} using ServiceRequest::setContent;
ParameterValueType parameter(const ParameterNameType &name)const { using ServiceRequest::setParameter;
return parameter(name); using ServiceRequest::setParameters;
} using ServiceRequest::setResourcePath;
void removeParameter(const ParameterNameType &name) { using ServiceRequest::setProduct;
removeParameter(name); using ServiceRequest::setVersion;
} };
void setContent(const char *data, size_t size) {
setContent(data, size);
}
void setParameter(const ParameterNameType &name, const ParameterValueType &value) {
setParameter(name, value);
}
void setParameters(const ParameterCollection &params) {
setParameters(params);
}
void setResourcePath(const std::string &path) {
setResourcePath(path);
}
void setProduct(const std::string &product) {
setProduct(product);
}
void setVersion(const std::string &version) {
setVersion(version);
}
};
} }
TEST(RoaServiceClient, basic) {
TEST(RoaServiceClient, b0) {
const ClientConfiguration config; const ClientConfiguration config;
std::string key = "accessKeyId"; std::string key = "accessKeyId";
@@ -85,65 +54,38 @@ TEST(RoaServiceClient, b0) {
const Credentials credentials(key, secret, token); const Credentials credentials(key, secret, token);
// const CommonClient cc1(credentials, cfg);
// CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
TestRoaClient client("ecs", std::make_shared<SimpleCredentialsProvider>(credentials), config); TestRoaClient client("ecs", std::make_shared<SimpleCredentialsProvider>(credentials), config);
const string product = "ECS"; const string product = "ECS";
const string version = "1.0"; const string version = "1.0";
RoaServiceRequest roa_req(product, version); RoaServiceRequest roa_req(product, version);
// roa_req.setParameter("key1", "value1");
// roa_req.setQueryParameter("query_k1", "query_v1");
// string content = "test-content";
// roa_req.setContent(content.c_str(), content.length());
client.makeRequest("cn-shanghai", roa_req, HttpRequest::Method::Get); RoaServiceClient::JsonOutcome out = client.makeRequest("cn-shanghai", roa_req, HttpRequest::Method::Get);
EXPECT_TRUE(out.error().errorCode() == "NetworkError");
RoaServiceRequest req(product, version); RoaServiceRequest req(product, version);
req.setParameter("a", "b"); req.setParameter("a", "b");
req.setContent("123456789", 9); req.setContent("123456789", 9);
client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get); HttpRequest http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
EXPECT_TRUE(http_req.url().toString() == "https://cn-shanghai/?a=b");
EXPECT_TRUE(http_req.header("Accept") == "application/json");
EXPECT_TRUE(http_req.header("Content-Length") == "9");
EXPECT_TRUE(http_req.header("Content-Type") == "application/octet-stream");
EXPECT_TRUE(http_req.header("Content-MD5") == "JfnnlDI7RTiF9RgfG2JNCw==");
const string date = "Fri, 11 Jan 2019 14:08:39 GMT";
EXPECT_TRUE(http_req.header("Date").length() == date.length());
EXPECT_TRUE(http_req.header("Host") == "cn-shanghai");
EXPECT_TRUE(http_req.header("x-sdk-cient") == "");
EXPECT_TRUE(http_req.header("x-acs-region-id") == "cn-hangzhou");
EXPECT_TRUE(http_req.header("x-acs-security-token") == token);
EXPECT_TRUE(http_req.header("x-acs-signature-method") == "HMAC-SHA1");
EXPECT_TRUE(http_req.header("x-acs-signature-version") == "1.0");
EXPECT_TRUE(http_req.header("x-acs-version") == "1.0");
const string nounce = "8a013b14-7bac-4652-8b5f-c02c8924e4ae";
EXPECT_TRUE(http_req.header("x-acs-signature-nonce").length() == nounce.length());
EXPECT_TRUE(http_req.header("Authorization").find("acs accessKeyId:") != string::npos);
} }
TEST(RoaServiceClient, basic) {
const string service = "ECS";
const std::string regionId = "cn-shanghai";
const std::string hostname = "hostname";
const std::string user = "user";
const std::string password = "password";
uint16_t port = 12345;
const NetworkProxy proxy(NetworkProxy::Http, hostname, port, user, password);
HmacSha1Signer sig;
const std::string access_key = "accessKeyId";
const std::string access_secret = "accessKeySecret";
const std::string session_token = "sessionToken";
Credentials credentials(access_key, access_secret, session_token);
ClientConfiguration config(regionId, proxy);
RoaServiceClient roa(service,
std::make_shared<SimpleCredentialsProvider>(credentials),
config);
TestRoaClient client(service, std::make_shared<SimpleCredentialsProvider>(credentials), config);
HttpMessage::HeaderCollection headers;
// client.canonicalizedHeaders(headers);
// REQUIRE(roa.actionName() == action);
}
// EcsClient::EcsClient(const Credentials &credentials, const ClientConfiguration &configuration) :
// RpcServiceClient(SERVICE_NAME, std::make_shared<SimpleCredentialsProvider>(credentials), configuration)
// {
// auto locationClient = std::make_shared<LocationClient>(credentials, configuration);
// endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), SERVICE_NAME, "ecs");
// }