unit test for InstanceProfileCredentialsProvider
This commit is contained in:
@@ -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.
|
||||||
@@ -23,11 +23,11 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "CredentialsProvider.h"
|
#include "CredentialsProvider.h"
|
||||||
#include "Credentials.h"
|
#include "Credentials.h"
|
||||||
|
#include "../src/EcsMetadataFetcher.h"
|
||||||
|
|
||||||
namespace AlibabaCloud
|
namespace AlibabaCloud
|
||||||
{
|
{
|
||||||
class EcsMetadataFetcher;
|
class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider, public EcsMetadataFetcher
|
||||||
class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds = 3600);
|
InstanceProfileCredentialsProvider(const std::string &roleName, int durationSeconds = 3600);
|
||||||
@@ -35,7 +35,9 @@ namespace AlibabaCloud
|
|||||||
|
|
||||||
std::string roleName()const;
|
std::string roleName()const;
|
||||||
virtual Credentials getCredentials() override;
|
virtual Credentials getCredentials() override;
|
||||||
|
using EcsMetadataFetcher::roleName;
|
||||||
|
using EcsMetadataFetcher::setRoleName;
|
||||||
|
using EcsMetadataFetcher::getMetadata;
|
||||||
private:
|
private:
|
||||||
void loadCredentials();
|
void loadCredentials();
|
||||||
bool checkExpiry()const;
|
bool checkExpiry()const;
|
||||||
@@ -44,7 +46,6 @@ namespace AlibabaCloud
|
|||||||
Credentials cachedCredentials_;
|
Credentials cachedCredentials_;
|
||||||
int durationSeconds_;
|
int durationSeconds_;
|
||||||
std::chrono::system_clock::time_point expiry_;
|
std::chrono::system_clock::time_point expiry_;
|
||||||
EcsMetadataFetcher *fetcher_;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -40,6 +40,10 @@ void EcsMetadataFetcher::setRoleName(const std::string & roleName)
|
|||||||
roleName_ = 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::string EcsMetadataFetcher::getMetadata(const std::string host, const std::string in_path)
|
||||||
{
|
{
|
||||||
std::stringstream path;
|
std::stringstream path;
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ 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(const std::string host, const std::string path);
|
||||||
|
virtual std::string getMetadata();
|
||||||
private:
|
private:
|
||||||
std::string roleName_;
|
std::string roleName_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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.
|
||||||
@@ -27,23 +27,17 @@ using namespace AlibabaCloud;
|
|||||||
|
|
||||||
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds):
|
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::string & roleName, int durationSeconds):
|
||||||
CredentialsProvider(),
|
CredentialsProvider(),
|
||||||
|
EcsMetadataFetcher(),
|
||||||
durationSeconds_(durationSeconds),
|
durationSeconds_(durationSeconds),
|
||||||
cachedMutex_(),
|
cachedMutex_(),
|
||||||
cachedCredentials_("", ""),
|
cachedCredentials_("", ""),
|
||||||
fetcher_(new EcsMetadataFetcher),
|
|
||||||
expiry_()
|
expiry_()
|
||||||
{
|
{
|
||||||
fetcher_->setRoleName(roleName);
|
setRoleName(roleName);
|
||||||
}
|
}
|
||||||
|
|
||||||
InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider()
|
InstanceProfileCredentialsProvider::~InstanceProfileCredentialsProvider()
|
||||||
{
|
{
|
||||||
delete fetcher_;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string InstanceProfileCredentialsProvider::roleName()const
|
|
||||||
{
|
|
||||||
return fetcher_->roleName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Credentials InstanceProfileCredentialsProvider::getCredentials()
|
Credentials InstanceProfileCredentialsProvider::getCredentials()
|
||||||
@@ -68,16 +62,16 @@ void InstanceProfileCredentialsProvider::loadCredentials()
|
|||||||
std::lock_guard<std::mutex> locker(cachedMutex_);
|
std::lock_guard<std::mutex> locker(cachedMutex_);
|
||||||
if (checkExpiry())
|
if (checkExpiry())
|
||||||
{
|
{
|
||||||
auto outcome = fetcher_->getMetadata();
|
auto outcome = getMetadata();
|
||||||
Json::Value value;
|
Json::Value value;
|
||||||
Json::Reader reader;
|
Json::Reader reader;
|
||||||
if (reader.parse(outcome, value))
|
if (reader.parse(outcome, value))
|
||||||
{
|
{
|
||||||
if (value["Code"] == nullptr
|
if (value["Code"].empty()
|
||||||
&&value["AccessKeyId"] == nullptr
|
&&value["AccessKeyId"].empty()
|
||||||
&&value["AccessKeySecret"] == nullptr
|
&&value["AccessKeySecret"].empty()
|
||||||
&&value["SecurityToken"] == nullptr
|
&&value["SecurityToken"].empty()
|
||||||
&&value["Expiration"] == nullptr)
|
&&value["Expiration"].empty())
|
||||||
{
|
{
|
||||||
cachedCredentials_ = Credentials("","");
|
cachedCredentials_ = Credentials("","");
|
||||||
return;
|
return;
|
||||||
@@ -96,7 +90,7 @@ void InstanceProfileCredentialsProvider::loadCredentials()
|
|||||||
std::tm tm = {};
|
std::tm tm = {};
|
||||||
#if defined(__GNUG__) && __GNUC__ < 5
|
#if defined(__GNUG__) && __GNUC__ < 5
|
||||||
strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
|
strptime(expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
|
||||||
#else
|
#else
|
||||||
std::stringstream ss(expiration);
|
std::stringstream ss(expiration);
|
||||||
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
|
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ add_executable(core_ut
|
|||||||
url_ut.cc
|
url_ut.cc
|
||||||
utils_ut.cc
|
utils_ut.cc
|
||||||
|
|
||||||
|
instanceprofilecredentialsprovider_ut.cc
|
||||||
|
|
||||||
locationclient_ut.cc
|
locationclient_ut.cc
|
||||||
location_model_describeendpoints_request_ut.cc
|
location_model_describeendpoints_request_ut.cc
|
||||||
location_model_describeendpoints_result_ut.cc
|
location_model_describeendpoints_result_ut.cc
|
||||||
|
|||||||
56
test/core/instanceprofilecredentialsprovider_ut.cc
Normal file
56
test/core/instanceprofilecredentialsprovider_ut.cc
Normal file
@@ -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());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user