Skip to content

Commit

Permalink
more tests to bring cov up to ~96%
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Aug 13, 2024
1 parent db4cd00 commit 44408ee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
28 changes: 28 additions & 0 deletions hack/kind-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,31 @@ metadata:
namespace: default
type: Opaque
EOF

## 'another' namespace
kubectl apply -f - <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: another
EOF

## secret 'gopher' in namespace 'another' (single key)
kubectl apply -f - <<EOF
apiVersion: v1
data:
foo: YmFy
kind: Secret
metadata:
name: gopher
namespace: another
type: Opaque
EOF

## 'empty' namespace
kubectl apply -f - <<EOF
apiVersion: v1
kind: Namespace
metadata:
name: empty
EOF
19 changes: 11 additions & 8 deletions pkg/cmd/view-secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,15 @@ func (c *CommandOpts) Retrieve(cmd *cobra.Command) error {
secretMap[v.Metadata.Name] = v
}

err := huh.NewSelect[string]().
Title(secretListTitle).
Description(fmt.Sprintf(secretListDescription, len(secretList.Items))).
Options(huh.NewOptions(opts...)...).
Value(&c.secretName).
Run()
err := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title(secretListTitle).
Description(fmt.Sprintf(secretListDescription, len(secretList.Items))).
Options(huh.NewOptions(opts...)...).
Value(&c.secretName),
),
).WithProgramOptions(tea.WithInput(cmd.InOrStdin()), tea.WithOutput(cmd.OutOrStdout())).Run()
if err != nil {
return err

Check warning on line 193 in pkg/cmd/view-secret.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/view-secret.go#L193

Added line #L193 was not covered by tests
}
Expand All @@ -198,10 +201,10 @@ func (c *CommandOpts) Retrieve(cmd *cobra.Command) error {
}

if c.quiet {
return ProcessSecret(os.Stdout, io.Discard, os.Stdin, secret, c.secretKey, c.decodeAll)
return ProcessSecret(cmd.OutOrStdout(), io.Discard, cmd.InOrStdin(), secret, c.secretKey, c.decodeAll)
}

return ProcessSecret(os.Stdout, os.Stderr, os.Stdin, secret, c.secretKey, c.decodeAll)
return ProcessSecret(cmd.OutOrStdout(), cmd.OutOrStderr(), cmd.InOrStdin(), secret, c.secretKey, c.decodeAll)
}

// ProcessSecret takes the secret and user input to determine the output
Expand Down
34 changes: 23 additions & 11 deletions pkg/cmd/view-secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ func TestParseArgs(t *testing.T) {

func TestNewCmdViewSecret(t *testing.T) {
tests := map[string]struct {
args []string
wantErr error
args []string
feedkeys string
want string
wantErr error
}{
"all": {args: []string{"test", "--all"}},
"custom ctx": {args: []string{"test", "--context", "gotest"}},
"custom kubecfg": {args: []string{"test", "--kubeconfig", "cfg"}},
"custom ns": {args: []string{"test", "--namespace", "bob"}},
"impersonate group": {args: []string{"test", "--as-group", "golovers"}},
"impersonate user": {args: []string{"test", "--as", "gopher"}},
"invalid arg count": {args: []string{"a", "b", "c"}, wantErr: errors.New("accepts between 0 and 2 arg(s), received 3")},
"quiet": {args: []string{"test", "--all", "--quiet"}},
"unknown flag": {args: []string{"--version"}, wantErr: errors.New("unknown flag: --version")},
"all": {args: []string{"test", "--all"}, want: `key1='value1'\nkey2='value2'`},
"custom ctx": {args: []string{"test", "--context", "gotest"}},
"custom kubecfg": {args: []string{"test", "--kubeconfig", "cfg"}},
"custom ns (does not exist)": {args: []string{"test", "--namespace", "bob"}, want: `Error from server (NotFound): namespaces "bob" not found`},
"custom ns (no secret)": {args: []string{"test", "--namespace", "another"}, want: `Error from server (NotFound): secrets "test" not found`},
"custom ns (valid secret)": {args: []string{"gopher", "--namespace", "another"}, want: `Viewing only available key: foo\nbar`},
"impersonate group": {args: []string{"test", "--as", "gopher"}},
"impersonate user & group": {args: []string{"test", "--as", "gopher", "--as-group", "golovers"}},
// make bootstrap sources 2 test secrets in the default namespace, select the first one and print all values
"interactive": {args: []string{"--all"}, feedkeys: "\r", want: `key1='value1'\nkey2='value2'`},
"interactive custom ns (no secret)": {args: []string{"--namespace", "empty"}, wantErr: ErrNoSecretFound},
"invalid arg count": {args: []string{"a", "b", "c"}, wantErr: errors.New("accepts between 0 and 2 arg(s), received 3")},
"quiet": {args: []string{"test2", "--quiet"}, want: `value1`},
"unknown flag": {args: []string{"--version"}, wantErr: errors.New("unknown flag: --version")},
}

for name, tt := range tests {
Expand All @@ -71,8 +78,13 @@ func TestNewCmdViewSecret(t *testing.T) {

cmd := NewCmdViewSecret()
outBuf := bytes.NewBufferString("")
readBuf := &strings.Reader{}
if tt.feedkeys != "" {
readBuf = strings.NewReader(tt.feedkeys)
}

cmd.SetOut(outBuf)
cmd.SetIn(readBuf)
cmd.SetArgs(tt.args)

err := cmd.Execute()
Expand Down

0 comments on commit 44408ee

Please sign in to comment.