-
Notifications
You must be signed in to change notification settings - Fork 10
/
tests.c
291 lines (246 loc) · 9.5 KB
/
tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <locale.h>
#include <glib.h>
#include <solv/testcase.h>
#define ARCH "x86_64"
#define PLATFORM "f29"
#define ADD_TEST(name, dir) \
g_test_add(name, TestData, dir, test_setup, test_run, test_teardown)
#define ADD_SOLV_FAIL_TEST(name, dir) \
g_test_add(name, TestData, dir, test_setup, test_broken_dep, test_teardown)
typedef struct _test_data {
GPtrArray *repos;
GStrv solvables;
GStrv excluded;
gchar *expected;
} TestData;
extern GPtrArray *fus_depsolve(const char *, const char *, const GStrv, const GStrv, const GStrv, GError **);
static void
test_broken_dep (TestData *td, gconstpointer data)
{
GStrv repos = (char **)td->repos->pdata;
const gchar *dir = data;
if (g_test_subprocess ())
{
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) result = NULL;
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
"*Can't resolve all solvables*");
result = fus_depsolve (ARCH, PLATFORM, NULL, repos, td->solvables, &error);
g_assert (result != NULL);
g_assert_no_error (error);
g_ptr_array_add (result, NULL); /* Need by g_strjoinv below */
g_autofree char *strres = g_strjoinv ("\n", (char **)result->pdata);
g_autofree char *diff = testcase_resultdiff (td->expected, strres);
g_assert_cmpstr (diff, ==, NULL);
g_test_assert_expected_messages ();
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
const char *probfile = g_test_get_filename (G_TEST_DIST, dir, "problems", NULL);
if (g_file_test (probfile, G_FILE_TEST_IS_REGULAR))
{
g_autofree char *content = NULL;
g_autoptr(GError) error = NULL;
g_file_get_contents (probfile, &content, NULL, &error);
g_assert_no_error (error);
g_assert (content != NULL);
g_test_trap_assert_stdout (content);
}
}
static void
test_fail_invalid_solvable (void)
{
GStrv repos = NULL;
gchar *solvables[] = {"invalid", NULL};
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) result = NULL;
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
"*Nothing matches 'invalid'*");
result = fus_depsolve (ARCH, PLATFORM, NULL, repos, solvables, &error);
g_assert (result == NULL);
g_assert_cmpstr (error->message, ==, "No solvables matched");
g_test_assert_expected_messages ();
}
static void
test_fail_no_solvables (void)
{
GStrv repos = NULL;
GStrv solvables = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) result = NULL;
result = fus_depsolve (ARCH, PLATFORM, NULL, repos, solvables, &error);
g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED);
g_assert (result == NULL);
g_assert_cmpstr (error->message, ==, "No solvables matched");
}
static void
test_invalid_repo (void)
{
gchar *repos[] = {"repo,repo,invalid/packages.repo"};
gchar *solvables[] = {"invalid"};
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) result = NULL;
result = fus_depsolve (ARCH, PLATFORM, NULL, repos, solvables, &error);
g_assert (result == NULL);
g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED);
g_assert_cmpstr (error->message, ==,
"Could not open invalid/packages.repo: No such file or directory");
}
static void
test_run (TestData *td, gconstpointer data)
{
GStrv repos = (char **)td->repos->pdata;
if (g_test_subprocess ())
{
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) result = NULL;
result = fus_depsolve (ARCH, PLATFORM, NULL, repos, td->solvables, &error);
g_assert_no_error (error);
g_assert (result != NULL);
g_ptr_array_add (result, NULL); /* Need by g_strjoinv below */
g_autofree char *strres = g_strjoinv ("\n", (char **)result->pdata);
g_autofree char *diff = testcase_resultdiff (td->expected, strres);
g_assert_cmpstr (diff, ==, NULL);
return;
}
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
g_test_trap_assert_stdout_unmatched ("*Problem * / *");
g_test_trap_assert_stderr_unmatched ("*Nothing matches*");
g_test_trap_assert_stderr_unmatched ("*Can't resolve all solvables*");
}
static void
test_order (TestData *td, gconstpointer data)
{
const gchar *dir = data;
g_autoptr(GError) error = NULL;
g_autofree char *content = NULL;
const gchar *infile = g_test_get_filename (G_TEST_DIST, dir, "input", NULL);
g_file_get_contents (infile, &content, NULL, &error);
g_assert_no_error (error);
g_assert (content != NULL);
GStrv input = g_strsplit (content, "\n", -1);
int len = g_strv_length (input);
/* Try out some input permutations */
for (int i = 0; i < len / 2; i++) {
/* swap */
char *tmp = input[i];
input[i] = input[len - i - 1];
input[len - i - 1] = tmp;
test_run (td, data);
}
g_strfreev (input);
}
static void
test_teardown (TestData *tdata, gconstpointer data)
{
g_ptr_array_unref (tdata->repos);
if (tdata->solvables)
g_strfreev (tdata->solvables);
if (tdata->excluded)
g_strfreev (tdata->excluded);
if (tdata->expected)
g_free (tdata->expected);
}
static void
test_setup (TestData *tdata, gconstpointer data)
{
g_autoptr(GError) error = NULL;
const char *testname = data;
g_debug ("Setting up test %s", testname);
const char *testpath = g_test_get_filename (G_TEST_DIST, testname, NULL);
g_assert (g_file_test (testpath, G_FILE_TEST_IS_DIR));
/* Read input file */
g_autofree char *inpath = g_build_filename (testpath, "input", NULL);
g_assert (g_file_test (inpath, G_FILE_TEST_IS_REGULAR));
tdata->solvables = g_malloc (2 * sizeof(tdata->solvables));
tdata->solvables[0] = g_strconcat ("@", inpath, NULL);
tdata->solvables[1] = NULL;
/* Read expected output file */
g_autofree char *outpath = g_build_filename (testpath, "expected", NULL);
g_assert (g_file_test (outpath, G_FILE_TEST_IS_REGULAR));
g_file_get_contents (outpath, &tdata->expected, NULL, &error);
g_assert_no_error (error);
g_assert (tdata->expected);
/* Read excluded packages file */
g_autofree char *exclude_path = g_build_filename (testpath, "excludes", NULL);
if (g_file_test (exclude_path, G_FILE_TEST_IS_REGULAR))
{
g_autofree char *excluded = NULL;
g_file_get_contents (exclude_path, &excluded, NULL, &error);
g_assert_no_error (error);
g_assert (excluded != NULL);
tdata->excluded = g_strsplit (excluded, "\n", -1);
/* Remove spurious empty string */
size_t len = g_strv_length (tdata->excluded);
if (len && !*tdata->excluded[len - 1])
{
g_free (tdata->excluded[len - 1]);
tdata->excluded[len - 1] = NULL;
}
}
/* Read local test repositories */
tdata->repos = g_ptr_array_new_full (4, g_free);
g_autofree char *repo_path = g_build_filename (testpath, "packages.repo", NULL);
if (g_file_test (repo_path, G_FILE_TEST_IS_REGULAR))
{
g_ptr_array_add (tdata->repos, g_strdup_printf ("repo,repo,%s", repo_path));
g_debug (" repo: %s", repo_path);
}
g_autofree char *lookaside_path = g_build_filename (testpath, "lookaside.repo", NULL);
if (g_file_test (lookaside_path, G_FILE_TEST_IS_REGULAR))
{
g_ptr_array_add (tdata->repos, g_strdup_printf ("repo-0,lookaside,%s", lookaside_path));
g_debug (" lookaside: %s", lookaside_path);
}
g_autofree char *yaml_path = g_build_filename (testpath, "modules.yaml", NULL);
if (g_file_test (yaml_path, G_FILE_TEST_IS_REGULAR))
{
g_ptr_array_add (tdata->repos, g_strdup_printf ("yaml,modular,%s", yaml_path));
g_debug (" yaml: %s", yaml_path);
}
g_ptr_array_add (tdata->repos, NULL);
}
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_bug_base ("https://github.com/fedora-modularity/fus/issues");
/*
* By default, g_test_init set log flags to be fatal on CRITICAL and WARNING
* levels. We overwrite this setting here so that fus does not abort on
* g_warning. This allows us to check both for warning messages and the
* result of the solving.
*/
g_log_set_always_fatal (G_LOG_FATAL_MASK | G_LOG_LEVEL_CRITICAL);
ADD_TEST ("/ursine/simple", "ursine");
ADD_TEST ("/ursine/masking", "masking");
ADD_TEST ("/require/negative", "negative");
ADD_TEST ("/require/positive", "positive");
ADD_TEST ("/require/empty", "empty");
ADD_TEST ("/require/alternatives", "alternatives");
ADD_TEST ("/module/empty", "empty-module");
ADD_TEST ("/solvable-selection/pull-bare", "pull-bare");
ADD_TEST ("/solvable-selection/pull-from-default-stream", "pull-default-module");
ADD_TEST ("/solvable-selection/explicit-nevra", "explicit-nevra");
ADD_TEST ("/ursine/ignore-weak-deps", "weak-deps");
g_test_add ("/solvable-selection/order",
TestData,
"order",
test_setup,
test_order,
test_teardown);
g_test_add_func ("/fail/invalid-repo", test_invalid_repo);
g_test_add_func ("/fail/no-solvables", test_fail_no_solvables);
g_test_add_func ("/fail/invalid-solvable", test_fail_invalid_solvable);
ADD_TEST ("/ursine/default-stream-dep", "default-stream");
ADD_TEST ("/ursine/prefer-over-non-default-stream", "non-default-stream");
ADD_TEST ("/lookaside/same-repo", "input-as-lookaside");
ADD_SOLV_FAIL_TEST ("/fail/ursine/broken", "ursine-broken");
ADD_SOLV_FAIL_TEST ("/fail/module/broken", "module-broken");
ADD_SOLV_FAIL_TEST ("/fail/moddep/broken", "moddep-broken");
ADD_TEST ("/module/multiple", "build-in-more-modules");
ADD_TEST ("/modulemd-packager-v3/static-context", "static-context");
return g_test_run ();
}