Skip to content

Commit

Permalink
[tests] Refactor repeated mocking for daemon RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ricab committed Oct 25, 2023
1 parent 420e217 commit 54b2502
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions tests/unix/test_daemon_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ struct TestDaemonRpc : public mpt::DaemonTestFixture
return mpt::MockDaemon(config_builder.build());
}

void mock_empty_list_reply(mpt::MockDaemon& mock_daemon)
{
EXPECT_CALL(mock_daemon, list(_, _, _)).WillOnce([](auto, auto* server, auto* status_promise) {
mp::ListReply reply;
reply.mutable_instance_list();
server->Write(reply);
status_promise->set_value(grpc::Status::OK);
});
}

std::unique_ptr<mpt::MockCertProvider> mock_cert_provider{std::make_unique<mpt::MockCertProvider>()};
std::unique_ptr<mpt::MockCertStore> mock_cert_store{std::make_unique<mpt::MockCertStore>()};

Expand Down Expand Up @@ -202,12 +212,7 @@ TEST_F(TestDaemonRpc, listCertExistsCompletesSuccesfully)
EXPECT_CALL(*mock_cert_store, verify_cert(StrEq(mpt::client_cert))).WillOnce(Return(true));

mpt::MockDaemon daemon{make_secure_server()};
EXPECT_CALL(daemon, list(_, _, _)).WillOnce([](auto, auto* server, auto* status_promise) {
mp::ListReply reply;
reply.mutable_instance_list();
server->Write(reply);
status_promise->set_value(grpc::Status::OK);
});
mock_empty_list_reply(daemon);

send_command({"list"});
}
Expand All @@ -221,12 +226,7 @@ TEST_F(TestDaemonRpc, listNoCertsExistWillVerifyAndComplete)
EXPECT_CALL(*mock_cert_store, add_cert(StrEq(mpt::client_cert))).Times(1);

mpt::MockDaemon daemon{make_secure_server()};
EXPECT_CALL(daemon, list(_, _, _)).WillOnce([](auto, auto* server, auto* status_promise) {
mp::ListReply reply;
reply.mutable_instance_list();
server->Write(reply);
status_promise->set_value(grpc::Status::OK);
});
mock_empty_list_reply(daemon);

send_command({"list"});
}
Expand Down Expand Up @@ -308,12 +308,7 @@ TEST_F(TestDaemonRpc, listSettingServerPermissionsFailLogsErrorAndExits)
logger_scope.mock_logger->expect_log(mpl::Level::error, error_msg);
logger_scope.mock_logger->expect_log(mpl::Level::error, "Failed to set up autostart prerequisites", AnyNumber());

EXPECT_CALL(daemon, list(_, _, _)).WillOnce([](auto, auto* server, auto* status_promise) {
mp::ListReply reply;
reply.mutable_instance_list();
server->Write(reply);
status_promise->set_value(grpc::Status::OK);
});
mock_empty_list_reply(daemon);

send_command({"list"});
}

0 comments on commit 54b2502

Please sign in to comment.