-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve cluster|project list-members subcommands (#399)
- Loading branch information
1 parent
0267f06
commit 3d831ec
Showing
12 changed files
with
648 additions
and
171 deletions.
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.