Repository: kubernetes/minikube
Pull request #8710 (2020-07-13 16:09:17)

Add unit tests for register package
<!-- 🎉 Thank you for contributing to minikube! 🎉 Here are some hints to get your PR merged faster:

1. Your PR title will be included in the release notes, choose it carefully
2. If the PR fixes an issue, add "fixes #<issue number>" to the description.
3. If the PR is a user interface change, please include a "before" and "after" example.
4. If the PR is a large design change, please include an enhancement proposal:
https://github.com/kubernetes/minikube/tree/master/enhancements
-->



Patch:
diff --git a/pkg/minikube/out/register/cloud_events.go b/pkg/minikube/out/register/cloud_events.go
index 5fe4d6ab6..ead8240c1 100644
--- a/pkg/minikube/out/register/cloud_events.go
+++ b/pkg/minikube/out/register/cloud_events.go
@@ -18,6 +18,8 @@ package register
 
 import (
 	"fmt"
+	"io"
+	"os"
 
 	cloudevents "github.com/cloudevents/sdk-go/v2"
 	"github.com/golang/glog"
@@ -28,6 +30,11 @@ const (
 	specVersion = "1.0"
 )
 
+var (
+	outputFile io.Writer = os.Stdout
+	getUUID              = randomID
+)
+
 func printAsCloudEvent(log Log, data map[string]string) {
 	event := cloudevents.NewEvent()
 	event.SetSource("https://minikube.sigs.k8s.io/")
@@ -36,10 +43,14 @@ func printAsCloudEvent(log Log, data map[string]string) {
 	if err := event.SetData(cloudevents.ApplicationJSON, data); err != nil {
 		glog.Warningf("error setting data: %v", err)
 	}
-	event.SetID(guuid.New().String())
+	event.SetID(getUUID())
 	json, err := event.MarshalJSON()
 	if err != nil {
 		glog.Warningf("error marashalling event: %v", err)
 	}
-	fmt.Println(string(json))
+	fmt.Fprintln(outputFile, string(json))
+}
+
+func randomID() string {
+	return guuid.New().String()
 }


Test patch:
diff --git a/pkg/minikube/out/register/json_test.go b/pkg/minikube/out/register/json_test.go
new file mode 100644
index 000000000..c24556f0f
--- /dev/null
+++ b/pkg/minikube/out/register/json_test.go
@@ -0,0 +1,65 @@
+/*
+Copyright 2020 The Kubernetes Authors All rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package register
+
+import (
+	"bytes"
+	"fmt"
+	"os"
+	"testing"
+)
+
+func TestPrintStep(t *testing.T) {
+	expected := `{"data":{"currentstep":"0","message":"message","name":"Initial Minikube Setup","totalsteps":"%v"},"datacontenttype":"application/json","id":"random-id","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.step"}`
+	expected = fmt.Sprintf(expected, Reg.totalSteps())
+	expected += "\n"
+
+	buf := bytes.NewBuffer([]byte{})
+	outputFile = buf
+	defer func() { outputFile = os.Stdout }()
+
+	getUUID = func() string {
+		return "random-id"
+	}
+
+	PrintStep("message")
+	actual := buf.String()
+
+	if actual != expected {
+		t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
+	}
+}
+
+func TestPrintInfo(t *testing.T) {
+	expected := `{"data":{"message":"info"},"datacontenttype":"application/json","id":"random-id","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.info"}`
+	expected += "\n"
+
+	buf := bytes.NewBuffer([]byte{})
+	outputFile = buf
+	defer func() { outputFile = os.Stdout }()
+
+	getUUID = func() string {
+		return "random-id"
+	}
+
+	PrintInfo("info")
+	actual := buf.String()
+
+	if actual != expected {
+		t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
+	}
+}
diff --git a/pkg/minikube/out/register/register_test.go b/pkg/minikube/out/register/register_test.go
new file mode 100644
index 000000000..0b70b01a8
--- /dev/null
+++ b/pkg/minikube/out/register/register_test.go
@@ -0,0 +1,48 @@
+/*
+Copyright 2020 The Kubernetes Authors All rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package register
+
+import (
+	"bytes"
+	"fmt"
+	"os"
+	"testing"
+)
+
+func TestSetCurrentStep(t *testing.T) {
+	secondStep := Reg.steps[1]
+	Reg.SetStep(secondStep)
+
+	expected := `{"data":{"currentstep":"1","message":"message","name":"%s","totalsteps":"%v"},"datacontenttype":"application/json","id":"random-id","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.step"}`
+	expected = fmt.Sprintf(expected, secondStep, Reg.totalSteps())
+	expected += "\n"
+
+	buf := bytes.NewBuffer([]byte{})
+	outputFile = buf
+	defer func() { outputFile = os.Stdout }()
+
+	getUUID = func() string {
+		return "random-id"
+	}
+
+	PrintStep("message")
+	actual := buf.String()
+
+	if actual != expected {
+		t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
+	}
+}