Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions contrib/endpoints/src/api_manager/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,80 @@ TEST(Config, TestCorsDisabled) {
ASSERT_EQ(nullptr, method1);
}

TEST(Config, TestInvalidMetricRules) {
MockApiManagerEnvironmentWithLog env;
// There is no http.rule or api.method to match the selector.
static const char config_text[] = R"(

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.

Please add comment to state why this is invalid
// There is no http.rule or api.method to match the selector.

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.

name: "Service.Name"
quota {
metric_rules {
selector: "GetShelves"
metric_costs {
key: "test.googleapis.com/operation/read_book"
value: 100
}
}
}
)";

std::unique_ptr<Config> config = Config::Create(&env, config_text, "");
EXPECT_EQ(nullptr, config);
}

TEST(Config, TestMetricRules) {
MockApiManagerEnvironmentWithLog env;
static const char config_text[] = R"(
name: "Service.Name"
http {
rules {
selector: "DeleteShelf"
delete: "/shelves"
}
rules {
selector: "GetShelves"
get: "/shelves"
}
}
quota {
metric_rules {
selector: "GetShelves"
metric_costs {
key: "test.googleapis.com/operation/get_shelves"
value: 100
}
}
metric_rules {
selector: "DeleteShelf"
metric_costs {
key: "test.googleapis.com/operation/delete_shelves"
value: 200
}
}
}
)";

std::unique_ptr<Config> config = Config::Create(&env, config_text, "");
ASSERT_TRUE(config);

auto method1 = config->GetMethodInfo("GET", "/shelves");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To keep the code consistent with Envoy, I suggest avoiding over usage of auto method. In the Envoy codebase, we use explicit types most of the time

ASSERT_NE(nullptr, method1);

auto metric_cost_vector = method1->metric_cost_vector();
ASSERT_EQ(1, metric_cost_vector.size());

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.

but we DO want to test 2 metrics case. You can either convert the vector to a "set", then compare the "set". or sort the vector first,

ASSERT_EQ("test.googleapis.com/operation/get_shelves",
metric_cost_vector[0].first);
ASSERT_EQ(100, metric_cost_vector[0].second);

auto method2 = config->GetMethodInfo("DELETE", "/shelves");
ASSERT_NE(nullptr, method1);

metric_cost_vector = method2->metric_cost_vector();
ASSERT_EQ(1, metric_cost_vector.size());
ASSERT_EQ("test.googleapis.com/operation/delete_shelves",
metric_cost_vector[0].first);
ASSERT_EQ(200, metric_cost_vector[0].second);
}

} // namespace

} // namespace api_manager
Expand Down