Repository: redhat-openshift-ecosystem/openshift-preflight
Pull request #698 (2022-06-15 21:28:31)

Add runtime assets tests
Part of #620 

NOTE: This builds on #697 so I'll put do-not-merge on this until I can rebase. That said, it's ready for review. The only files that changed are **cmd/runtime_assets.go** and **cmd/runtime_assets_test.go**. 

This PR adds runtime-assets testing by:
- Extracting write operations to an io.Writer
- Extracting formatting to its own function
- Passing through the cmd output to the io.Writer

Doesn't quite reach 100% because of two error paths that I'm not hitting. Seemed like too much fragmenting of the code just to hit the two error paths, but we hit the same error path in at least one test.

I'm seeing 85.7% on this file now. Okay for the moment.

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/698#" title="Author self-approved">komish</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)~~ [komish]

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":[]} -->
dcibot: -  SUCCESS https://www.distributed-ci.io/jobs/8ea0fc69-c16d-4303-a92f-cf1e61b07349/jobStates
 from https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/698
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/698#pullrequestreview-1008206885" title="Approved">acornett21</a>*, *<a href="https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/698#" title="Author self-approved">komish</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,komish]

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":[]} -->
acornett21: I just noticed this needs a `make fmt` but other then that, this should get GTG.
openshift-ci[bot]: New changes are detected. LGTM label has been removed.
komish: > I just noticed this needs a `make fmt` but other then that, this should get GTG.

Fixed. I don't know why I haven't added a pre-commit hook for this x_x already.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/50094005/badge)](https://coveralls.io/builds/50094005)

Coverage increased (+0.8%) to 61.617% when pulling **a4a053d0d191d4d79f7b85b13a6127935b19148e on komish:add-runtime-assets-tests** into **f5f30cc1f591dc0870613b3adc7356a90e5f03f7 on redhat-openshift-ecosystem:main**.

dcibot: -  SUCCESS https://www.distributed-ci.io/jobs/e844d7dd-e042-4800-8990-b850a6cc9fb0/jobStates
 from https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/698
dcibot: -  SUCCESS https://www.distributed-ci.io/jobs/7a33e817-0f63-428a-b0e3-389f042ea01e/jobStates
 from https://github.com/redhat-openshift-ecosystem/openshift-preflight/pull/698

Patch:
diff --git a/cmd/runtime_assets.go b/cmd/runtime_assets.go
index 69f01dc..7c17af2 100644
--- a/cmd/runtime_assets.go
+++ b/cmd/runtime_assets.go
@@ -1,8 +1,10 @@
 package cmd
 
 import (
+	"context"
 	"encoding/json"
 	"fmt"
+	"io"
 
 	"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime"
 	"github.com/spf13/cobra"
@@ -16,17 +18,36 @@ var runtimeAssetsCmd = &cobra.Command{
 }
 
 func runtimeAssetsRunE(cmd *cobra.Command, args []string) error {
-	assets := runtime.Assets(cmd.Context())
+	if err := printAssets(cmd.Context(), cmd.OutOrStdout()); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func printAssets(ctx context.Context, w io.Writer) error {
+	assets := runtime.Assets(ctx)
 
-	assetsJSON, err := json.MarshalIndent(assets, "", "    ")
+	assetsJSON, err := prettyPrintJSON(assets)
 	if err != nil {
 		return err
 	}
 
-	fmt.Println(string(assetsJSON))
+	fmt.Fprintln(w, string(assetsJSON))
 	return nil
 }
 
+// prettyPrintJSON marhals v with standard pretty print spacing and returns
+// it in string form.
+func prettyPrintJSON(v interface{}) (string, error) {
+	json, err := json.MarshalIndent(v, "", "    ")
+	if err != nil {
+		return "", err
+	}
+
+	return string(json), nil
+}
+
 func init() {
 	rootCmd.AddCommand(runtimeAssetsCmd)
 }


Test patch:
diff --git a/cmd/runtime_assets_test.go b/cmd/runtime_assets_test.go
new file mode 100644
index 0000000..11c8029
--- /dev/null
+++ b/cmd/runtime_assets_test.go
@@ -0,0 +1,63 @@
+package cmd
+
+import (
+	"bytes"
+	"context"
+	"encoding/json"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+	"github.com/redhat-openshift-ecosystem/openshift-preflight/certification/runtime"
+)
+
+var _ = Describe("runtime-assets test", func() {
+	Context("When formatting JSON data", func() {
+		It("should be formatted in a standard format", func() {
+			in := map[string]interface{}{
+				"foo":  "bar",
+				"this": []string{"that", "theother"},
+			}
+			expected := "{\n    \"foo\": \"bar\",\n    \"this\": [\n        \"that\",\n        \"theother\"\n    ]\n}"
+			res, err := prettyPrintJSON(in)
+			Expect(err).ToNot(HaveOccurred())
+			Expect(res).To(Equal(expected))
+		})
+
+		Context("With invalid data", func() {
+			It("should throw an error", func() {
+				// channels are not supported in json.
+				_, err := prettyPrintJSON(make(chan int))
+				Expect(err).To(HaveOccurred())
+			})
+		})
+	})
+
+	Context("When printing the runtime assets", func() {
+		It("should print successfully and match the actual data", func() {
+			buf := bytes.NewBuffer([]byte{})
+			err := printAssets(context.TODO(), buf)
+			Expect(err).ToNot(HaveOccurred())
+
+			var printed runtime.AssetData
+			json.Unmarshal(buf.Bytes(), &printed)
+
+			actual := runtime.Assets(context.TODO())
+
+			Expect(printed).To(BeEquivalentTo(actual))
+		})
+	})
+
+	Context("When calling the runtime-assets cobra command", func() {
+		It("should print successfully and match the actual data", func() {
+			out, err := executeCommand(rootCmd, "runtime-assets")
+			Expect(err).ToNot(HaveOccurred())
+
+			var printed runtime.AssetData
+			json.Unmarshal([]byte(out), &printed)
+
+			actual := runtime.Assets(context.TODO())
+
+			Expect(printed).To(BeEquivalentTo(actual))
+		})
+	})
+})