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

Unittests for InstanceMonitoredResource and InstanceResourceMetadata#105

Merged
dhrupadb merged 10 commits intomasterfrom
dhrupadb-instance-metadata-query-unittests
Mar 30, 2018
Merged

Unittests for InstanceMonitoredResource and InstanceResourceMetadata#105
dhrupadb merged 10 commits intomasterfrom
dhrupadb-instance-metadata-query-unittests

Conversation

@dhrupadb
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/updater.h
void StopUpdater();

private:
friend class InstanceTest;
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated

class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated

class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
}

TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) {
Environment env(*(new Configuration()));
Copy link
Copy Markdown
Contributor

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
"InstanceZone: us-east1-b\n"
));
InstanceReader reader(config);
const auto& result = reader.MetadataQuery();
Copy link
Copy Markdown
Contributor

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();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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)};
Copy link
Copy Markdown
Contributor

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};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

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));

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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);
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Comment thread test/instance_unittest.cc Outdated
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

@dhrupadb dhrupadb left a comment

Choose a reason for hiding this comment

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

PTAL

Comment thread src/updater.h
void StopUpdater();

private:
friend class InstanceTest;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated

class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated

class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
Copy Markdown
Contributor Author

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.

Comment thread test/instance_unittest.cc Outdated
}

TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) {
Environment env(*(new Configuration()));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
"InstanceZone: us-east1-b\n"
));
InstanceReader reader(config);
const auto& result = reader.MetadataQuery();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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)};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
Copy link
Copy Markdown
Contributor Author

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.

Comment thread test/instance_unittest.cc Outdated
return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
Copy Markdown
Contributor

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.

Comment thread test/instance_unittest.cc Outdated
Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
Copy Markdown
Contributor

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.

Comment thread 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)};
Copy link
Copy Markdown
Contributor

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));

Comment thread test/instance_unittest.cc Outdated
EXPECT_EQ(1, result.size());
const MetadataUpdater::ResourceMetadata& rm = result[0];
EXPECT_EQ(MonitoredResource("gce_instance", {
{"instance_id", "1234567891011"},
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
{"zone", "us-east1-b"}
}), GetMonitoredResource(rm));
EXPECT_EQ(ids_exp, GetResourceIds(rm));
EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore,
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor Author

@dhrupadb dhrupadb left a comment

Choose a reason for hiding this comment

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

PTAL

Comment thread test/instance_unittest.cc Outdated
{"zone", "us-east1-b"}
}), GetMonitoredResource(rm));
EXPECT_EQ(ids_exp, GetResourceIds(rm));
EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
EXPECT_EQ(1, result.size());
const MetadataUpdater::ResourceMetadata& rm = result[0];
EXPECT_EQ(MonitoredResource("gce_instance", {
{"instance_id", "1234567891011"},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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)};
Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/updater.h Outdated
std::move(other.metadata_)) {}

const MetadataStore::Metadata& metadata() const {
return metadata_;
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. lol.

Comment thread src/updater.h Outdated
}

private:
friend class MetadataUpdater;
Copy link
Copy Markdown
Contributor

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_.?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
));
InstanceReader reader(config);
const auto result = reader.MetadataQuery();
const std::vector<std::string> ids_expected({"", "1234567891011"});
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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)};
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

@dhrupadb dhrupadb left a comment

Choose a reason for hiding this comment

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

PTAL

Comment thread src/updater.h Outdated
std::move(other.metadata_)) {}

const MetadataStore::Metadata& metadata() const {
return metadata_;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. lol.

Comment thread src/updater.h Outdated
}

private:
friend class MetadataUpdater;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
));
InstanceReader reader(config);
const auto result = reader.MetadataQuery();
const std::vector<std::string> ids_expected({"", "1234567891011"});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread test/instance_unittest.cc Outdated
Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread 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)};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/updater.h Outdated
}

private:
friend class MetadataUpdater;
Copy link
Copy Markdown
Contributor

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

Comment thread src/updater.h Outdated
void UpdateMetadataCallback(ResourceMetadata&& result) {
store_->UpdateMetadata(result.resource, std::move(result.metadata));
store_->UpdateMetadata(result.resource_,
std::move(result.metadata_));
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor Author

@dhrupadb dhrupadb left a comment

Choose a reason for hiding this comment

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

PTAL

Comment thread src/updater.h Outdated
}

private:
friend class MetadataUpdater;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/updater.h Outdated
void UpdateMetadataCallback(ResourceMetadata&& result) {
store_->UpdateMetadata(result.resource, std::move(result.metadata));
store_->UpdateMetadata(result.resource_,
std::move(result.metadata_));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

@ACEmilG ACEmilG left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
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:

@dhrupadb dhrupadb force-pushed the dhrupadb-instance-metadata-query-unittests branch from 5916675 to 03b2733 Compare March 30, 2018 19:50
@dhrupadb
Copy link
Copy Markdown
Contributor Author

Rebased.

@dhrupadb dhrupadb merged commit 7438d9e into master Mar 30, 2018
@dhrupadb dhrupadb deleted the dhrupadb-instance-metadata-query-unittests branch March 30, 2018 19:52
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