forked from mindersec/minder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifact_get_test.go
More file actions
93 lines (77 loc) · 3.02 KB
/
artifact_get_test.go
File metadata and controls
93 lines (77 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-FileCopyrightText: Copyright 2026 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
package artifact
// JSON output is not tested because protojson formatting is not stable across environments.
// This can cause flaky tests due to spacing differences.
// See maintainer discussion in PR #6417.
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 TestArtifactGetCommand(t *testing.T) {
setupSuccess := func(t *testing.T, ctrl *gomock.Controller) context.Context {
t.Helper()
artifactClient := mockv1.NewMockArtifactServiceClient(ctrl)
profileClient := mockv1.NewMockProfileServiceClient(ctrl)
artifactResp := &minderv1.GetArtifactByIdResponse{}
cli.LoadFixture(t, "mock_artifact_get.json", artifactResp)
artifactClient.EXPECT().
GetArtifactById(gomock.Any(), gomock.Any()).
Return(artifactResp, nil).
Times(1)
listProfilesResp := &minderv1.ListProfilesResponse{}
cli.LoadFixture(t, "mock_list_profiles.json", listProfilesResp)
profileClient.EXPECT().
ListProfiles(gomock.Any(), gomock.Any()).
Return(listProfilesResp, nil).
Times(1)
statusResp := &minderv1.GetProfileStatusByNameResponse{}
cli.LoadFixture(t, "mock_profile_status.json", statusResp)
profileClient.EXPECT().
GetProfileStatusByName(gomock.Any(), gomock.Any()).
Return(statusResp, nil).
Times(1)
ctx := cli.WithRPCClient[minderv1.ArtifactServiceClient](context.Background(), artifactClient)
ctx = cli.WithRPCClient[minderv1.ProfileServiceClient](ctx, profileClient)
return ctx
}
tests := []cli.CmdTestCase{
{
Name: "get artifact - table output",
Args: []string{"artifact", "get", "-i", "111", "-o", "table"},
MockSetup: setupSuccess,
GoldenFileName: "artifact_get.table",
},
{
Name: "get artifact - yaml output",
Args: []string{"artifact", "get", "-i", "111", "-o", "yaml"},
MockSetup: setupSuccess,
GoldenFileName: "artifact_get.yaml",
},
{
Name: "server error handling",
Args: []string{"artifact", "get", "-i", "111"},
MockSetup: func(t *testing.T, ctrl *gomock.Controller) context.Context {
t.Helper()
artifactClient := mockv1.NewMockArtifactServiceClient(ctrl)
profileClient := mockv1.NewMockProfileServiceClient(ctrl)
artifactClient.EXPECT().
GetArtifactById(gomock.Any(), gomock.Any()).
Return(nil, status.Error(codes.NotFound, "artifact not found")).
Times(1)
ctx := cli.WithRPCClient[minderv1.ArtifactServiceClient](context.Background(), artifactClient)
ctx = cli.WithRPCClient[minderv1.ProfileServiceClient](ctx, profileClient)
return ctx
},
ExpectedError: "artifact not found",
},
}
cli.RunCmdTests(t, tests, ArtifactCmd)
}