Skip to content

Commit de8e6cb

Browse files
committed
added a test for multiple services definition
updated test to use .find before creatng the profile update to use a service object instead of using getvalue
1 parent f394a3a commit de8e6cb

File tree

4 files changed

+138
-61
lines changed

4 files changed

+138
-61
lines changed

src/aws-cpp-sdk-core/include/aws/core/config/AWSConfigFileProfileConfigLoader.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <aws/core/config/AWSProfileConfigLoaderBase.h>
99

1010
#include <aws/core/utils/memory/stl/AWSString.h>
11-
#include <aws/core/utils/memory/stl/AWSMap.h>
1211

1312
namespace Aws
1413
{
@@ -47,7 +46,6 @@ namespace Aws
4746
private:
4847
Aws::String m_fileName;
4948
bool m_useProfilePrefix;
50-
Aws::Map<Aws::String, Aws::Map<Aws::String, Aws::String>> m_services;
5149
};
5250
}
5351
}

src/aws-cpp-sdk-core/include/aws/core/config/AWSProfileConfig.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ namespace Aws
2020
class Profile
2121
{
2222
public:
23+
/*
24+
* Data container for service endpoints.
25+
*/
26+
class Services
27+
{
28+
public:
29+
inline const Aws::Map<Aws::String, Aws::String>& GetEndpoints() const { return m_endpoints; }
30+
inline const Aws::String& GetServiceBlockName() const { return m_name; }
31+
inline bool IsSet() const { return !m_name.empty(); }
32+
private:
33+
friend class ConfigFileProfileFSM;
34+
void SetEndpoints(Aws::Map<Aws::String, Aws::String>&& endpoints, Aws::String name) {
35+
m_endpoints = std::move(endpoints);
36+
m_name = std::move(name);
37+
}
38+
Aws::Map<Aws::String, Aws::String> m_endpoints;
39+
Aws::String m_name;
40+
};
41+
2342
/*
2443
* Data container for a sso-session config entry.
2544
* This is independent of the general profile configuration and used by a bearer auth token provider.
@@ -94,10 +113,7 @@ namespace Aws
94113
return iter->second;
95114
}
96115

97-
inline Aws::Crt::Optional<Aws::String> GetServicesName() const {
98-
const Aws::String& service = GetValue("services");
99-
return service.empty() ? Aws::Crt::Optional<Aws::String>() : Aws::Crt::Optional<Aws::String>(service);
100-
}
116+
inline const Services& GetServices() const { return m_services; }
101117

102118
inline Aws::Crt::Optional<Aws::String> GetEndpointUrl() const {
103119
const Aws::String& endpoint = GetValue("endpoint_url");
@@ -110,6 +126,7 @@ namespace Aws
110126
inline void SetSsoSession(SsoSession&& value) { m_ssoSessionSet = true; m_ssoSession = std::move(value); }
111127

112128
private:
129+
friend class ConfigFileProfileFSM;
113130
Aws::String m_name;
114131
Aws::String m_region;
115132
Aws::Auth::AWSCredentials m_credentials;
@@ -123,6 +140,7 @@ namespace Aws
123140
Aws::String m_ssoRoleName;
124141
Aws::String m_defaultsMode;
125142
Aws::Map<Aws::String, Aws::String> m_allKeyValPairs;
143+
Services m_services;
126144

127145
bool m_ssoSessionSet = false;
128146
SsoSession m_ssoSession;

src/aws-cpp-sdk-core/source/config/AWSConfigFileProfileConfigLoader.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ namespace Aws
116116
{}
117117

118118
const Aws::Map<String, Profile>& GetProfiles() const { return m_foundProfiles; }
119-
const Aws::Map<String, Aws::Map<String, String>>& GetServices() const { return m_services; }
120119

121120
void ParseStream(Aws::IStream& stream)
122121
{
@@ -207,6 +206,22 @@ namespace Aws
207206

208207
FlushSection(currentState, currentSectionName, currentKeyValues);
209208

209+
// Resolve service endpoints
210+
for (auto& profilePair : m_foundProfiles)
211+
{
212+
Profile& profile = profilePair.second;
213+
const Aws::String& servicesRef = profile.GetValue("services");
214+
if (!servicesRef.empty())
215+
{
216+
auto servicesBlk = m_services.find(servicesRef);
217+
Aws::Map<Aws::String, Aws::String> endpoints;
218+
if (servicesBlk != m_services.end()) {
219+
endpoints = std::move(servicesBlk->second);
220+
}
221+
profile.m_services.SetEndpoints(std::move(endpoints), servicesRef);
222+
}
223+
}
224+
210225
// Put sso-sessions into profiles
211226
for(auto& profile : m_foundProfiles)
212227
{
@@ -641,15 +656,13 @@ namespace Aws
641656
bool AWSConfigFileProfileConfigLoader::LoadInternal()
642657
{
643658
m_profiles.clear();
644-
m_services.clear();
645659

646660
Aws::IFStream inputFile(m_fileName.c_str());
647661
if(inputFile)
648662
{
649663
ConfigFileProfileFSM parser(m_useProfilePrefix);
650664
parser.ParseStream(inputFile);
651665
m_profiles = parser.GetProfiles();
652-
m_services = parser.GetServices();
653666
return m_profiles.size() > 0;
654667
}
655668

tests/aws-cpp-sdk-core-tests/aws/config/ServiceEndpointsConfigFileLoaderTest.cpp

Lines changed: 100 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestServiceSpecificEndpoints)
3939
ASSERT_NE(profiles.end(), profiles.find("default"));
4040

4141
// Test global endpoint
42-
const auto& profile = profiles["default"];
42+
auto profileIt = profiles.find("default");
43+
ASSERT_NE(profiles.end(), profileIt);
44+
const auto& profile = profileIt->second;
4345
auto globalEndpoint = profile.GetEndpointUrl();
4446
ASSERT_TRUE(globalEndpoint.has_value());
4547
ASSERT_STREQ("https://global.example.com", globalEndpoint->c_str());
4648

47-
// Test services name is parsed correctly
48-
auto servicesName = profile.GetServicesName();
49-
ASSERT_TRUE(servicesName.has_value());
50-
ASSERT_STREQ("myservices", servicesName->c_str());
51-
49+
// Test services endpoints are parsed correctly
50+
const auto& services = profile.GetServices();
51+
ASSERT_TRUE(services.IsSet());
52+
const auto& endpoints = services.GetEndpoints();
53+
ASSERT_EQ(2u, endpoints.size());
54+
ASSERT_EQ("http://localhost:9000", endpoints.at("S3"));
55+
ASSERT_EQ("http://localhost:8000", endpoints.at("DYNAMODB"));
5256
}
5357

5458
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestServiceSpecificEndpointsOnly)
@@ -66,16 +70,20 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestServiceSpecificEndpointsOnly)
6670
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
6771
ASSERT_TRUE(loader.Load());
6872
auto profiles = loader.GetProfiles();
69-
const auto& profile = profiles["dev-minio"];
73+
auto profileIt = profiles.find("dev-minio");
74+
ASSERT_NE(profiles.end(), profileIt);
75+
const auto& profile = profileIt->second;
7076

7177
// Test that global endpoint is null when not set
7278
auto globalEndpoint = profile.GetEndpointUrl();
7379
ASSERT_FALSE(globalEndpoint.has_value());
7480

75-
// Test services name is parsed correctly
76-
auto servicesName = profile.GetServicesName();
77-
ASSERT_TRUE(servicesName.has_value());
78-
ASSERT_STREQ("s3-minio", servicesName->c_str());
81+
// Test services endpoints are parsed correctly
82+
const auto& services = profile.GetServices();
83+
ASSERT_TRUE(services.IsSet());
84+
const auto& endpoints = services.GetEndpoints();
85+
ASSERT_EQ(1u, endpoints.size());
86+
ASSERT_EQ("https://play.min.io:9000", endpoints.at("S3"));
7987
}
8088

8189
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestGlobalEndpointOnly)
@@ -90,16 +98,18 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestGlobalEndpointOnly)
9098
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
9199
ASSERT_TRUE(loader.Load());
92100
auto profiles = loader.GetProfiles();
93-
const auto& profile = profiles["dev-global"];
101+
auto profileIt = profiles.find("dev-global");
102+
ASSERT_NE(profiles.end(), profileIt);
103+
const auto& profile = profileIt->second;
94104

95105
// Test global endpoint
96106
auto globalEndpoint = profile.GetEndpointUrl();
97107
ASSERT_TRUE(globalEndpoint.has_value());
98108
ASSERT_STREQ("https://play.min.io:9000", globalEndpoint->c_str());
99109

100-
// Test that services name is not set
101-
auto servicesName = profile.GetServicesName();
102-
ASSERT_FALSE(servicesName.has_value());
110+
// Test that services endpoints are not set
111+
const auto& services = profile.GetServices();
112+
ASSERT_FALSE(services.IsSet());
103113
}
104114

105115
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestServiceSpecificAndGlobalEndpoints)
@@ -118,12 +128,16 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestServiceSpecificAndGlobalEndpoin
118128
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
119129
ASSERT_TRUE(loader.Load());
120130
auto profiles = loader.GetProfiles();
121-
const auto& profile = profiles["dev-s3-specific-and-global"];
131+
auto profileIt = profiles.find("dev-s3-specific-and-global");
132+
ASSERT_NE(profiles.end(), profileIt);
133+
const auto& profile = profileIt->second;
122134

123-
// Test services name is parsed correctly
124-
auto servicesName = profile.GetServicesName();
125-
ASSERT_TRUE(servicesName.has_value());
126-
ASSERT_STREQ("s3-specific-and-global", servicesName->c_str());
135+
// Test services endpoints are parsed correctly
136+
const auto& services = profile.GetServices();
137+
ASSERT_TRUE(services.IsSet());
138+
const auto& endpoints = services.GetEndpoints();
139+
ASSERT_EQ(1u, endpoints.size());
140+
ASSERT_EQ("https://play.min.io:9000", endpoints.at("S3"));
127141

128142
// Test global endpoint
129143
auto globalEndpoint = profile.GetEndpointUrl();
@@ -148,12 +162,17 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestMultipleServicesInDefinition)
148162
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
149163
ASSERT_TRUE(loader.Load());
150164
auto profiles = loader.GetProfiles();
151-
const auto& profile = profiles["dev"];
165+
auto profileIt = profiles.find("dev");
166+
ASSERT_NE(profiles.end(), profileIt);
167+
const auto& profile = profileIt->second;
152168

153-
// Test services name is parsed correctly
154-
auto servicesName = profile.GetServicesName();
155-
ASSERT_TRUE(servicesName.has_value());
156-
ASSERT_STREQ("testing-s3-and-eb", servicesName->c_str());
169+
// Test services endpoints are parsed correctly
170+
const auto& services = profile.GetServices();
171+
ASSERT_TRUE(services.IsSet());
172+
const auto& endpoints = services.GetEndpoints();
173+
ASSERT_EQ(2u, endpoints.size());
174+
ASSERT_EQ("http://localhost:4567", endpoints.at("S3"));
175+
ASSERT_EQ("http://localhost:8000", endpoints.at("ELASTIC_BEANSTALK"));
157176
}
158177

159178
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestIgnoreGlobalEndpointInServicesSection)
@@ -170,16 +189,19 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestIgnoreGlobalEndpointInServicesS
170189
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
171190
ASSERT_TRUE(loader.Load());
172191
auto profiles = loader.GetProfiles();
173-
const auto& profile = profiles["testing"];
192+
auto profileIt = profiles.find("testing");
193+
ASSERT_NE(profiles.end(), profileIt);
194+
const auto& profile = profileIt->second;
174195

175196
// Test that global endpoint in services section is ignored
176197
auto globalEndpoint = profile.GetEndpointUrl();
177198
ASSERT_FALSE(globalEndpoint.has_value());
178199

179-
// Test that services name is parsed correctly
180-
auto servicesName = profile.GetServicesName();
181-
ASSERT_TRUE(servicesName.has_value());
182-
ASSERT_STREQ("bad-service-definition", servicesName->c_str());
200+
// Test that services endpoints are empty (global endpoint_url ignored)
201+
const auto& services = profile.GetServices();
202+
ASSERT_TRUE(services.IsSet());
203+
const auto& endpoints = services.GetEndpoints();
204+
ASSERT_EQ(0u, endpoints.size());
183205
}
184206

185207
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestSourceProfileEndpointIsolation)
@@ -202,13 +224,19 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestSourceProfileEndpointIsolation)
202224
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
203225
ASSERT_TRUE(loader.Load());
204226
auto profiles = loader.GetProfiles();
205-
const auto& profileB = profiles["B"];
206-
const auto& profileA = profiles["A"];
227+
auto profileBIt = profiles.find("B");
228+
ASSERT_NE(profiles.end(), profileBIt);
229+
const auto& profileB = profileBIt->second;
230+
auto profileAIt = profiles.find("A");
231+
ASSERT_NE(profiles.end(), profileAIt);
232+
const auto& profileA = profileAIt->second;
207233

208-
// Test that profile B has services name
209-
auto servicesBName = profileB.GetServicesName();
210-
ASSERT_TRUE(servicesBName.has_value());
211-
ASSERT_STREQ("profileB", servicesBName->c_str());
234+
// Test that profile B has services endpoints
235+
const auto& servicesB = profileB.GetServices();
236+
ASSERT_TRUE(servicesB.IsSet());
237+
const auto& endpointsB = servicesB.GetEndpoints();
238+
ASSERT_EQ(1u, endpointsB.size());
239+
ASSERT_EQ("https://profile-b-ec2-endpoint.aws", endpointsB.at("EC2"));
212240

213241
// Test that profile B has no global endpoint (doesn't inherit from profile A)
214242
auto globalEndpointB = profileB.GetEndpointUrl();
@@ -220,9 +248,10 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestSourceProfileEndpointIsolation)
220248
ASSERT_STREQ("https://profile-a-endpoint.aws/", globalEndpointA->c_str());
221249

222250
// Test that profile A has no services name
223-
auto servicesAName = profileA.GetServicesName();
224-
ASSERT_FALSE(servicesAName.has_value());
251+
const auto& servicesA = profileA.GetServices();
252+
ASSERT_FALSE(servicesA.IsSet());
225253
}
254+
226255
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestIgnoreConfiguredEndpointUrls)
227256
{
228257
TempFile configFile(std::ios_base::out | std::ios_base::trunc);
@@ -242,18 +271,37 @@ TEST_F(ServiceEndpointsConfigFileLoaderTest, TestIgnoreConfiguredEndpointUrls)
242271
auto profiles = loader.GetProfiles();
243272

244273
// Test flag is parsed and stored
245-
ASSERT_STREQ("true", profiles["default"].GetValue("ignore_configured_endpoint_urls").c_str());
246-
ASSERT_STREQ("TRUE", profiles["test"].GetValue("ignore_configured_endpoint_urls").c_str());
247-
ASSERT_STREQ("", profiles["empty"].GetValue("ignore_configured_endpoint_urls").c_str());
248-
249-
// Test absent key returns empty string
250-
TempFile configFile2(std::ios_base::out | std::ios_base::trunc);
251-
configFile2 << "[profile absent]\n";
252-
configFile2 << "region = us-east-1\n";
253-
configFile2.flush();
254-
255-
AWSConfigFileProfileConfigLoader loader2(configFile2.GetFileName(), true);
256-
ASSERT_TRUE(loader2.Load());
257-
auto profiles2 = loader2.GetProfiles();
258-
ASSERT_STREQ("", profiles2["absent"].GetValue("ignore_configured_endpoint_urls").c_str());
274+
ASSERT_STREQ("true", profiles.find("default")->second.GetValue("ignore_configured_endpoint_urls").c_str());
275+
ASSERT_STREQ("TRUE", profiles.find("test")->second.GetValue("ignore_configured_endpoint_urls").c_str());
276+
ASSERT_STREQ("", profiles.find("empty")->second.GetValue("ignore_configured_endpoint_urls").c_str());
277+
}
278+
279+
TEST_F(ServiceEndpointsConfigFileLoaderTest, TestMultipleServicesDefinitions)
280+
{
281+
TempFile configFile(std::ios_base::out | std::ios_base::trunc);
282+
ASSERT_TRUE(configFile.good());
283+
284+
configFile << "[services foo]\n";
285+
configFile << "s3 =\n";
286+
configFile << " endpoint_url = http://foo.com\n";
287+
configFile << "\n[services bar]\n";
288+
configFile << "s3 =\n";
289+
configFile << " endpoint_url = http://bar.com\n";
290+
configFile << "\n[profile dev]\n";
291+
configFile << "services = foo\n";
292+
configFile.flush();
293+
294+
AWSConfigFileProfileConfigLoader loader(configFile.GetFileName(), true);
295+
ASSERT_TRUE(loader.Load());
296+
auto profiles = loader.GetProfiles();
297+
auto profileIt = profiles.find("dev");
298+
ASSERT_NE(profiles.end(), profileIt);
299+
const auto& profile = profileIt->second;
300+
301+
// Test services endpoints are parsed correctly
302+
const auto& services = profile.GetServices();
303+
ASSERT_TRUE(services.IsSet());
304+
const auto& endpoints = services.GetEndpoints();
305+
ASSERT_EQ(1u, endpoints.size());
306+
ASSERT_EQ("http://foo.com", endpoints.at("S3"));
259307
}

0 commit comments

Comments
 (0)