-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(refactoring) UT helpers lib KIKIMR-20902 (#1391)
- Loading branch information
Showing
4 changed files
with
168 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters