From 6b76771e776d48c5899e1126f0242b6c118b2c37 Mon Sep 17 00:00:00 2001 From: Ilnaz Nizametdinov Date: Tue, 30 Jan 2024 00:21:14 +0300 Subject: [PATCH] (refactoring) UT helpers lib KIKIMR-20902 (#1391) --- ydb/core/tx/replication/ut_helpers/test_env.h | 148 ++++++++++++++++++ ydb/core/tx/replication/ut_helpers/ya.make | 16 ++ ydb/core/tx/replication/ydb_proxy/ut/ya.make | 4 +- .../tx/replication/ydb_proxy/ydb_proxy_ut.cpp | 146 +---------------- 4 files changed, 168 insertions(+), 146 deletions(-) create mode 100644 ydb/core/tx/replication/ut_helpers/test_env.h create mode 100644 ydb/core/tx/replication/ut_helpers/ya.make diff --git a/ydb/core/tx/replication/ut_helpers/test_env.h b/ydb/core/tx/replication/ut_helpers/test_env.h new file mode 100644 index 000000000000..d4ca801b454a --- /dev/null +++ b/ydb/core/tx/replication/ut_helpers/test_env.h @@ -0,0 +1,148 @@ +#include +#include +#include +#include + +#include + +namespace NKikimr::NReplication { + +template +class TEnv { + static constexpr char DomainName[] = "Root"; + + static NKikimrPQ::TPQConfig MakePqConfig() { + NKikimrPQ::TPQConfig config; + config.SetRequireCredentialsInNewProtocol(false); + return config; + } + + template + 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)...)); + Sender = Server.GetRuntime()->AllocateEdgeActor(); + } + + void Login(ui64 schemeShardId, const TString& user, const TString& password) { + auto req = MakeHolder(); + req->Record.SetUser(user); + req->Record.SetPassword(password); + ForwardToTablet(*Server.GetRuntime(), schemeShardId, Sender, req.Release()); + + auto resp = Server.GetRuntime()->GrabEdgeEvent(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 + auto Send(const TActorId& recipient, IEventBase* ev) { + SendAsync(recipient, ev); + return Server.GetRuntime()->GrabEdgeEvent(Sender); + } + + template + auto Send(IEventBase* ev) { + return Send(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; +}; + +} diff --git a/ydb/core/tx/replication/ut_helpers/ya.make b/ydb/core/tx/replication/ut_helpers/ya.make new file mode 100644 index 000000000000..43d2e7ab0264 --- /dev/null +++ b/ydb/core/tx/replication/ut_helpers/ya.make @@ -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() diff --git a/ydb/core/tx/replication/ydb_proxy/ut/ya.make b/ydb/core/tx/replication/ydb_proxy/ut/ya.make index 5ab140af1158..befb75975e4a 100644 --- a/ydb/core/tx/replication/ydb_proxy/ut/ya.make +++ b/ydb/core/tx/replication/ydb_proxy/ut/ya.make @@ -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( diff --git a/ydb/core/tx/replication/ydb_proxy/ydb_proxy_ut.cpp b/ydb/core/tx/replication/ydb_proxy/ydb_proxy_ut.cpp index 7e7cb303b8d0..1fc3b8d97342 100644 --- a/ydb/core/tx/replication/ydb_proxy/ydb_proxy_ut.cpp +++ b/ydb/core/tx/replication/ydb_proxy/ydb_proxy_ut.cpp @@ -1,155 +1,13 @@ #include "ydb_proxy.h" -#include -#include -#include +#include +#include #include -#include -#include - namespace NKikimr::NReplication { Y_UNIT_TEST_SUITE(YdbProxyTests) { - template - class TEnv { - static constexpr char DomainName[] = "Root"; - - static NKikimrPQ::TPQConfig MakePqConfig() { - NKikimrPQ::TPQConfig config; - config.SetRequireCredentialsInNewProtocol(false); - return config; - } - - template - 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)...)); - Sender = Server.GetRuntime()->AllocateEdgeActor(); - } - - void Login(ui64 schemeShardId, const TString& user, const TString& password) { - auto req = MakeHolder(); - req->Record.SetUser(user); - req->Record.SetPassword(password); - ForwardToTablet(*Server.GetRuntime(), schemeShardId, Sender, req.Release()); - - auto resp = Server.GetRuntime()->GrabEdgeEvent(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 - auto Send(const TActorId& recipient, IEventBase* ev) { - SendAsync(recipient, ev); - return Server.GetRuntime()->GrabEdgeEvent(Sender); - } - - template - auto Send(IEventBase* ev) { - return Send(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