YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/placement_info-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <string>
34
#include <gtest/gtest.h>
35
36
#include "yb/common/placement_info.h"
37
#include "yb/util/test_macros.h"
38
39
namespace yb {
40
41
// Test the tablespace info parsing.
42
1
TEST(PlacementInfoTest, TestTablespaceJsonProcessing) {
43
  // Variables to be used throughout the test.
44
1
  const string& valid_json =
45
1
      "{\"num_replicas\":3,\"placement_blocks\":"
46
1
      "[{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\",\"min_num_replicas\":2},"
47
1
      "{\"cloud\":\"c2\",\"region\":\"r2\",\"zone\":\"z2\",\"min_num_replicas\":1}]}";
48
49
1
  QLValuePB option, invalid_option;
50
1
  option.set_string_value("replica_placement=" + valid_json);
51
1
  invalid_option.set_string_value("read_replica_placement=" + valid_json);
52
53
1
  vector<QLValuePB> options;
54
55
  // Negative tests.
56
  // 1. Empty input.
57
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
58
59
  // 2. Invalid number of options.
60
1
  options.push_back(option);
61
1
  options.push_back(invalid_option);
62
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
63
64
  // 3. Invalid option name.
65
1
  options.clear();
66
1
  options.push_back(invalid_option);
67
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
68
69
  // 4. Empty json.
70
1
  options.clear();
71
1
  QLValuePB opt_empty_value;
72
1
  opt_empty_value.set_string_value("replica_placement=[{}]");
73
1
  options.emplace_back(opt_empty_value);
74
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
75
76
  // 5. Missing num_replicas field.
77
1
  options.clear();
78
1
  QLValuePB invalid_json_option;
79
1
  invalid_json_option.set_string_value("replica_placement={\"placement_blocks\":"
80
1
      "[{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\",\"min_num_replicas\":3}]}");
81
1
  options.emplace_back(invalid_json_option);
82
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
83
84
  // 6. Invalid value for num_replicas field.
85
1
  options.clear();
86
1
  invalid_json_option.set_string_value(
87
1
      "replica_placement={\"num_replicas\":\"abc\",\"placement_blocks\":"
88
1
      "[{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\",\"min_num_replicas\":3}]}");
89
1
  options.emplace_back(invalid_json_option);
90
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
91
92
  // 7. Missing placement blocks field.
93
1
  options.clear();
94
1
  invalid_json_option.set_string_value("replica_placement={\"num_replicas\":3}");
95
1
  options.emplace_back(invalid_json_option);
96
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
97
98
  // 8. Missing keys in placement blocks.
99
1
  options.clear();
100
1
  invalid_json_option.set_string_value(
101
1
      "replica_placement={\"num_replicas\":\"abc\",\"placement_blocks\":"
102
1
      "[{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\"}]}");
103
1
  options.emplace_back(invalid_json_option);
104
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
105
106
  // 9. Invalid format for "min_num_replicas".
107
1
  options.clear();
108
1
  invalid_json_option.set_string_value(
109
1
        "replica_placement={\"num_replicas\":3,\"placement_blocks\":"
110
1
        "[{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\",\"min_num_replicas\":\"abc\"}]}");
111
1
  options.emplace_back(invalid_json_option);
112
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
113
114
  // 10. Invalid json.
115
1
  options.clear();
116
1
  invalid_json_option.set_string_value("replica_placement=["
117
1
      "{\"cloud\":\"c1\",\"region\":\"r1\",\"zone\":\"z1\",\"min_number_of_replica");
118
1
  options.emplace_back(invalid_json_option);
119
1
  ASSERT_NOK(PlacementInfoConverter::FromQLValue(options));
120
121
  // 11. Test whether total replication factor is populated correctly.
122
1
  options.clear();
123
1
  options.push_back(option);
124
1
  PlacementInfoConverter::Placement result = EXPECT_RESULT(
125
1
      PlacementInfoConverter::FromQLValue(options));
126
1
  const auto placement_infos = result.placement_infos;
127
1
  ASSERT_EQ(result.num_replicas, 3);
128
129
  // 12. Test whether the cloud/region/zone information has been populated correctly.
130
1
  ASSERT_EQ(placement_infos.size(), 2);
131
2
  for (auto& placement_block : placement_infos) {
132
2
    if (placement_block.cloud == "c1") {
133
1
      ASSERT_EQ(placement_block.region, "r1");
134
1
      ASSERT_EQ(placement_block.zone, "z1");
135
1
      ASSERT_EQ(placement_block.min_num_replicas, 2);
136
1
      continue;
137
1
    }
138
1
    ASSERT_EQ(placement_block.cloud, "c2");
139
1
    ASSERT_EQ(placement_block.region, "r2");
140
1
    ASSERT_EQ(placement_block.zone, "z2");
141
1
    ASSERT_EQ(placement_block.min_num_replicas, 1);
142
1
  }
143
144
1
}
145
146
} // namespace yb