added test files for profile and profile/status command - #6420
Conversation
70bb101 to
6eeeedc
Compare
| // GetDataSourceClient returns the DataSourceServiceClient, a cleanup function to close the connection and an error | ||
| func GetDataSourceClient(cmd *cobra.Command) (minderv1.DataSourceServiceClient, func(), error) { | ||
| ctx, cancel := cli.GetAppContext(cmd.Context(), viper.GetViper()) | ||
| cmd.SetContext(ctx) | ||
|
|
||
| if mockClient, ok := cli.GetRPCClient[minderv1.DataSourceServiceClient](ctx); ok { | ||
| return mockClient, func() { cancel() }, nil | ||
| } | ||
|
|
||
| conn, err := cli.GrpcForCommand(cmd, viper.GetViper()) | ||
| if err != nil { | ||
| cancel() | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| return minderv1.NewDataSourceServiceClient(conn), func() { | ||
| cancel() | ||
| _ = conn.Close() | ||
| }, nil | ||
| } |
There was a problem hiding this comment.
I'm noticing that these seem very repetitive; can we use a generic version?
| // GetDataSourceClient returns the DataSourceServiceClient, a cleanup function to close the connection and an error | |
| func GetDataSourceClient(cmd *cobra.Command) (minderv1.DataSourceServiceClient, func(), error) { | |
| ctx, cancel := cli.GetAppContext(cmd.Context(), viper.GetViper()) | |
| cmd.SetContext(ctx) | |
| if mockClient, ok := cli.GetRPCClient[minderv1.DataSourceServiceClient](ctx); ok { | |
| return mockClient, func() { cancel() }, nil | |
| } | |
| conn, err := cli.GrpcForCommand(cmd, viper.GetViper()) | |
| if err != nil { | |
| cancel() | |
| return nil, nil, err | |
| } | |
| return minderv1.NewDataSourceServiceClient(conn), func() { | |
| cancel() | |
| _ = conn.Close() | |
| }, nil | |
| } | |
| type Cleanup = func() | |
| // GetCLIClient takes a factory for a GRPC client service and returns | |
| // a client, a cleanup function to close the connection and an error | |
| func GetCLIClient[T interface](cmd *cobra.Command, client func(grpc.ClientConnInterface) T) (T, Cleanup, error) { | |
| ctx, cancel := cli.GetAppContext(cmd.Context(), viper.GetViper()) | |
| cmd.SetContext(ctx) | |
| if mockClient, ok := cli.GetRPCClient[T](ctx); ok { | |
| return mockClient, func() { cancel() }, nil | |
| } | |
| conn, err := cli.GrpcForCommand(cmd, viper.GetViper()) | |
| if err != nil { | |
| cancel() | |
| return nil, nil, err | |
| } | |
| return client(conn), func() { | |
| cancel() | |
| _ = conn.Close() | |
| }, nil | |
| } |
|
|
||
| type protoWithProfileStatus interface { | ||
| proto.Message | ||
| protoreflect.ProtoMessage |
There was a problem hiding this comment.
we should prefer proto.Message, but a bunch of early usage (before my arrival) used the protoreflect package, and it got copied lots of places.
| protoreflect.ProtoMessage | |
| proto.Message |
There was a problem hiding this comment.
This looks terrible (yaml wrapped, etc), but it is what it is. Maybe file an issue that "Rule definition" is not very helpful in the current apply output?
There was a problem hiding this comment.
(Thanks for doing this, btw! It makes it much easier to see where the formatting sucks)
There was a problem hiding this comment.
Yea i'll file an issue
| Successfully created new profile named: mock-attestation-profile | ||
| Successfully updated existing profile named: mock-dependabot-profile | ||
| ENTITY │ RULE │ RULE PARAMS │ RULE DEFINITION |
There was a problem hiding this comment.
Interesting... if one of these profiles has two rules, what does the output look like? It feels like we might need a delimiter (extra column wouldn't fit) for the profile name.
There was a problem hiding this comment.
i made table to render after every success profile creation , does it look better now?
There was a problem hiding this comment.
Modulo https://github.com/mindersec/minder/pull/6420/changes#r3150092088, yes.
Thanks for doing this -- I'm inclined to defer output cleanup on the CLI until after we have the existing output captured by these tests (but maybe we file issues for the cleanup).
| name: mock-profile | ||
| repository: | ||
| - type: mock-rule-type | ||
| type: profile | ||
| version: v1 | ||
| --- | ||
| def: | ||
| eval: | ||
| data_sources: | ||
| - name: mock-data-source | ||
| name: mock-rule-type | ||
| type: rule-type | ||
| version: v1 | ||
| --- | ||
| name: mock-data-source | ||
| type: data-source | ||
| version: v1 |
There was a problem hiding this comment.
This output is weird -- can we fill in enough fields that it would make sense? (It's also annoying that the field sort "feels" random when it's alphabetical rather than by some other canonical ordering ala Kubernetes.)
There was a problem hiding this comment.
yes the alphabetical sort makes these hard to parse visually, i think i can open a follow-up pr for canonical ordering of fields
| } | ||
| defer closeRules() | ||
|
|
||
| // TODO: it would be nice if this were just a list of rules... |
There was a problem hiding this comment.
I think this TODO may be done?
There was a problem hiding this comment.
TODO actually got misplaced during a refactor. i think its still unresolved because the protobuf schema still partitions rules by entity type?
6eeeedc to
0884068
Compare
Signed-off-by: DharunMR <maddharun56@gmail.com>
0884068 to
ffeb4b9
Compare
evankanderson
left a comment
There was a problem hiding this comment.
I'm going to merge this to un-block #6417 and others.
|
|
||
| ENTITY │ RULE NAME │ STATUS │ DETAILS | ||
| ─────────────────────────┼──────────────────────┼─────────┼───────────────────────────────────────── | ||
| acme-corp/mock-repo │ │ Ok │ Mock rule evaluation succeeded. |
There was a problem hiding this comment.
Empty rule name here is weird, but it seems like we're passing the tests, so this should be a follow up fix in coordination with #6417.
| Successfully created new profile named: mock-attestation-profile | ||
| Successfully updated existing profile named: mock-dependabot-profile | ||
| ENTITY │ RULE │ RULE PARAMS │ RULE DEFINITION |
There was a problem hiding this comment.
Modulo https://github.com/mindersec/minder/pull/6420/changes#r3150092088, yes.
Thanks for doing this -- I'm inclined to defer output cleanup on the CLI until after we have the existing output captured by these tests (but maybe we file issues for the cleanup).
| name: mock-profile | ||
| repository: | ||
| - type: mock-rule-type | ||
| type: profile | ||
| version: v1 | ||
| --- | ||
| def: | ||
| eval: | ||
| data_sources: | ||
| - name: mock-data-source | ||
| name: mock-rule-type | ||
| type: rule-type | ||
| version: v1 | ||
| --- | ||
| name: mock-data-source | ||
| type: data-source | ||
| version: v1 |
| applyCmd.Flags().StringArrayP("file", "f", []string{}, | ||
| "Path to the YAML defining the profile (or - for stdin). Can be specified multiple files") |
There was a problem hiding this comment.
It's okay this time, but let's try to separate behavior changes from e.g. test and refactoring changes that don't change behavior.
| var datasources []string | ||
| for _, rtName := range ruletypes { |
There was a problem hiding this comment.
This refactor makes it harder to review the PR because I now have to start a second context (what's going on with these functions?) in addition to the rule output changes. While it's super-tempting to fix things as you see them, it's easier to review 2 focused PRs than one PR that mixes behavior. (Renaming all the ruletype variables also introduces a bunch of extra diffs.)
There was a problem hiding this comment.
sure i will follw it
| lw := utf8.RuneCountInString(line) | ||
| lw := text.RuneWidthWithoutEscSequences(line) |
There was a problem hiding this comment.
yea, this printing color and emoji as code instead as it is
Description
cmd/cli/app/profileandcmd/cli/app/profile/statuspackage to modernize its testing architecture, improve separation of concerns. Stripped out manualctxandgrpc.ClientConnarguments from command execution functions to align with standard CobraRunEsignatures. Implemented the "backpack" pattern usingcmd.SetContext(ctx)andcli.WithRPCClientto inject mock clients cleanly during testing.applycommand to accept and process multiple YAML file paths🧪 How to Test
go test ./...(Should PASS)-updateflag:go test ./cmd/cli/app/profile/ -updateandgo test ./cmd/cli/app/profile/status -update