diff --git a/cmd/cli/version.go b/cmd/cli/version.go index 2ca1b24fdd..975d4a88d9 100644 --- a/cmd/cli/version.go +++ b/cmd/cli/version.go @@ -106,7 +106,7 @@ func (v *versionCmd) getMeshVersion() (*remoteVersionInfo, error) { return nil, err } if len(controllerPods.Items) == 0 { - return nil, errors.Errorf("No mesh found in namespace [%s]", v.namespace) + return &remoteVersionInfo{}, nil } controllerPod := controllerPods.Items[0] @@ -142,6 +142,10 @@ func (r *remoteVersion) proxyGetMeshVersion(pod string, namespace string, client func (v *versionCmd) outputVersionInfo(versionInfo versionInfo) { fmt.Fprintf(v.out, "CLI Version: %#v\n", *versionInfo.cliVersionInfo) if versionInfo.remoteVersionInfo != nil { - fmt.Fprintf(v.out, "Mesh [%s] Version: %#v\n", versionInfo.remoteVersionInfo.meshName, *versionInfo.remoteVersionInfo.version) + if versionInfo.remoteVersionInfo.meshName != "" { + fmt.Fprintf(v.out, "Mesh [%s] Version: %#v\n", versionInfo.remoteVersionInfo.meshName, *versionInfo.remoteVersionInfo.version) + } else { + fmt.Fprintf(v.out, "Mesh Version: No control plane found in namespace [%s]\n", v.namespace) + } } } diff --git a/cmd/cli/version_test.go b/cmd/cli/version_test.go index 7d786925a0..7bffd79e51 100644 --- a/cmd/cli/version_test.go +++ b/cmd/cli/version_test.go @@ -40,10 +40,10 @@ func TestGetMeshVersion(t *testing.T) { name: "no mesh in namespace", namespace: "test", remoteVersion: nil, - remoteVersionInfo: nil, + remoteVersionInfo: &remoteVersionInfo{}, controllerPods: []*corev1.Pod{}, proxyGetMeshVersionErr: nil, - expectedErr: errors.Errorf("No mesh found in namespace [test]"), + expectedErr: nil, }, { name: "mesh in namespace and proxyGetMeshVersion fails", @@ -139,7 +139,20 @@ func TestOutputVersionInfo(t *testing.T) { clientOnly bool }{ { - name: "cli and no mesh", + name: "cli and mesh versions with no control plane installed", + versionInfo: versionInfo{ + cliVersionInfo: &version.Info{ + Version: "v0.0.0", + GitCommit: "xxxxxxx", + BuildDate: "0000-00-00-00:00", + }, + remoteVersionInfo: &remoteVersionInfo{}, + }, + expected: "CLI Version: version.Info{Version:\"v0.0.0\", GitCommit:\"xxxxxxx\", BuildDate:\"0000-00-00-00:00\"}\nMesh Version: No control plane found in namespace [test]\n", + clientOnly: false, + }, + { + name: "cli version only", versionInfo: versionInfo{ cliVersionInfo: &version.Info{ Version: "v0.0.0", @@ -151,7 +164,7 @@ func TestOutputVersionInfo(t *testing.T) { clientOnly: true, }, { - name: "cli and mesh", + name: "cli and mesh versions with control plane installed", versionInfo: versionInfo{ cliVersionInfo: &version.Info{ Version: "v0.0.0", @@ -170,6 +183,19 @@ func TestOutputVersionInfo(t *testing.T) { expected: "CLI Version: version.Info{Version:\"v0.0.0\", GitCommit:\"xxxxxxx\", BuildDate:\"0000-00-00-00:00\"}\nMesh [test] Version: version.Info{Version:\"v0.0.0\", GitCommit:\"xxxxxxx\", BuildDate:\"0000-00-00-00:00\"}\n", clientOnly: false, }, + { + name: "cli and mesh versions with no remote version info", + versionInfo: versionInfo{ + cliVersionInfo: &version.Info{ + Version: "v0.0.0", + GitCommit: "xxxxxxx", + BuildDate: "0000-00-00-00:00", + }, + remoteVersionInfo: nil, + }, + expected: "CLI Version: version.Info{Version:\"v0.0.0\", GitCommit:\"xxxxxxx\", BuildDate:\"0000-00-00-00:00\"}\n", + clientOnly: false, + }, } for _, test := range tests { @@ -180,10 +206,11 @@ func TestOutputVersionInfo(t *testing.T) { cmd := versionCmd{ out: buf, clientOnly: test.clientOnly, + namespace: "test", } cmd.outputVersionInfo(test.versionInfo) - assert.Equal(buf.String(), test.expected) + assert.Equal(test.expected, buf.String()) }) } }