Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 92 additions & 1 deletion test/environment_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,51 @@ TEST(TemporaryFile, Basic) {
EXPECT_FALSE(boost::filesystem::exists(path));
}

//
// Tests for values that can be set in configuration.
//
TEST_F(EnvironmentTest, ValuesFromConfig) {
Configuration config(std::istringstream(
"InstanceId: some-instance-id\n"
"InstanceResourceType: some-instance-resource-type\n"
"InstanceZone: some-instance-zone\n"
"KubernetesClusterLocation: some-kubernetes-cluster-location\n"
"KubernetesClusterName: some-kubernetes-cluster-name\n"
));
Environment environment(config);
EXPECT_EQ("some-instance-id", environment.InstanceId());
EXPECT_EQ("some-instance-resource-type", environment.InstanceResourceType());
EXPECT_EQ("some-instance-zone", environment.InstanceZone());
EXPECT_EQ("some-kubernetes-cluster-location",
environment.KubernetesClusterLocation());
EXPECT_EQ("some-kubernetes-cluster-name",
environment.KubernetesClusterName());
}

TEST_F(EnvironmentTest, NumericProjectIdFromConfigNewStyleCredentials) {
TemporaryFile credentials_file(
std::string(test_info_->name()) + "_creds.json",
"{\"client_email\":\"user@12345.iam.gserviceaccount.com\","
"\"private_key\":\"some_key\"}");
Configuration config(std::istringstream(
"CredentialsFile: '" + credentials_file.FullPath().native() + "'\n"
));
Environment environment(config);
EXPECT_EQ("12345", environment.NumericProjectId());
}

TEST_F(EnvironmentTest, NumericProjectIdFromConfigOldStyleCredentials) {
TemporaryFile credentials_file(
std::string(test_info_->name()) + "_creds.json",
"{\"client_email\":\"12345-hash@developer.gserviceaccount.com\","
"\"private_key\":\"some_key\"}");
Configuration config(std::istringstream(
"CredentialsFile: '" + credentials_file.FullPath().native() + "'\n"
));
Environment environment(config);
EXPECT_EQ("12345", environment.NumericProjectId());
}

TEST_F(EnvironmentTest, ReadApplicationDefaultCredentialsSucceeds) {
TemporaryFile credentials_file(
std::string(test_info_->name()) + "_creds.json",
Expand Down Expand Up @@ -106,7 +151,10 @@ TEST_F(EnvironmentTest, ReadApplicationDefaultCredentialsCaches) {
EXPECT_EQ("some_key", environment.CredentialsPrivateKey());
}

TEST_F(EnvironmentTest, GetMetadataString) {
//
// Tests for values that can be read from metadata server.
//
TEST_F(EnvironmentTest, GetMetadataStringWithFakeServer) {
testing::FakeServer server;
server.SetResponse("/a/b/c", "hello");

Expand All @@ -118,4 +166,47 @@ TEST_F(EnvironmentTest, GetMetadataString) {
EXPECT_EQ("", environment.GetMetadataString("unknown/path"));
}

TEST_F(EnvironmentTest, ValuesFromMetadataServer) {
testing::FakeServer server;
server.SetResponse("/instance/attributes/cluster-location",
"some-cluster-location");
server.SetResponse("/instance/attributes/cluster-name", "some-cluster-name");
server.SetResponse("/instance/id", "some-instance-id");
server.SetResponse("/instance/zone",
"projects/some-project/zones/some-instance-zone");
server.SetResponse("/project/numeric-project-id", "12345");

Configuration config;
Environment environment(config);
SetMetadataServerUrlForTest(&environment, server.GetUrl());

EXPECT_EQ("some-cluster-location", environment.KubernetesClusterLocation());
EXPECT_EQ("some-cluster-name", environment.KubernetesClusterName());
EXPECT_EQ("some-instance-id", environment.InstanceId());
EXPECT_EQ("some-instance-zone", environment.InstanceZone());
EXPECT_EQ("12345", environment.NumericProjectId());
}

TEST_F(EnvironmentTest, KubernetesClusterLocationFromMetadataServerKubeEnv) {
testing::FakeServer server;
server.SetResponse("/instance/attributes/kube-env",
"KEY: value\n"
"ZONE: some-kube-env-zone\n");

Configuration config;
Environment environment(config);
SetMetadataServerUrlForTest(&environment, server.GetUrl());

EXPECT_EQ("some-kube-env-zone", environment.KubernetesClusterLocation());
}

//
// Tests for values with hardcoded defaults.
//
TEST_F(EnvironmentTest, InstanceResourceTypeDefault) {
Configuration config;
Environment environment(config);
EXPECT_EQ("gce_instance", environment.InstanceResourceType());
}

} // namespace google