Skip to content

Commit 3456c95

Browse files
authored
Add testing fixture for Assuan client and server (#13)
This fixture starts a createrepo-agent server in a thread and connects a client to that server so that tests can be implemented to validate operations performed by the client.
1 parent 2f05bc6 commit 3456c95

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ macro(add_cache_test NAME)
1515
add_test(NAME ${NAME} COMMAND test_${NAME})
1616
endmacro()
1717

18+
macro(add_integration_test NAME)
19+
add_executable(test_${NAME} "integration_main.cpp" "test_${NAME}.cpp")
20+
target_link_libraries(test_${NAME}
21+
${GTEST_LIBRARIES}
22+
createrepo-agent-lib)
23+
add_test(NAME ${NAME} COMMAND test_${NAME})
24+
endmacro()
25+
1826
add_test(NAME agent_help COMMAND createrepo-agent --help)
1927
add_test(NAME agent_version COMMAND createrepo-agent --version)
2028

@@ -25,3 +33,5 @@ add_cache_test(copy_file)
2533
add_cache_test(repo_cache_flush)
2634
add_cache_test(repo_cache_invalidate)
2735
add_cache_test(repo_cache_modify)
36+
37+
add_integration_test(smoke)

test/integration_main.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 <gpgme.h>
17+
#include <gtest/gtest.h>
18+
19+
int main(int argc, char * argv[])
20+
{
21+
int ret;
22+
23+
gpgrt_check_version(NULL);
24+
gpgme_check_version(NULL);
25+
assuan_sock_init();
26+
::testing::InitGoogleTest(&argc, argv);
27+
ret = RUN_ALL_TESTS();
28+
assuan_sock_deinit();
29+
return ret;
30+
}

test/integration_utils.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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_

test/test_smoke.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 <gtest/gtest.h>
16+
17+
#include "integration_utils.hpp"
18+
19+
class smoke : public CRATempServer {};
20+
21+
TEST_P(smoke, commit) {
22+
gpg_error_t rc = assuan_transact(client, "COMMIT", NULL, NULL, NULL, NULL, NULL, NULL);
23+
EXPECT_FALSE(rc);
24+
}
25+
INSTANTIATE_TEST_SUITE_P( , smoke, testing::Values("empty", "populated"), smoke::PrintParamName);

0 commit comments

Comments
 (0)