|
| 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 "yb/integration-tests/cql_test_base.h" |
| 15 | + |
| 16 | +#include "yb/tserver/mini_tablet_server.h" |
| 17 | +#include "yb/tserver/tablet_server.h" |
| 18 | +#include "yb/util/test_util.h" |
| 19 | +#include "yb/util/thread.h" |
| 20 | + |
| 21 | +using std::make_unique; |
| 22 | +using std::string; |
| 23 | +using std::unique_ptr; |
| 24 | + |
| 25 | +DECLARE_bool(TEST_mini_cluster_mode); |
| 26 | + |
| 27 | +namespace yb { |
| 28 | + |
| 29 | +const string kDefaultTableName = "users"; |
| 30 | + |
| 31 | +class CqlBackupTest : public CqlTestBase<MiniCluster> { |
| 32 | + public: |
| 33 | + virtual ~CqlBackupTest() = default; |
| 34 | + |
| 35 | + void SetUp() override { |
| 36 | + FLAGS_TEST_mini_cluster_mode = true; // Provide correct '--fs_data_dirs' via TS Web UI. |
| 37 | + CqlTestBase<MiniCluster>::SetUp(); |
| 38 | + |
| 39 | + backup_dir_ = GetTempDir("backup"); |
| 40 | + session_ = make_unique<CassandraSession>( |
| 41 | + ASSERT_RESULT(EstablishSession(driver_.get()))); |
| 42 | + } |
| 43 | + |
| 44 | + void cql(const string& query) { |
| 45 | + ASSERT_OK(session_->ExecuteQuery(query)); |
| 46 | + } |
| 47 | + |
| 48 | + void createTestTable(const string& table_name = kDefaultTableName) { |
| 49 | + cql("CREATE TABLE " + table_name + "(userid INT PRIMARY KEY, fullname TEXT)"); |
| 50 | + cql("INSERT INTO " + table_name + " (userid, fullname) values (1, 'yb');"); |
| 51 | + |
| 52 | + auto result = ASSERT_RESULT(session_->ExecuteWithResult("SELECT count(*) FROM " + table_name)); |
| 53 | + auto iterator = result.CreateIterator(); |
| 54 | + ASSERT_TRUE(iterator.Next()); |
| 55 | + auto count = iterator.Row().Value(0).As<int64>(); |
| 56 | + EXPECT_EQ(count, 1); |
| 57 | + } |
| 58 | + |
| 59 | + yb::ThreadPtr stopWebServerAndStartAfter(size_t tsIdx, double startAfterSec) { |
| 60 | + Webserver* web_server = cluster_->mini_tablet_server(tsIdx)->server()->TEST_web_server(); |
| 61 | + CHECK_NOTNULL(web_server)->Stop(); |
| 62 | + SleepFor(MonoDelta::FromSeconds(0.5)); // Let the server stop listening. |
| 63 | + |
| 64 | + // A thread that starts the stopped WebServer after some time. |
| 65 | + yb::ThreadPtr thread; |
| 66 | + EXPECT_OK(yb::Thread::Create( |
| 67 | + CURRENT_TEST_NAME(), "web_server_starter", |
| 68 | + [web_server, startAfterSec]() { |
| 69 | + // Start the server after the specified number of seconds. |
| 70 | + SleepFor(MonoDelta::FromSeconds(startAfterSec)); |
| 71 | + EXPECT_OK(web_server->Start()); |
| 72 | + }, &thread)); |
| 73 | + return thread; |
| 74 | + } |
| 75 | + |
| 76 | + protected: |
| 77 | + string backup_dir_; |
| 78 | + unique_ptr<CassandraSession> session_; |
| 79 | +}; |
| 80 | + |
| 81 | +TEST_F(CqlBackupTest, YB_DISABLE_TEST_IN_SANITIZERS_OR_MAC(TestBackupWithoutTSWebUI)) { |
| 82 | + createTestTable(); |
| 83 | + |
| 84 | + // A thread that starts the stopped WebServer after 130 sec. as retry round = 110 sec. |
| 85 | + yb::ThreadPtr thread = stopWebServerAndStartAfter(0, 130); |
| 86 | + |
| 87 | + ASSERT_OK(RunBackupCommand( |
| 88 | + {"--backup_location", backup_dir_, "--keyspace", kCqlTestKeyspace, "create"})); |
| 89 | + |
| 90 | + thread->Join(); |
| 91 | + cql("DROP TABLE " + kDefaultTableName); |
| 92 | + LOG(INFO) << "Test finished: " << CURRENT_TEST_CASE_AND_TEST_NAME_STR(); |
| 93 | +} |
| 94 | + |
| 95 | +TEST_F(CqlBackupTest, YB_DISABLE_TEST_IN_SANITIZERS_OR_MAC(TestBackupRestoreWithoutTSWebUI)) { |
| 96 | + createTestTable(); |
| 97 | + |
| 98 | + ASSERT_OK(RunBackupCommand( |
| 99 | + {"--backup_location", backup_dir_, "--keyspace", kCqlTestKeyspace, "create"})); |
| 100 | + |
| 101 | + // A thread that starts the stopped WebServer after 110 sec. as retry round = 90 sec. |
| 102 | + yb::ThreadPtr thread = stopWebServerAndStartAfter(0, 110); |
| 103 | + |
| 104 | + ASSERT_OK(RunBackupCommand( |
| 105 | + {"--backup_location", backup_dir_, "--keyspace", kCqlTestKeyspace, "restore"})); |
| 106 | + |
| 107 | + thread->Join(); |
| 108 | + cql("DROP TABLE " + kDefaultTableName); |
| 109 | + LOG(INFO) << "Test finished: " << CURRENT_TEST_CASE_AND_TEST_NAME_STR(); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace yb |
0 commit comments