Skip to content

Commit 9849922

Browse files
mmathesiussgallagher
authored andcommitted
If modulemd-packager documents are found while reading directly into a module index,
report them as subdocument failures and discard them. Signed-off-by: Merlin Mathesius <[email protected]>
1 parent 6cb6069 commit 9849922

File tree

4 files changed

+138
-81
lines changed

4 files changed

+138
-81
lines changed

modulemd/modulemd-module-index.c

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -155,51 +155,12 @@ add_subdoc (ModulemdModuleIndex *self,
155155
switch (doctype)
156156
{
157157
case MODULEMD_YAML_DOC_PACKAGER:
158-
if (modulemd_subdocument_info_get_mdversion (subdoc) <
159-
MD_PACKAGER_VERSION_TWO)
160-
{
161-
g_set_error (error,
162-
MODULEMD_YAML_ERROR,
163-
MMD_YAML_ERROR_PARSE,
164-
"Invalid mdversion for a packager document");
165-
return FALSE;
166-
}
167-
168-
if (modulemd_subdocument_info_get_mdversion (subdoc) ==
169-
MD_PACKAGER_VERSION_THREE)
170-
{
171-
packager = modulemd_packager_v3_parse_yaml (subdoc, error);
172-
173-
index =
174-
modulemd_packager_v3_to_stream_v2_ext (packager, &nested_error);
175-
if (!index)
176-
{
177-
g_propagate_error (error, g_steal_pointer (&nested_error));
178-
return FALSE;
179-
}
180-
181-
if (autogen_module_name)
182-
{
183-
/* nothing to do here since module/stream names were already
184-
* auto-generated in order to add them to the index
185-
*/
186-
}
187-
188-
/* merge index with override = FALSE and strict_default_streams = TRUE */
189-
if (!modulemd_module_index_merge (
190-
index, self, FALSE, TRUE, &nested_error))
191-
{
192-
g_propagate_error (error, g_steal_pointer (&nested_error));
193-
return FALSE;
194-
}
195-
196-
g_clear_object (&index);
197-
break;
198-
}
199-
200-
/* Fall through intentionally
201-
* We will handle the v2 packager format below
202-
*/
158+
g_set_error (error,
159+
MODULEMD_YAML_ERROR,
160+
MMD_YAML_ERROR_PARSE,
161+
"modulemd-packager document ignored while reading into a "
162+
"module index");
163+
return FALSE;
203164

204165
case MODULEMD_YAML_DOC_MODULESTREAM:
205166
switch (modulemd_subdocument_info_get_mdversion (subdoc))
@@ -212,7 +173,7 @@ add_subdoc (ModulemdModuleIndex *self,
212173
case MD_MODULESTREAM_VERSION_TWO:
213174
stream =
214175
MODULEMD_MODULE_STREAM (modulemd_module_stream_v2_parse_yaml (
215-
subdoc, strict, doctype == MODULEMD_YAML_DOC_PACKAGER, error));
176+
subdoc, strict, FALSE, error));
216177
break;
217178

218179
default:

modulemd/tests/test-modulemd-moduleindex.c

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ module_index_test_read (void)
196196
g_autoptr (GPtrArray) failures = NULL;
197197
ModulemdSubdocumentInfo *subdoc = NULL;
198198
g_autofree gchar *yaml_path = NULL;
199+
g_auto (GStrv) module_names = NULL;
199200

200201
/* Read the specification files all in */
201202
index = modulemd_module_index_new ();
@@ -223,24 +224,31 @@ module_index_test_read (void)
223224
g_clear_pointer (&yaml_path, g_free);
224225
g_clear_pointer (&failures, g_ptr_array_unref);
225226

226-
/* The modulemd-packager definition
227-
* This should fail to be read into a ModuleIndex because it provides no
228-
* NSVCA information. It should only be importable via
229-
* ModuleStream.read_file()
227+
/* The modulemd-packager v2 definition
228+
* Reading a packager document into an index can only be done by calling
229+
* modulemd_read_packager_file(). Attempting to read it directly
230+
* using modulemd_module_index_update_from_file() should ignore the document
231+
* and report it as a failure.
230232
*/
231233
yaml_path = g_strdup_printf ("%s/yaml_specs/modulemd_packager_v2.yaml",
232234
g_getenv ("MESON_SOURCE_ROOT"));
233235
ret = modulemd_module_index_update_from_file (
234236
index, yaml_path, TRUE, &failures, &error);
237+
modulemd_subdocument_info_debug_dump_failures (failures);
235238
g_assert_no_error (error);
236239
g_assert_cmpint (failures->len, ==, 1);
237240
g_assert_false (ret);
238241
g_assert_error (
239242
modulemd_subdocument_info_get_gerror (g_ptr_array_index (failures, 0)),
240-
MODULEMD_ERROR,
241-
MMD_ERROR_MISSING_REQUIRED);
243+
MODULEMD_YAML_ERROR,
244+
MMD_YAML_ERROR_PARSE);
242245
g_clear_pointer (&yaml_path, g_free);
243246
g_clear_pointer (&failures, g_ptr_array_unref);
247+
/* the index must still contain only the "foo" module */
248+
module_names = modulemd_module_index_get_module_names_as_strv (index);
249+
g_assert_cmpint (g_strv_length (module_names), ==, 1);
250+
g_assert_true (g_strv_contains ((const gchar *const *)module_names, "foo"));
251+
g_clear_pointer (&module_names, g_strfreev);
244252

245253
/* The translation definitions */
246254
yaml_path = g_strdup_printf ("%s/yaml_specs/modulemd_translations_v1.yaml",
@@ -373,6 +381,58 @@ module_index_test_read_mixed (void)
373381
}
374382

375383

384+
static void
385+
module_index_test_read_mixed_packager (void)
386+
{
387+
gboolean ret;
388+
g_autoptr (ModulemdModuleIndex) index = NULL;
389+
g_autofree gchar *yaml_path = NULL;
390+
g_autoptr (GPtrArray) failures = NULL;
391+
g_autoptr (GError) error = NULL;
392+
g_autofree gchar *output = NULL;
393+
g_auto (GStrv) module_names = NULL;
394+
395+
index = modulemd_module_index_new ();
396+
397+
/* Reading a packager document into an index can only be done by calling
398+
* modulemd_read_packager_file(). Attempting to directly read a file
399+
* containing a mix of stream and packager documents using
400+
* modulemd_module_index_update_from_file() should trigger errors and
401+
* ignore the packager documents while successfully loading the streams.
402+
*/
403+
yaml_path = g_strdup_printf ("%s/stream_packager_mix.yaml",
404+
g_getenv ("TEST_DATA_PATH"));
405+
g_assert_nonnull (yaml_path);
406+
407+
ret = modulemd_module_index_update_from_file (
408+
index, yaml_path, TRUE, &failures, &error);
409+
modulemd_subdocument_info_debug_dump_failures (failures);
410+
g_assert_no_error (error);
411+
g_assert_cmpint (failures->len, ==, 2);
412+
g_assert_false (ret);
413+
g_assert_error (
414+
modulemd_subdocument_info_get_gerror (g_ptr_array_index (failures, 0)),
415+
MODULEMD_YAML_ERROR,
416+
MMD_YAML_ERROR_PARSE);
417+
g_assert_error (
418+
modulemd_subdocument_info_get_gerror (g_ptr_array_index (failures, 1)),
419+
MODULEMD_YAML_ERROR,
420+
MMD_YAML_ERROR_PARSE);
421+
/* Make sure the streams were successfully loaded into the index */
422+
module_names = modulemd_module_index_get_module_names_as_strv (index);
423+
g_assert_cmpint (g_strv_length (module_names), ==, 2);
424+
g_assert_true (g_strv_contains ((const gchar *const *)module_names, "foo"));
425+
g_assert_true (g_strv_contains ((const gchar *const *)module_names, "qux"));
426+
g_clear_pointer (&module_names, g_strfreev);
427+
g_clear_pointer (&failures, g_ptr_array_unref);
428+
429+
/* Verify that we can output it cleanly */
430+
output = modulemd_module_index_dump_to_string (index, &error);
431+
g_assert_nonnull (output);
432+
g_assert_null (error);
433+
}
434+
435+
376436
static void
377437
module_index_test_read_unknown (void)
378438
{
@@ -1810,6 +1870,9 @@ main (int argc, char *argv[])
18101870
g_test_add_func ("/modulemd/v2/module/index/read/mixed",
18111871
module_index_test_read_mixed);
18121872

1873+
g_test_add_func ("/modulemd/v2/module/index/read/mixed_packager",
1874+
module_index_test_read_mixed_packager);
1875+
18131876
g_test_add_func ("/modulemd/v2/module/index/read/unknown",
18141877
module_index_test_read_unknown);
18151878

modulemd/tests/test-modulemd-packager-v3.c

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -602,50 +602,40 @@ packager_test_read_to_index (void)
602602
g_autoptr (GError) error = NULL;
603603
g_autoptr (GPtrArray) failures = NULL;
604604
g_autofree gchar *yaml_path = NULL;
605-
g_autofree gchar *yaml_str = NULL;
606-
g_autofree gchar *expected_path = NULL;
607-
g_autofree gchar *expected_str = NULL;
605+
g_auto (GStrv) module_names = NULL;
608606

609607
/* create an index */
610608
index = modulemd_module_index_new ();
611609

612-
/* The modulemd-packager v3 definition */
610+
/* The modulemd-packager v3 definition
611+
* Reading a packager v3 document into an index can only be done by calling
612+
* modulemd_read_packager_file() and then calling
613+
* modulemd_packager_v3_convert_to_index(), or internally by calling
614+
* modulemd_packager_v3_to_stream_v2_ext(). Attempting to read it directly
615+
* using modulemd_module_index_update_from_file() should trigger a warning
616+
* and ignore the document.
617+
*/
613618
yaml_path = g_strdup_printf ("%s/yaml_specs/modulemd_packager_v3.yaml",
614619
g_getenv ("MESON_SOURCE_ROOT"));
615620
ret = modulemd_module_index_update_from_file (
616621
index, yaml_path, TRUE, &failures, &error);
617622
g_assert_no_error (error);
618623
modulemd_subdocument_info_debug_dump_failures (failures);
619-
g_assert_true (ret);
620-
g_assert_cmpint (failures->len, ==, 0);
624+
g_assert_cmpint (failures->len, ==, 1);
625+
g_assert_false (ret);
626+
g_assert_error (
627+
modulemd_subdocument_info_get_gerror (g_ptr_array_index (failures, 0)),
628+
MODULEMD_YAML_ERROR,
629+
MMD_YAML_ERROR_PARSE);
621630
g_clear_pointer (&yaml_path, g_free);
622631
g_clear_pointer (&failures, g_ptr_array_unref);
623632

624-
g_assert_cmpint (MD_MODULESTREAM_VERSION_TWO,
625-
==,
626-
modulemd_module_index_get_stream_mdversion (index));
627-
628-
yaml_str = modulemd_module_index_dump_to_string (index, &error);
629-
g_assert_no_error (error);
630-
g_assert_nonnull (yaml_str);
631-
632-
g_debug ("packager_test_read_to_index() dump of v2 index:\n%s", yaml_str);
633-
634-
/* the index should contain the packager v2 document converted to stream v2 */
635-
expected_path = g_strdup_printf ("%s/upgrades/packager_v3_to_stream_v2.yaml",
636-
g_getenv ("TEST_DATA_PATH"));
637-
g_assert_nonnull (expected_path);
638-
g_assert_true (
639-
g_file_get_contents (expected_path, &expected_str, NULL, &error));
640-
g_assert_no_error (error);
641-
g_assert_nonnull (expected_str);
642-
g_clear_pointer (&expected_path, g_free);
643-
644-
g_assert_cmpstr (expected_str, ==, yaml_str);
633+
/* the index must still be empty */
634+
module_names = modulemd_module_index_get_module_names_as_strv (index);
635+
g_assert_cmpint (g_strv_length (module_names), ==, 0);
636+
g_clear_pointer (&module_names, g_strfreev);
645637

646638
g_clear_object (&index);
647-
g_clear_pointer (&yaml_str, g_free);
648-
g_clear_pointer (&expected_str, g_free);
649639
}
650640

651641

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
document: modulemd
3+
version: 1
4+
data:
5+
name: foo
6+
stream: a
7+
summary: Example 1
8+
description: Description 1
9+
license:
10+
module:
11+
- MIT
12+
...
13+
---
14+
document: modulemd-packager
15+
version: 2
16+
data:
17+
summary: Example 2
18+
description: Description 2
19+
license:
20+
module:
21+
- MIT
22+
...
23+
---
24+
document: modulemd-packager
25+
version: 3
26+
data:
27+
name: baz
28+
stream: c
29+
summary: Example 3
30+
description: Description 3
31+
...
32+
---
33+
document: modulemd
34+
version: 2
35+
data:
36+
name: qux
37+
stream: d
38+
summary: Example 4
39+
description: Description 4
40+
license:
41+
module:
42+
- MIT
43+
...

0 commit comments

Comments
 (0)