Skip to content

Commit f605fff

Browse files
More ExternalIPsConfig unit tests
Co-authored-by: JoukoVirtanen <jouko@stackrox.com>
1 parent be59b85 commit f605fff

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

collector/lib/ExternalIPsConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ class ExternalIPsConfig {
4141

4242
std::ostream& operator<<(std::ostream& os, const ExternalIPsConfig& config);
4343

44-
} // end namespace collector
44+
} // end namespace collector

collector/test/CollectorConfigTest.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "CollectorArgs.h"
66
#include "CollectorConfig.h"
7+
#include "ConfigLoader.h"
78
#include "gmock/gmock.h"
89
#include "gtest/gtest.h"
910

@@ -155,4 +156,91 @@ TEST(CollectorConfigTest, TestEnableExternalIpsRuntimeConfig) {
155156
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::BOTH));
156157
}
157158

159+
TEST(CollectorConfigTest, TestIsEnabledIngress) {
160+
std::string yamlStr = R"(
161+
networking:
162+
externalIps:
163+
enabled: enabled
164+
direction: ingress
165+
)";
166+
167+
YAML::Node yamlNode = YAML::Load(yamlStr);
168+
CollectorConfig config;
169+
ASSERT_EQ(ConfigLoader(config).LoadConfiguration(yamlNode), ConfigLoader::SUCCESS) << "Input: " << yamlStr;
170+
171+
auto runtime_config = config.GetRuntimeConfig();
172+
173+
EXPECT_TRUE(runtime_config.has_value());
174+
175+
EXPECT_EQ(Direction::INGRESS, config.GetExternalIPsConf().GetDirection());
176+
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::INGRESS));
177+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::EGRESS));
178+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::BOTH));
179+
}
180+
181+
TEST(CollectorConfigTest, TestIsEnabledEgress) {
182+
std::string yamlStr = R"(
183+
networking:
184+
externalIps:
185+
enabled: enabled
186+
direction: egress
187+
)";
188+
189+
YAML::Node yamlNode = YAML::Load(yamlStr);
190+
CollectorConfig config;
191+
ASSERT_EQ(ConfigLoader(config).LoadConfiguration(yamlNode), ConfigLoader::SUCCESS) << "Input: " << yamlStr;
192+
193+
auto runtime_config = config.GetRuntimeConfig();
194+
195+
EXPECT_TRUE(runtime_config.has_value());
196+
197+
EXPECT_EQ(Direction::EGRESS, config.GetExternalIPsConf().GetDirection());
198+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::INGRESS));
199+
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::EGRESS));
200+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::BOTH));
201+
}
202+
203+
TEST(CollectorConfigTest, TestIsEnabledBoth) {
204+
std::string yamlStr = R"(
205+
networking:
206+
externalIps:
207+
enabled: enabled
208+
direction: both
209+
)";
210+
211+
YAML::Node yamlNode = YAML::Load(yamlStr);
212+
CollectorConfig config;
213+
ASSERT_EQ(ConfigLoader(config).LoadConfiguration(yamlNode), ConfigLoader::SUCCESS) << "Input: " << yamlStr;
214+
215+
auto runtime_config = config.GetRuntimeConfig();
216+
217+
EXPECT_TRUE(runtime_config.has_value());
218+
219+
EXPECT_EQ(Direction::BOTH, config.GetExternalIPsConf().GetDirection());
220+
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::INGRESS));
221+
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::EGRESS));
222+
EXPECT_TRUE(config.GetExternalIPsConf().IsEnabled(Direction::BOTH));
223+
}
224+
225+
TEST(CollectorConfigTest, TestIsEnabledNone) {
226+
std::string yamlStr = R"(
227+
networking:
228+
externalIps:
229+
enabled: disabled
230+
)";
231+
232+
YAML::Node yamlNode = YAML::Load(yamlStr);
233+
CollectorConfig config;
234+
ASSERT_EQ(ConfigLoader(config).LoadConfiguration(yamlNode), ConfigLoader::SUCCESS) << "Input: " << yamlStr;
235+
236+
auto runtime_config = config.GetRuntimeConfig();
237+
238+
EXPECT_TRUE(runtime_config.has_value());
239+
240+
EXPECT_EQ(Direction::NONE, config.GetExternalIPsConf().GetDirection());
241+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::INGRESS));
242+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::EGRESS));
243+
EXPECT_FALSE(config.GetExternalIPsConf().IsEnabled(Direction::BOTH));
244+
}
245+
158246
} // namespace collector

0 commit comments

Comments
 (0)