]>
git.ipfire.org Git - people/ms/pakfire.git/blob - tests/testsuite.c
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2017 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <linux/limits.h>
25 #include "testsuite.h"
27 #include <pakfire/logging.h>
28 #include <pakfire/mount.h>
29 #include <pakfire/pakfire.h>
30 #include <pakfire/util.h>
32 #define TMP_TEMPLATE "/tmp/pakfire-test.XXXXXX"
36 static int test_run(int i
, struct test
* t
) {
37 struct pakfire_ctx
* ctx
= NULL
;
38 struct pakfire
* p
= NULL
;
41 char root
[PATH_MAX
] = TEST_ROOTFS
"/pakfire-test-XXXXXX";
44 // Create test root directory
45 char* tmp
= pakfire_mkdtemp(root
);
47 LOG("Could not create temporary directory %s: %m\n", root
);
51 LOG("running %s (%s)\n", t
->name
, root
);
53 // Create a new context
54 r
= pakfire_ctx_create(&t
->ctx
, TEST_CONFIG_FILE
);
56 LOG("Could not create context: %m\n");
60 // Set the log level to DEBUG
61 pakfire_ctx_set_log_level(t
->ctx
, LOG_DEBUG
);
63 // Log everything to the console
64 pakfire_ctx_set_log_callback(t
->ctx
, pakfire_log_stderr
, NULL
);
66 // Open the configuration file
67 c
= fopen(TEST_SRC_PATH
"/pakfire.conf", "r");
69 LOG("Could not open configuration file: %m\n");
74 // Create a pakfire instance (if requested)
75 if (t
->flags
& TEST_WANTS_PAKFIRE
) {
76 r
= pakfire_create(&t
->pakfire
, t
->ctx
, root
, NULL
, c
, 0);
78 LOG("ERROR: Could not initialize pakfire: %m\n");
82 // Check if the instance was created properly
83 if (r
== 0 && !t
->pakfire
) {
84 LOG("ERROR: Pakfire was not initialized, but no error was raised: %m\n");
88 // Copy command into environment
89 r
= pakfire_bind(t
->pakfire
, TEST_STUB_COMMAND
, "/command", 0);
91 LOG("ERROR: Could not copy command: %m\n");
96 // Create a HTTP client (if requested)
97 if (t
->flags
& TEST_WANTS_HTTPCLIENT
) {
98 r
= pakfire_httpclient_create(&t
->httpclient
, t
->ctx
);
100 LOG("ERROR: Could not initialize the HTTP client: %s\n", strerror(-r
));
108 LOG("Test failed with error code: %d\n", r
);
111 // Release HTTP client
113 pakfire_httpclient_unref(t
->httpclient
);
117 p
= pakfire_unref(t
->pakfire
);
119 // Check if Pakfire was actually released
121 LOG("Error: Pakfire instance was not released in test %d\n", i
);
125 // Reset pointer (just in case)
131 ctx
= pakfire_ctx_unref(t
->ctx
);
133 // Check if the context was actually released
135 LOG("Error: Context was not released in test %s\n", t
->name
);
139 // Reset pointer (just in case)
143 // Close the configuration file
148 pakfire_rmtree(root
, 0);
153 int __testsuite_add_test(const char* name
, int (*func
)(const struct test
* t
), int flags
) {
154 // Check if any space is left
155 if (ts
.num
>= MAX_TESTS
) {
156 LOG("ERROR: We are out of space for tests\n");
160 struct test
* test
= &ts
.tests
[ts
.num
++];
170 static int check_whether_to_run(const struct test
* t
, const int argc
, const char* argv
[]) {
171 // Run all tests when nothing has been selected
175 // Check if this test has been listed
176 for (unsigned int i
= 1; i
< argc
; i
++) {
177 if (strcmp(t
->name
, argv
[i
]) == 0)
184 int testsuite_run(int argc
, const char* argv
[]) {
185 for (unsigned int i
= 0; i
< ts
.num
; i
++) {
186 struct test
* test
= &ts
.tests
[i
];
188 // Skip any tests that should not be run
189 if (!check_whether_to_run(test
, argc
, argv
))
193 int r
= test_run(i
, test
);
201 FILE* test_mktemp(char** path
) {
208 int r
= asprintf(&p
, "%s", TMP_TEMPLATE
);
216 // If we want a named temporary file, we set path
220 // Otherwise we unlink the path and free p
226 return fdopen(fd
, "w+");
229 char* test_mkdtemp() {
230 char path
[] = TMP_TEMPLATE
;
232 char* p
= mkdtemp(path
);