Skip to content

Commit

Permalink
Adapt tests
Browse files Browse the repository at this point in the history
Signed-off-by: JACQUES Francois <[email protected]>
  • Loading branch information
JACQUES Francois committed Nov 13, 2024
1 parent bc3a410 commit 23b0581
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
55 changes: 55 additions & 0 deletions test/extensions/filters/http/ext_authz/ext_authz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,57 @@ INSTANTIATE_TEST_SUITE_P(ParameterizedFilterConfig, HttpFilterTestParam,
testing::Combine(testing::Bool(), testing::Bool()),
HttpFilterTestParam::ParamsToString);

class ExtAuthzLoggingInfoTest
: public testing::TestWithParam<std::tuple<std::string /* field_name */, absl::optional<uint64_t> /* value */>> {
public:
ExtAuthzLoggingInfoTest() : logging_info_({}) {}

void SetUp() override {
std::string fieldName = std::get<0>(GetParam());
absl::optional<uint64_t> optional = std::get<1>(GetParam());
if(optional.has_value()) {
if (fieldName == "latency_us") {
logging_info_.setLatency({optional.value()});
}
if (fieldName == "bytesSent") {
logging_info_.setBytesSent(optional.value());
}
if (fieldName == "bytesReceived") {
logging_info_.setBytesReceived(optional.value());
}
}
}

void test() {
ASSERT_TRUE(logging_info_.hasFieldSupport());
absl::optional<uint64_t> optional = std::get<1>(GetParam());
if (optional.has_value()) {
EXPECT_THAT(logging_info_.getField(std::get<0>(GetParam())),
testing::VariantWith<int64_t>(optional.value()));
} else {
EXPECT_THAT(logging_info_.getField(std::get<0>(GetParam())),
testing::VariantWith<absl::monostate>(absl::monostate{}));
}
}

static std::string
ParamsToString(const testing::TestParamInfo<std::tuple<std::string, absl::optional<uint64_t>>>& info) {
return absl::StrCat(std::get<1>(info.param).has_value() ? "" : "no_",
std::get<0>(info.param),
std::get<1>(info.param).has_value() ? absl::StrCat("_", std::to_string(std::get<1>(info.param).value())) : "");
}

ExtAuthzLoggingInfo logging_info_;
};

INSTANTIATE_TEST_SUITE_P(ExtAuthzLoggingInfoTestValid, ExtAuthzLoggingInfoTest,
testing::Combine(testing::Values("latency_us", "bytesSent", "bytesReceived"),
testing::Values(absl::optional<uint64_t>{}, absl::optional<uint64_t>{0}, absl::optional<uint64_t>{1})),
ExtAuthzLoggingInfoTest::ParamsToString);
INSTANTIATE_TEST_SUITE_P(ExtAuthzLoggingInfoTestInvalid, ExtAuthzLoggingInfoTest,
testing::Values(std::make_tuple("wrong_property_name", absl::optional<uint64_t>{})),
ExtAuthzLoggingInfoTest::ParamsToString);

class EmitFilterStateTest
: public HttpFilterTestBase<testing::TestWithParam<
std::tuple<bool /*http_client*/, bool /*emit_stats*/, bool /*emit_filter_metadata*/>>> {
Expand Down Expand Up @@ -4064,6 +4115,10 @@ TEST_P(EmitFilterStateTest, PreexistingFilterStateSameTypeMutable) {
test(response);
}

TEST_P(ExtAuthzLoggingInfoTest, FieldTest) {
test();
}

} // namespace
} // namespace ExtAuthz
} // namespace HttpFilters
Expand Down
26 changes: 0 additions & 26 deletions test/extensions/filters/http/ext_authz/logging_test_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,6 @@ class LoggingTestFilter : public Http::PassThroughFilter {
ASSERT_NE(ext_authz_logging_info->upstreamHost(), nullptr);
EXPECT_EQ(ext_authz_logging_info->upstreamHost()->cluster().name(), expected_cluster_name_);
}

// Assert for CEL and accesslog field access
ASSERT_TRUE(ext_authz_logging_info->hasFieldSupport());
EXPECT_THAT(ext_authz_logging_info->getField("latency_us"),
testing::VariantWith<int64_t>(ext_authz_logging_info->latency().value().count()));
if (expect_envoy_grpc_specific_stats_) {
EXPECT_THAT(ext_authz_logging_info->getField("bytesSent"),
testing::VariantWith<int64_t>(ext_authz_logging_info->bytesSent().value()));
EXPECT_THAT(ext_authz_logging_info->getField("bytesReceived"),
testing::VariantWith<int64_t>(ext_authz_logging_info->bytesReceived().value()));
}

// Assert field absence and wrong field name
EXPECT_THAT(ext_authz_logging_info->getField("wrong_property_name"),
testing::VariantWith<absl::monostate>(absl::monostate{}));
ExtAuthz::ExtAuthzLoggingInfo* ext_authz_logging_info_mutable =
filter_state->getDataMutable<ExtAuthz::ExtAuthzLoggingInfo>(logging_id_);
ext_authz_logging_info_mutable->clearLatency();
ext_authz_logging_info_mutable->clearBytesSent();
ext_authz_logging_info_mutable->clearBytesReceived();
EXPECT_THAT(ext_authz_logging_info_mutable->getField("latency_us"),
testing::VariantWith<absl::monostate>(absl::monostate{}));
EXPECT_THAT(ext_authz_logging_info_mutable->getField("bytesSent"),
testing::VariantWith<absl::monostate>(absl::monostate{}));
EXPECT_THAT(ext_authz_logging_info_mutable->getField("bytesReceived"),
testing::VariantWith<absl::monostate>(absl::monostate{}));
}

private:
Expand Down

0 comments on commit 23b0581

Please sign in to comment.