Skip to content

Commit

Permalink
feat: add dashbaord uris for executions
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Oct 3, 2023
1 parent 3770f32 commit b31a4a3
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 14 deletions.
4 changes: 4 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4478,6 +4478,10 @@ components:
type: string
description: helm chart version
example: "1.4.14"
dashboardUri:
type: string
description: dashboard uri
example: "http://localhost:8080"

Repository:
description: repository representation for tests in git repositories
Expand Down
37 changes: 35 additions & 2 deletions cmd/kubectl-testkube/commands/common/render/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package render

import (
"encoding/json"
"fmt"
"io"
"os"

"gopkg.in/yaml.v2"

"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/kubeshop/testkube/pkg/utils"
Expand All @@ -21,7 +23,7 @@ const (
OutputPretty OutputType = "pretty"
)

type CliObjRenderer func(ui *ui.UI, obj interface{}) error
type CliObjRenderer func(client client.Client, ui *ui.UI, obj interface{}) error

func RenderJSON(obj interface{}, w io.Writer) error {
return json.NewEncoder(w).Encode(obj)
Expand Down Expand Up @@ -63,7 +65,7 @@ func RenderPrettyList(obj ui.TableData, w io.Writer) error {
return nil
}

func RenderExecutionResult(execution *testkube.Execution, logsOnly bool) {
func RenderExecutionResult(client client.Client, execution *testkube.Execution, logsOnly bool) {

result := execution.ExecutionResult
if result == nil {
Expand All @@ -84,6 +86,11 @@ func RenderExecutionResult(execution *testkube.Execution, logsOnly bool) {
if !logsOnly {
duration := execution.EndTime.Sub(execution.StartTime)
ui.Success("Test execution completed with success in " + duration.String())

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

PrintExecutionURIs(execution, info.DashboardUri)
}

case result.IsAborted():
Expand All @@ -99,6 +106,11 @@ func RenderExecutionResult(execution *testkube.Execution, logsOnly bool) {
ui.UseStderr()
ui.Warn("Test execution failed:\n")
ui.Errf(result.ErrorMessage)

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

PrintExecutionURIs(execution, info.DashboardUri)
}

ui.Info(result.Output)
Expand All @@ -118,3 +130,24 @@ func RenderExecutionResult(execution *testkube.Execution, logsOnly bool) {
}

}

func PrintExecutionURIs(execution *testkube.Execution, dashboardURI string) {
ui.NL()
ui.Link("Test URI:", fmt.Sprintf("%s/tests/%s", dashboardURI, execution.TestName))
ui.Link("Test Execution URI:", fmt.Sprintf("%s/tests/%s/executions/%s", dashboardURI,
execution.TestName, execution.Id))
ui.NL()
}

func PrintTestSuiteExecutionURIs(execution *testkube.TestSuiteExecution, dashboardURI string) {
ui.NL()
testSuiteName := ""
if execution.TestSuite != nil {
testSuiteName = execution.TestSuite.Name
}

ui.Link("Test Suite URI:", fmt.Sprintf("%s/test-suites/%s", dashboardURI, testSuiteName))
ui.Link("Test Suite Execution URI:", fmt.Sprintf("%s/test-suites/%s/executions/%s", dashboardURI,
testSuiteName, execution.Id))
ui.NL()
}
8 changes: 7 additions & 1 deletion cmd/kubectl-testkube/commands/common/render/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/pkg/ui"
)

Expand All @@ -15,7 +16,12 @@ func Obj(cmd *cobra.Command, obj interface{}, w io.Writer, renderer ...CliObjRen
switch outputType {
case OutputPretty:
if len(renderer) > 0 { // if custom renderer is set render using custom pretty renderer
return renderer[0](ui.NewUI(ui.Verbose, w), obj)
client, _, err := common.GetClient(cmd)
if err != nil {
return err
}

return renderer[0](client, ui.NewUI(ui.Verbose, w), obj)
}
return RenderYaml(obj, w) // fallback to yaml
case OutputYAML:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/tests/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewGetExecutionCmd() *cobra.Command {
ui.ExitOnError("getting test execution: "+executionID, err)

if logsOnly {
render.RenderExecutionResult(&execution, logsOnly)
render.RenderExecutionResult(client, &execution, logsOnly)
} else {
err = render.Obj(cmd, execution, os.Stdout, renderer.ExecutionRenderer)
ui.ExitOnError("rendering execution", err)
Expand Down
5 changes: 3 additions & 2 deletions cmd/kubectl-testkube/commands/tests/renderer/execution_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/renderer"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func ExecutionRenderer(ui *ui.UI, obj interface{}) error {
func ExecutionRenderer(client client.Client, ui *ui.UI, obj interface{}) error {
execution, ok := obj.(testkube.Execution)
if !ok {
return fmt.Errorf("can't render execution, expecrted obj to be testkube.Execution but got '%T'", obj)
Expand Down Expand Up @@ -56,7 +57,7 @@ func ExecutionRenderer(ui *ui.UI, obj interface{}) error {
ui.Warn(" Auth type: ", execution.Content.Repository.AuthType)
}

render.RenderExecutionResult(&execution, false)
render.RenderExecutionResult(client, &execution, false)

ui.NL()

Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/tests/renderer/test_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/renderer"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)
Expand All @@ -14,7 +15,7 @@ type mountParams struct {
path string
}

func TestRenderer(ui *ui.UI, obj interface{}) error {
func TestRenderer(client client.Client, ui *ui.UI, obj interface{}) error {
test, ok := obj.(testkube.Test)
if !ok {
return fmt.Errorf("can't use '%T' as testkube.Test in RenderObj for test", obj)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func NewRunTestCmd() *cobra.Command {
ui.ExitOnError("getting recent execution data id:"+execution.Id, err)
}

render.RenderExecutionResult(&execution, false)
render.RenderExecutionResult(client, &execution, false)

if execution.Id != "" {
if downloadArtifactsEnabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package renderer
import (
"fmt"

"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func TestSourceRenderer(ui *ui.UI, obj interface{}) error {
func TestSourceRenderer(client client.Client, ui *ui.UI, obj interface{}) error {
testSource, ok := obj.(testkube.TestSource)
if !ok {
return fmt.Errorf("can't use '%T' as testkube.TestSource in RenderObj for test source", obj)
Expand Down
13 changes: 12 additions & 1 deletion cmd/kubectl-testkube/commands/testsuites/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
apiclientv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
Expand Down Expand Up @@ -37,7 +38,7 @@ func printExecution(execution testkube.TestSuiteExecution, startTime time.Time)
ui.NL()
}

func uiPrintExecutionStatus(execution testkube.TestSuiteExecution) {
func uiPrintExecutionStatus(client apiclientv1.Client, execution testkube.TestSuiteExecution) {
if execution.Status == nil {
return
}
Expand All @@ -52,9 +53,19 @@ func uiPrintExecutionStatus(execution testkube.TestSuiteExecution) {
case execution.IsPassed():
ui.Success("Test Suite execution completed with sucess in " + execution.Duration)

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

render.PrintTestSuiteExecutionURIs(&execution, info.DashboardUri)

case execution.IsFailed():
ui.UseStderr()
ui.Errf("Test Suite execution failed")

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

render.PrintTestSuiteExecutionURIs(&execution, info.DashboardUri)
os.Exit(1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"os"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func TestSuiteExecutionRenderer(ui *ui.UI, obj interface{}) error {
func TestSuiteExecutionRenderer(client client.Client, ui *ui.UI, obj interface{}) error {
execution, ok := obj.(testkube.TestSuiteExecution)
if !ok {
return fmt.Errorf("can't render execution, expecrted obj to be testkube.Execution but got '%T'", obj)
Expand All @@ -26,6 +28,12 @@ func TestSuiteExecutionRenderer(ui *ui.UI, obj interface{}) error {
ui.Warn("Type: ", execution.RunningContext.Type_)
ui.Warn("Context:", execution.RunningContext.Context)
}

info, err := client.GetServerInfo()
ui.ExitOnError("getting server info", err)

render.PrintTestSuiteExecutionURIs(&execution, info.DashboardUri)

ui.Table(execution, os.Stdout)

ui.NL()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"strings"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/renderer"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func TestSuiteRenderer(ui *ui.UI, obj interface{}) error {
func TestSuiteRenderer(client client.Client, ui *ui.UI, obj interface{}) error {
ts, ok := obj.(testkube.TestSuite)
if !ok {
return fmt.Errorf("can't use '%T' as testkube.TestSuite in RenderObj for test suite", obj)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/testsuites/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func NewRunTestSuiteCmd() *cobra.Command {
printExecution(execution, startTime)
ui.ExitOnError("getting recent execution data id:"+execution.Id, err)

uiPrintExecutionStatus(execution)
uiPrintExecutionStatus(client, execution)

uiShellTestSuiteGetCommandBlock(execution.Id)
if execution.Id != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/testsuites/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewWatchTestSuiteExecutionCmd() *cobra.Command {
printExecution(execution, startTime)
ui.ExitOnError("getting recent execution data id:"+execution.Id, err)

uiPrintExecutionStatus(execution)
uiPrintExecutionStatus(client, execution)
uiShellTestSuiteGetCommandBlock(execution.Id)
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/app/api/v1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (s *TestkubeAPI) InfoHandler() fiber.Handler {
EnvId: os.Getenv(cloudEnvIdEnvName),
OrgId: os.Getenv(cloudOrgIdEnvName),
HelmchartVersion: s.helmchartVersion,
DashboardUri: s.dashboardURI,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func NewTestkubeAPI(
graphqlPort: graphqlPort,
artifactsStorage: artifactsStorage,
TemplatesClient: templatesClient,
dashboardURI: dashboardURI,
helmchartVersion: helmchartVersion,
mode: mode,
eventsBus: eventsBus,
Expand Down Expand Up @@ -182,6 +183,7 @@ type TestkubeAPI struct {
graphqlPort string
artifactsStorage storage.ArtifactsStorage
TemplatesClient *templatesclientv1.TemplatesClient
dashboardURI string
helmchartVersion string
mode string
eventsBus bus.Bus
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_server_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ type ServerInfo struct {
EnvId string `json:"envId,omitempty"`
// helm chart version
HelmchartVersion string `json:"helmchartVersion,omitempty"`
// dashboard uri
DashboardUri string `json:"dashboardUri,omitempty"`
}

0 comments on commit b31a4a3

Please sign in to comment.