Skip to content

Commit 5865dff

Browse files
committed
[test_daemon] add unit test to validate future signal handler
Added the `ensure_that_on_restart_future_completes` test case to validate that the daemon destructor waits for the future completion and also delivers the pending signals. Also suppressed a few mock naggings. Signed-off-by: Mustafa Kemal Gilor <[email protected]>
1 parent f31c71a commit 5865dff

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/test_daemon.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ struct Daemon : public mpt::DaemonTestFixture
118118
EXPECT_CALL(mock_platform, multipass_storage_location()).Times(AnyNumber()).WillRepeatedly(Return(QString()));
119119
EXPECT_CALL(mock_platform, create_alias_script(_, _)).WillRepeatedly(Return());
120120
EXPECT_CALL(mock_platform, remove_alias_script(_)).WillRepeatedly(Return());
121+
EXPECT_CALL(mock_platform, setup_permission_inheritance(_)).Times(AnyNumber()).WillRepeatedly(Return());
122+
EXPECT_CALL(mock_platform, bridge_nomenclature).Times(AnyNumber()).WillRepeatedly(Return("notabridge"));
121123
}
122124

123125
void SetUp() override
@@ -356,6 +358,60 @@ TEST_F(Daemon, blueprintsURLOverrideIsCorrect)
356358
EXPECT_EQ(downloader->downloaded_urls.front(), QUrl::fromLocalFile(test_blueprints_zip).toString());
357359
}
358360

361+
TEST_F(Daemon, ensure_that_on_restart_future_completes)
362+
{
363+
auto mock_factory = use_a_mock_vm_factory();
364+
365+
constexpr auto instance_json = R"({
366+
"yakety-yak": {
367+
"deleted": false,
368+
"disk_space": "3232323232",
369+
"mac_addr": "ab:cd:ef:12:34:56",
370+
"mem_size": "2323232323",
371+
"metadata": {},
372+
"mounts": [],
373+
"num_cores": 4,
374+
"ssh_username": "ubuntu",
375+
"state": 4
376+
}
377+
})";
378+
const auto [temp_dir, _] = plant_instance_json(instance_json);
379+
config_builder.data_directory = temp_dir->path();
380+
config_builder.vault = std::make_unique<NiceMock<mpt::MockVMImageVault>>();
381+
382+
// This VM was running before, but not now.
383+
auto mock_vm = std::make_unique<NiceMock<mpt::MockVirtualMachine>>("yakety-yak");
384+
EXPECT_CALL(*mock_vm, current_state)
385+
.WillOnce(Return(mp::VirtualMachine::State::stopped))
386+
.WillOnce(Return(mp::VirtualMachine::State::stopped));
387+
EXPECT_CALL(*mock_vm, start).Times(1);
388+
389+
std::mutex mtx;
390+
std::condition_variable cv;
391+
bool ready = false;
392+
// update_state is called by the finished() handler of the future. If it's called, then
393+
// everything's ok.
394+
EXPECT_CALL(*mock_vm, update_state)
395+
.WillOnce([&cv, &mtx, &ready] {
396+
// Ensure that update_state is delayed until daemon's destructor call.
397+
std::unique_lock lock(mtx);
398+
cv.wait(lock, [&] { return ready; });
399+
// Wait a bit to ensure that daemon's destructor has been run
400+
std::this_thread::sleep_for(std::chrono::milliseconds{50});
401+
});
402+
EXPECT_CALL(*mock_vm, wait_until_ssh_up).Times(1);
403+
EXPECT_CALL(*mock_factory, create_virtual_machine).WillOnce(Return(std::move(mock_vm)));
404+
405+
{
406+
mp::Daemon daemon{config_builder.build()};
407+
{
408+
std::lock_guard lock(mtx);
409+
ready = true;
410+
}
411+
cv.notify_one();
412+
}
413+
}
414+
359415
namespace
360416
{
361417
struct DaemonCreateLaunchTestSuite : public Daemon, public WithParamInterface<std::string>
@@ -2378,6 +2434,12 @@ struct DaemonIsBridged : public Daemon,
23782434
public WithParamInterface<
23792435
std::tuple<std::vector<mp::NetworkInterfaceInfo>, std::vector<mp::NetworkInterface>, bool>>
23802436
{
2437+
DaemonIsBridged()
2438+
{
2439+
EXPECT_CALL(mock_platform, bridge_nomenclature)
2440+
.Times(AnyNumber())
2441+
.WillRepeatedly(Return("this is not a bridge"));
2442+
}
23812443
};
23822444

23832445
TEST_P(DaemonIsBridged, is_bridged_works)

0 commit comments

Comments
 (0)