Blob Blame History Raw
From 85cc7687415a96db017acaf763d53abbc47d993f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Budai?= <ondrej@budai.cz>
Date: Mon, 16 Aug 2021 12:56:55 +0200
Subject: [PATCH] test/image: fix pipeline exports for v2 manifests
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously, we just assumed that all test manifests are of version 1, or we
should export the pipeline named assembler. However, this is no longer true
in RHEL 8.5 and 9 - they are only manifest v2 and they don't have a pipeline
named assembler.

This commit introduces a new way to guess the export name - if the manifest
is of version 1, we just export the assembler. In the case v2 manifest, the
last pipeline is exported.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
---
 cmd/osbuild-image-tests/main_test.go | 32 +++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/cmd/osbuild-image-tests/main_test.go b/cmd/osbuild-image-tests/main_test.go
index f6cbccc9..f73bab3d 100644
--- a/cmd/osbuild-image-tests/main_test.go
+++ b/cmd/osbuild-image-tests/main_test.go
@@ -482,6 +482,34 @@ func testImage(t *testing.T, testcase testcaseStruct, imagePath string) {
 	}
 }
 
+// guessPipelineToExport return a best-effort guess about which
+// pipeline should be exported when running osbuild for the testcase
+//
+// If this function detects that this is a version 1 manifest, it
+// always returns "assembler"
+//
+// For manifests version 2, the name of the last pipeline is returned.
+func guessPipelineToExport(rawManifest json.RawMessage) string {
+	const v1ManifestExportName = "assembler"
+	var v2Manifest struct {
+		Version   string `json:"version"`
+		Pipelines []struct {
+			Name string `json:"name,omitempty"`
+		} `json:"pipelines"`
+	}
+	err := json.Unmarshal(rawManifest, &v2Manifest)
+	if err != nil {
+		// if we cannot unmarshal, let's just assume that it's a version 1 manifest
+		return v1ManifestExportName
+	}
+
+	if v2Manifest.Version == "2" {
+		return v2Manifest.Pipelines[len(v2Manifest.Pipelines)-1].Name
+	}
+
+	return v1ManifestExportName
+}
+
 // runTestcase builds the pipeline specified in the testcase and then it
 // tests the result
 func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
@@ -494,9 +522,7 @@ func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
 		require.NoError(t, err, "error removing temporary output directory")
 	}()
 
-	// NOTE(akoutsou) 1to2t: new v2 manifests name their last pipeline
-	// "assembler" for compatibility with v1
-	exports := []string{"assembler"}
+	exports := []string{guessPipelineToExport(testcase.Manifest)}
 	err = runOsbuild(testcase.Manifest, store, outputDirectory, exports)
 	require.NoError(t, err)
 
-- 
2.31.1