Skip to content
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

(refactoring) UT helpers lib KIKIMR-20902 #1391

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions ydb/core/tx/replication/ut_helpers/test_env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <ydb/core/base/ticket_parser.h>
#include <ydb/core/protos/replication.pb.h>
#include <ydb/core/testlib/test_client.h>
#include <ydb/core/tx/schemeshard/schemeshard.h>

#include <library/cpp/testing/unittest/registar.h>

namespace NKikimr::NReplication {

template <bool UseDatabase = true>
class TEnv {
static constexpr char DomainName[] = "Root";

static NKikimrPQ::TPQConfig MakePqConfig() {
NKikimrPQ::TPQConfig config;
config.SetRequireCredentialsInNewProtocol(false);
return config;
}

template <typename... Args>
void Init(Args&&... args) {
auto grpcPort = PortManager.GetPort();

Server.EnableGRpc(grpcPort);
Server.SetupDefaultProfiles();
Client.InitRootScheme(DomainName);

Endpoint = "localhost:" + ToString(grpcPort);
Database = "/" + ToString(DomainName);

YdbProxy = Server.GetRuntime()->Register(CreateYdbProxy(
Endpoint, UseDatabase ? Database : "", std::forward<Args>(args)...));
Sender = Server.GetRuntime()->AllocateEdgeActor();
}

void Login(ui64 schemeShardId, const TString& user, const TString& password) {
auto req = MakeHolder<NSchemeShard::TEvSchemeShard::TEvLogin>();
req->Record.SetUser(user);
req->Record.SetPassword(password);
ForwardToTablet(*Server.GetRuntime(), schemeShardId, Sender, req.Release());

auto resp = Server.GetRuntime()->GrabEdgeEvent<NSchemeShard::TEvSchemeShard::TEvLoginResult>(Sender);
UNIT_ASSERT(resp->Get()->Record.GetError().empty());
UNIT_ASSERT(!resp->Get()->Record.GetToken().empty());
}

public:
TEnv(bool init = true)
: Settings(Tests::TServerSettings(PortManager.GetPort(), {}, MakePqConfig())
.SetDomainName(DomainName)
)
, Server(Settings)
, Client(Settings)
{
if (init) {
Init();
}
}

explicit TEnv(const TString& user, const TString& password)
: TEnv(false)
{
NKikimrReplication::TStaticCredentials staticCreds;
staticCreds.SetUser(user);
staticCreds.SetPassword(password);
Init(staticCreds);

const auto db = "/" + ToString(DomainName);
// create user & set owner
{
auto st = Client.CreateUser(db, user, password);
UNIT_ASSERT_VALUES_EQUAL(st, NMsgBusProxy::EResponseStatus::MSTATUS_OK);

Client.ModifyOwner("/", DomainName, user);
}
// init security state
{
auto resp = Client.Ls(db);

const auto& desc = resp->Record;
UNIT_ASSERT(desc.HasPathDescription());
UNIT_ASSERT(desc.GetPathDescription().HasDomainDescription());
UNIT_ASSERT(desc.GetPathDescription().GetDomainDescription().HasDomainKey());

Login(desc.GetPathDescription().GetDomainDescription().GetDomainKey().GetSchemeShard(), user, password);
}
// update security state
{
auto resp = Client.Ls(db);

const auto& desc = resp->Record;
UNIT_ASSERT(desc.HasPathDescription());
UNIT_ASSERT(desc.GetPathDescription().HasDomainDescription());
UNIT_ASSERT(desc.GetPathDescription().GetDomainDescription().HasSecurityState());

const auto& secState = desc.GetPathDescription().GetDomainDescription().GetSecurityState();
Server.GetRuntime()->Send(new IEventHandle(MakeTicketParserID(), Sender,
new TEvTicketParser::TEvUpdateLoginSecurityState(secState)));
}
}

void SendAsync(const TActorId& recipient, IEventBase* ev) {
Server.GetRuntime()->Send(new IEventHandle(recipient, Sender, ev));
}

template <typename TEvResponse>
auto Send(const TActorId& recipient, IEventBase* ev) {
SendAsync(recipient, ev);
return Server.GetRuntime()->GrabEdgeEvent<TEvResponse>(Sender);
}

template <typename TEvResponse>
auto Send(IEventBase* ev) {
return Send<TEvResponse>(YdbProxy, ev);
}

auto& GetRuntime() {
return *Server.GetRuntime();
}

const NYdb::TDriver& GetDriver() const {
return Server.GetDriver();
}

const TString& GetEndpoint() const {
return Endpoint;
}

const TString& GetDatabase() const {
return Database;
}

const TActorId& GetSender() const {
return Sender;
}

private:
TPortManager PortManager;
Tests::TServerSettings Settings;
Tests::TServer Server;
Tests::TClient Client;
TString Endpoint;
TString Database;
TActorId YdbProxy;
TActorId Sender;
};

}
16 changes: 16 additions & 0 deletions ydb/core/tx/replication/ut_helpers/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LIBRARY()

PEERDIR(
ydb/core/base
ydb/core/protos
ydb/core/testlib/default
library/cpp/testing/unittest
)

SRCS(
test_env.h
)

YQL_LAST_ABI_VERSION()

END()
4 changes: 2 additions & 2 deletions ydb/core/tx/replication/ydb_proxy/ut/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ SIZE(MEDIUM)
TIMEOUT(600)

PEERDIR(
library/cpp/testing/unittest
ydb/core/testlib/default
ydb/core/tx/replication/ut_helpers
ydb/public/sdk/cpp/client/ydb_topic
library/cpp/testing/unittest
)

SRCS(
Expand Down
146 changes: 2 additions & 144 deletions ydb/core/tx/replication/ydb_proxy/ydb_proxy_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,155 +1,13 @@
#include "ydb_proxy.h"

#include <ydb/core/protos/replication.pb.h>
#include <ydb/core/testlib/test_client.h>
#include <ydb/core/tx/schemeshard/schemeshard.h>
#include <ydb/core/tx/replication/ut_helpers/test_env.h>
#include <ydb/public/sdk/cpp/client/ydb_topic/topic.h>

#include <library/cpp/testing/unittest/registar.h>

#include <ydb/core/base/ticket_parser.h>
#include <ydb/public/sdk/cpp/client/ydb_topic/topic.h>

namespace NKikimr::NReplication {

Y_UNIT_TEST_SUITE(YdbProxyTests) {
template <bool UseDatabase = true>
class TEnv {
static constexpr char DomainName[] = "Root";

static NKikimrPQ::TPQConfig MakePqConfig() {
NKikimrPQ::TPQConfig config;
config.SetRequireCredentialsInNewProtocol(false);
return config;
}

template <typename... Args>
void Init(Args&&... args) {
auto grpcPort = PortManager.GetPort();

Server.EnableGRpc(grpcPort);
Server.SetupDefaultProfiles();
Client.InitRootScheme(DomainName);

Endpoint = "localhost:" + ToString(grpcPort);
Database = "/" + ToString(DomainName);

YdbProxy = Server.GetRuntime()->Register(CreateYdbProxy(
Endpoint, UseDatabase ? Database : "", std::forward<Args>(args)...));
Sender = Server.GetRuntime()->AllocateEdgeActor();
}

void Login(ui64 schemeShardId, const TString& user, const TString& password) {
auto req = MakeHolder<NSchemeShard::TEvSchemeShard::TEvLogin>();
req->Record.SetUser(user);
req->Record.SetPassword(password);
ForwardToTablet(*Server.GetRuntime(), schemeShardId, Sender, req.Release());

auto resp = Server.GetRuntime()->GrabEdgeEvent<NSchemeShard::TEvSchemeShard::TEvLoginResult>(Sender);
UNIT_ASSERT(resp->Get()->Record.GetError().empty());
UNIT_ASSERT(!resp->Get()->Record.GetToken().empty());
}

public:
TEnv(bool init = true)
: Settings(Tests::TServerSettings(PortManager.GetPort(), {}, MakePqConfig())
.SetDomainName(DomainName)
)
, Server(Settings)
, Client(Settings)
{
if (init) {
Init();
}
}

explicit TEnv(const TString& user, const TString& password)
: TEnv(false)
{
NKikimrReplication::TStaticCredentials staticCreds;
staticCreds.SetUser(user);
staticCreds.SetPassword(password);
Init(staticCreds);

const auto db = "/" + ToString(DomainName);
// create user & set owner
{
auto st = Client.CreateUser(db, user, password);
UNIT_ASSERT_VALUES_EQUAL(st, NMsgBusProxy::EResponseStatus::MSTATUS_OK);

Client.ModifyOwner("/", DomainName, user);
}
// init security state
{
auto resp = Client.Ls(db);

const auto& desc = resp->Record;
UNIT_ASSERT(desc.HasPathDescription());
UNIT_ASSERT(desc.GetPathDescription().HasDomainDescription());
UNIT_ASSERT(desc.GetPathDescription().GetDomainDescription().HasDomainKey());

Login(desc.GetPathDescription().GetDomainDescription().GetDomainKey().GetSchemeShard(), user, password);
}
// update security state
{
auto resp = Client.Ls(db);

const auto& desc = resp->Record;
UNIT_ASSERT(desc.HasPathDescription());
UNIT_ASSERT(desc.GetPathDescription().HasDomainDescription());
UNIT_ASSERT(desc.GetPathDescription().GetDomainDescription().HasSecurityState());

const auto& secState = desc.GetPathDescription().GetDomainDescription().GetSecurityState();
Server.GetRuntime()->Send(new IEventHandle(MakeTicketParserID(), Sender,
new TEvTicketParser::TEvUpdateLoginSecurityState(secState)));
}
}

void SendAsync(const TActorId& recipient, IEventBase* ev) {
Server.GetRuntime()->Send(new IEventHandle(recipient, Sender, ev));
}

template <typename TEvResponse>
auto Send(const TActorId& recipient, IEventBase* ev) {
SendAsync(recipient, ev);
return Server.GetRuntime()->GrabEdgeEvent<TEvResponse>(Sender);
}

template <typename TEvResponse>
auto Send(IEventBase* ev) {
return Send<TEvResponse>(YdbProxy, ev);
}

auto& GetRuntime() {
return *Server.GetRuntime();
}

const NYdb::TDriver& GetDriver() const {
return Server.GetDriver();
}

const TString& GetEndpoint() const {
return Endpoint;
}

const TString& GetDatabase() const {
return Database;
}

const TActorId& GetSender() const {
return Sender;
}

private:
TPortManager PortManager;
Tests::TServerSettings Settings;
Tests::TServer Server;
Tests::TClient Client;
TString Endpoint;
TString Database;
TActorId YdbProxy;
TActorId Sender;
};

Y_UNIT_TEST(MakeDirectory) {
TEnv env;
// ok
Expand Down
Loading