-
Notifications
You must be signed in to change notification settings - Fork 11
Unittests for InstanceMonitoredResource and InstanceResourceMetadata #105
Conversation
| void StopUpdater(); | ||
|
|
||
| private: | ||
| friend class InstanceTest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a blank line between this and the first function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just call this mr and inline the call to InstanceReader::InstanceResource(env) into the EXPECT_EQ. Also below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in-lined the MonitoredResource call and changed mr_actual to mr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might as well inline the call being tested, then, and get rid of the local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these can be static.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
const MetadataStore::Metadata& GetMetadata(
const MetadataUpdater::ResourceMetadata& rm) {
return rm->metadata;
}? You don't actually need the ownership, you just want to get a reference to compare with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here: the ResourceMetadata object already owns that value — just get a const reference to look at (and avoid a copy):
const MonitoredResource& GetMonitoredResource(
const MetadataUpdater::ResourceMetadata& rm) {
return rm->resource;
}Also in GetResourceIds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a const reference being returned. Also in GetResourceIds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| } | ||
|
|
||
| TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) { | ||
| Environment env(*(new Configuration())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leaks, please don't do that. Just say:
Configuration config;
Environment env(config);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "InstanceZone: us-east1-b\n" | ||
| )); | ||
| InstanceReader reader(config); | ||
| const auto& result = reader.MetadataQuery(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, and here you have to go the other way and copy, otherwise you are storing a reference to a temporary. So:
const auto result = reader.MetadataQuery();There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const std::vector<std:string> ids_expected{"", "1234567891011};There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parentheses were optional, BTW.
I also suggested renaming it to ids_expected, but now that you've inlined the monitored resource, might as well inline this too:
EXPECT_EQ(std::vector<std::string>{"", "1234567891011"}, GetResourceIds(rm));Don't have an easy way to test this now, but it might even auto-convert to a vector:
EXPECT_EQ({"", "1234567891011"}, GetResourceIds(rm));There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry I missed the rename. Also inline auto-convert doesn't work actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auto-convert doesn't, but inlining should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; | ||
| EXPECT_EQ(1, result.size()); | ||
| const MetadataUpdater::ResourceMetadata& rm = result.at(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result[0] should also work, and is clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused on this expectation. Reading the code, I think this will actually make an HTTP call to the metadata server. Is it possible to execute this test without mocks set up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. Totally missed that. Let's remove this test for now and add it when we have mocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this error out then though? The fact that there's a default gce_instance in the config that this falls over to, that should be behavior we test. I intended this test originally as one that caught an error when we didn't get a valid response from the API.
test/instance_unittest.cc
Outdated
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. Totally missed that. Let's remove this test for now and add it when we have mocks.
dhrupadb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
| void StopUpdater(); | ||
|
|
||
| private: | ||
| friend class InstanceTest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in-lined the MonitoredResource call and changed mr_actual to mr.
test/instance_unittest.cc
Outdated
| } | ||
|
|
||
| TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) { | ||
| Environment env(*(new Configuration())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "InstanceZone: us-east1-b\n" | ||
| )); | ||
| InstanceReader reader(config); | ||
| const auto& result = reader.MetadataQuery(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; | ||
| EXPECT_EQ(1, result.size()); | ||
| const MetadataUpdater::ResourceMetadata& rm = result.at(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this error out then though? The fact that there's a default gce_instance in the config that this falls over to, that should be behavior we test. I intended this test originally as one that caught an error when we didn't get a valid response from the API.
test/instance_unittest.cc
Outdated
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a const reference being returned. Also in GetResourceIds.
test/instance_unittest.cc
Outdated
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might as well inline the call being tested, then, and get rid of the local.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parentheses were optional, BTW.
I also suggested renaming it to ids_expected, but now that you've inlined the monitored resource, might as well inline this too:
EXPECT_EQ(std::vector<std::string>{"", "1234567891011"}, GetResourceIds(rm));Don't have an easy way to test this now, but it might even auto-convert to a vector:
EXPECT_EQ({"", "1234567891011"}, GetResourceIds(rm));
test/instance_unittest.cc
Outdated
| EXPECT_EQ(1, result.size()); | ||
| const MetadataUpdater::ResourceMetadata& rm = result[0]; | ||
| EXPECT_EQ(MonitoredResource("gce_instance", { | ||
| {"instance_id", "1234567891011"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do a 2-space indent here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| {"zone", "us-east1-b"} | ||
| }), GetMonitoredResource(rm)); | ||
| EXPECT_EQ(ids_exp, GetResourceIds(rm)); | ||
| EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh? Just say EXPECT_TRUE(GetResourceMetadata(rm).ignore)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
dhrupadb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
test/instance_unittest.cc
Outdated
| {"zone", "us-east1-b"} | ||
| }), GetMonitoredResource(rm)); | ||
| EXPECT_EQ(ids_exp, GetResourceIds(rm)); | ||
| EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| EXPECT_EQ(1, result.size()); | ||
| const MetadataUpdater::ResourceMetadata& rm = result[0]; | ||
| EXPECT_EQ(MonitoredResource("gce_instance", { | ||
| {"instance_id", "1234567891011"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry I missed the rename. Also inline auto-convert doesn't work actually.
src/updater.h
Outdated
| std::move(other.metadata_)) {} | ||
|
|
||
| const MetadataStore::Metadata& metadata() const { | ||
| return metadata_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to put the whole function on one line, saving some vertical space. These are accessors, they don't deserve it. 😄 No need to put blank lines between them either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. lol.
src/updater.h
Outdated
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding a comment like // Needs write access to metadata_.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a line comment instead (on the same line as the friend declaration).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| )); | ||
| InstanceReader reader(config); | ||
| const auto result = reader.MetadataQuery(); | ||
| const std::vector<std::string> ids_expected({"", "1234567891011"}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be right next to the EXPECT_EQ if you're not inlining it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auto-convert doesn't, but inlining should.
dhrupadb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
src/updater.h
Outdated
| std::move(other.metadata_)) {} | ||
|
|
||
| const MetadataStore::Metadata& metadata() const { | ||
| return metadata_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. lol.
src/updater.h
Outdated
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| )); | ||
| InstanceReader reader(config); | ||
| const auto result = reader.MetadataQuery(); | ||
| const std::vector<std::string> ids_expected({"", "1234567891011"}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/instance_unittest.cc
Outdated
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/updater.h
Outdated
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a line comment instead (on the same line as the friend declaration).
src/updater.h
Outdated
| void UpdateMetadataCallback(ResourceMetadata&& result) { | ||
| store_->UpdateMetadata(result.resource, std::move(result.metadata)); | ||
| store_->UpdateMetadata(result.resource_, | ||
| std::move(result.metadata_)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fit on one line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
dhrupadb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PTAL
src/updater.h
Outdated
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/updater.h
Outdated
| void UpdateMetadataCallback(ResourceMetadata&& result) { | ||
| store_->UpdateMetadata(result.resource, std::move(result.metadata)); | ||
| store_->UpdateMetadata(result.resource_, | ||
| std::move(result.metadata_)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
ACEmilG
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ![]()
5916675 to
03b2733
Compare
|
Rebased. |
No description provided.