forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register file and Get executions functionality (flyteorg#22)
- Loading branch information
1 parent
7e68dec
commit 0b3e01a
Showing
16 changed files
with
854 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package get | ||
|
||
import ( | ||
"context" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/lyft/flytestdlib/logger" | ||
|
||
"github.com/lyft/flytectl/cmd/config" | ||
cmdCore "github.com/lyft/flytectl/cmd/core" | ||
"github.com/lyft/flytectl/pkg/printer" | ||
) | ||
|
||
var executionColumns = []printer.Column{ | ||
{"Name", "$.id.name"}, | ||
{"Workflow Name", "$.closure.workflowId.name"}, | ||
{"Type", "$.closure.workflowId.resourceType"}, | ||
{"Phase", "$.closure.phase"}, | ||
{"Started", "$.closure.startedAt"}, | ||
{"Elapsed Time", "$.closure.duration"}, | ||
} | ||
|
||
func ExecutionToProtoMessages(l []*admin.Execution) []proto.Message { | ||
messages := make([]proto.Message, 0, len(l)) | ||
for _, m := range l { | ||
messages = append(messages, m) | ||
} | ||
return messages | ||
} | ||
|
||
func getExecutionFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error { | ||
adminPrinter := printer.Printer{} | ||
var executions []* admin.Execution | ||
if len(args) > 0 { | ||
name := args[0] | ||
execution, err := cmdCtx.AdminClient().GetExecution(ctx, &admin.WorkflowExecutionGetRequest{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: config.GetConfig().Project, | ||
Domain: config.GetConfig().Domain, | ||
Name: name, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
executions = append(executions, execution) | ||
} else { | ||
executionList, err := cmdCtx.AdminClient().ListExecutions(ctx, &admin.ResourceListRequest{ | ||
Limit: 100, | ||
Id: &admin.NamedEntityIdentifier{ | ||
Project: config.GetConfig().Project, | ||
Domain: config.GetConfig().Domain, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
executions = executionList.Executions | ||
} | ||
logger.Infof(ctx, "Retrieved %v executions", len(executions)) | ||
err := adminPrinter.Print(config.GetConfig().MustOutputFormat(), executionColumns, ExecutionToProtoMessages(executions)...) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package get | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/lyft/flytectl/cmd/config" | ||
cmdCore "github.com/lyft/flytectl/cmd/core" | ||
"github.com/lyft/flyteidl/clients/go/admin/mocks" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"testing" | ||
) | ||
|
||
const projectValue = "dummyProject" | ||
const domainValue = "dummyDomain" | ||
const executionNameValue = "e124" | ||
const launchPlanNameValue = "lp_name" | ||
const launchPlanVersionValue = "lp_version" | ||
const workflowNameValue = "wf_name" | ||
const workflowVersionValue = "wf_version" | ||
|
||
func TestListExecutionFunc(t *testing.T) { | ||
ctx := context.Background() | ||
config.GetConfig().Project = projectValue | ||
config.GetConfig().Domain = domainValue | ||
config.GetConfig().Output = "json" | ||
var args []string | ||
mockClient := new(mocks.AdminServiceClient) | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream) | ||
execListRequest := &admin.ResourceListRequest{ | ||
Limit: 100, | ||
Id: &admin.NamedEntityIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
}, | ||
} | ||
executionResponse := &admin.Execution{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
Spec: &admin.ExecutionSpec{ | ||
LaunchPlan: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: launchPlanNameValue, | ||
Version: launchPlanVersionValue, | ||
}, | ||
}, | ||
Closure: &admin.ExecutionClosure{ | ||
WorkflowId: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: workflowNameValue, | ||
Version: workflowVersionValue, | ||
}, | ||
Phase: core.WorkflowExecution_SUCCEEDED, | ||
}, | ||
} | ||
var executions []*admin.Execution | ||
executions = append(executions, executionResponse) | ||
executionList := &admin.ExecutionList{ | ||
Executions: executions, | ||
} | ||
mockClient.OnListExecutionsMatch(ctx, execListRequest).Return(executionList, nil) | ||
err := getExecutionFunc(ctx, args, cmdCtx) | ||
assert.Nil(t, err) | ||
mockClient.AssertCalled(t, "ListExecutions", ctx, execListRequest) | ||
} | ||
|
||
func TestListExecutionFuncWithError(t *testing.T) { | ||
ctx := context.Background() | ||
config.GetConfig().Project = projectValue | ||
config.GetConfig().Domain = domainValue | ||
config.GetConfig().Output = "json" | ||
var args []string | ||
mockClient := new(mocks.AdminServiceClient) | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream) | ||
execListRequest := &admin.ResourceListRequest{ | ||
Limit: 100, | ||
Id: &admin.NamedEntityIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
}, | ||
} | ||
executionResponse := &admin.Execution{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
Spec: &admin.ExecutionSpec{ | ||
LaunchPlan: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: launchPlanNameValue, | ||
Version: launchPlanVersionValue, | ||
}, | ||
}, | ||
Closure: &admin.ExecutionClosure{ | ||
WorkflowId: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: workflowNameValue, | ||
Version: workflowVersionValue, | ||
}, | ||
Phase: core.WorkflowExecution_SUCCEEDED, | ||
}, | ||
} | ||
var executions []*admin.Execution | ||
executions = append(executions, executionResponse) | ||
mockClient.OnListExecutionsMatch(ctx, execListRequest).Return(nil, errors.New("Executions NotFound.")) | ||
err := getExecutionFunc(ctx, args, cmdCtx) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, err, errors.New("Executions NotFound.")) | ||
mockClient.AssertCalled(t, "ListExecutions", ctx, execListRequest) | ||
} | ||
|
||
func TestGetExecutionFunc(t *testing.T) { | ||
ctx := context.Background() | ||
config.GetConfig().Project = projectValue | ||
config.GetConfig().Domain = domainValue | ||
config.GetConfig().Output = "json" | ||
mockClient := new(mocks.AdminServiceClient) | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream) | ||
execGetRequest := &admin.WorkflowExecutionGetRequest{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
} | ||
executionResponse := &admin.Execution{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
Spec: &admin.ExecutionSpec{ | ||
LaunchPlan: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: launchPlanNameValue, | ||
Version: launchPlanVersionValue, | ||
}, | ||
}, | ||
Closure: &admin.ExecutionClosure{ | ||
WorkflowId: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: workflowNameValue, | ||
Version: workflowVersionValue, | ||
}, | ||
Phase: core.WorkflowExecution_SUCCEEDED, | ||
}, | ||
} | ||
var executions []*admin.Execution | ||
executions = append(executions, executionResponse) | ||
args := []string{executionNameValue} | ||
mockClient.OnGetExecutionMatch(ctx, execGetRequest).Return(executionResponse, nil) | ||
err := getExecutionFunc(ctx, args, cmdCtx) | ||
assert.Nil(t, err) | ||
mockClient.AssertCalled(t, "GetExecution", ctx, execGetRequest) | ||
} | ||
|
||
func TestGetExecutionFuncWithError(t *testing.T) { | ||
ctx := context.Background() | ||
config.GetConfig().Project = projectValue | ||
config.GetConfig().Domain = domainValue | ||
config.GetConfig().Output = "json" | ||
mockClient := new(mocks.AdminServiceClient) | ||
mockOutStream := new(io.Writer) | ||
cmdCtx := cmdCore.NewCommandContext(mockClient, *mockOutStream) | ||
execGetRequest := &admin.WorkflowExecutionGetRequest{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
} | ||
executionResponse := &admin.Execution{ | ||
Id: &core.WorkflowExecutionIdentifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: executionNameValue, | ||
}, | ||
Spec: &admin.ExecutionSpec{ | ||
LaunchPlan: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: launchPlanNameValue, | ||
Version: launchPlanVersionValue, | ||
}, | ||
}, | ||
Closure: &admin.ExecutionClosure{ | ||
WorkflowId: &core.Identifier{ | ||
Project: projectValue, | ||
Domain: domainValue, | ||
Name: workflowNameValue, | ||
Version: workflowVersionValue, | ||
}, | ||
Phase: core.WorkflowExecution_SUCCEEDED, | ||
}, | ||
} | ||
var executions []*admin.Execution | ||
executions = append(executions, executionResponse) | ||
args := []string{executionNameValue} | ||
mockClient.OnGetExecutionMatch(ctx, execGetRequest).Return(nil, errors.New("Execution NotFound.")) | ||
err := getExecutionFunc(ctx, args, cmdCtx) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, err, errors.New("Execution NotFound.")) | ||
mockClient.AssertCalled(t, "GetExecution", ctx, execGetRequest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package get | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestCreateGetCommand(t *testing.T) { | ||
getCommand := CreateGetCommand() | ||
assert.Equal(t, getCommand.Use , "get") | ||
assert.Equal(t, getCommand.Short , "Retrieve various resource.") | ||
fmt.Println(getCommand.Commands()) | ||
assert.Equal(t, len(getCommand.Commands()), 5) | ||
cmdNouns := getCommand.Commands() | ||
// Sort by Use value. | ||
sort.Slice(cmdNouns, func(i, j int) bool { | ||
return cmdNouns[i].Use < cmdNouns[j].Use | ||
}) | ||
assert.Equal(t, cmdNouns[0].Use, "execution") | ||
assert.Equal(t, cmdNouns[0].Aliases, []string{"executions"}) | ||
assert.Equal(t, cmdNouns[0].Short, "Retrieves execution resources.") | ||
assert.Equal(t, cmdNouns[1].Use, "launchplan") | ||
assert.Equal(t, cmdNouns[1].Aliases, []string{"launchplans"}) | ||
assert.Equal(t, cmdNouns[1].Short, "Retrieves launchplan resources.") | ||
assert.Equal(t, cmdNouns[2].Use, "project") | ||
assert.Equal(t, cmdNouns[2].Aliases, []string{"projects"}) | ||
assert.Equal(t, cmdNouns[2].Short, "Retrieves project resources.") | ||
assert.Equal(t, cmdNouns[3].Use, "task") | ||
assert.Equal(t, cmdNouns[3].Aliases, []string{"tasks"}) | ||
assert.Equal(t, cmdNouns[3].Short, "Retrieves task resources.") | ||
assert.Equal(t, cmdNouns[4].Use, "workflow") | ||
assert.Equal(t, cmdNouns[4].Aliases, []string{"workflows"}) | ||
assert.Equal(t, cmdNouns[4].Short, "Retrieves workflow resources.") | ||
} |
Oops, something went wrong.