Эх сурвалжийг харах

unit test for InstanceProfileCredentialsProvider

zhangzifa 7 жил өмнө
parent
commit
e00a04b071

+ 8 - 7
core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h

@@ -1,12 +1,12 @@
 /*
  * Copyright 2009-2017 Alibaba Cloud 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.
@@ -23,11 +23,11 @@
 #include <string>
 #include "CredentialsProvider.h"
 #include "Credentials.h"
+#include "../src/EcsMetadataFetcher.h"
 
 namespace AlibabaCloud
 {
-	class EcsMetadataFetcher;
-	class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider
+	class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider, public EcsMetadataFetcher
 	{
 	public:
 		InstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds = 3600);
@@ -35,7 +35,9 @@ namespace AlibabaCloud
 
 		std::string roleName()const;
 		virtual Credentials getCredentials() override;
-
+		using EcsMetadataFetcher::roleName;
+		using EcsMetadataFetcher::setRoleName;
+		using EcsMetadataFetcher::getMetadata;
 	private:
 		void loadCredentials();
 		bool checkExpiry()const;
@@ -44,7 +46,6 @@ namespace AlibabaCloud
 		Credentials cachedCredentials_;
 		int durationSeconds_;
 		std::chrono::system_clock::time_point expiry_;
-		EcsMetadataFetcher *fetcher_;
 	};
 }
 #endif

+ 4 - 0
core/src/EcsMetadataFetcher.cc

@@ -40,6 +40,10 @@ void EcsMetadataFetcher::setRoleName(const std::string & roleName)
 	roleName_ = roleName;
 }
 
+std::string EcsMetadataFetcher::getMetadata() {
+	return getMetadata(METADATA_SERVICE_HOST, URL_IN_ECS_METADATA);
+}
+
 std::string EcsMetadataFetcher::getMetadata(const std::string host, const std::string in_path)
 {
 	std::stringstream path;

+ 2 - 1
core/src/EcsMetadataFetcher.h

@@ -35,7 +35,8 @@ namespace AlibabaCloud
 
 		std::string roleName()const;
 		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(const std::string host, const std::string path);
+		virtual std::string getMetadata();
 	private:
 		std::string roleName_;
 	};

+ 12 - 18
core/src/InstanceProfileCredentialsProvider.cc

@@ -1,12 +1,12 @@
 /*
  * Copyright 2009-2017 Alibaba Cloud 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.
@@ -27,23 +27,17 @@ using namespace AlibabaCloud;
 
 InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds):
 	CredentialsProvider(),
+	EcsMetadataFetcher(),
 	durationSeconds_(durationSeconds),
 	cachedMutex_(),
 	cachedCredentials_("", ""),
-	fetcher_(new EcsMetadataFetcher),
 	expiry_()
 {
-	fetcher_->setRoleName(roleName);
+	setRoleName(roleName);
 }
 
 InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider()
 {
-	delete fetcher_;
-}
-
-std::string InstanceProfileCredentialsProvider::roleName()const
-{
-	return fetcher_->roleName();
 }
 
 Credentials InstanceProfileCredentialsProvider::getCredentials()
@@ -68,16 +62,16 @@ void InstanceProfileCredentialsProvider::loadCredentials()
 		std::lock_guard<std::mutex> locker(cachedMutex_);
 		if (checkExpiry())
 		{
-			auto outcome = fetcher_->getMetadata();
+			auto outcome = getMetadata();
 			Json::Value value;
 			Json::Reader reader;
 			if (reader.parse(outcome, value))
 			{
-				if (value["Code"] == nullptr
-					&&value["AccessKeyId"] == nullptr
-					&&value["AccessKeySecret"] == nullptr
-					&&value["SecurityToken"] == nullptr
-					&&value["Expiration"] == nullptr)
+				if (value["Code"].empty()
+					&&value["AccessKeyId"].empty()
+					&&value["AccessKeySecret"].empty()
+					&&value["SecurityToken"].empty()
+					&&value["Expiration"].empty())
 				{
 					cachedCredentials_ = Credentials("","");
 					return;
@@ -96,7 +90,7 @@ void InstanceProfileCredentialsProvider::loadCredentials()
 				std::tm tm = {};
 #if defined(__GNUG__) && __GNUC__ < 5
 				strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
-#else			
+#else
 				std::stringstream ss(expiration);
 				ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
 #endif

+ 2 - 0
test/core/CMakeLists.txt

@@ -64,6 +64,8 @@ add_executable(core_ut
   url_ut.cc
   utils_ut.cc
 
+  instanceprofilecredentialsprovider_ut.cc
+
   locationclient_ut.cc
   location_model_describeendpoints_request_ut.cc
   location_model_describeendpoints_result_ut.cc

+ 56 - 0
test/core/instanceprofilecredentialsprovider_ut.cc

@@ -0,0 +1,56 @@
+#include <iostream>
+#include <stdio.h>
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "alibabacloud/core/InstanceProfileCredentialsProvider.h"
+
+using namespace std;
+using namespace AlibabaCloud;
+
+using ::testing::_;
+using ::testing::DefaultValue;
+
+
+class mockInstanceProfileCredentialsProvider: public InstanceProfileCredentialsProvider {
+ public:
+  mockInstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds):
+
+  InstanceProfileCredentialsProvider(roleName, durationSeconds) {}
+  MOCK_METHOD0(getMetadata, std::string());
+};
+
+TEST(InstanceProfileCredentialsProvider, basic) {
+  mockInstanceProfileCredentialsProvider p("test-role-name", 1800);
+
+  DefaultValue<std::string>::Set("{\"Code\":\"test-code\",\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"SecurityToken\":\"any-token\",\"Expiration\":3233}");
+  EXPECT_CALL(p, getMetadata());
+
+  Credentials credentials = p.getCredentials();
+  EXPECT_TRUE(credentials.accessKeyId() == "any-key");
+  EXPECT_TRUE(credentials.accessKeySecret() == "any-secret");
+  EXPECT_TRUE(credentials.sessionToken() == "any-token");
+}
+
+TEST(InstanceProfileCredentialsProvider, empty) {
+  mockInstanceProfileCredentialsProvider p("test-role-name", 1800);
+
+  DefaultValue<std::string>::Set("{}");
+  EXPECT_CALL(p, getMetadata());
+
+  Credentials credentials = p.getCredentials();
+  EXPECT_TRUE(credentials.accessKeyId().empty());
+  EXPECT_TRUE(credentials.accessKeySecret().empty());
+  EXPECT_TRUE(credentials.sessionToken().empty());
+}
+
+TEST(InstanceProfileCredentialsProvider, empty_1) {
+  mockInstanceProfileCredentialsProvider p("test-role-name", 1800);
+
+  DefaultValue<std::string>::Set("");
+  EXPECT_CALL(p, getMetadata());
+
+  Credentials credentials = p.getCredentials();
+  EXPECT_TRUE(credentials.accessKeyId().empty());
+  EXPECT_TRUE(credentials.accessKeySecret().empty());
+  EXPECT_TRUE(credentials.sessionToken().empty());
+}