Skip to content

Commit

Permalink
Fetch console URL form daemon for 'console' cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored and praveenkumar committed Nov 18, 2022
1 parent dc5339a commit e9f3ae9
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 32 deletions.
23 changes: 9 additions & 14 deletions cmd/crc/cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"io"
"os"

"github.com/crc-org/crc/pkg/crc/api/client"
"github.com/crc-org/crc/pkg/crc/daemonclient"
crcErrors "github.com/crc-org/crc/pkg/crc/errors"
"github.com/crc-org/crc/pkg/crc/machine"
"github.com/crc-org/crc/pkg/crc/machine/state"
"github.com/crc-org/crc/pkg/crc/machine/types"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
Expand All @@ -33,21 +33,16 @@ var consoleCmd = &cobra.Command{
Short: "Open the OpenShift Web Console in the default browser",
Long: `Open the OpenShift Web Console in the default browser or print its URL or credentials`,
RunE: func(cmd *cobra.Command, args []string) error {
return runConsole(os.Stdout, newMachine(), consolePrintURL, consolePrintCredentials, outputFormat)
return runConsole(os.Stdout, daemonclient.New(), consolePrintURL, consolePrintCredentials, outputFormat)
},
}

func showConsole(client machine.Client) (*types.ConsoleResult, error) {
if err := checkIfMachineMissing(client); err != nil {
// In case of machine doesn't exist then consoleResult error
// should be updated so that when rendering the result it have
// error details also.
return nil, err
}
return client.GetConsoleURL()
func showConsole(client *daemonclient.Client) (*client.ConsoleResult, error) {
res, err := client.APIClient.WebconsoleURL()
return res, err
}

func runConsole(writer io.Writer, client machine.Client, consolePrintURL, consolePrintCredentials bool, outputFormat string) error {
func runConsole(writer io.Writer, client *daemonclient.Client, consolePrintURL, consolePrintCredentials bool, outputFormat string) error {
result, err := showConsole(client)
return render(&consoleResult{
Success: err == nil,
Expand Down Expand Up @@ -106,14 +101,14 @@ func (s *consoleResult) prettyPrintTo(writer io.Writer) error {
return nil
}

func toState(result *types.ConsoleResult) state.State {
func toState(result *client.ConsoleResult) state.State {
if result == nil {
return state.Error
}
return result.State
}

func toConsoleClusterConfig(result *types.ConsoleResult) *clusterConfig {
func toConsoleClusterConfig(result *client.ConsoleResult) *clusterConfig {
if result == nil {
return nil
}
Expand Down
51 changes: 45 additions & 6 deletions cmd/crc/cmd/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,69 @@ package cmd

import (
"bytes"
"errors"
"fmt"
"testing"

apiTypes "github.com/crc-org/crc/pkg/crc/api/client"
"github.com/crc-org/crc/pkg/crc/daemonclient"
"github.com/crc-org/crc/pkg/crc/machine/fakemachine"
"github.com/crc-org/crc/pkg/crc/machine/state"
"github.com/crc-org/crc/pkg/crc/machine/types"
mocks "github.com/crc-org/crc/test/mocks/api"
"github.com/stretchr/testify/assert"
)

var DummyClusterConfig = types.ClusterConfig{
ClusterType: "openshift",
ClusterCACert: "MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ",
KubeConfig: "/tmp/kubeconfig",
KubeAdminPass: "foobar",
ClusterAPI: "https://foo.testing:6443",
WebConsoleURL: "https://console.foo.testing:6443",
ProxyConfig: nil,
}

func setUpClientForConsole(t *testing.T) *daemonclient.Client {
client := mocks.NewClient(t)

client.On("WebconsoleURL").Return(
&apiTypes.ConsoleResult{
ClusterConfig: DummyClusterConfig,
State: state.Running,
}, nil)
return &daemonclient.Client{
APIClient: client,
}
}

func setUpFailingClientForConsole(t *testing.T) *daemonclient.Client {
client := mocks.NewClient(t)

client.On("WebconsoleURL").Return(
nil, errors.New("console failed"))
return &daemonclient.Client{
APIClient: client,
}
}

func TestConsolePlainSuccess(t *testing.T) {
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, fakemachine.NewClient(), true, false, ""))
assert.NoError(t, runConsole(out, setUpClientForConsole(t), true, false, ""))
assert.Equal(t, fmt.Sprintf("%s\n", fakemachine.DummyClusterConfig.WebConsoleURL), out.String())
}

func TestConsolePlainError(t *testing.T) {
out := new(bytes.Buffer)
assert.EqualError(t, runConsole(out, fakemachine.NewFailingClient(), true, false, ""), "console failed")
assert.EqualError(t, runConsole(out, setUpFailingClientForConsole(t), true, false, ""), "console failed")
}

func TestConsoleWithPrintCredentialsPlainSuccess(t *testing.T) {
expectedOut := fmt.Sprintf(`To login as a regular user, run 'oc login -u developer -p developer %s'.
To login as an admin, run 'oc login -u kubeadmin -p %s %s'
`, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, fakemachine.NewClient(), false, true, ""))
assert.NoError(t, runConsole(out, setUpClientForConsole(t), false, true, ""))
assert.Equal(t, expectedOut, out.String())
}

Expand All @@ -35,7 +74,7 @@ To login as a regular user, run 'oc login -u developer -p developer %s'.
To login as an admin, run 'oc login -u kubeadmin -p %s %s'
`, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass, fakemachine.DummyClusterConfig.ClusterAPI)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, fakemachine.NewClient(), true, true, ""))
assert.NoError(t, runConsole(out, setUpClientForConsole(t), true, true, ""))
assert.Equal(t, expectedOut, out.String())
}

Expand All @@ -58,12 +97,12 @@ func TestConsoleJSONSuccess(t *testing.T) {
}
}`, fakemachine.DummyClusterConfig.ClusterCACert, fakemachine.DummyClusterConfig.WebConsoleURL, fakemachine.DummyClusterConfig.ClusterAPI, fakemachine.DummyClusterConfig.KubeAdminPass)
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, fakemachine.NewClient(), false, false, jsonFormat))
assert.NoError(t, runConsole(out, setUpClientForConsole(t), false, false, jsonFormat))
assert.JSONEq(t, expectedJSONOut, out.String())
}

func TestConsoleJSONError(t *testing.T) {
out := new(bytes.Buffer)
assert.NoError(t, runConsole(out, fakemachine.NewFailingClient(), false, false, jsonFormat))
assert.NoError(t, runConsole(out, setUpFailingClientForConsole(t), false, false, jsonFormat))
assert.JSONEq(t, `{"error":"console failed", "success":false}`, out.String())
}
2 changes: 1 addition & 1 deletion pkg/crc/api/api_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ var testCases = []testCase{
// webconsoleurl
{
request: get("webconsoleurl"),
response: jSon(`{"ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null}}`),
response: jSon(`{"ClusterConfig":{"ClusterType":"openshift","ClusterCACert":"MIIDODCCAiCgAwIBAgIIRVfCKNUa1wIwDQYJ","KubeConfig":"/tmp/kubeconfig","KubeAdminPass":"foobar","ClusterAPI":"https://foo.testing:6443","WebConsoleURL":"https://console.foo.testing:6443","ProxyConfig":null},"State":"Running"}`),
},

// webconsoleurl with failure
Expand Down
10 changes: 5 additions & 5 deletions pkg/crc/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Client interface {
Start(config StartConfig) (StartResult, error)
Stop() error
Delete() error
WebconsoleURL() (ConsoleResult, error)
WebconsoleURL() (*ConsoleResult, error)
GetConfig(configs []string) (GetConfigResult, error)
SetConfig(configs SetConfigRequest) (SetOrUnsetConfigResult, error)
UnsetConfig(configs []string) (SetOrUnsetConfigResult, error)
Expand Down Expand Up @@ -104,17 +104,17 @@ func (c *client) Delete() error {
return err
}

func (c *client) WebconsoleURL() (ConsoleResult, error) {
func (c *client) WebconsoleURL() (*ConsoleResult, error) {
var cr = ConsoleResult{}
body, err := c.sendGetRequest("/webconsoleurl")
if err != nil {
return cr, err
return &cr, err
}
err = json.Unmarshal(body, &cr)
if err != nil {
return cr, err
return &cr, err
}
return cr, nil
return &cr, nil
}

func (c *client) GetConfig(configs []string) (GetConfigResult, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/api/client/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/crc-org/crc/pkg/crc/machine/state"
"github.com/crc-org/crc/pkg/crc/machine/types"
"github.com/crc-org/crc/pkg/crc/preset"
)
Expand Down Expand Up @@ -32,6 +33,7 @@ type ClusterStatusResult struct {

type ConsoleResult struct {
ClusterConfig types.ClusterConfig
State state.State
}

// SetOrUnsetConfigResult struct is used to return the result of
Expand Down
7 changes: 7 additions & 0 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,19 @@ func (h *Handler) Delete(c *context) error {
}

func (h *Handler) GetWebconsoleInfo(c *context) error {
if err := machine.CheckIfMachineMissing(h.Client); err != nil {
// In case of machine doesn't exist then consoleResult error
// should be updated so that when rendering the result it have
// error details also.
return err
}
res, err := h.Client.GetConsoleURL()
if err != nil {
return err
}
return c.JSON(http.StatusOK, client.ConsoleResult{
ClusterConfig: res.ClusterConfig,
State: res.State,
})
}

Expand Down
17 changes: 16 additions & 1 deletion pkg/crc/machine/exists.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package machine

import "fmt"
import (
"fmt"

crcErr "github.com/crc-org/crc/pkg/crc/errors"
)

func (client *client) Exists() (bool, error) {
libMachineAPIClient, cleanup := createLibMachineClient()
Expand All @@ -11,3 +15,14 @@ func (client *client) Exists() (bool, error) {
}
return exists, nil
}

func CheckIfMachineMissing(client Client) error {
exists, err := client.Exists()
if err != nil {
return err
}
if !exists {
return crcErr.VMNotExist
}
return nil
}
12 changes: 7 additions & 5 deletions test/mocks/api/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9f3ae9

Please sign in to comment.