Skip to content

Commit

Permalink
Test SelectTask
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Nov 25, 2024
1 parent 51a7899 commit 21756f1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func runExec(ctx context.Context, client *ecs.Client, region string, clusterId s
if err != nil {
return err
}
task, err := selector.SelectTask(ctx, client, *cluster.ClusterArn, *service.ServiceName, taskId)
task, err := selector.SelectTask(ctx, client, defaultSelector, *cluster.ClusterArn, *service.ServiceName, taskId)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions selector/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func SelectCluster(ctx context.Context, client Client, selector Selector, cluste
return nil, fmt.Errorf("no cluster '%v' found", clusterId)
}

func SelectService(ctx context.Context, client Client, selector Selector, clusterId string, serviceId string) (*types.Service, error) {
func SelectService(ctx context.Context, client Client, selector Selector, clusterArn string, serviceId string) (*types.Service, error) {
if serviceId == "" {
listServices, err := client.ListServices(ctx, &ecs.ListServicesInput{
Cluster: &clusterId,
Cluster: &clusterArn,
})
if err != nil {
return nil, err
Expand All @@ -67,7 +67,7 @@ func SelectService(ctx context.Context, client Client, selector Selector, cluste
serviceId = serviceArn
}
describeService, err := client.DescribeServices(ctx, &ecs.DescribeServicesInput{
Cluster: &clusterId,
Cluster: &clusterArn,
Services: []string{serviceId},
})
if err != nil {
Expand All @@ -79,23 +79,23 @@ func SelectService(ctx context.Context, client Client, selector Selector, cluste
return nil, fmt.Errorf("no service '%v' found", serviceId)
}

func SelectTask(ctx context.Context, client Client, clusterId string, serviceId string, taskId string) (*types.Task, error) {
func SelectTask(ctx context.Context, client Client, selector Selector, clusterArn string, serviceName string, taskId string) (*types.Task, error) {
if taskId == "" {
listTasks, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterId,
ServiceName: &serviceId,
Cluster: &clusterArn,
ServiceName: &serviceName,
})
if err != nil {
return nil, err
}
taskArn, err := pterm.DefaultInteractiveSelect.WithOptions(listTasks.TaskArns).Show("Task")
taskArn, err := selector.Select("Task", listTasks.TaskArns)
if err != nil {
return nil, err
}
taskId = taskArn
}
describeTasks, err := client.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterId,
Cluster: &clusterArn,
Tasks: []string{taskId},
})
if err != nil {
Expand Down
79 changes: 66 additions & 13 deletions selector/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,58 @@ import (
type SpyClient struct{}

func (s *SpyClient) DescribeClusters(ctx context.Context, input *ecs.DescribeClustersInput, options ...func(*ecs.Options)) (*ecs.DescribeClustersOutput, error) {
clusterName := input.Clusters[0]
clusterArn := fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:cluster/%s", clusterName)
cluster := input.Clusters[0]
var clusterArn string
if strings.Contains(cluster, "/") {
clusterArn = cluster
} else {
clusterArn = fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:cluster/%s", cluster)
}
return &ecs.DescribeClustersOutput{Clusters: []types.Cluster{
{ClusterName: &clusterName, ClusterArn: &clusterArn},
{ClusterArn: &clusterArn},
}}, nil
}

func (s *SpyClient) DescribeServices(ctx context.Context, input *ecs.DescribeServicesInput, options ...func(*ecs.Options)) (*ecs.DescribeServicesOutput, error) {
clusterSplices := strings.Split(*input.Cluster, "/")
serviceName := input.Services[0]
serviceArn := fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:service/%s/%s", clusterSplices[1], serviceName)
clusterName := strings.Split(*input.Cluster, "/")[1]
serviceArn := fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:service/%s/%s", clusterName, input.Services[0])
return &ecs.DescribeServicesOutput{Services: []types.Service{
{ServiceName: &serviceName, ServiceArn: &serviceArn},
{ServiceArn: &serviceArn},
}}, nil
}

func (s *SpyClient) DescribeTasks(ctx context.Context, input *ecs.DescribeTasksInput, options ...func(*ecs.Options)) (*ecs.DescribeTasksOutput, error) {
return nil, nil
task := input.Tasks[0]
var taskArn string
if strings.Contains(task, "arn:aws:ecs") {
taskArn = task
} else {
taskArn = fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:task/%s", task)
}
return &ecs.DescribeTasksOutput{Tasks: []types.Task{
{TaskArn: &taskArn},
}}, nil
}

func (s *SpyClient) ListClusters(ctx context.Context, input *ecs.ListClustersInput, options ...func(*ecs.Options)) (*ecs.ListClustersOutput, error) {
return &ecs.ListClustersOutput{ClusterArns: []string{"cluster-1", "cluster-2"}}, nil
clusterArns := []string{
"arn:aws:ecs:us-east-1:111111111111:cluster/cluster-1",
"arn:aws:ecs:us-east-1:111111111111:cluster/cluster-2",
}
return &ecs.ListClustersOutput{ClusterArns: clusterArns}, nil
}

func (s *SpyClient) ListServices(ctx context.Context, input *ecs.ListServicesInput, options ...func(*ecs.Options)) (*ecs.ListServicesOutput, error) {
return &ecs.ListServicesOutput{ServiceArns: []string{"service-1", "service-2"}}, nil
}

func (s *SpyClient) ListTasks(ctx context.Context, input *ecs.ListTasksInput, options ...func(*ecs.Options)) (*ecs.ListTasksOutput, error) {
return nil, nil
clusterSplices := strings.Split(*input.Cluster, "/")
taskArns := []string{
fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:task/%s/7f9ea0d0011a41b7b3f6c37cb29cd25b", clusterSplices[1]),
fmt.Sprintf("arn:aws:ecs:us-east-1:111111111111:task/%s/e2c735b1aca94012b37e03d8fe1bfb5f", clusterSplices[1]),
}
return &ecs.ListTasksOutput{TaskArns: taskArns}, nil
}

type SpySelector struct{}
Expand Down Expand Up @@ -85,10 +107,10 @@ func TestSelectorCluster(t *testing.T) {
func TestSelectorService(t *testing.T) {
client := &SpyClient{}
selector := &SpySelector{}
clusterId := "arn:aws:ecs:us-east-1:111111111111:cluster/cluster"
clusterArn := "arn:aws:ecs:us-east-1:111111111111:cluster/cluster"

t.Run("serviceId is empty", func(t *testing.T) {
service, err := SelectService(context.TODO(), client, selector, clusterId, "")
service, err := SelectService(context.TODO(), client, selector, clusterArn, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -100,7 +122,7 @@ func TestSelectorService(t *testing.T) {
})

t.Run("serviceId is not empty", func(t *testing.T) {
service, err := SelectService(context.TODO(), client, selector, clusterId, "service")
service, err := SelectService(context.TODO(), client, selector, clusterArn, "service")
if err != nil {
t.Fatal(err)
}
Expand All @@ -111,3 +133,34 @@ func TestSelectorService(t *testing.T) {
}
})
}

func TestSelectorTask(t *testing.T) {
client := &SpyClient{}
selector := &SpySelector{}
clusterArn := "arn:aws:ecs:us-east-1:111111111111:cluster/cluster"
serviceName := "service"

t.Run("taskId is empty", func(t *testing.T) {
task, err := SelectTask(context.TODO(), client, selector, clusterArn, serviceName, "")
if err != nil {
t.Fatal(err)
}
got := *task.TaskArn
want := "arn:aws:ecs:us-east-1:111111111111:task/cluster/7f9ea0d0011a41b7b3f6c37cb29cd25b"
if got != want {
t.Errorf("got %q want %q", got, want)
}
})

t.Run("taskId is not empty", func(t *testing.T) {
task, err := SelectTask(context.TODO(), client, selector, clusterArn, serviceName, "cluster/44d9c0c0af0348ec9f57e4e413293c6b")
if err != nil {
t.Fatal(err)
}
got := *task.TaskArn
want := "arn:aws:ecs:us-east-1:111111111111:task/cluster/44d9c0c0af0348ec9f57e4e413293c6b"
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}

0 comments on commit 21756f1

Please sign in to comment.