Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Graphviz interfaces and unit tests (#106)
Browse files Browse the repository at this point in the history
* Linter fixes

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* Added some coverage

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* Added interface for the mehtods used from graphviz

Signed-off-by: Prafulla Mahindrakar <[email protected]>

* Fixed the workflow config flag file and bug fixes

Signed-off-by: Prafulla Mahindrakar <[email protected]>
(cherry picked from commit 77be561)

* Added unit tests

Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored Jun 18, 2021
1 parent 990713a commit 3528d2f
Show file tree
Hide file tree
Showing 10 changed files with 835 additions and 86 deletions.
19 changes: 15 additions & 4 deletions cmd/config/subcommand/workflow/config_flags.go

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

44 changes: 0 additions & 44 deletions cmd/config/subcommand/workflow/config_flags_test.go

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

6 changes: 2 additions & 4 deletions cmd/config/subcommand/workflow/workflow_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/flyteorg/flytectl/pkg/filters"
)

//go:generate pflags Config --default-var DefaultConfig
//go:generate pflags Config --default-var DefaultConfig --bind-default-var

var (
DefaultConfig = &Config{
Expand All @@ -17,6 +17,4 @@ type Config struct {
Version string `json:"version" pflag:",version of the workflow to be fetched."`
Latest bool `json:"latest" pflag:", flag to indicate to fetch the latest version, version flag will be ignored in this case"`
Filter filters.Filters `json:"filter" pflag:","`
Visualize string `json:"visualize" pflag:",optional flag to visualize a workflow as one of [png, dot, svg, jpg]"`
OutputFile string `json:"output_file" pflag:",path and a filename of where the output image should be dumped. This can only be used in concert with visualize"`
}
}
1 change: 1 addition & 0 deletions cmd/get/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package get

import (
"context"

workflowconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/workflow"
"github.com/flyteorg/flytectl/pkg/ext"
"github.com/flyteorg/flytestdlib/logger"
Expand Down
8 changes: 4 additions & 4 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/browser"
"net/url"
"os"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/kataras/tablewriter"
"github.com/landoop/tableprinter"
"github.com/pkg/browser"
"github.com/yalp/jsonpath"
)

Expand All @@ -31,7 +31,7 @@ const (
OutputFormatDOTURL
)

const GraphVisualizationServiceUrl = "http://graph.flyte.org/#"
const GraphVisualizationServiceURL = "http://graph.flyte.org/#"

func OutputFormats() []string {
var v []string
Expand Down Expand Up @@ -164,8 +164,8 @@ func (p Printer) Print(format OutputFormat, columns []Column, messages ...proto.
return errors.Wrapf("VisualizationError", err, "failed to visualize workflow")
}
if format == OutputFormatDOTURL {
urlToOpen := GraphVisualizationServiceUrl + url.PathEscape(graphStr)
fmt.Println("Opening the browser at "+ urlToOpen)
urlToOpen := GraphVisualizationServiceURL + url.PathEscape(graphStr)
fmt.Println("Opening the browser at " + urlToOpen)
return browser.OpenURL(urlToOpen)
}
fmt.Println(graphStr)
Expand Down
134 changes: 131 additions & 3 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package printer

import (
"encoding/json"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -27,6 +28,14 @@ func LaunchplanToProtoMessages(l []*admin.LaunchPlan) []proto.Message {
return messages
}

func WorkflowToProtoMessages(l []*admin.Workflow) []proto.Message {
messages := make([]proto.Message, 0, len(l))
for _, m := range l {
messages = append(messages, m)
}
return messages
}

// TODO Convert this to a Testable Example. For some reason the comparison fails
func TestJSONToTable(t *testing.T) {
d := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -57,9 +66,9 @@ func TestJSONToTable(t *testing.T) {
}

func TestOutputFormats(t *testing.T) {
expected := []string{"TABLE", "JSON", "YAML"}
expected := []string{"TABLE", "JSON", "YAML", "DOT", "DOTURL"}
outputs := OutputFormats()
assert.Equal(t, 3, len(outputs))
assert.Equal(t, 5, len(outputs))
assert.Equal(t, expected, outputs)
}

Expand All @@ -77,7 +86,7 @@ func TestOutputFormatStringErr(t *testing.T) {
}

func TestIsAOutputFormat(t *testing.T) {
o := OutputFormat(4)
o := OutputFormat(5)
check := o.IsAOutputFormat()
assert.Equal(t, false, check)

Expand Down Expand Up @@ -123,4 +132,123 @@ func TestPrint(t *testing.T) {
assert.Nil(t, err)
err = p.Print(OutputFormat(2), lp, LaunchplanToProtoMessages(launchPlans)...)
assert.Nil(t, err)
err = p.Print(OutputFormat(3), lp, LaunchplanToProtoMessages(launchPlans)...)
assert.NotNil(t, err)
err = p.Print(OutputFormat(4), lp, LaunchplanToProtoMessages(launchPlans)...)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("visualization is only supported on workflows"), err)

sortedListLiteralType := core.Variable{
Type: &core.LiteralType{
Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{
Type: &core.LiteralType_Simple{
Simple: core.SimpleType_INTEGER,
},
},
},
},
}
variableMap := map[string]*core.Variable{
"sorted_list1": &sortedListLiteralType,
"sorted_list2": &sortedListLiteralType,
}

var compiledTasks []*core.CompiledTask
compiledTasks = append(compiledTasks, &core.CompiledTask{
Template: &core.TaskTemplate{
Id: &core.Identifier{
Project: "dummyProject",
Domain: "dummyDomain",
Name: "dummyName",
Version: "dummyVersion",
},
Interface: &core.TypedInterface{
Inputs: &core.VariableMap{
Variables: variableMap,
},
},
},
})

workflow1 := &admin.Workflow{
Id: &core.Identifier{
Name: "task1",
Version: "v1",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 1, Nanos: 0},
CompiledWorkflow: &core.CompiledWorkflowClosure{
Tasks: compiledTasks,
},
},
}

workflows := []*admin.Workflow{workflow1}

err = p.Print(OutputFormat(3), lp, WorkflowToProtoMessages(workflows)...)
assert.Nil(t, err)
workflows = []*admin.Workflow{}
err = p.Print(OutputFormat(3), lp, WorkflowToProtoMessages(workflows)...)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("atleast one workflow required for visualization"), err)
var badCompiledTasks []*core.CompiledTask
badCompiledTasks = append(badCompiledTasks, &core.CompiledTask{
Template: &core.TaskTemplate{},
})
badWorkflow := &admin.Workflow{
Id: &core.Identifier{
Name: "task1",
Version: "v1",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 1, Nanos: 0},
CompiledWorkflow: &core.CompiledWorkflowClosure{
Tasks: badCompiledTasks,
},
},
}
workflows = []*admin.Workflow{badWorkflow}
err = p.Print(OutputFormat(3), lp, WorkflowToProtoMessages(workflows)...)
assert.NotNil(t, err)

assert.Equal(t, fmt.Errorf("no template found in the workflow task template:<> "), errors.Unwrap(err))

badWorkflow2 := &admin.Workflow{
Id: &core.Identifier{
Name: "task1",
Version: "v1",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 1, Nanos: 0},
CompiledWorkflow: nil,
},
}
workflows = []*admin.Workflow{badWorkflow2}
err = p.Print(OutputFormat(3), lp, WorkflowToProtoMessages(workflows)...)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("empty workflow closure"), errors.Unwrap(err))

var badSubWorkflow []*core.CompiledWorkflow
badSubWorkflow = append(badSubWorkflow, &core.CompiledWorkflow{
Template: &core.WorkflowTemplate{},
})

badWorkflow3 := &admin.Workflow{
Id: &core.Identifier{
Name: "task1",
Version: "v1",
},
Closure: &admin.WorkflowClosure{
CreatedAt: &timestamppb.Timestamp{Seconds: 1, Nanos: 0},
CompiledWorkflow: &core.CompiledWorkflowClosure{
Tasks: compiledTasks,
SubWorkflows: badSubWorkflow,
},
},
}
workflows = []*admin.Workflow{badWorkflow3}
err = p.Print(OutputFormat(3), lp, WorkflowToProtoMessages(workflows)...)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("no template found in the sub workflow template:<> "), errors.Unwrap(err))
}
Loading

0 comments on commit 3528d2f

Please sign in to comment.