|
| 1 | +// Copyright 2025 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <assuan.h> |
| 16 | +#include <algorithm> |
| 17 | +#include <chrono> |
| 18 | +#include <string> |
| 19 | +#include <thread> |
| 20 | + |
| 21 | +#include "createrepo-agent/command.h" |
| 22 | +#include "createrepo-agent/common.h" |
| 23 | +#include "createrepo-agent/server.h" |
| 24 | +#include "utils.hpp" |
| 25 | + |
| 26 | +#ifndef INTEGRATION_UTILS_HPP_ |
| 27 | +#define INTEGRATION_UTILS_HPP_ |
| 28 | + |
| 29 | +namespace fs = std::filesystem; |
| 30 | + |
| 31 | +class TempRepo : public TempDir, public testing::WithParamInterface<std::string_view> |
| 32 | +{ |
| 33 | +public: |
| 34 | + static |
| 35 | + std::string |
| 36 | + PrintParamName(const testing::TestParamInfo<ParamType> & info) |
| 37 | + { |
| 38 | + return std::string(info.param); |
| 39 | + } |
| 40 | + |
| 41 | +protected: |
| 42 | + void |
| 43 | + SetUp() override |
| 44 | + { |
| 45 | + fs::path fixture = fs::absolute("fixtures") / GetParam(); |
| 46 | + ASSERT_TRUE(fs::is_directory(fixture)); |
| 47 | + |
| 48 | + TempDir::SetUp(); |
| 49 | + |
| 50 | + fs::copy(fixture, temp_dir); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +class CRATempServer : public TempRepo |
| 55 | +{ |
| 56 | +protected: |
| 57 | + void |
| 58 | + SetUp() override |
| 59 | + { |
| 60 | + TempRepo::SetUp(); |
| 61 | + ASSERT_FALSE(assuan_new(&client)); |
| 62 | + |
| 63 | + sock_path = temp_dir / CRA_SOCK_NAME; |
| 64 | + |
| 65 | + server_fd = create_server_socket(sock_path.c_str()); |
| 66 | + ASSERT_NE(server_fd, ASSUAN_INVALID_FD); |
| 67 | + |
| 68 | + /* Start the server thread */ |
| 69 | + handler_thread = std::thread(command_handler, server_fd, temp_dir.c_str()); |
| 70 | + |
| 71 | + /* Wait for client to connect */ |
| 72 | + for (int i = 0; assuan_socket_connect(client, sock_path.c_str(), ASSUAN_INVALID_PID, 0); i++) { |
| 73 | + ASSERT_LT(i, 10); |
| 74 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void |
| 79 | + TearDown() override |
| 80 | + { |
| 81 | + if (!::testing::Test::HasFailure()) { |
| 82 | + EXPECT_FALSE(assuan_transact(client, "SHUTDOWN", NULL, NULL, NULL, NULL, NULL, NULL)); |
| 83 | + assuan_release(client); |
| 84 | + } |
| 85 | + |
| 86 | + if (::testing::Test::HasFailure()) { |
| 87 | + /* Something has already failed. Detach the server thread and let it |
| 88 | + * die independently so that we don't clobber the real failure */ |
| 89 | + handler_thread.detach(); |
| 90 | + } else { |
| 91 | + handler_thread.join(); |
| 92 | + assuan_sock_close(server_fd); |
| 93 | + server_fd = ASSUAN_INVALID_FD; |
| 94 | + } |
| 95 | + |
| 96 | + TempRepo::TearDown(); |
| 97 | + } |
| 98 | + |
| 99 | + fs::path sock_path; |
| 100 | + assuan_context_t client; |
| 101 | + |
| 102 | +private: |
| 103 | + assuan_fd_t server_fd = ASSUAN_INVALID_FD; |
| 104 | + std::thread handler_thread; |
| 105 | +}; |
| 106 | + |
| 107 | +#endif // INTEGRATION_UTILS_HPP_ |
0 commit comments