From dd3dd4fdf1067263ebe73b792af071a8131abd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Mon, 13 Nov 2023 15:41:10 +0300 Subject: [PATCH] Add unit tests for errors package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın --- pkg/controller/external_nofork_test.go | 23 ++ pkg/terraform/errors/errors_test.go | 288 +++++++++++++++++++++++++ 2 files changed, 311 insertions(+) diff --git a/pkg/controller/external_nofork_test.go b/pkg/controller/external_nofork_test.go index b7fa928b..3109f669 100644 --- a/pkg/controller/external_nofork_test.go +++ b/pkg/controller/external_nofork_test.go @@ -140,6 +140,29 @@ func TestNoForkConnect(t *testing.T) { ots: ots, }, }, + "HCL": { + args: args{ + setupFn: func(_ context.Context, _ client.Client, _ xpresource.Managed) (terraform.Setup, error) { + return terraform.Setup{}, nil + }, + cfg: cfg, + obj: &fake.Terraformed{ + Parameterizable: fake.Parameterizable{ + Parameters: map[string]any{ + "name": " ${jsonencode({\n type = \"object\"\n })}", + "map": map[string]any{ + "key": "value", + }, + "list": []any{"elem1", "elem2"}, + }, + }, + Observable: fake.Observable{ + Observation: map[string]any{}, + }, + }, + ots: ots, + }, + }, } for name, tc := range cases { diff --git a/pkg/terraform/errors/errors_test.go b/pkg/terraform/errors/errors_test.go index e1602254..a554c24b 100644 --- a/pkg/terraform/errors/errors_test.go +++ b/pkg/terraform/errors/errors_test.go @@ -321,3 +321,291 @@ func TestNewPlanFailed(t *testing.T) { }) } } + +func TestNewRetryScheduleError(t *testing.T) { + type args struct { + invocationCount, ttl int + } + tests := map[string]struct { + args + wantErrMessage string + }{ + "Successful": { + args: args{ + invocationCount: 101, + ttl: 100, + }, + wantErrMessage: "native provider reuse budget has been exceeded: invocationCount: 101, ttl: 100", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + err := NewRetryScheduleError(tc.args.invocationCount, tc.args.ttl) + got := err.Error() + if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" { + t.Errorf("\nNewRetryScheduleError(...): -want message, +got message:\n%s", diff) + } + }) + } +} + +func TestIsRetryScheduleError(t *testing.T) { + var nilErr *retrySchedule + type args struct { + err error + } + tests := map[string]struct { + args + want bool + }{ + "NilError": { + args: args{}, + want: false, + }, + "NilRetryScheduleError": { + args: args{ + err: nilErr, + }, + want: true, + }, + "NonRetryScheduleError": { + args: args{ + err: errorBoom, + }, + want: false, + }, + "Successful": { + args: args{err: NewRetryScheduleError(101, 100)}, + want: true, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + if got := IsRetryScheduleError(tc.args.err); got != tc.want { + t.Errorf("IsRetryScheduleError() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestNewAsyncCreateFailed(t *testing.T) { + type args struct { + err error + } + tests := map[string]struct { + args + wantErrMessage string + }{ + "Successful": { + args: args{ + err: errors.New("already exists"), + }, + wantErrMessage: "async create failed: already exists", + }, + "Nil": { + args: args{ + err: nil, + }, + wantErrMessage: "", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + err := NewAsyncCreateFailed(tc.args.err) + got := "" + if err != nil { + got = err.Error() + } + if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" { + t.Errorf("\nNewAsyncCreateFailed(...): -want message, +got message:\n%s", diff) + } + }) + } +} + +func TestIsAsyncCreateFailed(t *testing.T) { + var nilErr *asyncCreateFailed + type args struct { + err error + } + tests := map[string]struct { + args + want bool + }{ + "NilError": { + args: args{}, + want: false, + }, + "NilAsyncCreateError": { + args: args{ + err: nilErr, + }, + want: true, + }, + "NonAsyncCreateError": { + args: args{ + err: errorBoom, + }, + want: false, + }, + "Successful": { + args: args{err: NewAsyncCreateFailed(errors.New("test"))}, + want: true, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + if got := IsAsyncCreateFailed(tc.args.err); got != tc.want { + t.Errorf("IsAsyncCreateFailed() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestAsyncUpdateFailed(t *testing.T) { + type args struct { + err error + } + tests := map[string]struct { + args + wantErrMessage string + }{ + "Successful": { + args: args{ + err: errors.New("immutable field"), + }, + wantErrMessage: "async update failed: immutable field", + }, + "Nil": { + args: args{ + err: nil, + }, + wantErrMessage: "", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + err := NewAsyncUpdateFailed(tc.args.err) + got := "" + if err != nil { + got = err.Error() + } + if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" { + t.Errorf("\nAsyncUpdateFailed(...): -want message, +got message:\n%s", diff) + } + }) + } +} + +func TestIsAsyncUpdateFailed(t *testing.T) { + var nilErr *asyncUpdateFailed + type args struct { + err error + } + tests := map[string]struct { + args + want bool + }{ + "NilError": { + args: args{}, + want: false, + }, + "NilAsyncUpdateError": { + args: args{ + err: nilErr, + }, + want: true, + }, + "NonAsyncUpdateError": { + args: args{ + err: errorBoom, + }, + want: false, + }, + "Successful": { + args: args{err: NewAsyncUpdateFailed(errors.New("test"))}, + want: true, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + if got := IsAsyncUpdateFailed(tc.args.err); got != tc.want { + t.Errorf("IsAsyncUpdateFailed() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestAsyncDeleteFailed(t *testing.T) { + type args struct { + err error + } + tests := map[string]struct { + args + wantErrMessage string + }{ + "Successful": { + args: args{ + err: errors.New("dependency violation"), + }, + wantErrMessage: "async delete failed: dependency violation", + }, + "Nil": { + args: args{ + err: nil, + }, + wantErrMessage: "", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + err := NewAsyncDeleteFailed(tc.args.err) + got := "" + if err != nil { + got = err.Error() + } + if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" { + t.Errorf("\nAsyncDeleteFailed(...): -want message, +got message:\n%s", diff) + } + }) + } +} + +func TestIsAsyncDeleteFailed(t *testing.T) { + var nilErr *asyncDeleteFailed + type args struct { + err error + } + tests := map[string]struct { + args + want bool + }{ + "NilError": { + args: args{}, + want: false, + }, + "NilAsyncUpdateError": { + args: args{ + err: nilErr, + }, + want: true, + }, + "NonAsyncUpdateError": { + args: args{ + err: errorBoom, + }, + want: false, + }, + "Successful": { + args: args{err: NewAsyncDeleteFailed(errors.New("test"))}, + want: true, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + if got := IsAsyncDeleteFailed(tc.args.err); got != tc.want { + t.Errorf("IsAsyncDeleteFailed() = %v, want %v", got, tc.want) + } + }) + } +}