-
Notifications
You must be signed in to change notification settings - Fork 96
fix(cli): clarify rule type deletion errors for bundles vs profiles #6310
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
Open
Aryanburnwal05
wants to merge
1
commit into
mindersec:main
Choose a base branch
from
Aryanburnwal05:fix/issue-5306-clear-delete-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2023 The Minder Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ruletype | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" | ||
| ) | ||
|
|
||
| func TestIsBundleError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| err error | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "nil error", | ||
| err: nil, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "standard error containing lower case bundle", | ||
| err: errors.New("cannot delete rule type from bundle"), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "grpc structured error containing upper case BUNDLE", | ||
| err: status.Errorf(codes.InvalidArgument, "cannot delete rule type from BUNDLE"), | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "unrelated error", | ||
| err: errors.New("some other error"), | ||
| expected: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| result := isBundleError(tt.err) | ||
| require.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExtractProfiles(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| errStr string | ||
| expected []string | ||
| }{ | ||
| { | ||
| name: "single profile", | ||
| errStr: "cannot delete: rule type is used by profiles foo", | ||
| expected: []string{"foo"}, | ||
| }, | ||
| { | ||
| name: "multiple profiles", | ||
| errStr: "cannot delete: rule type is used by profiles foo, bar, baz", | ||
| expected: []string{"foo", "bar", "baz"}, | ||
| }, | ||
| { | ||
| name: "no profiles mentioned", | ||
| errStr: "some other error", | ||
| expected: []string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| result := extractProfiles(tt.errStr) | ||
| require.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // mockRuleTypeServiceClient is a simple mock for testing deleteRuleTypes without gomock | ||
| type mockRuleTypeServiceClient struct { | ||
| minderv1.RuleTypeServiceClient | ||
| deleteFunc func(ctx context.Context, in *minderv1.DeleteRuleTypeRequest, opts ...grpc.CallOption) (*minderv1.DeleteRuleTypeResponse, error) | ||
| } | ||
|
|
||
| func (m *mockRuleTypeServiceClient) DeleteRuleType(ctx context.Context, in *minderv1.DeleteRuleTypeRequest, opts ...grpc.CallOption) (*minderv1.DeleteRuleTypeResponse, error) { | ||
| if m.deleteFunc != nil { | ||
| return m.deleteFunc(ctx, in, opts...) | ||
| } | ||
| return &minderv1.DeleteRuleTypeResponse{}, nil | ||
| } | ||
|
|
||
| func TestDeleteRuleTypes_Categorization(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mockClient := &mockRuleTypeServiceClient{ | ||
| deleteFunc: func(ctx context.Context, in *minderv1.DeleteRuleTypeRequest, opts ...grpc.CallOption) (*minderv1.DeleteRuleTypeResponse, error) { | ||
| switch in.GetId() { | ||
| case "rule1": | ||
| return &minderv1.DeleteRuleTypeResponse{}, nil | ||
| case "rule2": | ||
| return nil, status.Errorf(codes.InvalidArgument, "cannot delete rule type from bundle") | ||
| case "rule3": | ||
| return nil, status.Errorf(codes.FailedPrecondition, "cannot delete: rule type rule3 is used by profiles prof1, prof2") | ||
| default: | ||
| return nil, errors.New("unknown error") | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| rules := []*minderv1.RuleType{ | ||
| {Id: "rule1", Name: "successful_rule"}, | ||
|
Check failure on line 126 in cmd/cli/app/ruletype/ruletype_delete_test.go
|
||
| {Id: "rule2", Name: "bundle_rule"}, | ||
|
Check failure on line 127 in cmd/cli/app/ruletype/ruletype_delete_test.go
|
||
| {Id: "rule3", Name: "profile_rule"}, | ||
|
Check failure on line 128 in cmd/cli/app/ruletype/ruletype_delete_test.go
|
||
| } | ||
|
|
||
| deleted, bundleBlocked, profileBlocked := deleteRuleTypes(context.Background(), mockClient, rules, "project_id") | ||
|
|
||
| require.Len(t, deleted, 1) | ||
| require.Equal(t, "successful_rule", deleted[0]) | ||
|
|
||
| require.Len(t, bundleBlocked, 1) | ||
| require.Equal(t, "bundle_rule", bundleBlocked[0]) | ||
|
|
||
| require.Len(t, profileBlocked, 1) | ||
| require.Equal(t, "profile_rule", profileBlocked[0].Name) | ||
| require.Equal(t, []string{"prof1", "prof2"}, profileBlocked[0].Profiles) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think
Idneeds to be a pointer to string