Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dl239 committed Nov 7, 2023
1 parent 0437f75 commit 40cea6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/zk/zk_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool ZkClient::Init(int log_level, const std::string& log_file) {
PDLOG(WARNING, "auth failed. schema: %s cert: %s", auth_schema_.c_str(), cert_.c_str());
return false;
}
acl_vector_ = acl_vector_;
acl_vector_ = ZOO_CREATOR_ALL_ACL;
PDLOG(INFO, "auth ok. schema: %s cert: %s", auth_schema_.c_str(), cert_.c_str());
}
return true;
Expand Down Expand Up @@ -387,9 +387,11 @@ bool ZkClient::GetNodeValueAndStat(const char* node, std::string* value, Stat* s

bool ZkClient::DeleteNode(const std::string& node) {
std::lock_guard<std::mutex> lock(mu_);
if (zoo_delete(zk_, node.c_str(), -1) == ZOK) {
int ret = zoo_delete(zk_, node.c_str(), -1);
if (ret == ZOK) {
return true;
}
PDLOG(WARNING, "delete %s failed. error no is %d", node.c_str(), ret);
return false;
}

Expand Down
42 changes: 42 additions & 0 deletions src/zk/zk_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,48 @@ TEST_F(ZkClientTest, ZkNodeChange) {
ASSERT_TRUE(detect.load());
}

TEST_F(ZkClientTest, Auth) {
std::string node = "/openmldb_auth/node1";
{
ZkClient client("127.0.0.1:6181", "", 1000, "127.0.0.1:9527", "/openmldb_auth", "digest", "user1:123456");
bool ok = client.Init();
ASSERT_TRUE(ok);

int ret = client.IsExistNode(node);
ASSERT_EQ(ret, 1);
ok = client.CreateNode(node, "value");
ASSERT_TRUE(ok);
ret = client.IsExistNode(node);
ASSERT_EQ(ret, 0);
}
{
ZkClient client("127.0.0.1:6181", "", 1000, "127.0.0.1:9527", "/openmldb_auth", "", "");
bool ok = client.Init();
ASSERT_TRUE(ok);
std::string value;
ASSERT_FALSE(client.GetNodeValue(node, value));
ASSERT_FALSE(client.CreateNode("/openmldb_auth/node1/dd", "aaa"));
}
{
ZkClient client("127.0.0.1:6181", "", 1000, "127.0.0.1:9527", "/openmldb_auth", "digest", "user1:wrong");
bool ok = client.Init();
ASSERT_TRUE(ok);
std::string value;
ASSERT_FALSE(client.GetNodeValue(node, value));
ASSERT_FALSE(client.CreateNode("/openmldb_auth/node1/dd", "aaa"));
}
{
ZkClient client("127.0.0.1:6181", "", 1000, "127.0.0.1:9527", "/openmldb_auth", "digest", "user1:123456");
bool ok = client.Init();
ASSERT_TRUE(ok);
std::string value;
ASSERT_TRUE(client.GetNodeValue(node, value));
ASSERT_EQ("value", value);
ASSERT_TRUE(client.DeleteNode(node));
ASSERT_TRUE(client.DeleteNode("/openmldb_auth"));
}
}

} // namespace zk
} // namespace openmldb

Expand Down

0 comments on commit 40cea6d

Please sign in to comment.