blob: e878ca60f64bb39f62a04952fe4a6a1f83e0315c [file] [log] [blame] [edit]
#include <filesystem> // NOLINT
#include <fstream>
#include <memory>
#include <string>
#include <system_error> // NOLINT
#include "baremetal.grpc.pb.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/log/log.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "grpcblob_server.h"
#include "grpcpp/client_context.h"
#include "grpcpp/create_channel.h"
#include "grpcpp/security/credentials.h"
#include "grpcpp/server_context.h"
#include "grpcpp/support/status.h"
namespace blobs {
namespace {
using ::grpc::Channel;
using ::grpc::ClientContext;
using ::grpc::Status;
using ::phosphor::baremetal::BMInstancePropertiesTransferRequest;
using ::phosphor::baremetal::BMInstancePropertiesTransferResponse;
using ::phosphor::baremetal::BmModeRequest;
using ::phosphor::baremetal::BmModeResponse;
using ::phosphor::baremetal::LinuxBootDoneRequest;
using ::phosphor::baremetal::LinuxBootDoneResponse;
using phosphor::baremetal::grpc_gen::BaremetalTransferService;
#ifndef EXPECT_OK
#define EXPECT_OK(x) EXPECT_TRUE(x.ok())
#endif
class BaremetalTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
server_ = std::make_unique<GrpcBlobServer>("/tmp");
server_->start();
// 10166: (insecure gRPC), default case - used for tests here
// 10167: (insecure gRPC), CN 0 of multi-CN system
// 10168: (insecure gRPC), CN 1 of multi-CN system
std::shared_ptr<Channel> channel = grpc::CreateChannel(
"localhost:10166", grpc::InsecureChannelCredentials());
baremetal_stub_ = BaremetalTransferService::NewStub(channel);
}
static void TearDownTestSuite() {
absl::SleepFor(absl::Seconds(5));
server_->stop();
server_.reset();
baremetal_stub_.reset();
}
static void createFile(const std::filesystem::path& path) {
std::error_code ec;
std::filesystem::path dirPath = path.parent_path();
// Create the directories recursively
bool created = std::filesystem::create_directories(dirPath, ec);
ASSERT_TRUE(created || !ec) << "Failed to create directory: " << dirPath
<< " Error: " << ec.message();
std::ofstream filename(path);
ASSERT_TRUE(filename.good())
<< "Failed to create file: " << path;
}
static void removeFile(const std::filesystem::path& path) {
std::error_code ec;
if (std::filesystem::exists(path)) {
std::filesystem::remove(path, ec);
ASSERT_FALSE(std::filesystem::exists(path))
<< "Failed to remove file: " << path;
}
}
static std::unique_ptr<GrpcBlobServer> server_;
static std::unique_ptr<BaremetalTransferService::Stub> baremetal_stub_;
};
std::unique_ptr<GrpcBlobServer> BaremetalTest::server_;
std::unique_ptr<BaremetalTransferService::Stub> BaremetalTest::baremetal_stub_;
TEST_F(BaremetalTest, ServerStartsAndStops) { EXPECT_NE(server_, nullptr); }
TEST_F(BaremetalTest, BmModeTransferNonBm) {
BmModeRequest request;
BmModeResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->BmModeTransfer(&context, request, &response));
EXPECT_EQ(phosphor::baremetal::BmMode::BMMODE_NON_BM, response.bm_mode());
}
TEST_F(BaremetalTest, BmModeTransferBmSteadyState) {
createFile("/tmp/run/bm-ready.flag");
BmModeRequest request;
BmModeResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->BmModeTransfer(&context, request, &response));
EXPECT_EQ(phosphor::baremetal::BmMode::BMMODE_STEADY_STATE,
response.bm_mode());
removeFile("/tmp/run/bm-ready.flag");
removeFile("/tmp/run/bm-drive-cleaning.flag0");
}
TEST_F(BaremetalTest, BmModeTransferBmCleaningDone) {
createFile("/tmp/run/bm-drive-cleaning-done.flag0");
BmModeRequest request;
BmModeResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->BmModeTransfer(&context, request, &response));
EXPECT_EQ(phosphor::baremetal::BmMode::BMMODE_BM, response.bm_mode());
removeFile("/tmp/run/bm-drive-cleaning-done.flag0");
removeFile("/tmp/run/bm-drive-cleaning-done-ack.flag0");
}
TEST_F(BaremetalTest, BmModeTransferBmCleaningDoneAck) {
createFile("/tmp/run/bm-drive-cleaning-done-ack.flag0");
BmModeRequest request;
BmModeResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->BmModeTransfer(&context, request, &response));
EXPECT_EQ(phosphor::baremetal::BmMode::BMMODE_BM, response.bm_mode());
removeFile("/tmp/run/bm-drive-cleaning-done-ack.flag0");
}
TEST_F(BaremetalTest, LinuxBootDone) {
LinuxBootDoneRequest request;
LinuxBootDoneResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->LinuxBootDone(&context, request, &response));
}
TEST_F(BaremetalTest, BMInstancePropertiesTransfer) {
const std::string base_path = "/tmp/run/bm-instance/";
const int instance = 0;
const std::string asset_tag_path =
base_path + "asset-tag" + std::to_string(instance);
const std::string board_serial_number_path =
base_path + "board-serial-number" + std::to_string(instance);
const std::string family_path =
base_path + "family" + std::to_string(instance);
const std::string product_name_path =
base_path + "product-name" + std::to_string(instance);
const std::string sku_path = base_path + "sku" + std::to_string(instance);
const std::string system_serial_number_path =
base_path + "system-serial-number" + std::to_string(instance);
const std::string uuid_path = base_path + "uuid" + std::to_string(instance);
createFile(asset_tag_path);
createFile(board_serial_number_path);
createFile(family_path);
createFile(product_name_path);
createFile(sku_path);
createFile(system_serial_number_path);
createFile(uuid_path);
std::ofstream asset_tag(asset_tag_path);
asset_tag << "asset_tag_value";
asset_tag.close();
std::ofstream board_serial_number(board_serial_number_path);
board_serial_number << "board_serial_number_value";
board_serial_number.close();
std::ofstream family(family_path);
family << "family_value";
family.close();
std::ofstream product_name(product_name_path);
product_name << "product_name_value";
product_name.close();
std::ofstream sku(sku_path);
sku << "sku_value";
sku.close();
std::ofstream system_serial_number(system_serial_number_path);
system_serial_number << "system_serial_number_value";
system_serial_number.close();
std::ofstream uuid(uuid_path);
uuid << "uuid_value";
uuid.close();
BMInstancePropertiesTransferRequest request;
BMInstancePropertiesTransferResponse response;
ClientContext context;
EXPECT_OK(baremetal_stub_->BMInstancePropertiesTransfer(&context, request,
&response));
LOG(INFO) << "response: " << response.DebugString();
EXPECT_EQ(response.asset_tag(), "asset_tag_value");
EXPECT_EQ(response.board_serial_number(), "board_serial_number_value");
EXPECT_EQ(response.family(), "family_value");
EXPECT_EQ(response.product_name(), "product_name_value");
EXPECT_EQ(response.sku(), "sku_value");
EXPECT_EQ(response.system_serial_number(), "system_serial_number_value");
EXPECT_EQ(response.uuid(), "uuid_value");
removeFile(asset_tag_path);
removeFile(board_serial_number_path);
removeFile(family_path);
removeFile(product_name_path);
removeFile(sku_path);
removeFile(system_serial_number_path);
removeFile(uuid_path);
}
} // namespace
} // namespace blobs