/Users/deen/code/yugabyte-db/src/yb/tools/ldb_test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <gtest/gtest.h> |
15 | | |
16 | | #include "yb/client/client.h" |
17 | | #include "yb/client/schema.h" |
18 | | #include "yb/client/session.h" |
19 | | #include "yb/client/table.h" |
20 | | #include "yb/client/table_creator.h" |
21 | | #include "yb/client/yb_op.h" |
22 | | |
23 | | #include "yb/integration-tests/mini_cluster.h" |
24 | | #include "yb/integration-tests/yb_mini_cluster_test_base.h" |
25 | | |
26 | | #include "yb/tablet/tablet_metadata.h" |
27 | | #include "yb/tablet/tablet_peer.h" |
28 | | |
29 | | #include "yb/tools/data_gen_util.h" |
30 | | |
31 | | #include "yb/util/path_util.h" |
32 | | #include "yb/util/random.h" |
33 | | #include "yb/util/random_util.h" |
34 | | #include "yb/util/result.h" |
35 | | #include "yb/util/status.h" |
36 | | #include "yb/util/subprocess.h" |
37 | | #include "yb/util/test_util.h" |
38 | | |
39 | | using namespace std::literals; |
40 | | |
41 | | namespace yb { |
42 | | namespace tools { |
43 | | |
44 | | using client::YBClient; |
45 | | using client::YBClientBuilder; |
46 | | using client::YBSchema; |
47 | | using client::YBSchemaBuilder; |
48 | | using client::YBTableCreator; |
49 | | using client::YBTableName; |
50 | | using client::YBTable; |
51 | | |
52 | | static const char* const kTabletUtilToolName = "ldb"; |
53 | | static const char* const kNamespace = "ldb_test_namespace"; |
54 | | static const char* const kTableName = "my_table"; |
55 | | static constexpr int32_t kNumTablets = 1; |
56 | | static constexpr int32_t kNumTabletServers = 1; |
57 | | |
58 | | class YBTabletUtilTest : public YBMiniClusterTestBase<MiniCluster> { |
59 | | public: |
60 | 0 | YBTabletUtilTest() : random_(0) { |
61 | 0 | } |
62 | | |
63 | 0 | void SetUp() override { |
64 | 0 | YBMiniClusterTestBase::SetUp(); |
65 | 0 | MiniClusterOptions opts; |
66 | |
|
67 | 0 | opts.num_tablet_servers = kNumTabletServers; |
68 | |
|
69 | 0 | cluster_.reset(new MiniCluster(opts)); |
70 | 0 | ASSERT_OK(cluster_->Start()); |
71 | |
|
72 | 0 | YBSchema schema; |
73 | 0 | YBSchemaBuilder b; |
74 | 0 | b.AddColumn("k")->Type(INT64)->NotNull()->HashPrimaryKey(); |
75 | 0 | ASSERT_OK(b.Build(&schema)); |
76 | |
|
77 | 0 | client_ = ASSERT_RESULT(cluster_->CreateClient()); |
78 | | |
79 | | // Create the namespace. |
80 | 0 | ASSERT_OK(client_->CreateNamespace(kNamespace)); |
81 | | |
82 | | // Create the table. |
83 | 0 | const YBTableName table_name(YQL_DATABASE_CQL, kNamespace, kTableName); |
84 | 0 | ASSERT_OK(client_ |
85 | 0 | ->NewTableCreator() |
86 | 0 | ->table_name(table_name) |
87 | 0 | .table_type(client::YBTableType::YQL_TABLE_TYPE) |
88 | 0 | .schema(&schema) |
89 | 0 | .num_tablets(kNumTablets) |
90 | 0 | .wait(true) |
91 | 0 | .Create()); |
92 | |
|
93 | 0 | ASSERT_OK(client_->OpenTable(table_name, &table_)); |
94 | 0 | } |
95 | | |
96 | 0 | void DoTearDown() override { |
97 | 0 | client_.reset(); |
98 | 0 | cluster_->Shutdown(); |
99 | 0 | } |
100 | | |
101 | | protected: |
102 | | |
103 | 0 | CHECKED_STATUS WriteData() { |
104 | 0 | auto session = client_->NewSession(); |
105 | 0 | session->SetTimeout(5s); |
106 | |
|
107 | 0 | std::shared_ptr<client::YBqlWriteOp> insert(table_->NewQLWrite()); |
108 | 0 | auto req = insert->mutable_request(); |
109 | 0 | GenerateDataForRow(table_->schema(), 17 /* record_id */, &random_, req); |
110 | |
|
111 | 0 | session->Apply(insert); |
112 | 0 | RETURN_NOT_OK(session->Flush()); |
113 | 0 | return Status::OK(); |
114 | 0 | } |
115 | | |
116 | 0 | Result<string> GetTabletDbPath() { |
117 | 0 | for (const auto& peer : cluster_->GetTabletPeers(0)) { |
118 | 0 | if (peer->table_type() == TableType::YQL_TABLE_TYPE) { |
119 | 0 | return peer->tablet_metadata()->rocksdb_dir(); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | return STATUS(IllegalState, "Did not find tablet peer with YCQL table"); |
123 | 0 | } |
124 | | |
125 | | std::unique_ptr<YBClient> client_; |
126 | | std::shared_ptr<YBTable> table_; |
127 | | Random random_; |
128 | | }; |
129 | | |
130 | | |
131 | 0 | TEST_F(YBTabletUtilTest, VerifySingleKeyIsFound) { |
132 | 0 | string output; |
133 | 0 | ASSERT_OK(WriteData()); |
134 | 0 | ASSERT_OK(cluster_->FlushTablets(tablet::FlushMode::kSync, tablet::FlushFlags::kAll)); |
135 | 0 | string db_path = ASSERT_RESULT(GetTabletDbPath()); |
136 | |
|
137 | 0 | vector<string> argv = { |
138 | 0 | GetToolPath(kTabletUtilToolName), |
139 | 0 | "dump", |
140 | 0 | "--compression_type=snappy", |
141 | 0 | "--db=" + db_path |
142 | 0 | }; |
143 | 0 | ASSERT_OK(Subprocess::Call(argv, &output)); |
144 | |
|
145 | 0 | ASSERT_NE(output.find("Keys in range: 1"), string::npos); |
146 | 0 | } |
147 | | |
148 | | } // namespace tools |
149 | | } // namespace yb |