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

Add test for GetNodeMetadata in kubernetes #102

Merged
merged 7 commits into from
Mar 30, 2018

Conversation

ACEmilG
Copy link
Contributor

@ACEmilG ACEmilG commented Mar 29, 2018

No description provided.

test/Makefile Outdated
@@ -63,6 +75,9 @@ init-submodules:
$(YAML_CPP_LIBS):
cd $(SRC_DIR) && $(MAKE) $@

$(CPP_NETLIB_LIBS):
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep these in the same order as src/Makefile (i.e., move this above the $(YAML_CPP_LIBS) target).

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
@@ -83,6 +98,12 @@ base64_unittest: $(GTEST_LIB) base64_unittest.o $(SRC_DIR)/base64.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
configuration_unittest: $(GTEST_LIB) $(YAML_CPP_LIBS) configuration_unittest.o $(SRC_DIR)/configuration.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
kubernetes_unittest: $(GTEST_LIB) kubernetes_unittest.o $(SRC_DIR)/kubernetes.o
$(SRC_DIR)/json.o $(SRC_DIR)/logging.o $(SRC_DIR)/environment.o
Copy link
Member

Choose a reason for hiding this comment

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

I think you meant these as a continuation of the dependencies, right?.. Let's just keep them all on one line for now, even if it runs over 80 characters.

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.

@@ -0,0 +1,70 @@
#include "../src/kubernetes.h"
#include "../src/updater.h"
#include "../src/configuration.h"
Copy link
Member

Choose a reason for hiding this comment

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

Let's alphabetize these.

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.

{"version", json::string("TestVersion")},
{"raw", json::object({
{"infrastructureResource",
InstanceReader::InstanceResource(environment).ToJSON()},
Copy link
Member

Choose a reason for hiding this comment

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

Just construct the instance resource JSON, rather than replicating the construction from the source. We can test this function separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean here - can you give some psudocode?

Copy link
Member

Choose a reason for hiding this comment

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

Better — I can give you code 😄:

          {"infrastructureResource", json::object({
            {"type", json::string("gce_instance")},
            {"labels", json::object({
              {"instance_id", json::string("TestID")},
              {"zone", json::string("TestZone")},
            })},
          })},

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. However, to me, this seems more like testing the InstanceReader::InstanceResource function to me; If that function changes, this test will break.

Copy link
Member

Choose a reason for hiding this comment

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

We are actually testing that the metadata for a Kubernetes node looks as expected, regardless of the InstanceReader::InstanceResource implementation. If that function's implementation changes, we'll need to fix the call, not the test.

json::value node_json = json::object({
{"metadata", json::object({
{"name", json::string("testname")},
{"creationTimestamp", json::string("2018-03-03T01:23:45.678901234Z")}
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep trailing commas in these key/value pairs for inline JSON. 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.

Added.


TEST_F(KubernetesTest, GetNodeMetadata) {
Configuration config(std::stringstream(
"KubernetesClusterName: 'TestClusterName'\n"
Copy link
Member

Choose a reason for hiding this comment

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

Single quotes behave weirdly in YAML. Since these are all bare words, let's just not quote them at all?..

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

})}
});
const json::Object* node = node_json->As<json::Object>();
MetadataUpdater::ResourceMetadata metadata(
Copy link
Member

Choose a reason for hiding this comment

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

const auto 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.

Changed

Copy link
Member

Choose a reason for hiding this comment

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

You forgot to change the parens to =...

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.

{"node_name", "testname"},
{"location", "TestClusterLocation"}
}));
EXPECT_EQ(metadata.metadata.version, "TestVersion");
Copy link
Member

Choose a reason for hiding this comment

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

metadata.metadata looks weird (and metadata.metadata.metadata even weirder 😄). How about we call it rmd (for "resource metadata")?

Copy link
Contributor Author

@ACEmilG ACEmilG Mar 29, 2018

Choose a reason for hiding this comment

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

While I agree it looks weird, I think changing the name to rmd would be more confusing. I'm not sure how to get around having metadata.metadata.metadata, since that really seems to be what this is.

Copy link
Member

Choose a reason for hiding this comment

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

How about just m? It's a test, so variable naming can be a little looser.

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.

test/Makefile Outdated
-lcppnetlib-client-connections -lcppnetlib-server-parsers -lnetwork-uri \
-lpthread -lboost_program_options -lboost_system -lboost_thread \
-lboost_filesystem -lyaml-cpp -lyajl -lcrypto -lssl
LDFLAGS=-L$(CPP_NETLIB_LIBDIR) -L$(NETWORK_URI_LIBDIR) -L$(YAML_CPP_LIBDIR)
Copy link
Member

Choose a reason for hiding this comment

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

Let's put LDFLAGS above LDLIBS to make it consistent with src/Makefile (yes, I know this pre-dates your change, but you're already touching both 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.

Done.

test/Makefile Outdated
LDLIBS=-lpthread -lboost_program_options -lyaml-cpp -lyajl
LDFLAGS=-L$(YAML_CPP_LIBDIR)
LDLIBS=\
-lcppnetlib-client-connections -lcppnetlib-server-parsers -lnetwork-uri \
Copy link
Member

Choose a reason for hiding this comment

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

Let's make the order of the libraries match the one in src/Makefile exactly (since the set is now identical).

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.

})}
});
const json::Object* node = node_json->As<json::Object>();
MetadataUpdater::ResourceMetadata metadata(
Copy link
Member

Choose a reason for hiding this comment

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

You forgot to change the parens to =...

{"node_name", "testname"},
{"location", "TestClusterLocation"}
}));
EXPECT_EQ(metadata.metadata.version, "TestVersion");
Copy link
Member

Choose a reason for hiding this comment

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

How about just m? It's a test, so variable naming can be a little looser.

"KubernetesClusterName: TestClusterName\n"
"KubernetesClusterLocation: TestClusterLocation\n"
"MetadataIngestionRawContentVersion: TestVersion\n"
"InstanceZone: TestZone\n"
Copy link
Member

Choose a reason for hiding this comment

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

Should also add "InstanceResourceType: gce_instance\n".

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

{"version", json::string("TestVersion")},
{"raw", json::object({
{"infrastructureResource",
InstanceReader::InstanceResource(environment).ToJSON()},
Copy link
Member

Choose a reason for hiding this comment

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

Better — I can give you code 😄:

          {"infrastructureResource", json::object({
            {"type", json::string("gce_instance")},
            {"labels", json::object({
              {"instance_id", json::string("TestID")},
              {"zone", json::string("TestZone")},
            })},
          })},

})},
})},
{"api", json::object({
{"version", json::string("1.6")}, // Hard-coded in kubernetes.cc
Copy link
Member

Choose a reason for hiding this comment

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

Nit: still missing the trailing ..

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

class KubernetesTest : public ::testing::Test {
protected:
Copy link
Member

Choose a reason for hiding this comment

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

Please indent access modifiers by 1 space, and the class body by 2 spaces (style guide).

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

"KubernetesClusterLocation: TestClusterLocation\n"
"MetadataIngestionRawContentVersion: TestVersion\n"
"InstanceZone: TestZone\n"
"InstanceResourceType: gce_instance\n"
Copy link
Member

Choose a reason for hiding this comment

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

Let's put this above InstanceZone.

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

{"version", json::string("TestVersion")},
{"raw", json::object({
{"infrastructureResource",
InstanceReader::InstanceResource(environment).ToJSON()},
Copy link
Member

Choose a reason for hiding this comment

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

We are actually testing that the metadata for a Kubernetes node looks as expected, regardless of the InstanceReader::InstanceResource implementation. If that function's implementation changes, we'll need to fix the call, not the test.

@@ -0,0 +1,72 @@
#include "../src/configuration.h"
#include "../src/instance.h"
Copy link
Member

Choose a reason for hiding this comment

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

No longer used.

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

@igorpeshansky igorpeshansky requested a review from dhrupadb March 30, 2018 13:48
Copy link
Member

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

Copy link
Contributor

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

One minor nit comment, otherwise
LGTM 👌

"InstanceId: TestID\n"
));
Environment environment(config);
KubernetesReader reader(config, nullptr);
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 comment with what we're nulling out here -- makes it easier to understand.

@ACEmilG ACEmilG merged commit f6b9b59 into master Mar 30, 2018
@ACEmilG ACEmilG deleted the ACEmilG-GetNodeMetadata-unittest branch March 30, 2018 14:59
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