Repository: redhat-openshift-ecosystem/openshift-preflight
Pull request #532 (2022-03-30 19:39:54)

Fix source rpm name parsing
Some source rpm names are breaking the parsing logic we have (that was
very close to the CVP version). We couldn't use the CVP version, as the
golang regex engine does not support positive lookaheads. So, this patch
instead utilizes Split on '-', then puts the source name back together
with any '-', minus the last two entries. Unit tests provided.

Signed-off-by: Brad P. Crochet <brad@redhat.com>

openshift-ci[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#" title="Author self-approved">bcrochet</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=redhat-openshift-ecosystem%2Fopenshift-preflight).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/redhat-openshift-ecosystem/openshift-preflight/blob/main/OWNERS)~~ [bcrochet]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#pullrequestreview-926639091" title="Approved">acornett21</a>*, *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#" title="Author self-approved">bcrochet</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=redhat-openshift-ecosystem%2Fopenshift-preflight).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/redhat-openshift-ecosystem/openshift-preflight/blob/main/OWNERS)~~ [acornett21,bcrochet]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci[bot]: New changes are detected. LGTM label has been removed.
acornett21: I tested this PR with all RHEL9 beta images and there wasn't any issues.
openshift-ci[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#pullrequestreview-926639091" title="Approved">acornett21</a>*, *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#" title="Author self-approved">bcrochet</a>*, *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/532#pullrequestreview-926744383" title="Approved">jomkz</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=redhat-openshift-ecosystem%2Fopenshift-preflight).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/redhat-openshift-ecosystem/openshift-preflight/blob/main/OWNERS)~~ [acornett21,bcrochet,jomkz]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/certification/internal/engine/engine.go b/certification/internal/engine/engine.go
index 098e454..0337db2 100644
--- a/certification/internal/engine/engine.go
+++ b/certification/internal/engine/engine.go
@@ -421,6 +421,11 @@ func writeCertImage(ctx context.Context, imageRef certification.ImageReference)
 	return nil
 }
 
+func getBgName(srcrpm string) string {
+	parts := strings.Split(srcrpm, "-")
+	return strings.Join(parts[0:len(parts)-2], "-")
+}
+
 func writeRPMManifest(ctx context.Context, containerFSPath string) error {
 	pkgList, err := rpm.GetPackageList(ctx, containerFSPath)
 	if err != nil {
@@ -435,7 +440,7 @@ func writeRPMManifest(ctx context.Context, containerFSPath string) error {
 
 		// accounting for the fact that not all packages have a source rpm
 		if len(packageInfo.SourceRpm) > 0 {
-			bgName = packageInfo.SourceRpm[0:strings.LastIndex(strings.SplitAfter(packageInfo.SourceRpm, ".")[0], "-")]
+			bgName = getBgName(packageInfo.SourceRpm)
 			endChop = strings.TrimPrefix(strings.TrimSuffix(regexp.MustCompile("(-[0-9].*)").FindString(packageInfo.SourceRpm), ".rpm"), "-")
 
 			srpmNevra = fmt.Sprintf("%s-%d:%s", bgName, packageInfo.Epoch, endChop)


Test patch:
diff --git a/certification/internal/engine/engine_test.go b/certification/internal/engine/engine_test.go
new file mode 100644
index 0000000..8cd3c39
--- /dev/null
+++ b/certification/internal/engine/engine_test.go
@@ -0,0 +1,32 @@
+package engine
+
+import (
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+)
+
+var _ = Describe("Source RPM name function", func() {
+	Context("With a source rpm name", func() {
+		Context("And a normal source rpm name", func() {
+			It("should parse bash-5.1.8-2.el9.src.rpm to bash", func() {
+				expected := "bash"
+				actual := getBgName("bash-5.1.8-2.el9.src.rpm")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+		Context("And a slightly annoying source rpm name", func() {
+			It("should parse python3.9-3.9.6-6.el9.src.rpm to python3.9", func() {
+				expected := "python3.9"
+				actual := getBgName("python3.9-3.9.6-6.el9.src.rpm")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+		Context("And a source rpm name with a bunch of -'s", func() {
+			It("should parse python-pip-21.0.1-6.el9.src.rpm to bash", func() {
+				expected := "python-pip"
+				actual := getBgName("python-pip-21.0.1-6.el9.src.rpm")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+	})
+})
diff --git a/certification/internal/engine/internal_engine_suite_test.go b/certification/internal/engine/internal_engine_suite_test.go
new file mode 100644
index 0000000..910baa0
--- /dev/null
+++ b/certification/internal/engine/internal_engine_suite_test.go
@@ -0,0 +1,13 @@
+package engine
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+)
+
+func TestInternalEngine(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "Internal Engine Suite")
+}