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

Conversation

@dhrupadb
Copy link
Contributor

No description provided.

void StopUpdater();

private:
friend class InstanceTest;
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 add a blank line between this and the first 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.

Done.

Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

Done.


class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Done.


class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
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:

  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
Contributor Author

Choose a reason for hiding this comment

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

Done.

return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) {
Environment env(*(new Configuration()));
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

"InstanceZone: us-east1-b\n"
));
InstanceReader reader(config);
const auto& result = reader.MetadataQuery();
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

"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
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
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
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

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
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
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 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
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
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.

MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
Copy link
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
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

void StopUpdater();

private:
friend class InstanceTest;
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.


class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
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.


class InstanceTest : public ::testing::Test {
protected:
const MetadataStore::Metadata&& GetResourceMetadata(
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.

return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
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.

Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
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.

}

TEST_F(InstanceTest, GetInstanceMonitoredResourceWithEmptyConfig) {
Environment env(*(new Configuration()));
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.

"InstanceZone: us-east1-b\n"
));
InstanceReader reader(config);
const auto& result = reader.MetadataQuery();
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.

"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
Contributor Author

Choose a reason for hiding this comment

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

Done.

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
Contributor Author

Choose a reason for hiding this comment

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

Done.

MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
"gce_instance", {{"instance_id", ""}, {"zone", ""}});
Copy link
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.

return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
Copy link
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.

Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
Copy link
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.

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

EXPECT_EQ(1, result.size());
const MetadataUpdater::ResourceMetadata& rm = result[0];
EXPECT_EQ(MonitoredResource("gce_instance", {
{"instance_id", "1234567891011"},
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 do a 2-space indent here.

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.

{"zone", "us-east1-b"}
}), GetMonitoredResource(rm));
EXPECT_EQ(ids_exp, GetResourceIds(rm));
EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore,
Copy link
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
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
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

{"zone", "us-east1-b"}
}), GetMonitoredResource(rm));
EXPECT_EQ(ids_exp, GetResourceIds(rm));
EXPECT_EQ(MetadataStore::Metadata::IGNORED().ignore,
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(1, result.size());
const MetadataUpdater::ResourceMetadata& rm = result[0];
EXPECT_EQ(MonitoredResource("gce_instance", {
{"instance_id", "1234567891011"},
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.

return std::move(rm->metadata);
}

MonitoredResource GetMonitoredResource(
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.

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

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

const MetadataStore::Metadata& metadata() const {
return metadata_;
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 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
Contributor Author

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;
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 adding a comment like // Needs write access to metadata_.?

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.

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 make this a line comment instead (on the same line as the friend declaration).

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.

));
InstanceReader reader(config);
const auto result = reader.MetadataQuery();
const std::vector<std::string> ids_expected({"", "1234567891011"});
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

"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
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
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

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

const MetadataStore::Metadata& metadata() const {
return metadata_;
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. lol.

src/updater.h Outdated
}

private:
friend class MetadataUpdater;
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.

));
InstanceReader reader(config);
const auto result = reader.MetadataQuery();
const std::vector<std::string> ids_expected({"", "1234567891011"});
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.

Environment env(config);
MonitoredResource mr_actual =
InstanceReader::InstanceResource(env);
MonitoredResource mr_expected(
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.

"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
Contributor Author

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;
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 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_));
Copy link
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
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
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

src/updater.h Outdated
}

private:
friend class MetadataUpdater;
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.

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