-
Notifications
You must be signed in to change notification settings - Fork 165
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
Improve cluster|project list-members subcommands #399
Open
pmatseykanets
wants to merge
6
commits into
rancher:main
Choose a base branch
from
pmatseykanets:list-members-output
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c43261
Improve cluster|project list-members subcommands
pmatseykanets 693bfec
Test subcommands output
pmatseykanets e27a17e
Mark tests as Parallel
pmatseykanets bd59214
Add format and quiet flags
pmatseykanets 4601629
Remove no longer neeeded arg
pmatseykanets 35e55d6
Add missing subtest names
pmatseykanets 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 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 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,99 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/rancher/norman/types" | ||
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func TestListClusterMembers(t *testing.T) { | ||
t.Parallel() | ||
|
||
now := time.Now() | ||
|
||
userConfig := &fakeUserConfig{ | ||
FocusedClusterFunc: func() string { | ||
return "c-fn7lc" | ||
}, | ||
} | ||
|
||
created := now.Format(time.RFC3339) | ||
crtbs := &fakeCRTBLister{ | ||
ListFunc: func(opts *types.ListOpts) (*managementClient.ClusterRoleTemplateBindingCollection, error) { | ||
return &managementClient.ClusterRoleTemplateBindingCollection{ | ||
Data: []managementClient.ClusterRoleTemplateBinding{ | ||
{ | ||
Resource: types.Resource{ | ||
ID: "c-fn7lc:creator-cluster-owner", | ||
}, | ||
Created: created, | ||
RoleTemplateID: "cluster-owner", | ||
UserPrincipalID: "local://user-2p7w6", | ||
}, | ||
{ | ||
Resource: types.Resource{ | ||
ID: "c-fn7lc:crtb-qd49d", | ||
}, | ||
Created: created, | ||
RoleTemplateID: "cluster-member", | ||
GroupPrincipalID: "okta_group://b4qkhsnliz", | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
} | ||
|
||
principals := &fakePrincipalGetter{ | ||
ByIDFunc: func(id string) (*managementClient.Principal, error) { | ||
id, err := url.PathUnescape(id) | ||
require.NoError(t, err) | ||
|
||
switch id { | ||
case "local://user-2p7w6": | ||
return &managementClient.Principal{ | ||
Name: "Default Admin", | ||
LoginName: "admin", | ||
Provider: "local", | ||
PrincipalType: "user", | ||
}, nil | ||
case "okta_group://b4qkhsnliz": | ||
return &managementClient.Principal{ | ||
Name: "DevOps", | ||
LoginName: "devops", | ||
Provider: "okta", | ||
PrincipalType: "group", | ||
}, nil | ||
default: | ||
return nil, fmt.Errorf("not found") | ||
} | ||
}, | ||
} | ||
|
||
flagSet := flag.NewFlagSet("test", flag.ContinueOnError) | ||
cctx := cli.NewContext(nil, flagSet, nil) | ||
|
||
var out bytes.Buffer | ||
|
||
err := listClusterMembers(cctx, &out, userConfig, crtbs, principals) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, out) | ||
|
||
humanCreated := now.Format(humanTimeFormat) | ||
want := [][]string{ | ||
{"BINDING-ID", "MEMBER", "ROLE", "CREATED"}, | ||
{"c-fn7lc:creator-cluster-owner", "Default Admin (Local User)", "cluster-owner", humanCreated}, | ||
{"c-fn7lc:crtb-qd49d", "DevOps (Okta Group)", "cluster-member", humanCreated}, | ||
} | ||
|
||
got := parseTabWriterOutput(&out) | ||
assert.Equal(t, want, got) | ||
} |
This file contains 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,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"strings" | ||
|
||
"github.com/rancher/norman/types" | ||
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" | ||
) | ||
|
||
type fakePrincipalGetter struct { | ||
ByIDFunc func(id string) (*managementClient.Principal, error) | ||
} | ||
|
||
func (g *fakePrincipalGetter) ByID(id string) (*managementClient.Principal, error) { | ||
if g.ByIDFunc != nil { | ||
return g.ByIDFunc(id) | ||
} | ||
return nil, nil | ||
} | ||
|
||
type fakeUserConfig struct { | ||
FocusedClusterFunc func() string | ||
FocusedProjectFunc func() string | ||
} | ||
|
||
func (c *fakeUserConfig) FocusedCluster() string { | ||
if c.FocusedClusterFunc != nil { | ||
return c.FocusedClusterFunc() | ||
} | ||
return "" | ||
} | ||
|
||
func (c *fakeUserConfig) FocusedProject() string { | ||
if c.FocusedProjectFunc != nil { | ||
return c.FocusedProjectFunc() | ||
} | ||
return "" | ||
} | ||
|
||
type fakeCRTBLister struct { | ||
ListFunc func(opts *types.ListOpts) (*managementClient.ClusterRoleTemplateBindingCollection, error) | ||
} | ||
|
||
func (f *fakeCRTBLister) List(opts *types.ListOpts) (*managementClient.ClusterRoleTemplateBindingCollection, error) { | ||
if f.ListFunc != nil { | ||
return f.ListFunc(opts) | ||
} | ||
return nil, nil | ||
} | ||
|
||
type fakePRTBLister struct { | ||
ListFunc func(opts *types.ListOpts) (*managementClient.ProjectRoleTemplateBindingCollection, error) | ||
} | ||
|
||
func (f *fakePRTBLister) List(opts *types.ListOpts) (*managementClient.ProjectRoleTemplateBindingCollection, error) { | ||
if f.ListFunc != nil { | ||
return f.ListFunc(opts) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func parseTabWriterOutput(r io.Reader) [][]string { | ||
var parsed [][]string | ||
scanner := bufio.NewScanner(r) | ||
for scanner.Scan() { | ||
var fields []string | ||
for _, field := range strings.Split(scanner.Text(), " ") { | ||
if field == "" { | ||
continue | ||
} | ||
fields = append(fields, strings.TrimSpace(field)) | ||
} | ||
parsed = append(parsed, fields) | ||
} | ||
return parsed | ||
} |
Oops, something went wrong.
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.
Great change for testability