Skip to content

added test files for profile and profile/status command - #6420

Merged
evankanderson merged 1 commit into
mindersec:mainfrom
DharunMR:test-profile-cmd
Apr 30, 2026
Merged

added test files for profile and profile/status command#6420
evankanderson merged 1 commit into
mindersec:mainfrom
DharunMR:test-profile-cmd

Conversation

@DharunMR

Copy link
Copy Markdown
Contributor

Description

  • This PR introduces a comprehensive refactor of the cmd/cli/app/profile and cmd/cli/app/profile/status package to modernize its testing architecture, improve separation of concerns. Stripped out manual ctx and grpc.ClientConn arguments from command execution functions to align with standard Cobra RunE signatures. Implemented the "backpack" pattern using cmd.SetContext(ctx) and cli.WithRPCClient to inject mock clients cleanly during testing.
  • Modified the apply command to accept and process multiple YAML file paths

🧪 How to Test

  1. Run standard tests: go test ./... (Should PASS)
  2. Run test if any changes made in cmd code then add -update flag: go test ./cmd/cli/app/profile/ -update and go test ./cmd/cli/app/profile/status -update

@DharunMR
DharunMR requested a review from a team as a code owner April 27, 2026 01:21
@DharunMR
DharunMR force-pushed the test-profile-cmd branch 5 times, most recently from 70bb101 to 6eeeedc Compare April 27, 2026 03:21
@coveralls

coveralls commented Apr 27, 2026

Copy link
Copy Markdown

Coverage Status

Coverage is 60.574%DharunMR:test-profile-cmd into mindersec:main. No base build found for mindersec:main.

Comment thread cmd/cli/app/datasource/datasource.go Outdated
Comment on lines +31 to +50
// 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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm noticing that these seem very repetitive; can we use a generic version?

Suggested change
// 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
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍


type protoWithProfileStatus interface {
proto.Message
protoreflect.ProtoMessage

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
protoreflect.ProtoMessage
proto.Message

Comment thread cmd/cli/app/profile/status/status_list.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Thanks for doing this, btw! It makes it much easier to see where the formatting sucks)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea i'll file an issue

Comment on lines +1 to +3
Successfully created new profile named: mock-attestation-profile
Successfully updated existing profile named: mock-dependabot-profile
ENTITY │ RULE │ RULE PARAMS │ RULE DEFINITION

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i made table to render after every success profile creation , does it look better now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment on lines +1 to +17
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes the alphabetical sort makes these hard to parse visually, i think i can open a follow-up pr for canonical ordering of fields

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmd/cli/app/profile/export.go Outdated
}
defer closeRules()

// TODO: it would be nice if this were just a list of rules...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this TODO may be done?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO actually got misplaced during a refactor. i think its still unresolved because the protobuf schema still partitions rules by entity type?

Comment thread cmd/cli/app/profile/get.go Outdated
Signed-off-by: DharunMR <maddharun56@gmail.com>

@evankanderson evankanderson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1 to +3
Successfully created new profile named: mock-attestation-profile
Successfully updated existing profile named: mock-dependabot-profile
ENTITY │ RULE │ RULE PARAMS │ RULE DEFINITION

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment on lines +1 to +17
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +142 to +143
applyCmd.Flags().StringArrayP("file", "f", []string{},
"Path to the YAML defining the profile (or - for stdin). Can be specified multiple files")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +124 to +125
var datasources []string
for _, rtName := range ruletypes {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure i will follw it

Comment on lines -104 to +105
lw := utf8.RuneCountInString(line)
lw := text.RuneWidthWithoutEscSequences(line)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, was this a bug?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, this printing color and emoji as code instead as it is

@evankanderson
evankanderson merged commit a1a8103 into mindersec:main Apr 30, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants