Skip to content

Commit 5e99df1

Browse files
committed
Add log checks for checking the root_id is working properly
Signed-off-by: Ingwon Song <[email protected]>
1 parent e6c445a commit 5e99df1

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

include/proxy-wasm/context.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,7 @@ class ContextBase : public RootInterface,
315315
}
316316

317317
// Properties
318-
WasmResult getProperty(std::string_view path, std::string *result) override {
319-
if (path == "plugin_root_id") {
320-
*result = root_id_;
321-
return WasmResult::Ok;
322-
}
318+
WasmResult getProperty(std::string_view /* path */, std::string * /* result */) override {
323319
return unimplemented();
324320
}
325321
WasmResult setProperty(std::string_view /* key */,

test/test_data/configure_check.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestRootContext1 : public RootContext {
2222
public:
2323
explicit TestRootContext1(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
2424
bool onConfigure(size_t s) override {
25-
// LOG_TRACE("onConfigure in TestRootContext1");
25+
LOG_TRACE("onConfigure in TestRootContext1");
2626
return true;
2727
}
2828
};
@@ -36,7 +36,7 @@ class TestRootContext2 : public RootContext {
3636
public:
3737
explicit TestRootContext2(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
3838
bool onConfigure(size_t s) override {
39-
// LOG_TRACE("onConfigure in TestRootContext2");
39+
LOG_TRACE("onConfigure in TestRootContext2");
4040
return true;
4141
}
4242
};

test/utility.h

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,22 @@ class TestIntegration : public WasmVmIntegration {
9191
class TestContext : public ContextBase {
9292
public:
9393
TestContext(WasmBase *wasm) : ContextBase(wasm) {}
94+
TestContext(WasmBase *wasm, const std::shared_ptr<PluginBase> &plugin)
95+
: ContextBase(wasm, plugin) {}
9496

9597
WasmResult log(uint32_t /*log_level*/, std::string_view message) override {
9698
log_ += std::string(message) + "\n";
9799
return WasmResult::Ok;
98100
}
99101

102+
WasmResult getProperty(std::string_view path, std::string *result) override {
103+
if (path == "plugin_root_id") {
104+
*result = root_id_;
105+
return WasmResult::Ok;
106+
}
107+
return unimplemented();
108+
}
109+
100110
bool isLogEmpty() { return log_.empty(); }
101111

102112
bool isLogged(std::string_view message) { return log_.find(message) != std::string::npos; }
@@ -118,10 +128,37 @@ class TestContext : public ContextBase {
118128

119129
class TestWasm : public WasmBase {
120130
public:
121-
TestWasm(std::unique_ptr<WasmVm> wasm_vm, std::unordered_map<std::string, std::string> envs = {})
122-
: WasmBase(std::move(wasm_vm), "", "", "", std::move(envs), {}) {}
131+
TestWasm(std::unique_ptr<WasmVm> wasm_vm, std::string_view vm_id,
132+
std::string_view vm_configuration, std::string_view vm_key,
133+
std::unordered_map<std::string, std::string> envs,
134+
AllowedCapabilitiesMap allowed_capabilities)
135+
: WasmBase(std::move(wasm_vm), vm_id, vm_configuration, vm_key, std::move(envs),
136+
std::move(allowed_capabilities)) {}
137+
138+
TestWasm(std::unique_ptr<WasmVm> wasm_vm, std::unordered_map<std::string, std::string> envs)
139+
: TestWasm(std::move(wasm_vm), "", "", "", std::move(envs), {}) {}
140+
141+
TestWasm(std::unique_ptr<WasmVm> wasm_vm) : TestWasm(std::move(wasm_vm), {}) {}
142+
143+
TestWasm(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, const WasmVmFactory &factory)
144+
: TestWasm(base_wasm_handle, factory, nullptr) {}
145+
146+
TestWasm(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, const WasmVmFactory &factory,
147+
std::function<void(TestContext *)> root_context_cb)
148+
: WasmBase(base_wasm_handle, factory), root_context_cb_(std::move(root_context_cb)) {}
123149

124150
ContextBase *createVmContext() override { return new TestContext(this); };
151+
152+
ContextBase *createRootContext(const std::shared_ptr<PluginBase> &plugin) override {
153+
auto *ctx = new TestContext(this, plugin);
154+
if (root_context_cb_) {
155+
root_context_cb_(ctx);
156+
}
157+
return ctx;
158+
}
159+
160+
private:
161+
std::function<void(TestContext *)> root_context_cb_;
125162
};
126163

127164
class TestVm : public testing::TestWithParam<std::string> {

test/wasm_test.cc

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ TEST_P(TestVm, AlwaysApplyCanaryForDifferentRootID) {
129129
// Define callbacks.
130130
WasmHandleFactory wasm_handle_factory =
131131
[this, vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
132-
auto base_wasm = std::make_shared<WasmBase>(newVm(), vm_id, vm_config, vm_key,
132+
auto base_wasm = std::make_shared<TestWasm>(newVm(), vm_id, vm_config, vm_key,
133133
std::unordered_map<std::string, std::string>{},
134134
AllowedCapabilitiesMap{});
135135
return std::make_shared<WasmHandleBase>(base_wasm);
@@ -138,17 +138,20 @@ TEST_P(TestVm, AlwaysApplyCanaryForDifferentRootID) {
138138
WasmHandleCloneFactory wasm_handle_clone_factory =
139139
[this](const std::shared_ptr<WasmHandleBase> &base_wasm_handle)
140140
-> std::shared_ptr<WasmHandleBase> {
141-
auto wasm = std::make_shared<WasmBase>(base_wasm_handle,
141+
auto wasm = std::make_shared<TestWasm>(base_wasm_handle,
142142
[this]() -> std::unique_ptr<WasmVm> { return newVm(); });
143143
return std::make_shared<WasmHandleBase>(wasm);
144144
};
145145

146146
auto canary_count = 0;
147+
TestContext *root_context_in_canary = nullptr;
147148
WasmHandleCloneFactory wasm_handle_clone_factory_for_canary =
148-
[&canary_count, this](const std::shared_ptr<WasmHandleBase> &base_wasm_handle)
149+
[&canary_count, &root_context_in_canary,
150+
this](const std::shared_ptr<WasmHandleBase> &base_wasm_handle)
149151
-> std::shared_ptr<WasmHandleBase> {
150-
auto wasm = std::make_shared<WasmBase>(base_wasm_handle,
151-
[this]() -> std::unique_ptr<WasmVm> { return newVm(); });
152+
auto wasm = std::make_shared<TestWasm>(
153+
base_wasm_handle, [this]() -> std::unique_ptr<WasmVm> { return newVm(); },
154+
[&root_context_in_canary](TestContext *ctx) { root_context_in_canary = ctx; });
152155
canary_count++;
153156
return std::make_shared<WasmHandleBase>(wasm);
154157
};
@@ -165,22 +168,28 @@ TEST_P(TestVm, AlwaysApplyCanaryForDifferentRootID) {
165168
// Create a first plugin.
166169
const auto plugin_1 = std::make_shared<PluginBase>(plugin_name, root_id_1, vm_id, engine_,
167170
plugin_config, fail_open, plugin_key);
168-
// Create base Wasm via createWasm.
171+
// Create a base Wasm by createWasm.
169172
auto base_wasm_handle_1 = createWasm(vm_key, source, plugin_1, wasm_handle_factory,
170173
wasm_handle_clone_factory_for_canary, false);
171174
ASSERT_TRUE(base_wasm_handle_1 && base_wasm_handle_1->wasm());
172175

176+
// Check if it ran for root context 1
177+
EXPECT_TRUE(root_context_in_canary->isLogged("onConfigure in TestRootContext1"));
178+
173179
// Create a first plugin.
174180
const auto plugin_2 = std::make_shared<PluginBase>(plugin_name, root_id_2, vm_id, engine_,
175181
plugin_config, fail_open, plugin_key);
176-
// Create base Wasm via createWasm.
182+
// Create a base Wasm by createWasm.
177183
auto base_wasm_handle_2 = createWasm(vm_key, source, plugin_2, wasm_handle_factory,
178184
wasm_handle_clone_factory_for_canary, false);
179185
ASSERT_TRUE(base_wasm_handle_2 && base_wasm_handle_2->wasm());
180186

181-
// Base Wasm
187+
// Check if it ran for root context 2
188+
EXPECT_TRUE(root_context_in_canary->isLogged("onConfigure in TestRootContext2"));
189+
190+
// Base Wasm should be equal, because the same vm_key is used.
182191
EXPECT_EQ(base_wasm_handle_1->wasm(), base_wasm_handle_2->wasm());
183-
// Plugin key should be different with each other, because root_id is different.
192+
// Plugin key should be different with each other, because root_ids are different.
184193
EXPECT_NE(plugin_1->key(), plugin_2->key());
185194
// For each create Wasm, canary should be done.
186195
EXPECT_EQ(canary_count, 2);

0 commit comments

Comments
 (0)