From acf2dfb19b9a9a5af7d555f43a2c84165434f31d Mon Sep 17 00:00:00 2001 From: lukaszkowalczyk98 Date: Wed, 22 Nov 2023 12:44:10 +0100 Subject: [PATCH] Fix get object mock and add proper delete function testing --- client/delete_test.go | 26 ++++++++++++++++++ client/template_test.go | 27 ++++++++++++++++--- test/backend/test.go | 10 ++++--- test/resources/source.not_existing_secret.tpl | 2 ++ test/resources/source.secret.tpl | 1 - 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 test/resources/source.not_existing_secret.tpl diff --git a/client/delete_test.go b/client/delete_test.go index 56390c1..59ff307 100644 --- a/client/delete_test.go +++ b/client/delete_test.go @@ -23,4 +23,30 @@ func TestDelete(t *testing.T) { if err != nil { t.Error(err) } + + tests := []struct { + name string + key string + want string + wantErr bool + }{ + { + name: "test non existing key", + key: test.Key9, + want: "", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := treasury.ReadValue(tt.key) + if (err != nil) != tt.wantErr { + t.Errorf("Client.ReadValue() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Client.ReadValue() = %v, want %v", got, tt.want) + } + }) + } } diff --git a/client/template_test.go b/client/template_test.go index da67b8b..ea2b4f0 100644 --- a/client/template_test.go +++ b/client/template_test.go @@ -9,7 +9,8 @@ import ( ) const ( - templateTestSourceFile = "../test/resources/source.secret.tpl" + templateTestSourceFile = "../test/resources/source.existing_secret.tpl" + templateTestSourceFile2 = "../test/resources/source.not_existing_secret.tpl" templateTestDestinationFile = "../test/output/destination.secret" templateTestDestinationParentDir = "../test/output" ) @@ -29,8 +30,28 @@ func TestTemplate(t *testing.T) { "Name": "some_testing_template", } - if err := treasury.Template(templateTestSourceFile, templateTestDestinationFile, 0, map[string]string{}, envMap); err != nil { - t.Error("Could not generate secret file from template. Error: ", err.Error()) + tests := []struct { + file string + wantErr bool + }{ + { + file: templateTestSourceFile, + wantErr: false, + }, + { + file: templateTestSourceFile2, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.file, func(t *testing.T) { + err := treasury.Template(tt.file, templateTestDestinationFile, 0, map[string]string{}, envMap) + if (err != nil) != tt.wantErr { + t.Errorf("Failed to use treasury template, error = %v, wantErr %v", err, tt.wantErr) + return + } + }) } _, err = os.Stat(templateTestDestinationParentDir) diff --git a/test/backend/test.go b/test/backend/test.go index 7a97440..75320b3 100644 --- a/test/backend/test.go +++ b/test/backend/test.go @@ -64,6 +64,12 @@ func (m *MockBackendClient) PutObject(input *types.PutObjectInput) error { } func (m *MockBackendClient) GetObject(input *types.GetObjectInput) (*types.GetObjectOutput, error) { + if _, ok := KeyValueMap[input.Key]; !ok { + return &types.GetObjectOutput{ + Value: "", + }, fmt.Errorf("Missing key:%s in KeyValue map", input.Key) + } + return &types.GetObjectOutput{ Value: KeyValueMap[input.Key], }, nil @@ -86,9 +92,5 @@ func (m *MockBackendClient) DeleteObject(input *types.DeleteObjectInput) error { delete(KeyValueMap, input.Key) - if _, ok := KeyValueMap[input.Key]; ok { - return errors.New(fmt.Sprintf("Failed to delete key:%s from KeyValue map", input.Key)) - } - return nil } diff --git a/test/resources/source.not_existing_secret.tpl b/test/resources/source.not_existing_secret.tpl new file mode 100644 index 0000000..e3a8ca5 --- /dev/null +++ b/test/resources/source.not_existing_secret.tpl @@ -0,0 +1,2 @@ +APPLICATION_NON_EXISTING_KEY={{ read "test/none/user_api_pass" }} +NAME={{ .Name }} diff --git a/test/resources/source.secret.tpl b/test/resources/source.secret.tpl index d832fe0..a5e3567 100644 --- a/test/resources/source.secret.tpl +++ b/test/resources/source.secret.tpl @@ -1,3 +1,2 @@ APPLICATION_SECRET_KEY={{ read "test/webapp/user_api_pass" }} -APPLICATION_NON_EXISTING_KEY={{ read "test/none/user_api_pass" }} NAME={{ .Name }}