|
|
@@ -1,69 +1,92 @@
|
|
|
#include <iostream>
|
|
|
#include <stdio.h>
|
|
|
#include "gtest/gtest.h"
|
|
|
+#include "gmock/gmock.h"
|
|
|
#include "alibabacloud/core/EndpointProvider.h"
|
|
|
|
|
|
using namespace std;
|
|
|
using namespace AlibabaCloud;
|
|
|
using namespace AlibabaCloud::Location;
|
|
|
|
|
|
-TEST(EndpointProvider, basic) {
|
|
|
- ClientConfiguration configuration("cn-hangzhou");
|
|
|
-
|
|
|
- char* key = getenv("ENV_AccessKeyId");
|
|
|
- char* secret = getenv("ENV_AccessKeySecret");
|
|
|
-
|
|
|
- bool no_key_provided = false;
|
|
|
-
|
|
|
- string accessKeyId, accessKeySecret;
|
|
|
- if (key == nullptr) {
|
|
|
- accessKeyId = "no-AccessKeyId";
|
|
|
- no_key_provided = true;
|
|
|
- } else {
|
|
|
- accessKeyId = string(key);
|
|
|
- }
|
|
|
- if (secret == nullptr) {
|
|
|
- accessKeySecret = "no-AccessKeySecret";
|
|
|
- no_key_provided = true;
|
|
|
- } else {
|
|
|
- accessKeySecret = string(secret);
|
|
|
- }
|
|
|
- auto locationClient = std::make_shared<LocationClient>(accessKeyId, accessKeySecret, configuration);
|
|
|
-
|
|
|
- EndpointProvider ep(locationClient, configuration.regionId(), "Ecs", "ecs");
|
|
|
- EndpointProvider::EndpointOutcome out = ep.getEndpoint();
|
|
|
-
|
|
|
- if (no_key_provided) {
|
|
|
- EXPECT_TRUE(out.error().errorCode() == "InvalidAccessKeyId.NotFound");
|
|
|
- } else {
|
|
|
- EXPECT_TRUE(out.result() == "ecs-cn-hangzhou.aliyuncs.com");
|
|
|
- }
|
|
|
+using ::testing::_;
|
|
|
+using ::testing::DefaultValue;
|
|
|
+
|
|
|
+
|
|
|
+class mockEndpointProvider: public EndpointProvider {
|
|
|
+ public:
|
|
|
+ mockEndpointProvider(
|
|
|
+ const Credentials &credentials,
|
|
|
+ const ClientConfiguration &configuration,
|
|
|
+ const std::string ®ionId,
|
|
|
+ const std::string &product,
|
|
|
+ const std::string &serviceCode = std::string(),
|
|
|
+ int durationSeconds = 3600
|
|
|
+ ):
|
|
|
+ EndpointProvider(credentials, configuration, regionId, product, serviceCode, durationSeconds) {}
|
|
|
+
|
|
|
+ MOCK_CONST_METHOD1(describeEndpoints, LocationClient::DescribeEndpointsOutcome(const Model::DescribeEndpointsRequest &request));
|
|
|
+};
|
|
|
+
|
|
|
+TEST(EndpointProvider, service_code_empty) {
|
|
|
+ const Credentials sub_user_credentials("key", "secret");
|
|
|
+ ClientConfiguration config; // default is cn-hangzhou
|
|
|
+ // config.setEndpoint("test-endpoint"); // endpoint should be empty
|
|
|
+
|
|
|
+ // invalid product
|
|
|
+ mockEndpointProvider provider1(sub_user_credentials, config, config.regionId(), "non-exist-product", "");
|
|
|
+ EndpointProvider::EndpointOutcome out1 = provider1.getEndpoint();
|
|
|
+ EXPECT_TRUE(out1.error().errorCode() == "InvalidRegionId");
|
|
|
+
|
|
|
+ // ecs has no global_endpoint
|
|
|
+ mockEndpointProvider provider2(sub_user_credentials, config, config.regionId(), "ecs", "");
|
|
|
+ EndpointProvider::EndpointOutcome out2 = provider2.getEndpoint();
|
|
|
+ EXPECT_TRUE(out2.error().errorCode() == "InvalidRegionId");
|
|
|
+
|
|
|
+ // aegis has global_endpoint
|
|
|
+ mockEndpointProvider provider3(sub_user_credentials, config, config.regionId(), "aegis", "");
|
|
|
+ EndpointProvider::EndpointOutcome out3 = provider3.getEndpoint();
|
|
|
+ EXPECT_TRUE(out3.error().errorCode().empty());
|
|
|
+ EXPECT_TRUE(out3.result() == "aegis.cn-hangzhou.aliyuncs.com");
|
|
|
+
|
|
|
+ // arms has regional_endpoint, get from region
|
|
|
+ mockEndpointProvider provider4(sub_user_credentials, config, config.regionId(), "arms", "");
|
|
|
+ EndpointProvider::EndpointOutcome out4 = provider4.getEndpoint();
|
|
|
+ EXPECT_TRUE(out4.error().errorCode().empty());
|
|
|
+ EXPECT_TRUE(out4.result() == "arms.cn-hangzhou.aliyuncs.com");
|
|
|
}
|
|
|
|
|
|
-TEST(EndpointProvider, basic1) {
|
|
|
- ClientConfiguration configuration("cn-hangzhou");
|
|
|
+TEST(EndpointProvider, mock_remote) {
|
|
|
+ const Credentials sub_user_credentials("key", "secret");
|
|
|
+ ClientConfiguration config; // default is cn-hangzhou
|
|
|
+ // config.setEndpoint("test-endpoint");
|
|
|
|
|
|
- const string accessKeyId = "no-AccessKeyId";
|
|
|
- const string accessKeySecret = "no-AccessKeySecret";
|
|
|
+ mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "ecs", "ecs");
|
|
|
|
|
|
- auto locationClient = std::make_shared<LocationClient>(accessKeyId, accessKeySecret, configuration);
|
|
|
+ Model::DescribeEndpointsRequest request;
|
|
|
|
|
|
- EndpointProvider ep(locationClient, configuration.regionId(), "Ecs");
|
|
|
- EndpointProvider::EndpointOutcome out = ep.getEndpoint();
|
|
|
+ const string payload = "{\"RequestId\":\"test-request-id\",\"Success\":true,\"Endpoints\":{\"Endpoint\":[{\"Endpoint\":\"test-ep\",\"Id\":\"test-id\",\"Namespace\":\"test-namespace\",\"SerivceCode\":\"test-service-code\",\"Type\":\"test-type\",\"Protocols\":{\"Protocols\":[\"a\",\"b\"]}}]}}";
|
|
|
+ Location::Model::DescribeEndpointsResult res(payload);
|
|
|
+ LocationClient::DescribeEndpointsOutcome xout(res);
|
|
|
|
|
|
- EXPECT_TRUE(out.error().errorCode() == "InvalidRegionId");
|
|
|
-}
|
|
|
+ DefaultValue<LocationClient::DescribeEndpointsOutcome>::Set(xout);
|
|
|
+ EXPECT_CALL(provider, describeEndpoints(_));
|
|
|
+ EndpointProvider::EndpointOutcome out = provider.getEndpoint();
|
|
|
|
|
|
+ EXPECT_TRUE(out.error().errorCode().empty());
|
|
|
+ EXPECT_TRUE(out.result() == "test-ep");
|
|
|
+}
|
|
|
|
|
|
-TEST(EndpointProvider, basic2) {
|
|
|
- ClientConfiguration configuration("xxxcn-hangzhou");
|
|
|
+TEST(EndpointProvider, mock_remote_error) {
|
|
|
+ const Credentials sub_user_credentials("key", "secret");
|
|
|
+ ClientConfiguration config; // default is cn-hangzhou
|
|
|
+ // config.setEndpoint("test-endpoint");
|
|
|
|
|
|
- const string accessKeyId = "no-AccessKeyId";
|
|
|
- const string accessKeySecret = "no-AccessKeySecret";
|
|
|
+ mockEndpointProvider provider(sub_user_credentials, config, config.regionId(), "ecs", "ecs");
|
|
|
+ LocationClient::DescribeEndpointsOutcome xout(Error("any-error-code", "any-error-message"));
|
|
|
|
|
|
- auto locationClient = std::make_shared<LocationClient>(accessKeyId, accessKeySecret, configuration);
|
|
|
+ DefaultValue<LocationClient::DescribeEndpointsOutcome>::Set(xout);
|
|
|
+ EXPECT_CALL(provider, describeEndpoints(_));
|
|
|
+ EndpointProvider::EndpointOutcome out = provider.getEndpoint();
|
|
|
|
|
|
- EndpointProvider ep(locationClient, configuration.regionId(), "Ecs", "ecs");
|
|
|
- EndpointProvider::EndpointOutcome out = ep.getEndpoint();
|
|
|
- EXPECT_TRUE(out.error().errorCode() == "InvalidAccessKeyId.NotFound");
|
|
|
+ EXPECT_TRUE(out.error().errorCode() == "any-error-code");
|
|
|
}
|