From 75c2d8a34f5f1b15d5f82e4ec6c7a4a0542974b4 Mon Sep 17 00:00:00 2001 From: Jonas-Taha El Sesiy Date: Fri, 3 Jan 2025 17:43:04 +0100 Subject: [PATCH] more tests --- pkg/cmd/decode.go | 12 +++++++++--- pkg/cmd/decode_test.go | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/decode.go b/pkg/cmd/decode.go index 3a99af8..98dcc21 100644 --- a/pkg/cmd/decode.go +++ b/pkg/cmd/decode.go @@ -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 + } + b64dhelm, err := base64.StdEncoding.DecodeString(string(b64dk8s)) + if err != nil { + return "", err + } gz, err := gzip.NewReader(strings.NewReader(string(b64dhelm))) if err != nil { diff --git a/pkg/cmd/decode_test.go b/pkg/cmd/decode_test.go index d81e365..6e19187 100644 --- a/pkg/cmd/decode_test.go +++ b/pkg/cmd/decode_test.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/gzip" "encoding/base64" + "errors" "testing" "github.com/stretchr/testify/assert" @@ -11,8 +12,9 @@ import ( func TestDecode(t *testing.T) { tests := map[string]struct { - data func() Secret - want string + data func() Secret + want string + wantErr error }{ // base64 encoded "opaque": { @@ -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": { @@ -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\""), }, } @@ -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)