@@ -8,109 +8,63 @@ import (
88 "testing"
99
1010 "go.uber.org/mock/gomock"
11- "google.golang.org/grpc"
1211 "google.golang.org/grpc/codes"
1312 "google.golang.org/grpc/status"
1413
1514 "github.com/mindersec/minder/internal/util/cli"
1615 minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
16+ mockv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1/mock"
1717)
1818
19- type artifactClientStub struct {
20- getArtifactByIDResp * minderv1.GetArtifactByIdResponse
21- getArtifactByIDErr error
22- getArtifactByNameResp * minderv1.GetArtifactByNameResponse
23- getArtifactByNameErr error
24- }
25-
26- func (s * artifactClientStub ) ListArtifacts (context.Context , * minderv1.ListArtifactsRequest , ... grpc.CallOption ) (* minderv1.ListArtifactsResponse , error ) {
27- _ = s
28- return nil , status .Error (codes .Unimplemented , "unexpected call" )
29- }
30-
31- func (s * artifactClientStub ) GetArtifactById (_ context.Context , _ * minderv1.GetArtifactByIdRequest , _ ... grpc.CallOption ) (* minderv1.GetArtifactByIdResponse , error ) {
32- return s .getArtifactByIDResp , s .getArtifactByIDErr
33- }
34-
35- func (s * artifactClientStub ) GetArtifactByName (_ context.Context , _ * minderv1.GetArtifactByNameRequest , _ ... grpc.CallOption ) (* minderv1.GetArtifactByNameResponse , error ) {
36- return s .getArtifactByNameResp , s .getArtifactByNameErr
37- }
38-
39- type profileClientStub struct {
40- listProfilesResp * minderv1.ListProfilesResponse
41- listProfilesErr error
42- statusResp * minderv1.GetProfileStatusByNameResponse
43- statusErr error
44- }
45-
46- func (s * profileClientStub ) CreateProfile (context.Context , * minderv1.CreateProfileRequest , ... grpc.CallOption ) (* minderv1.CreateProfileResponse , error ) {
47- _ = s
48- return nil , status .Error (codes .Unimplemented , "unexpected call" )
49- }
50-
51- func (s * profileClientStub ) UpdateProfile (context.Context , * minderv1.UpdateProfileRequest , ... grpc.CallOption ) (* minderv1.UpdateProfileResponse , error ) {
52- _ = s
53- return nil , status .Error (codes .Unimplemented , "unexpected call" )
54- }
55-
56- func (s * profileClientStub ) PatchProfile (context.Context , * minderv1.PatchProfileRequest , ... grpc.CallOption ) (* minderv1.PatchProfileResponse , error ) {
57- _ = s
58- return nil , status .Error (codes .Unimplemented , "unexpected call" )
59- }
60-
61- func (s * profileClientStub ) DeleteProfile (context.Context , * minderv1.DeleteProfileRequest , ... grpc.CallOption ) (* minderv1.DeleteProfileResponse , error ) {
62- _ = s
63- return nil , status .Error (codes .Unimplemented , "unexpected call" )
64- }
65-
66- func (s * profileClientStub ) ListProfiles (_ context.Context , _ * minderv1.ListProfilesRequest , _ ... grpc.CallOption ) (* minderv1.ListProfilesResponse , error ) {
67- return s .listProfilesResp , s .listProfilesErr
68- }
69-
70- func (s * profileClientStub ) GetProfileById (context.Context , * minderv1.GetProfileByIdRequest , ... grpc.CallOption ) (* minderv1.GetProfileByIdResponse , error ) {
71- _ = s
72- return nil , status .Error (codes .Unimplemented , "unexpected call" )
73- }
74-
75- func (s * profileClientStub ) GetProfileByName (context.Context , * minderv1.GetProfileByNameRequest , ... grpc.CallOption ) (* minderv1.GetProfileByNameResponse , error ) {
76- _ = s
77- return nil , status .Error (codes .Unimplemented , "unexpected call" )
78- }
79-
80- func (s * profileClientStub ) GetProfileStatusByName (_ context.Context , _ * minderv1.GetProfileStatusByNameRequest , _ ... grpc.CallOption ) (* minderv1.GetProfileStatusByNameResponse , error ) {
81- return s .statusResp , s .statusErr
82- }
83-
84- func (s * profileClientStub ) GetProfileStatusById (context.Context , * minderv1.GetProfileStatusByIdRequest , ... grpc.CallOption ) (* minderv1.GetProfileStatusByIdResponse , error ) {
85- _ = s
86- return nil , status .Error (codes .Unimplemented , "unexpected call" )
87- }
88-
89- func (s * profileClientStub ) GetProfileStatusByProject (context.Context , * minderv1.GetProfileStatusByProjectRequest , ... grpc.CallOption ) (* minderv1.GetProfileStatusByProjectResponse , error ) {
90- _ = s
91- return nil , status .Error (codes .Unimplemented , "unexpected call" )
92- }
93-
94- // Tests rely on injected ArtifactServiceClient; profile evaluation is skipped in this mode.
95- //
9619//nolint:paralleltest // Cannot run in parallel because it swaps global Viper/Stdout state
9720func TestArtifactGetCommand (t * testing.T ) {
98- setupSuccess := func (t * testing.T , _ * gomock.Controller ) context.Context {
21+ setupSuccess := func (t * testing.T , ctrl * gomock.Controller ) context.Context {
9922 t .Helper ()
100- client := & artifactClientStub {}
10123
102- mockResp := & minderv1.GetArtifactByIdResponse {}
103- cli .LoadFixture (t , "mock_artifact_get.json" , mockResp )
24+ artifactClient := mockv1 .NewMockArtifactServiceClient (ctrl )
25+ profileClient := mockv1 .NewMockProfileServiceClient (ctrl )
26+
27+ artifactResp := & minderv1.GetArtifactByIdResponse {}
28+ cli .LoadFixture (t , "mock_artifact_get.json" , artifactResp )
10429
105- client .getArtifactByIDResp = mockResp
106- client .getArtifactByIDErr = nil
30+ artifactClient .EXPECT ().
31+ GetArtifactById (gomock .Any (), gomock .Any ()).
32+ Return (artifactResp , nil ).
33+ Times (1 )
10734
108- profileClient := & profileClientStub {
109- listProfilesResp : & minderv1.ListProfilesResponse {},
110- statusResp : & minderv1.GetProfileStatusByNameResponse {},
35+ listProfilesResp := & minderv1.ListProfilesResponse {
36+ Profiles : []* minderv1.Profile {
37+ {Name : "artifact-security-baseline" },
38+ },
39+ }
40+ profileClient .EXPECT ().
41+ ListProfiles (gomock .Any (), gomock .Any ()).
42+ Return (listProfilesResp , nil ).
43+ Times (1 )
44+
45+ statusResp := & minderv1.GetProfileStatusByNameResponse {
46+ RuleEvaluationStatus : []* minderv1.RuleEvaluationStatus {
47+ {
48+ ProfileId : "artifact-security-baseline" ,
49+ RuleDescriptionName : "Require artifact attestation" ,
50+ RuleTypeName : "artifact_attestation_slsa" ,
51+ Status : "failure" ,
52+ Details : "artifact attestation is disabled for this image" ,
53+ Guidance : "enable artifact attestations before release" ,
54+ RemediationUrl : "https://example.com/remediate/artifact-111" ,
55+ EntityInfo : map [string ]string {
56+ "name" : "owner-1/artifact-1" ,
57+ },
58+ Entity : "artifact" ,
59+ },
60+ },
11161 }
62+ profileClient .EXPECT ().
63+ GetProfileStatusByName (gomock .Any (), gomock .Any ()).
64+ Return (statusResp , nil ).
65+ Times (1 )
11266
113- ctx := cli .WithRPCClient [minderv1.ArtifactServiceClient ](context .Background (), client )
67+ ctx := cli .WithRPCClient [minderv1.ArtifactServiceClient ](context .Background (), artifactClient )
11468 ctx = cli .WithRPCClient [minderv1.ProfileServiceClient ](ctx , profileClient )
11569 return ctx
11670 }
@@ -137,12 +91,17 @@ func TestArtifactGetCommand(t *testing.T) {
13791 {
13892 Name : "server error handling" ,
13993 Args : []string {"artifact" , "get" , "-i" , "111" },
140- MockSetup : func (t * testing.T , _ * gomock.Controller ) context.Context {
94+ MockSetup : func (t * testing.T , ctrl * gomock.Controller ) context.Context {
14195 t .Helper ()
142- client := & artifactClientStub {getArtifactByIDErr : status .Error (codes .NotFound , "artifact not found" )}
143- profileClient := & profileClientStub {listProfilesResp : & minderv1.ListProfilesResponse {}, statusResp : & minderv1.GetProfileStatusByNameResponse {}}
96+ artifactClient := mockv1 .NewMockArtifactServiceClient (ctrl )
97+ profileClient := mockv1 .NewMockProfileServiceClient (ctrl )
98+
99+ artifactClient .EXPECT ().
100+ GetArtifactById (gomock .Any (), gomock .Any ()).
101+ Return (nil , status .Error (codes .NotFound , "artifact not found" )).
102+ Times (1 )
144103
145- ctx := cli .WithRPCClient [minderv1.ArtifactServiceClient ](context .Background (), client )
104+ ctx := cli .WithRPCClient [minderv1.ArtifactServiceClient ](context .Background (), artifactClient )
146105 ctx = cli .WithRPCClient [minderv1.ProfileServiceClient ](ctx , profileClient )
147106 return ctx
148107 },
0 commit comments