-
Notifications
You must be signed in to change notification settings - Fork 107
test(cli): add artifact CLI tests using mock-based golden framework #6419
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
base: main
Are you sure you want to change the base?
Changes from 20 commits
299d3af
46ea753
e2fba50
156b8f7
1268cee
662d3fa
b806f75
50167d8
1854d92
fbcda82
fb333b5
d85ea5d
0d81ca5
00599e6
2728e07
f6cc27a
231a024
f503cb3
8c4ab04
0fba74f
4556e51
9aa2a52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,12 @@ | |
| package artifact | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| "google.golang.org/grpc" | ||
|
|
||
| "github.com/mindersec/minder/cmd/cli/app" | ||
| "github.com/mindersec/minder/internal/util" | ||
|
|
@@ -25,69 +23,76 @@ var listCmd = &cobra.Command{ | |
| Use: "list", | ||
| Short: "List artifacts from a provider", | ||
| Long: `The artifact list subcommand will list artifacts from a provider.`, | ||
| RunE: cli.GRPCClientWrapRunE(listCommand), | ||
| } | ||
| PreRunE: func(cmd *cobra.Command, _ []string) error { | ||
| if err := viper.BindPFlags(cmd.Flags()); err != nil { | ||
| return fmt.Errorf("error binding flags: %w", err) | ||
| } | ||
|
|
||
| // listCommand is the artifact list subcommand | ||
| func listCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error { | ||
| client := minderv1.NewArtifactServiceClient(conn) | ||
| format := viper.GetString("output") | ||
| if !app.IsOutputFormatSupported(format) { | ||
| return cli.MessageAndError(fmt.Sprintf("Output format %s not supported", format), fmt.Errorf("invalid argument")) | ||
| } | ||
|
|
||
| provider := viper.GetString("provider") | ||
| project := viper.GetString("project") | ||
| format := viper.GetString("output") | ||
| fromFilter := viper.GetString("from") | ||
| return nil | ||
| }, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converting this from a function named |
||
| ctx := cmd.Context() | ||
|
|
||
| // Ensure the output format is supported | ||
| if !app.IsOutputFormatSupported(format) { | ||
| return cli.MessageAndError(fmt.Sprintf("Output format %s not supported", format), fmt.Errorf("invalid argument")) | ||
| } | ||
| client, closer, err := cli.GetCLIClient(cmd, minderv1.NewArtifactServiceClient) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer closer() | ||
|
|
||
| // No longer print usage on returned error, since we've parsed our inputs | ||
| // See https://github.com/spf13/cobra/issues/340#issuecomment-374617413 | ||
| cmd.SilenceUsage = true | ||
| provider := viper.GetString("provider") | ||
| project := viper.GetString("project") | ||
| format := viper.GetString("output") | ||
| fromFilter := viper.GetString("from") | ||
|
|
||
| artifactList, err := client.ListArtifacts(ctx, &minderv1.ListArtifactsRequest{ | ||
| Context: &minderv1.Context{Provider: &provider, Project: &project}, | ||
| From: fromFilter, | ||
| }, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return cli.MessageAndError("Couldn't list artifacts", err) | ||
| } | ||
|
|
||
| switch format { | ||
| case app.Table: | ||
| t := table.New(table.Simple, layouts.Default, cmd.OutOrStdout(), | ||
| []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) | ||
| for _, artifact := range artifactList.Results { | ||
| t.AddRow( | ||
| artifact.ArtifactPk, | ||
| artifact.Type, | ||
| artifact.GetOwner(), | ||
| artifact.GetName(), | ||
| artifact.Repository, | ||
| artifact.Visibility, | ||
| artifact.CreatedAt.AsTime().Format(time.RFC3339), | ||
| ) | ||
| cmd.SilenceUsage = true | ||
|
|
||
| } | ||
| t.Render() | ||
| case app.JSON: | ||
| out, err := util.GetJsonFromProto(artifactList) | ||
| artifactList, err := client.ListArtifacts(ctx, &minderv1.ListArtifactsRequest{ | ||
| Context: &minderv1.Context{Provider: &provider, Project: &project}, | ||
| From: fromFilter, | ||
| }) | ||
| if err != nil { | ||
| return cli.MessageAndError("Error getting json from proto", err) | ||
| return cli.MessageAndError("Couldn't list artifacts", err) | ||
| } | ||
| cmd.Println(out) | ||
| case app.YAML: | ||
| out, err := util.GetYamlFromProto(artifactList) | ||
| if err != nil { | ||
| return cli.MessageAndError("Error getting yaml from proto", err) | ||
|
|
||
| switch format { | ||
| case app.Table: | ||
| t := table.New(table.Simple, layouts.Default, cmd.OutOrStdout(), | ||
| []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) | ||
| for _, artifact := range artifactList.Results { | ||
| t.AddRow( | ||
| artifact.ArtifactPk, | ||
| artifact.Type, | ||
| artifact.GetOwner(), | ||
| artifact.GetName(), | ||
| artifact.Repository, | ||
| artifact.Visibility, | ||
| artifact.CreatedAt.AsTime().Format(time.RFC3339), | ||
| ) | ||
| } | ||
| t.Render() | ||
|
|
||
| case app.JSON: | ||
| out, err := util.GetJsonFromProto(artifactList) | ||
| if err != nil { | ||
| return cli.MessageAndError("Error getting json from proto", err) | ||
| } | ||
| cmd.Println(out) | ||
|
|
||
| case app.YAML: | ||
| out, err := util.GetYamlFromProto(artifactList) | ||
| if err != nil { | ||
| return cli.MessageAndError("Error getting yaml from proto", err) | ||
| } | ||
| cmd.Println(out) | ||
| } | ||
| cmd.Println(out) | ||
| } | ||
|
|
||
| return nil | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 The Minder Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package artifact | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "go.uber.org/mock/gomock" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| "github.com/mindersec/minder/internal/util/cli" | ||
| minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
| mockv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1/mock" | ||
| ) | ||
|
|
||
| //nolint:paralleltest // Cannot run in parallel because it swaps global Viper/Stdout state | ||
| func TestArtifactListCommand(t *testing.T) { | ||
| setupSuccess := func(t *testing.T, ctrl *gomock.Controller) context.Context { | ||
| t.Helper() | ||
| client := mockv1.NewMockArtifactServiceClient(ctrl) | ||
|
|
||
| mockResp := &minderv1.ListArtifactsResponse{} | ||
| cli.LoadFixture(t, "mock_artifact_list.json", mockResp) | ||
|
|
||
| client.EXPECT(). | ||
| ListArtifacts(gomock.Any(), gomock.Any()). | ||
| Return(mockResp, nil). | ||
| Times(1) | ||
|
|
||
| return cli.WithRPCClient[minderv1.ArtifactServiceClient](context.Background(), client) | ||
| } | ||
|
|
||
| tests := []cli.CmdTestCase{ | ||
| { | ||
| Name: "list artifacts - table output", | ||
| Args: []string{"artifact", "list", "-o", "table"}, | ||
| MockSetup: setupSuccess, | ||
| GoldenFileName: "artifact_list.table", | ||
| }, | ||
| { | ||
| Name: "list artifacts - yaml output", | ||
| Args: []string{"artifact", "list", "-o", "yaml"}, | ||
| MockSetup: setupSuccess, | ||
| GoldenFileName: "artifact_list.yaml", | ||
| }, | ||
| { | ||
| Name: "list artifacts with from filter", | ||
| Args: []string{"artifact", "list", "--from", "repository=org/repo", "-o", "table"}, | ||
| MockSetup: setupSuccess, | ||
| GoldenFileName: "artifact_list_from.table", | ||
| }, | ||
| { | ||
| Name: "server error handling", | ||
| Args: []string{"artifact", "list"}, | ||
| MockSetup: func(t *testing.T, ctrl *gomock.Controller) context.Context { | ||
| t.Helper() | ||
| client := mockv1.NewMockArtifactServiceClient(ctrl) | ||
| client.EXPECT(). | ||
| ListArtifacts(gomock.Any(), gomock.Any()). | ||
| Return(nil, status.Error(codes.Internal, "internal server error")). | ||
| Times(1) | ||
|
|
||
| return cli.WithRPCClient[minderv1.ArtifactServiceClient](context.Background(), client) | ||
| }, | ||
| ExpectedError: "internal server error", | ||
| }, | ||
| } | ||
|
|
||
| cli.RunCmdTests(t, tests, ArtifactCmd) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "results": [ | ||
| { | ||
| "artifactPk": "111", | ||
| "name": "artifact-1", | ||
| "type": "image", | ||
| "owner": "owner-1", | ||
| "repository": "org/repo", | ||
| "visibility": "public", | ||
| "createdAt": "2024-01-02T15:04:05Z" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ID │ TYPE │ OWNER │ NAME │ REPOSITORY │ VISIBILITY │ CREATION DATE | ||
| ───────┼────────┼──────────┼──────────────┼──────────────┼──────────────┼─────────────────────────── | ||
| 111 │ image │ owner-1 │ artifact-1 │ org/repo │ public │ 2024-01-02T15:04:05Z |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| results: | ||
| - artifactPk: "111" | ||
| createdAt: "2024-01-02T15:04:05Z" | ||
| name: artifact-1 | ||
| owner: owner-1 | ||
| repository: org/repo | ||
| type: image | ||
| visibility: public | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ID │ TYPE │ OWNER │ NAME │ REPOSITORY │ VISIBILITY │ CREATION DATE | ||
| ───────┼────────┼──────────┼──────────────┼──────────────┼──────────────┼─────────────────────────── | ||
| 111 │ image │ owner-1 │ artifact-1 │ org/repo │ public │ 2024-01-02T15:04:05Z |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes in this file don't appear meaningful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds one newline, removes one newline, and removes a useful comment about why json output is not tested.
I don't understand why this comment was resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/sachin9058/minder/blob/test/cli-standardize/cmd/cli/app/artifact/artifact_get_test.go
Ahh sorry Evan actually i forget to address this comment and resolved this comment accidently. Now i have fixed this file so that it matches exactly with the upstream.