Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Jan 3, 2025
1 parent 1c507de commit 75c2d8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pkg/cmd/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ func (s Secret) Decode(input string) (string, error) {
case Opaque:
b64d, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return "", nil
return "", err
}
return string(b64d), nil
case Helm:
b64dk8s, _ := base64.StdEncoding.DecodeString(input)
b64dhelm, _ := base64.StdEncoding.DecodeString(string(b64dk8s))
b64dk8s, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return "", err
}

Check warning on line 28 in pkg/cmd/decode.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/decode.go#L27-L28

Added lines #L27 - L28 were not covered by tests
b64dhelm, err := base64.StdEncoding.DecodeString(string(b64dk8s))
if err != nil {
return "", err
}

Check warning on line 32 in pkg/cmd/decode.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/decode.go#L31-L32

Added lines #L31 - L32 were not covered by tests

gz, err := gzip.NewReader(strings.NewReader(string(b64dhelm)))
if err != nil {
Expand Down
41 changes: 37 additions & 4 deletions pkg/cmd/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecode(t *testing.T) {
tests := map[string]struct {
data func() Secret
want string
data func() Secret
want string
wantErr error
}{
// base64 encoded
"opaque": {
Expand All @@ -25,6 +27,20 @@ func TestDecode(t *testing.T) {
}
},
"test\n",
nil,
},
// base64 encoded
"opaque invalid": {
func() Secret {
return Secret{
Data: map[string]string{
"key": "dGVzdAo}}}=",
},
Type: Opaque,
}
},
"",
base64.CorruptInputError(7),
},
// double base64 encoded + gzip'd
"helm": {
Expand All @@ -51,6 +67,16 @@ func TestDecode(t *testing.T) {
return res
},
"test\n",
nil,
},
"unknown secret": {
func() Secret {
return Secret{
Type: "invalid",
}
},
"",
errors.New("couldn't decode unknown secret type \"invalid\""),
},
}

Expand All @@ -59,11 +85,18 @@ func TestDecode(t *testing.T) {
t.Parallel()

data := tt.data()
want := tt.want

got, err := data.Decode(data.Data["key"])
if err != nil {
t.Errorf("got %v, want %v", got, want)
if tt.wantErr == nil {
assert.Fail(t, "unexpected error", err)
} else if err.Error() != tt.wantErr.Error() {
assert.Equal(t, tt.wantErr, err)
}
return
} else if tt.wantErr != nil {
assert.Fail(t, "expected error, got nil", tt.wantErr)
return
}

assert.Equal(t, tt.want, got)
Expand Down

0 comments on commit 75c2d8a

Please sign in to comment.