-
Notifications
You must be signed in to change notification settings - Fork 73
Test an example stream context with a logging plugin #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8649f8
Allow StreamContext testing via TestContext
martijneken 4684af1
Test an example Stream Context. HTTP handlers, logging only, no headers.
martijneken bee5be3
Add missing license header.
martijneken ae11dd5
Attempt to fix clang-tidy
martijneken d147041
Make newVm a static method for broader utility.
martijneken e22cc9e
Code review cleanups.
martijneken 42ec22e
Remove unnecessary pointer / heap allocation
martijneken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "gtest/gtest.h" | ||
#include "include/proxy-wasm/wasm.h" | ||
#include "test/utility.h" | ||
|
||
namespace proxy_wasm { | ||
|
||
INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()), | ||
[](const testing::TestParamInfo<std::string> &info) { | ||
return info.param; | ||
}); | ||
|
||
// TestVm is parameterized for each engine and creates a VM on construction. | ||
TEST_P(TestVm, HttpLogging) { | ||
// Read the wasm source. | ||
auto source = readTestWasmFile("http_logging.wasm"); | ||
ASSERT_FALSE(source.empty()); | ||
|
||
// Create a WasmBase and load the plugin. | ||
auto wasm = std::make_shared<TestWasm>(std::move(vm_)); | ||
ASSERT_TRUE(wasm->load(source, /*allow_precompiled=*/false)); | ||
ASSERT_TRUE(wasm->initialize()); | ||
|
||
// Create a plugin. | ||
const auto plugin = std::make_shared<PluginBase>( | ||
/*name=*/"test", /*root_id=*/"", /*vm_id=*/"", | ||
/*engine=*/wasm->wasm_vm()->getEngineName(), /*plugin_config=*/"", | ||
/*fail_open=*/false, /*key=*/""); | ||
|
||
// Create root context, call onStart(). | ||
ContextBase *root_context = wasm->start(plugin); | ||
ASSERT_TRUE(root_context != nullptr); | ||
|
||
// On the root context, call onConfigure(). | ||
ASSERT_TRUE(wasm->configure(root_context, plugin)); | ||
|
||
// Create a stream context. | ||
{ | ||
auto wasm_handle = std::make_shared<WasmHandleBase>(wasm); | ||
auto plugin_handle = std::make_shared<PluginHandleBase>(wasm_handle, plugin); | ||
auto stream_context = TestContext(wasm.get(), root_context->id(), plugin_handle); | ||
stream_context.onCreate(); | ||
EXPECT_TRUE(stream_context.isLogged("onCreate called")); | ||
stream_context.onRequestHeaders(/*headers=*/0, /*end_of_stream=*/false); | ||
EXPECT_TRUE(stream_context.isLogged("onRequestHeaders called")); | ||
stream_context.onResponseHeaders(/*headers=*/0, /*end_of_stream=*/false); | ||
EXPECT_TRUE(stream_context.isLogged("onResponseHeaders called")); | ||
stream_context.onDone(); | ||
EXPECT_TRUE(stream_context.isLogged("onDone called")); | ||
stream_context.onDelete(); | ||
EXPECT_TRUE(stream_context.isLogged("onDelete called")); | ||
} | ||
EXPECT_FALSE(wasm->isFailed()); | ||
} | ||
|
||
} // namespace proxy_wasm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "proxy_wasm_intrinsics.h" | ||
|
||
class LoggingContext : public Context { | ||
public: | ||
explicit LoggingContext(uint32_t id, RootContext *root) : Context(id, root) {} | ||
|
||
void onCreate() override { LOG_INFO("onCreate called"); } | ||
void onDelete() override { LOG_INFO("onDelete called"); } | ||
void onDone() override { LOG_INFO("onDone called"); } | ||
|
||
FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override { | ||
LOG_INFO("onRequestHeaders called"); | ||
return FilterHeadersStatus::Continue; | ||
} | ||
|
||
FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override { | ||
LOG_INFO("onResponseHeaders called"); | ||
return FilterHeadersStatus::Continue; | ||
} | ||
}; | ||
|
||
static RegisterContextFactory register_StaticContext(CONTEXT_FACTORY(LoggingContext), | ||
ROOT_FACTORY(RootContext)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.