Repository: redhat-openshift-ecosystem/openshift-preflight
Pull request #477 (2022-03-10 22:15:43)

Refactor url building for DRY
Make the URl building less repetitive, and add some unit tests.

Signed-off-by: Brad P. Crochet <brad@redhat.com>


Patch:
diff --git a/cmd/check_container.go b/cmd/check_container.go
index 5866114..d77eb4b 100644
--- a/cmd/check_container.go
+++ b/cmd/check_container.go
@@ -248,29 +248,3 @@ func init() {
 
 	checkCmd.AddCommand(checkContainerCmd)
 }
-
-func buildOverviewURL(projectID string) string {
-	connectURL := fmt.Sprintf("https://connect.redhat.com/projects/%s/overview", projectID)
-	pyxisHost := viper.GetString("pyxis_host")
-	s := strings.Split(pyxisHost, ".")
-
-	if pyxisHost != DefaultPyxisHost && len(s) > 3 {
-		env := s[1]
-		connectURL = fmt.Sprintf("https://connect.%s.redhat.com/projects/%s/overview", env, projectID)
-	}
-
-	return connectURL
-}
-
-func buildScanResultsURL(projectID string, imageID string) string {
-	connectURL := fmt.Sprintf("https://connect.redhat.com/projects/%s/images/%s/scan-results", projectID, imageID)
-	pyxisHost := viper.GetString("pyxis_host")
-	s := strings.Split(pyxisHost, ".")
-
-	if pyxisHost != DefaultPyxisHost && len(s) > 3 {
-		env := s[1]
-		connectURL = fmt.Sprintf("https://connect.%s.redhat.com/projects/%s/images/%s/scan-results", env, projectID, imageID)
-	}
-
-	return connectURL
-}
diff --git a/cmd/utils.go b/cmd/utils.go
index e0440c8..7362bae 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -1,6 +1,7 @@
 package cmd
 
 import (
+	"fmt"
 	"io"
 	"os"
 	"strings"
@@ -70,3 +71,24 @@ func preRunConfig(cmd *cobra.Command, args []string) {
 		log.Debug("config file not found, proceeding without it")
 	}
 }
+
+func buildConnectURL(projectID string) string {
+	connectURL := fmt.Sprintf("https://connect.redhat.com/projects/%s", projectID)
+	pyxisHost := viper.GetString("pyxis_host")
+	s := strings.Split(pyxisHost, ".")
+
+	if pyxisHost != DefaultPyxisHost && len(s) > 3 {
+		env := s[1]
+		connectURL = fmt.Sprintf("https://connect.%s.redhat.com/projects/%s", env, projectID)
+	}
+
+	return connectURL
+}
+
+func buildOverviewURL(projectID string) string {
+	return fmt.Sprintf("%s/overview", buildConnectURL(projectID))
+}
+
+func buildScanResultsURL(projectID string, imageID string) string {
+	return fmt.Sprintf("%s/images/%s/scan-results", buildConnectURL(projectID), imageID)
+}


Test patch:
diff --git a/cmd/utils_test.go b/cmd/utils_test.go
index 3c04045..4be3d50 100644
--- a/cmd/utils_test.go
+++ b/cmd/utils_test.go
@@ -1,8 +1,11 @@
 package cmd
 
 import (
+	"os"
+
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
+	"github.com/spf13/viper"
 )
 
 var _ = Describe("cmd package utility functions", func() {
@@ -30,4 +33,48 @@ var _ = Describe("cmd package utility functions", func() {
 			})
 		})
 	})
+
+	Describe("Test Connect URL builders", func() {
+		BeforeEach(func() {
+			viper.SetEnvPrefix("pflt")
+			viper.AutomaticEnv()
+		})
+		Context("Regular Connect URL", func() {
+			expected := "https://connect.redhat.com/projects/this-is-my-project-id"
+			It("should return a URL with just a project ID", func() {
+				actual := buildConnectURL("this-is-my-project-id")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+		Context("QA Connect URL", func() {
+			BeforeEach(func() {
+				os.Setenv("PFLT_PYXIS_HOST", "catalog.qa.redhat.com")
+			})
+			expected := "https://connect.qa.redhat.com/projects/this-is-my-project-id"
+			It("should return a URL for QA", func() {
+				actual := buildConnectURL("this-is-my-project-id")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+		Context("UAT Scan Results URL", func() {
+			BeforeEach(func() {
+				os.Setenv("PFLT_PYXIS_HOST", "catalog.uat.redhat.com")
+			})
+			expected := "https://connect.uat.redhat.com/projects/this-is-my-project-id/images/my-image-id/scan-results"
+			It("should return a URL for UAT", func() {
+				actual := buildScanResultsURL("this-is-my-project-id", "my-image-id")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+		Context("QA Overview URL", func() {
+			BeforeEach(func() {
+				os.Setenv("PFLT_PYXIS_HOST", "catalog.qa.redhat.com")
+			})
+			expected := "https://connect.qa.redhat.com/projects/this-is-my-project-id/overview"
+			It("should return a URL for QA", func() {
+				actual := buildOverviewURL("this-is-my-project-id")
+				Expect(actual).To(Equal(expected))
+			})
+		})
+	})
 })