1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author hongqing.hu |
17 | * \date Dec 2020 |
18 | * \brief |
19 | */ |
20 | |
21 | #include <gtest/gtest.h> |
22 | |
23 | #define private public |
24 | #include "repository/binlog/mysql_validator.h" |
25 | #include "mock_mysql_connector.h" |
26 | #undef private |
27 | |
28 | #include "repository/repository_common/error_code.h" |
29 | |
30 | using namespace ::proxima::be; |
31 | using namespace proxima::be::repository; |
32 | |
33 | class MysqlValidatorTest : public testing::Test { |
34 | protected: |
35 | void SetUp() { |
36 | mgr_ = std::make_shared<MysqlConnectorManager>(); |
37 | ASSERT_TRUE(mgr_); |
38 | connector_ = std::make_shared<MockMysqlConnector>(); |
39 | ASSERT_TRUE(connector_); |
40 | mgr_->put(connector_); |
41 | } |
42 | |
43 | void TearDown() {} |
44 | |
45 | MockMysqlResultWrapperPtr BuildSelectVersionResult() { |
46 | MockMysqlResultWrapperPtr result = |
47 | std::make_shared<MockMysqlResultWrapper>(); |
48 | result->append_field_meta("VERSION()" ); |
49 | std::vector<std::string> values1 = {"5.7.10-log" }; |
50 | result->append_row_values(values1); |
51 | return result; |
52 | } |
53 | |
54 | MockMysqlResultWrapperPtr BuildShowBinlogResult() { |
55 | MockMysqlResultWrapperPtr result = |
56 | std::make_shared<MockMysqlResultWrapper>(); |
57 | result->append_field_meta("Variable_name" ); |
58 | result->append_field_meta("Value" ); |
59 | std::vector<std::string> values1 = {"binlog_format" , "ROW" }; |
60 | result->append_row_values(values1); |
61 | return result; |
62 | } |
63 | |
64 | MockMysqlResultWrapperPtr BuildSelectDbResult() { |
65 | MockMysqlResultWrapperPtr result = |
66 | std::make_shared<MockMysqlResultWrapper>(); |
67 | result->append_field_meta("SCHEMA_NAME" ); |
68 | std::vector<std::string> values1 = {"mytest" }; |
69 | result->append_row_values(values1); |
70 | return result; |
71 | } |
72 | |
73 | protected: |
74 | MysqlConnectorManagerPtr mgr_{}; |
75 | MockMysqlConnectorPtr connector_{}; |
76 | }; |
77 | |
78 | TEST_F(MysqlValidatorTest, TestSimple) { |
79 | MysqlValidator validator(mgr_); |
80 | int ret = validator.init(); |
81 | ASSERT_EQ(ret, 0); |
82 | |
83 | MockMysqlResultWrapperPtr result = BuildSelectVersionResult(); |
84 | EXPECT_CALL(*connector_, execute_query(_, _, _)) |
85 | .WillOnce(Invoke([&result](const std::string &, |
86 | MysqlResultWrapperPtr *out, bool) -> int { |
87 | *out = result; |
88 | return 0; |
89 | })) |
90 | .RetiresOnSaturation(); |
91 | ASSERT_TRUE(validator.validate_version()); |
92 | |
93 | MockMysqlResultWrapperPtr result1 = BuildShowBinlogResult(); |
94 | EXPECT_CALL(*connector_, execute_query(_, _, _)) |
95 | .WillOnce(Invoke([&result1](const std::string &, |
96 | MysqlResultWrapperPtr *out, bool) -> int { |
97 | *out = result1; |
98 | return 0; |
99 | })) |
100 | .RetiresOnSaturation(); |
101 | ASSERT_TRUE(validator.validate_binlog_format()); |
102 | |
103 | const std::string connection_uri = "mysql://root:[email protected]:3306/mytest" ; |
104 | ailego::Uri uri; |
105 | ailego::Uri::Parse(connection_uri.c_str(), &uri); |
106 | |
107 | MockMysqlResultWrapperPtr result2 = BuildSelectDbResult(); |
108 | EXPECT_CALL(*connector_, uri()) |
109 | .WillOnce(Invoke([&uri]() -> const ailego::Uri & { return uri; })) |
110 | .RetiresOnSaturation(); |
111 | EXPECT_CALL(*connector_, execute_query(_, _, _)) |
112 | .WillOnce(Invoke([&result2](const std::string &, |
113 | MysqlResultWrapperPtr *out, bool) -> int { |
114 | *out = result2; |
115 | return 0; |
116 | })) |
117 | .RetiresOnSaturation(); |
118 | ASSERT_TRUE(validator.validate_database_exist()); |
119 | } |
120 | |