Unittests for InstanceMonitoredResource and InstanceResourceMetadata#105
Unittests for InstanceMonitoredResource and InstanceResourceMetadata#105
Conversation
| void StopUpdater(); | ||
|
|
||
| private: | ||
| friend class InstanceTest; |
There was a problem hiding this comment.
Let's add a blank line between this and the first function.
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
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.
Just in-lined the MonitoredResource call and changed mr_actual to mr.
There was a problem hiding this comment.
You might as well inline the call being tested, then, and get rid of the local.
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
All of these can be static.
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
There was a problem hiding this comment.
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.
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
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.
I don't see a const reference being returned. Also in GetResourceIds.
| } | ||
|
|
||
| TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) { | ||
| Environment env(*(new Configuration())); |
There was a problem hiding this comment.
This leaks, please don't do that. Just say:
Configuration config;
Environment env(config);| "InstanceZone: us-east1-b\n" | ||
| )); | ||
| InstanceReader reader(config); | ||
| const auto& result = reader.MetadataQuery(); |
There was a problem hiding this comment.
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();| "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.
const std::vector<std:string> ids_expected{"", "1234567891011};There was a problem hiding this comment.
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.
Done. Sorry I missed the rename. Also inline auto-convert doesn't work actually.
There was a problem hiding this comment.
Auto-convert doesn't, but inlining should.
| 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.
result[0] should also work, and is clearer.
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
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.
+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.
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.
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
+1. Totally missed that. Let's remove this test for now and add it when we have mocks.
| void StopUpdater(); | ||
|
|
||
| private: | ||
| friend class InstanceTest; |
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
|
|
||
| class InstanceTest : public ::testing::Test { | ||
| protected: | ||
| const MetadataStore::Metadata&& GetResourceMetadata( |
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
Just in-lined the MonitoredResource call and changed mr_actual to mr.
| } | ||
|
|
||
| TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) { | ||
| Environment env(*(new Configuration())); |
| "InstanceZone: us-east1-b\n" | ||
| )); | ||
| InstanceReader reader(config); | ||
| const auto& result = reader.MetadataQuery(); |
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
| 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); |
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( | ||
| "gce_instance", {{"instance_id", ""}, {"zone", ""}}); |
There was a problem hiding this comment.
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.
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
There was a problem hiding this comment.
I don't see a const reference being returned. Also in GetResourceIds.
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
There was a problem hiding this comment.
You might as well inline the call being tested, then, and get rid of the local.
| "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.
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));| 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.
Let's do a 2-space indent here.
| {"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.
Eh? Just say EXPECT_TRUE(GetResourceMetadata(rm).ignore)...
| {"zone", "us-east1-b"} | ||
| }), GetMonitoredResource(rm)); | ||
| EXPECT_EQ(ids_exp, GetResourceIds(rm)); | ||
| EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore, |
| EXPECT_EQ(1, result.size()); | ||
| const MetadataUpdater::ResourceMetadata& rm = result[0]; | ||
| EXPECT_EQ(MonitoredResource("gce_instance", { | ||
| {"instance_id", "1234567891011"}, |
| return std::move(rm->metadata); | ||
| } | ||
|
|
||
| MonitoredResource GetMonitoredResource( |
| "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.
Done. Sorry I missed the rename. Also inline auto-convert doesn't work actually.
| std::move(other.metadata_)) {} | ||
|
|
||
| const MetadataStore::Metadata& metadata() const { | ||
| return metadata_; |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
How about adding a comment like // Needs write access to metadata_.?
There was a problem hiding this comment.
Let's make this a line comment instead (on the same line as the friend declaration).
| )); | ||
| InstanceReader reader(config); | ||
| const auto result = reader.MetadataQuery(); | ||
| const std::vector<std::string> ids_expected({"", "1234567891011"}); |
There was a problem hiding this comment.
This should be right next to the EXPECT_EQ if you're not inlining it.
| "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.
Auto-convert doesn't, but inlining should.
| std::move(other.metadata_)) {} | ||
|
|
||
| const MetadataStore::Metadata& metadata() const { | ||
| return metadata_; |
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
| )); | ||
| InstanceReader reader(config); | ||
| const auto result = reader.MetadataQuery(); | ||
| const std::vector<std::string> ids_expected({"", "1234567891011"}); |
| Environment env(config); | ||
| MonitoredResource mr_actual = | ||
| InstanceReader::InstanceResource(env); | ||
| MonitoredResource mr_expected( |
| "gce_instance", | ||
| {{"instance_id", "1234567891011"}, {"zone", "us-east1-b"}}); | ||
| std::string vinit[] = {"", "1234567891011"}; | ||
| std::vector<std::string> ids_exp = {vinit, std::end(vinit)}; |
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
There was a problem hiding this comment.
Let's make this a line comment instead (on the same line as the friend declaration).
| 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.
Should fit on one line.
| } | ||
|
|
||
| private: | ||
| friend class MetadataUpdater; |
| void UpdateMetadataCallback(ResourceMetadata&& result) { | ||
| store_->UpdateMetadata(result.resource, std::move(result.metadata)); | ||
| store_->UpdateMetadata(result.resource_, | ||
| std::move(result.metadata_)); |
5916675 to
03b2733
Compare
|
Rebased. |
No description provided.