Skip to content

Commit 1357ea8

Browse files
authored
Added missing configuration unit tests (#82)
## This PR Adds missing flagd configuration unit tests. It tests generally established precedence of arguments, and verifies environment variable loading. ### How to test ``` bazelisk test //providers/flagd/tests:configuration_test ``` --------- Signed-off-by: Marcin Olko <molko@google.com>
1 parent 7e5e561 commit 1357ea8

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

providers/flagd/tests/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ cc_test(
1818
"@nlohmann_json//:json",
1919
],
2020
)
21+
22+
cc_test(
23+
name = "configuration_test",
24+
srcs = ["configuration_test.cpp"],
25+
deps = [
26+
"//providers/flagd/src:flagd_configuration",
27+
"@abseil-cpp//absl/strings",
28+
"@googletest//:gtest_main",
29+
],
30+
)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "flagd/configuration.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include "absl/strings/str_cat.h"
6+
7+
namespace flagd {
8+
9+
class ConfigurationTest : public ::testing::Test {
10+
protected:
11+
void SetUp() override { ClearFlagdEnvVars(); }
12+
13+
void TearDown() override { ClearFlagdEnvVars(); }
14+
15+
private:
16+
void ClearFlagdEnvVars() {
17+
unsetenv("FLAGD_HOST");
18+
unsetenv("FLAGD_PORT");
19+
unsetenv("FLAGD_TARGET_URI");
20+
unsetenv("FLAGD_TLS");
21+
unsetenv("FLAGD_SOCKET_PATH");
22+
unsetenv("FLAGD_SERVER_CERT_PATH");
23+
unsetenv("FLAGD_SOURCE_SELECTOR");
24+
unsetenv("FLAGD_PROVIDER_ID");
25+
}
26+
};
27+
28+
TEST_F(ConfigurationTest, DefaultValues) {
29+
FlagdProviderConfig config;
30+
const std::string default_host = "localhost";
31+
const int default_port = 8015;
32+
EXPECT_EQ(config.GetHost(), default_host);
33+
EXPECT_EQ(config.GetPort(), default_port);
34+
EXPECT_FALSE(config.GetTls());
35+
EXPECT_EQ(config.GetEffectiveTargetUri(),
36+
absl::StrCat(default_host, ":", default_port));
37+
}
38+
39+
TEST_F(ConfigurationTest, EnvironmentVariables) {
40+
const std::string host = "myhost";
41+
const int port = 9000;
42+
setenv("FLAGD_HOST", host.c_str(), 1);
43+
setenv("FLAGD_PORT", std::to_string(port).c_str(), 1);
44+
setenv("FLAGD_TLS", "true", 1);
45+
setenv("FLAGD_SOURCE_SELECTOR", "my-selector", 1);
46+
47+
FlagdProviderConfig config;
48+
EXPECT_EQ(config.GetHost(), host);
49+
EXPECT_EQ(config.GetPort(), port);
50+
EXPECT_TRUE(config.GetTls());
51+
EXPECT_TRUE(config.GetSelector().has_value());
52+
EXPECT_EQ(config.GetSelector().value(), "my-selector");
53+
EXPECT_EQ(config.GetEffectiveTargetUri(), absl::StrCat(host, ":", port));
54+
}
55+
56+
TEST_F(ConfigurationTest, EffectiveTargetUriPrecedence) {
57+
FlagdProviderConfig config;
58+
const std::string host = "host";
59+
const int port = 1234;
60+
61+
config.SetHost(host).SetPort(port);
62+
EXPECT_EQ(config.GetEffectiveTargetUri(), absl::StrCat(host, ":", port));
63+
64+
const std::string socket_path = "/tmp/flagd.sock";
65+
config.SetSocketPath(socket_path);
66+
EXPECT_EQ(config.GetEffectiveTargetUri(),
67+
absl::StrCat("unix://", socket_path));
68+
69+
const std::string target_uri = "grpc://custom:5000";
70+
config.SetTargetUri(target_uri);
71+
EXPECT_EQ(config.GetEffectiveTargetUri(), target_uri);
72+
}
73+
74+
TEST_F(ConfigurationTest, GetEffectiveCredentialsInsecure) {
75+
FlagdProviderConfig config;
76+
config.SetTls(false);
77+
absl::StatusOr<std::shared_ptr<grpc::ChannelCredentials>> creds =
78+
config.GetEffectiveCredentials();
79+
ASSERT_TRUE(creds.ok());
80+
EXPECT_NE(*creds, nullptr);
81+
}
82+
83+
TEST_F(ConfigurationTest, GetEffectiveCredentialsTlsNoCert) {
84+
FlagdProviderConfig config;
85+
config.SetTls(true);
86+
absl::StatusOr<std::shared_ptr<grpc::ChannelCredentials>> creds =
87+
config.GetEffectiveCredentials();
88+
ASSERT_TRUE(creds.ok());
89+
EXPECT_NE(*creds, nullptr);
90+
}
91+
92+
TEST_F(ConfigurationTest, GetEffectiveCredentialsExplicit) {
93+
FlagdProviderConfig config;
94+
std::shared_ptr<grpc::ChannelCredentials> my_creds =
95+
grpc::InsecureChannelCredentials();
96+
config.SetChannelCredentials(my_creds);
97+
absl::StatusOr<std::shared_ptr<grpc::ChannelCredentials>> creds =
98+
config.GetEffectiveCredentials();
99+
ASSERT_TRUE(creds.ok());
100+
EXPECT_EQ(*creds, my_creds);
101+
}
102+
103+
} // namespace flagd

0 commit comments

Comments
 (0)