Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.

Conversation

@ACEmilG
Copy link
Contributor

@ACEmilG ACEmilG commented Mar 14, 2018

No description provided.

Copy link
Contributor

@supriyagarg supriyagarg left a comment

Choose a reason for hiding this comment

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

Minor comment.

TestDefaultConfig(config);
}

TEST(ConfigurationTest, SpecificTest) {
Copy link
Contributor

@supriyagarg supriyagarg Mar 14, 2018

Choose a reason for hiding this comment

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

Rename SpecificTest to PopulatedYamlTest or similar for parity with the previous name.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or PopulatedConfigFileTest...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed tests to "NoConfigTest", "EmptyConfigTest", "PopulatedConfigTest"

MetadataAgentConfiguration();
int ParseArguments(int ac, char** av);
void ParseConfigFile(const std::string& filename);
void ParseYamlConfig(YAML::Node config);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to not expose the implementation details in the header, but instead define

  void ParseConfiguration(std::istream& input);

and use YAML::Load() instead of YAML::LoadFile() in the implementation of ParseConfigFile. Then you can just use std::stringstream in the test.

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

}

private:
void ParseConfigFile(const std::string& filename);
Copy link
Contributor

Choose a reason for hiding this comment

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

Keep this as private and add the test class as a friend instead. I think there's even a FRIEND_TEST macro...

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.

test/Makefile Outdated
CPP_NETLIB_DIR=$(LIBDIR)/cpp-netlib
YAML_CPP_DIR=$(LIBDIR)/yaml-cpp
YAML_CPP_LIBDIR=$(YAML_CPP_DIR)
SUBMODULE_DIRS=$(CPP_NETLIB_DIR) $(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.

Is this used anywhere? If not, let's remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

# 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.

test/Makefile Outdated
-DMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ \
-DYAML_CPP_BUILD_TOOLS=OFF

$(YAML_CPP_LIBS): build-yaml-cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of copying all of this make cruft from src/Makefile here, why not delegate:

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much better, changed.

TestDefaultConfig(config);
}

TEST(ConfigurationTest, BlankYamlTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about BlankConfigFileTest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified test names, see below

TestDefaultConfig(config);
}

TEST(ConfigurationTest, SpecificTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Or PopulatedConfigFileTest...


TEST(ConfigurationTest, SpecificTest) {
YAML::Node node = YAML::Load(
"ProjectId: TestProjectId\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

You should also test that comments are skipped appropriately, possibly in another test case...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added


namespace {

void TestDefaultConfig(google::MetadataAgentConfiguration& config) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not too happy about this, as it'll need to be updated when defaults change, but I don't have any better ideas for now, so let's keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed

EXPECT_EQ(false, config.MetadataReporterPurgeDeleted());
EXPECT_EQ(
"https://stackdriver.googleapis.com/"
"v1beta2/projects/{{project_id}}/resourceMetadata:batchUpdate",
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 eventually add a CheckConfiguration function that verifies, e.g., that these format strings have the appropriate substitution parameters, etc. Not asking you to do it now, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, left for later

std::lock_guard<std::mutex> lock(mutex_);
if (filename.empty()) return;
std::ifstream ifs;
ifs.open (filename, std::ifstream::in);
Copy link
Contributor

Choose a reason for hiding this comment

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

How about combining this with construction, i.e.:

std::ifstream input(filename);
ParseConfiguration(input);

? BTW, in is the default mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much better, thanks.

private:
friend class MetadataAgentConfigurationTest;
void ParseConfigFile(const std::string& filename);

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep this blank line to separate functions from instance variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced.

test/Makefile Outdated
base64_unittest: $(GTEST_LIB) base64_unittest.o $(SRC_DIR)/base64.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

configuration_unittest: $(GTEST_LIB) configuration_unittest.o $(SRC_DIR)/configuration.o
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it's still relevant -- if you don't add that dependency, then make test in a fresh checkout will fail to link (because it won't rebuild $(YAML_CPP_LIBS)).

test/Makefile Outdated
$(YAML_CPP_LIBS):
cd $(SRC_DIR) && $(MAKE) $@

build-yaml-cpp: $(YAML_CPP_DIR)/Makefile
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you're delegating $(YAML_CPP_LIBS) to src/Makefile, the build-yaml-cpp and yaml-cpp targets are no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks.

static void SetUpTestCase() {}
static void TearDownTestCase() {}

void TestDefaultConfig(MetadataAgentConfiguration& config) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't depend on any state in the fixture and only calls public methods. Is there any reason to not keep it as a standalone top-level function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason - moved out of the class.


class MetadataAgentConfigurationTest : public ::testing::Test {
public:
void CallParseConfiguration(MetadataAgentConfiguration& config, std::istream& stream) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just ParseConfiguration is fine.

};

TEST_F(MetadataAgentConfigurationTest, NoConfigTest) {
MetadataAgentConfiguration config;
Copy link
Contributor

Choose a reason for hiding this comment

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

You start every test with this. Might as well make this a field in the fixture.

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.

EXPECT_EQ(true, config.MetadataReporterPurgeDeleted());
}

TEST_F(MetadataAgentConfigurationTest, CommentSkippedTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also test for blank lines...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a new test


class MetadataAgentConfigurationTest : public ::testing::Test {
public:
void CallParseConfiguration(MetadataAgentConfiguration& config, std::istream& stream) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As written, this can be static.


class MetadataAgentConfigurationTest : public ::testing::Test {
public:
void CallParseConfiguration(MetadataAgentConfiguration& config, std::istream& stream) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you always pass in a stringstream initialized from a string, and you have to call this function anyway to get access to the private method, why not make this method take a string (well, const std::string&) and do the stream stuff in this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed all.

if (filename.empty()) return;

YAML::Node config = YAML::LoadFile(filename);
std::ifstream ifs(filename);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's replace ifs with input -- it reads better, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done.

}

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.

test/Makefile Outdated
base64_unittest: $(GTEST_LIB) base64_unittest.o $(SRC_DIR)/base64.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

configuration_unittest: $(GTEST_LIB) configuration_unittest.o $(SRC_DIR)/configuration.o $(YAML_CPP_LIBS)
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to put $(YAML_CPP_LIBS) first, otherwise the build of configuration.o will fail because the includes won't find yaml-cpp/yaml.h.

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, thanks.

test/Makefile Outdated
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

.PHONY: all test clean purge
.PHONY: all test clean purge yaml-cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

You've removed the yaml-cpp target, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, removed this from phony list.

namespace google {

class MetadataAgentConfigurationTest : public ::testing::Test {
public:
Copy link
Contributor

Choose a reason for hiding this comment

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

protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

class MetadataAgentConfigurationTest : public ::testing::Test {
public:
MetadataAgentConfiguration config;
static void ParseConfiguration(MetadataAgentConfiguration& config, const std::string configString) {
Copy link
Contributor

Choose a reason for hiding this comment

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

const std::string&.

class MetadataAgentConfigurationTest : public ::testing::Test {
public:
MetadataAgentConfiguration config;
static void ParseConfiguration(MetadataAgentConfiguration& config, const std::string configString) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, now that you have an instance variable, you might as well remove the config parameter and let it refer to the config instance variable. Of course, this can then no longer be static.

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.

}
};

static void TestDefaultConfig(MetadataAgentConfiguration& config) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also didn't notice earlier: this can take a const reference. However, if you make it an instance function, you should instead make it a const function (i.e.,

void VerifyDefaultConfig() const {
  ...
}

).

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.

EXPECT_EQ(3, config.MetadataApiNumThreads());
}

TEST_F(MetadataAgentConfigurationTest, EmptyLineTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Empty/Blank/.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.


TEST_F(MetadataAgentConfigurationTest, EmptyLineTest) {
ParseConfiguration(config,
"ProjectId: TestProjectId\n\n\n\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

While this is technically correct, it's easier to visualize what's going on if you move each of the "\n" to its own line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, changed.

Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

Minor nits.


class MetadataAgentConfigurationTest : public ::testing::Test {
protected:
void ParseConfiguration(const std::string& configString) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The Google style is to use snake_case for variables and parameter names. But you could just name it input instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to input.

ParseConfiguration(
"ProjectId: TestProjectId\n"
"MetadataApiNumThreads: 13\n"
"MetadataReporterPurgeDeleted: true");
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip: if you put the closing parenthesis on the next line, you can add extra lines even after the last one without affecting git blame. If you choose to do this, apply it below as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Changed below and above.

Copy link
Contributor

Choose a reason for hiding this comment

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

This only applied to cases where the only argument is a multi-line string with implicit concatenation. Not the EXPECT_EQ calls above (which have two arguments each, and the strings aren't even multi-line). Let's revert those two...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted, sorry about that.

MetadataAgentConfiguration config;
};

TEST_F(MetadataAgentConfigurationTest, NoConfigTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: the Test suffix is redundant (in the test names in all TEST_F lines).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

One remaining issue.

ParseConfiguration(
"ProjectId: TestProjectId\n"
"MetadataApiNumThreads: 13\n"
"MetadataReporterPurgeDeleted: true");
Copy link
Contributor

Choose a reason for hiding this comment

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

This only applied to cases where the only argument is a multi-line string with implicit concatenation. Not the EXPECT_EQ calls above (which have two arguments each, and the strings aren't even multi-line). Let's revert those two...

Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

One last look unearthed a few more minor things in the Makefile...

test/Makefile Outdated
CPPFLAGS+=-isystem $(GTEST_DIR)/include
CXXFLAGS=-std=c++11 -g -pthread
LDLIBS=-lpthread
CXXFLAGS=-std=c++11 -g -pthread -I$(YAML_CPP_DIR)/include
Copy link
Contributor

Choose a reason for hiding this comment

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

The -I directive should go into CPPFLAGS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved.

test/Makefile Outdated

YAML_CPP_LIBS=$(YAML_CPP_LIBDIR)/libyaml-cpp.a

CMAKE=cmake
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

TESTS=\
base64_unittest \
format_unittest \
base64_unittest \
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a merge conflict. Let's have only one of these (in alphabetical order).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct - fixed.

Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@ACEmilG ACEmilG merged commit 6368a19 into master Mar 16, 2018
igorpeshansky pushed a commit that referenced this pull request Mar 16, 2018
@igorpeshansky
Copy link
Contributor

FYI, I've backed out the merge and instead squashed all of the commits from this PR into one.

@igorpeshansky igorpeshansky deleted the ACEmilG-configuration-unit-tests branch March 16, 2018 19:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants