Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/program_options.hpp>
#include <iostream>
#include <map>
#include <fstream>

#include <yaml-cpp/yaml.h>

Expand Down Expand Up @@ -132,10 +133,15 @@ int MetadataAgentConfiguration::ParseArguments(int ac, char** av) {
}

void MetadataAgentConfiguration::ParseConfigFile(const std::string& filename) {
std::lock_guard<std::mutex> lock(mutex_);
if (filename.empty()) return;

YAML::Node config = YAML::LoadFile(filename);
std::ifstream input(filename);
ParseConfiguration(input);
}

void MetadataAgentConfiguration::ParseConfiguration(std::istream& input) {
YAML::Node config = YAML::Load(input);
std::lock_guard<std::mutex> lock(mutex_);
project_id_ =
config["ProjectId"].as<std::string>(kDefaultProjectId);
credentials_file_ =
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ class MetadataAgentConfiguration {
}

private:
friend class MetadataAgentConfigurationTest;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a blank line after this to visually separate it from the functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


void ParseConfigFile(const std::string& filename);
void ParseConfiguration(std::istream& input);

mutable std::mutex mutex_;
std::string project_id_;
Expand Down
15 changes: 12 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ GTEST_SRCS_=$(GTEST_SOURCEDIR)/*.cc $(GTEST_SOURCEDIR)/*.h $(GTEST_HEADERS)
# TODO: Factor out the common variables.
CPP_NETLIB_DIR=$(LIBDIR)/cpp-netlib
YAML_CPP_DIR=$(LIBDIR)/yaml-cpp
YAML_CPP_LIBDIR=$(YAML_CPP_DIR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really factor these into a common include file, but I am not asking you to do it now -- just musing.


CPPFLAGS+=-isystem $(GTEST_DIR)/include
YAML_CPP_LIBS=$(YAML_CPP_LIBDIR)/libyaml-cpp.a

CPPFLAGS+=-isystem $(GTEST_DIR)/include -I$(YAML_CPP_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread
LDLIBS=-lpthread
LDLIBS=-lpthread -lboost_program_options -lyaml-cpp
LDFLAGS=-L$(YAML_CPP_LIBDIR)

# Where to find code under test.
SRC_DIR=../src
Expand All @@ -22,6 +26,7 @@ TEST_SOURCES=$(wildcard $(TEST_DIR)/*_unittest.cc)
TEST_OBJS=$(TEST_SOURCES:$(TEST_DIR)/%.cc=%.o)
TESTS=\
base64_unittest \
configuration_unittest \
format_unittest \
time_unittest

Expand All @@ -48,6 +53,9 @@ init-submodules:
git submodule update --init $(GTEST_MODULE)
touch init-submodules

$(YAML_CPP_LIBS):
cd $(SRC_DIR) && $(MAKE) $@

$(SRC_DIR)/%.o: $(SRC_DIR)/%.cc
cd $(SRC_DIR) && $(MAKE) $(@:$(SRC_DIR)/%=%)

Expand All @@ -66,7 +74,8 @@ format_unittest: $(GTEST_LIB) format_unittest.o $(SRC_DIR)/format.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
base64_unittest: $(GTEST_LIB) base64_unittest.o $(SRC_DIR)/base64.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

configuration_unittest: $(GTEST_LIB) $(YAML_CPP_LIBS) configuration_unittest.o $(SRC_DIR)/configuration.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
time_unittest: $(GTEST_LIB) time_unittest.o $(SRC_DIR)/time.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

Expand Down
88 changes: 88 additions & 0 deletions test/configuration_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "../src/configuration.h"
#include "gtest/gtest.h"

namespace google {

class MetadataAgentConfigurationTest : public ::testing::Test {
protected:
void ParseConfiguration(const std::string& input) {
std::stringstream stream(input);
config.ParseConfiguration(stream);
}

void VerifyDefaultConfig() const {
EXPECT_EQ("", config.ProjectId());
EXPECT_EQ("", config.CredentialsFile());
EXPECT_EQ(3, config.MetadataApiNumThreads());
EXPECT_EQ(8000, config.MetadataApiPort());
EXPECT_EQ(".", config.MetadataApiResourceTypeSeparator());
EXPECT_EQ(60, config.MetadataReporterIntervalSeconds());
EXPECT_EQ(false, config.MetadataReporterPurgeDeleted());
EXPECT_EQ("https://stackdriver.googleapis.com/"
"v1beta2/projects/{{project_id}}/resourceMetadata:batchUpdate",
config.MetadataIngestionEndpointFormat());
EXPECT_EQ(8*1024*1024, config.MetadataIngestionRequestSizeLimitBytes());
EXPECT_EQ("0.1", config.MetadataIngestionRawContentVersion());
EXPECT_EQ(60*60, config.InstanceUpdaterIntervalSeconds());
EXPECT_EQ("", config.InstanceResourceType());
EXPECT_EQ(60, config.DockerUpdaterIntervalSeconds());
EXPECT_EQ("unix://%2Fvar%2Frun%2Fdocker.sock/",
config.DockerEndpointHost());
EXPECT_EQ("1.23", config.DockerApiVersion());
EXPECT_EQ("limit=30", config.DockerContainerFilter());
EXPECT_EQ(0, config.KubernetesUpdaterIntervalSeconds());
EXPECT_EQ("https://kubernetes.default.svc",
config.KubernetesEndpointHost());
EXPECT_EQ("", config.KubernetesPodLabelSelector());
EXPECT_EQ("", config.KubernetesClusterName());
EXPECT_EQ("", config.KubernetesClusterLocation());
EXPECT_EQ("", config.KubernetesNodeName());
EXPECT_EQ(true, config.KubernetesUseWatch());
EXPECT_EQ("", config.InstanceId());
EXPECT_EQ("", config.InstanceZone());
}

MetadataAgentConfiguration config;
};

TEST_F(MetadataAgentConfigurationTest, NoConfig) {
VerifyDefaultConfig();
}

TEST_F(MetadataAgentConfigurationTest, EmptyConfig) {
ParseConfiguration("");
VerifyDefaultConfig();
}

TEST_F(MetadataAgentConfigurationTest, PopulatedConfig) {
ParseConfiguration(
"ProjectId: TestProjectId\n"
"MetadataApiNumThreads: 13\n"
"MetadataReporterPurgeDeleted: true"
);
EXPECT_EQ("TestProjectId", config.ProjectId());
EXPECT_EQ(13, config.MetadataApiNumThreads());
EXPECT_EQ(true, config.MetadataReporterPurgeDeleted());
}

TEST_F(MetadataAgentConfigurationTest, CommentSkipped) {
ParseConfiguration(
"ProjectId: TestProjectId\n"
"#MetadataApiNumThreads: 13\n"
"MetadataReporterPurgeDeleted: true"
);
EXPECT_EQ(3, config.MetadataApiNumThreads());
}

TEST_F(MetadataAgentConfigurationTest, BlankLine) {
ParseConfiguration(
"ProjectId: TestProjectId\n"
"\n"
"\n"
"MetadataReporterPurgeDeleted: true"
);
EXPECT_EQ("TestProjectId", config.ProjectId());
EXPECT_EQ(true, config.MetadataReporterPurgeDeleted());
}

} // namespace google