/Users/deen/code/yugabyte-db/ent/src/yb/master/restore_sys_catalog_state_test.cc
Line | Count | Source |
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/master/master_backup.pb.h" |
17 | | #include "yb/master/master_snapshot_coordinator.h" |
18 | | #include "yb/master/restore_sys_catalog_state.h" |
19 | | |
20 | | #include "yb/util/oid_generator.h" |
21 | | #include "yb/util/result.h" |
22 | | #include "yb/util/test_macros.h" |
23 | | |
24 | | namespace yb { |
25 | | namespace master { |
26 | | |
27 | | void CheckMatch( |
28 | | const TableId& table_id, const SysTablesEntryPB& table_entry, RestoreSysCatalogState* state, |
29 | 18 | bool match) { |
30 | 18 | auto result = ASSERT_RESULT(state->TEST_MatchTable(table_id, table_entry)); |
31 | 18 | ASSERT_EQ(result, match); |
32 | 18 | } |
33 | | |
34 | 1 | TEST(RestoreSysCatalogStateTest, Filter) { |
35 | 1 | const NamespaceId kNamespaceId = GenerateObjectId(); |
36 | 1 | const NamespaceId kWrongNamespaceId = GenerateObjectId(); |
37 | 1 | const std::string kNamespaceName = "namespace"; |
38 | 1 | const std::string kWrongNamespaceName = "wrong namespace"; |
39 | 1 | const TableId kTableId = GenerateObjectId(); |
40 | 1 | const TableId kWrongTableId = GenerateObjectId(); |
41 | 1 | const std::string kTableName = "table"; |
42 | 1 | const std::string kWrongTableName = "wrong table"; |
43 | | |
44 | 1 | auto restoration = SnapshotScheduleRestoration { |
45 | 1 | .snapshot_id = TxnSnapshotId::GenerateRandom(), |
46 | 1 | .restore_at = HybridTime(), |
47 | 1 | .restoration_id = TxnSnapshotRestorationId::GenerateRandom(), |
48 | 1 | }; |
49 | 1 | RestoreSysCatalogState state(&restoration); |
50 | 1 | SysNamespaceEntryPB namespace_entry; |
51 | 1 | namespace_entry.set_name(kNamespaceName); |
52 | 1 | namespace_entry.set_database_type(YQLDatabase::YQL_DATABASE_CQL); |
53 | 1 | state.TEST_AddNamespace(kNamespaceId, namespace_entry); |
54 | | |
55 | 1 | SysTablesEntryPB table_entry; |
56 | 1 | table_entry.set_name(kTableName); |
57 | 1 | table_entry.set_namespace_id(kNamespaceId); |
58 | 1 | table_entry.set_namespace_name(kNamespaceName); |
59 | | |
60 | 1 | restoration.schedules.emplace_back(SnapshotScheduleId::Nil(), SnapshotScheduleFilterPB()); |
61 | 1 | auto& table_identifier = *restoration.schedules[0].second.mutable_tables()->add_tables(); |
62 | | |
63 | 2 | for (bool use_table_id : {true, false}) { |
64 | 2 | SCOPED_TRACE(Format("use_table_id: $0", use_table_id)); |
65 | 4 | for (bool good_table : {true, false}) { |
66 | 4 | SCOPED_TRACE(Format("good_table: $0", good_table)); |
67 | 4 | table_identifier.Clear(); |
68 | 4 | if (use_table_id) { |
69 | 1 | table_identifier.set_table_id(good_table ? kTableId : kWrongTableId); |
70 | 2 | CheckMatch(kTableId, table_entry, &state, good_table); |
71 | 2 | } else { |
72 | 1 | table_identifier.set_table_name(good_table ? kTableName : kWrongTableName); |
73 | 4 | for (bool use_namespace_id : {true, false}) { |
74 | 4 | SCOPED_TRACE(Format("use_namespace_id: $0", use_namespace_id)); |
75 | 8 | for (bool good_namespace : {true, false}) { |
76 | 8 | SCOPED_TRACE(Format("good_namespace: $0", good_namespace)); |
77 | 8 | auto& ns = *table_identifier.mutable_namespace_(); |
78 | 8 | ns.Clear(); |
79 | 8 | if (use_namespace_id) { |
80 | 2 | ns.set_id(good_namespace ? kNamespaceId : kWrongNamespaceId); |
81 | 4 | CheckMatch(kTableId, table_entry, &state, good_table && good_namespace); |
82 | 4 | } else { |
83 | 2 | ns.set_name(good_namespace ? kNamespaceName : kWrongNamespaceName); |
84 | 4 | CheckMatch(kTableId, table_entry, &state, good_table && good_namespace); |
85 | 4 | ns.set_database_type(YQLDatabase::YQL_DATABASE_CQL); |
86 | 4 | CheckMatch(kTableId, table_entry, &state, good_table && good_namespace); |
87 | 4 | ns.set_database_type(YQLDatabase::YQL_DATABASE_PGSQL); |
88 | 4 | CheckMatch(kTableId, table_entry, &state, false); |
89 | 4 | } |
90 | 8 | } |
91 | 4 | } |
92 | 2 | } |
93 | 4 | } |
94 | 2 | } |
95 | 1 | } |
96 | | |
97 | | } // namespace master |
98 | | } // namespace yb |