AWSCoreConfigurationTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Settings/SettingsRegistry.h>
  9. #include <AzFramework/StringFunc/StringFunc.h>
  10. #include <Configuration/AWSCoreConfiguration.h>
  11. #include <TestFramework/AWSCoreFixture.h>
  12. using namespace AWSCore;
  13. static constexpr const char TEST_VALID_RESOURCE_MAPPING_SETREG[] =
  14. R"({
  15. "Amazon":
  16. {
  17. "AWSCore": {
  18. "ProfileName": "testprofile",
  19. "ResourceMappingConfigFileName": "test_aws_resource_mappings.json",
  20. "AllowAWSMetadataCredentials": true,
  21. }
  22. }
  23. })";
  24. static constexpr const char TEST_INVALID_RESOURCE_MAPPING_SETREG[] =
  25. R"({
  26. "Amazon":
  27. {
  28. "AWSCore": {
  29. }
  30. }
  31. })";
  32. class AWSCoreConfigurationTest
  33. : public AWSCoreFixture
  34. {
  35. public:
  36. AWSCoreConfigurationTest()
  37. {
  38. m_setRegFilePath = (GetTestTempDirectoryPath() /
  39. AZ::SettingsRegistryInterface::RegistryFolder /
  40. AWSCore::AWSCoreConfiguration::AWSCoreConfigurationFileName).LexicallyNormal();
  41. }
  42. void SetUp() override
  43. {
  44. AWSCoreFixture::SetUp();
  45. m_awsCoreConfiguration = AZStd::make_unique<AWSCore::AWSCoreConfiguration>();
  46. CreateFile(m_setRegFilePath.Native(), TEST_VALID_RESOURCE_MAPPING_SETREG);
  47. m_localFileIO->SetAlias("@projectroot@", GetTestTempDirectoryPath().Native().c_str());
  48. }
  49. void TearDown() override
  50. {
  51. RemoveFile(m_setRegFilePath.Native());
  52. m_awsCoreConfiguration.reset();
  53. AWSCoreFixture::TearDown();
  54. }
  55. AZStd::unique_ptr<AWSCore::AWSCoreConfiguration> m_awsCoreConfiguration;
  56. AZ::IO::Path m_setRegFilePath;
  57. };
  58. TEST_F(AWSCoreConfigurationTest, InitConfig_NoSourceProjectFolderFound_ReturnEmptyConfigFilePath)
  59. {
  60. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  61. m_localFileIO->ClearAlias("@projectroot@");
  62. AZ_TEST_START_TRACE_SUPPRESSION;
  63. m_awsCoreConfiguration->InitConfig();
  64. AZ_TEST_STOP_TRACE_SUPPRESSION(1); // expect the above have thrown an AZ_Error
  65. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  66. EXPECT_TRUE(actualConfigFilePath.empty());
  67. }
  68. TEST_F(AWSCoreConfigurationTest, InitConfig_SettingsRegistryIsEmpty_ReturnEmptyConfigFilePath)
  69. {
  70. CreateFile(m_setRegFilePath.Native(), TEST_INVALID_RESOURCE_MAPPING_SETREG);
  71. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  72. m_awsCoreConfiguration->InitConfig();
  73. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  74. EXPECT_TRUE(actualConfigFilePath.empty());
  75. }
  76. TEST_F(AWSCoreConfigurationTest, InitConfig_LoadValidSettingsRegistry_ReturnNonEmptyConfigFilePath)
  77. {
  78. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  79. m_awsCoreConfiguration->InitConfig();
  80. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  81. EXPECT_FALSE(actualConfigFilePath.empty());
  82. }
  83. TEST_F(AWSCoreConfigurationTest, ReloadConfiguration_NoSourceProjectFolderFound_ReturnEmptyConfigFilePath)
  84. {
  85. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  86. m_localFileIO->ClearAlias("@projectroot@");
  87. m_awsCoreConfiguration->ReloadConfiguration();
  88. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  89. EXPECT_TRUE(actualConfigFilePath.empty());
  90. }
  91. TEST_F(AWSCoreConfigurationTest, ReloadConfiguration_LoadValidSettingsRegistryAfterInvalidOne_ReturnNonEmptyConfigFilePath)
  92. {
  93. CreateFile(m_setRegFilePath.Native(), TEST_INVALID_RESOURCE_MAPPING_SETREG);
  94. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  95. m_awsCoreConfiguration->InitConfig();
  96. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  97. auto actualProfileName = m_awsCoreConfiguration->GetProfileName();
  98. auto actualAllowAWSMetadataCredentials = m_awsCoreConfiguration->IsAllowedAWSMetadataCredentials();
  99. EXPECT_TRUE(actualConfigFilePath.empty());
  100. EXPECT_TRUE(actualProfileName == AWSCoreConfiguration::AWSCoreDefaultProfileName);
  101. EXPECT_FALSE(actualAllowAWSMetadataCredentials);
  102. CreateFile(m_setRegFilePath.Native(), TEST_VALID_RESOURCE_MAPPING_SETREG);
  103. m_awsCoreConfiguration->ReloadConfiguration();
  104. actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  105. actualProfileName = m_awsCoreConfiguration->GetProfileName();
  106. actualAllowAWSMetadataCredentials = m_awsCoreConfiguration->IsAllowedAWSMetadataCredentials();
  107. EXPECT_FALSE(actualConfigFilePath.empty());
  108. EXPECT_TRUE(actualProfileName != AWSCoreConfiguration::AWSCoreDefaultProfileName);
  109. EXPECT_TRUE(actualAllowAWSMetadataCredentials);
  110. }
  111. TEST_F(AWSCoreConfigurationTest, ReloadConfiguration_LoadInvalidSettingsRegistryAfterValidOne_ReturnEmptyConfigFilePath)
  112. {
  113. m_settingsRegistry->MergeSettingsFile(m_setRegFilePath.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  114. m_awsCoreConfiguration->InitConfig();
  115. auto actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  116. auto actualProfileName = m_awsCoreConfiguration->GetProfileName();
  117. auto actualAllowAWSMetadataCredentials = m_awsCoreConfiguration->IsAllowedAWSMetadataCredentials();
  118. EXPECT_FALSE(actualConfigFilePath.empty());
  119. EXPECT_TRUE(actualProfileName != AWSCoreConfiguration::AWSCoreDefaultProfileName);
  120. EXPECT_TRUE(actualAllowAWSMetadataCredentials);
  121. CreateFile(m_setRegFilePath.Native(), TEST_INVALID_RESOURCE_MAPPING_SETREG);
  122. m_awsCoreConfiguration->ReloadConfiguration();
  123. actualConfigFilePath = m_awsCoreConfiguration->GetResourceMappingConfigFilePath();
  124. actualProfileName = m_awsCoreConfiguration->GetProfileName();
  125. actualAllowAWSMetadataCredentials = m_awsCoreConfiguration->IsAllowedAWSMetadataCredentials();
  126. EXPECT_TRUE(actualConfigFilePath.empty());
  127. EXPECT_TRUE(actualProfileName == AWSCoreConfiguration::AWSCoreDefaultProfileName);
  128. EXPECT_FALSE(actualAllowAWSMetadataCredentials);
  129. }