Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests to ExecuteWithClient for pipelines cmds #2111

Merged
merged 27 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/conduit/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

type Client struct {
conn *grpc.ClientConn
apiv1.PipelineServiceClient
conn *grpc.ClientConn
PipelineServiceClient PipelineService
apiv1.ProcessorServiceClient
apiv1.ConnectorServiceClient
healthgrpc.HealthClient
Expand Down
103 changes: 103 additions & 0 deletions cmd/conduit/api/mock/pipeline_service.go

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

31 changes: 31 additions & 0 deletions cmd/conduit/api/pipeline_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2025 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"context"

apiv1 "github.com/conduitio/conduit/proto/api/v1"
"google.golang.org/grpc"
)

//go:generate mockgen -destination=mock/pipeline_service.go -package=mock . PipelineService

// PipelineService defines the methods of the PipelineServiceClient that are currently used by the CLI.
type PipelineService interface {
ListPipelines(ctx context.Context, in *apiv1.ListPipelinesRequest, opts ...grpc.CallOption) (*apiv1.ListPipelinesResponse, error)
GetPipeline(ctx context.Context, in *apiv1.GetPipelineRequest, opts ...grpc.CallOption) (*apiv1.GetPipelineResponse, error)
GetDLQ(ctx context.Context, in *apiv1.GetDLQRequest, opts ...grpc.CallOption) (*apiv1.GetDLQResponse, error)
}
1 change: 0 additions & 1 deletion cmd/conduit/internal/print_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ func DisplayConfigParams(cfg map[string]*configv1.Parameter) {
table.Body.Cells = append(table.Body.Cells, r)
}

table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
}

Expand Down
1 change: 0 additions & 1 deletion cmd/conduit/root/connectorplugins/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,5 @@ func displayConnectorPlugins(connectorPlugins []*apiv1.ConnectorPluginSpecificat

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
}
2 changes: 0 additions & 2 deletions cmd/conduit/root/connectors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ func displayConnectors(connectors []*apiv1.Connector) {
{Align: simpletable.AlignLeft, Text: internal.PrintTime(c.CreatedAt)},
{Align: simpletable.AlignLeft, Text: internal.PrintTime(c.UpdatedAt)},
}

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
}
18 changes: 12 additions & 6 deletions cmd/conduit/root/pipelines/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ var (
_ cecdysis.CommandWithExecuteWithClient = (*ListCommand)(nil)
_ ecdysis.CommandWithAliases = (*ListCommand)(nil)
_ ecdysis.CommandWithDocs = (*ListCommand)(nil)
_ ecdysis.CommandWithOutput = (*ListCommand)(nil)
)

type ListCommand struct{}
type ListCommand struct {
output ecdysis.Output
}

func (c *ListCommand) Docs() ecdysis.Docs {
return ecdysis.Docs{
Expand All @@ -45,6 +48,10 @@ be configured via --pipelines.path at the time of running Conduit.`,
}
}

func (c *ListCommand) Output(output ecdysis.Output) {
c.output = output
}

func (c *ListCommand) Aliases() []string { return []string{"ls"} }

func (c *ListCommand) Usage() string { return "list" }
Expand All @@ -59,14 +66,14 @@ func (c *ListCommand) ExecuteWithClient(ctx context.Context, client *api.Client)
return resp.Pipelines[i].Id < resp.Pipelines[j].Id
})

displayPipelines(resp.Pipelines)
c.output.Stdout(getPipelinesTable(resp.Pipelines))

return nil
}

func displayPipelines(pipelines []*apiv1.Pipeline) {
func getPipelinesTable(pipelines []*apiv1.Pipeline) string {
if len(pipelines) == 0 {
return
return ""
}

table := simpletable.New()
Expand All @@ -90,6 +97,5 @@ func displayPipelines(pipelines []*apiv1.Pipeline) {

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
return table.String()
}
107 changes: 107 additions & 0 deletions cmd/conduit/root/pipelines/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright © 2025 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pipelines

import (
"bytes"
"context"
"strings"
"testing"

"github.com/conduitio/ecdysis"
"github.com/matryer/is"
"go.uber.org/mock/gomock"

"github.com/conduitio/conduit/cmd/conduit/api"
"github.com/conduitio/conduit/cmd/conduit/api/mock"
apiv1 "github.com/conduitio/conduit/proto/api/v1"
)

func TestListCommandExecuteWithClient_WithPipelines(t *testing.T) {
is := is.New(t)

buf := new(bytes.Buffer)
out := &ecdysis.DefaultOutput{}
out.Output(buf, nil)

ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mock.NewMockPipelineService(ctrl)

mockService.EXPECT().ListPipelines(gomock.Any(), gomock.Any()).Return(&apiv1.ListPipelinesResponse{
Pipelines: []*apiv1.Pipeline{
{Id: "1", State: &apiv1.Pipeline_State{Status: apiv1.Pipeline_STATUS_RUNNING}},
{Id: "2", State: &apiv1.Pipeline_State{Status: apiv1.Pipeline_STATUS_STOPPED}},
{Id: "3", State: &apiv1.Pipeline_State{Status: apiv1.Pipeline_STATUS_RECOVERING}},
{Id: "4", State: &apiv1.Pipeline_State{Status: apiv1.Pipeline_STATUS_DEGRADED}},
},
}, nil)

client := &api.Client{
PipelineServiceClient: mockService,
}

cmd := &ListCommand{}
cmd.Output(out)

err := cmd.ExecuteWithClient(ctx, client)
is.NoErr(err)

output := buf.String()
is.True(len(output) > 0)
is.True(strings.Contains(output, "ID"))
raulb marked this conversation as resolved.
Show resolved Hide resolved
is.True(strings.Contains(output, "CREATED"))
is.True(strings.Contains(output, "LAST_UPDATED"))

is.True(strings.Contains(output, "1"))
is.True(strings.Contains(output, "running"))
is.True(strings.Contains(output, "2"))
is.True(strings.Contains(output, "stopped"))
is.True(strings.Contains(output, "3"))
is.True(strings.Contains(output, "recovering"))
is.True(strings.Contains(output, "4"))
is.True(strings.Contains(output, "degraded"))
}

func TestListCommandExecuteWithClient_EmptyResponse(t *testing.T) {
is := is.New(t)

buf := new(bytes.Buffer)
out := &ecdysis.DefaultOutput{}
out.Output(buf, nil)

ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockService := mock.NewMockPipelineService(ctrl)

mockService.EXPECT().ListPipelines(gomock.Any(), gomock.Any()).Return(&apiv1.ListPipelinesResponse{}, nil)

client := &api.Client{
PipelineServiceClient: mockService,
}

cmd := &ListCommand{}
cmd.Output(out)

err := cmd.ExecuteWithClient(ctx, client)
is.NoErr(err)

output := buf.String()
is.True(len(output) == 0)
}
1 change: 0 additions & 1 deletion cmd/conduit/root/processorplugins/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,5 @@ func displayProcessorPlugins(processorPlugins []*apiv1.ProcessorPluginSpecificat

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
}
1 change: 0 additions & 1 deletion cmd/conduit/root/processors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ func displayProcessors(processors []*apiv1.Processor) {

table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleDefault)
fmt.Println(table.String())
}
1 change: 0 additions & 1 deletion cmd/conduit/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func TestRootCommandExecuteWithVersionFlag(t *testing.T) {
is := is.New(t)

buf := new(bytes.Buffer)

out := &ecdysis.DefaultOutput{}
out.Output(buf, nil)

Expand Down