github.com/crossplane/upjet@v1.3.0/pkg/terraform/errors/errors_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package errors
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  var (
    15  	errorLog = []byte(`{"@level":"info","@message":"Terraform 1.0.3","@module":"terraform.ui","@timestamp":"2021-11-14T23:23:14.009380+03:00","terraform":"1.0.3","type":"version","ui":"0.1.0"}
    16  {"@level":"error","@message":"Error: Missing required argument","@module":"terraform.ui","@timestamp":"2021-11-14T23:23:14.576254+03:00","diagnostic":{"severity":"error","summary":"Missing required argument","detail":"The argument \"location\" is required, but no definition was found.","range":{"filename":"main.tf.json","start":{"line":24,"column":7,"byte":568},"end":{"line":24,"column":8,"byte":569}},"snippet":{"context":"resource.azurerm_resource_group.example","code":"      }","start_line":24,"highlight_start_offset":6,"highlight_end_offset":7,"values":[]}},"type":"diagnostic"}
    17  {"@level":"error","@message":"Error: Missing required argument","@module":"terraform.ui","@timestamp":"2021-11-14T23:23:14.576430+03:00","diagnostic":{"severity":"error","summary":"Missing required argument","detail":"The argument \"name\" is required, but no definition was found.","range":{"filename":"main.tf.json","start":{"line":24,"column":7,"byte":568},"end":{"line":24,"column":8,"byte":569}},"snippet":{"context":"resource.azurerm_resource_group.example","code":"      }","start_line":24,"highlight_start_offset":6,"highlight_end_offset":7,"values":[]}},"type":"diagnostic"}`)
    18  	errorBoom = errors.New("boom")
    19  )
    20  
    21  func TestIsApplyFailed(t *testing.T) {
    22  	var nilApplyErr *applyFailed
    23  	type args struct {
    24  		err error
    25  	}
    26  	tests := map[string]struct {
    27  		args args
    28  		want bool
    29  	}{
    30  		"NilError": {
    31  			args: args{},
    32  			want: false,
    33  		},
    34  		"NilApplyError": {
    35  			args: args{
    36  				err: nilApplyErr,
    37  			},
    38  			want: true,
    39  		},
    40  		"NonApplyError": {
    41  			args: args{
    42  				err: errorBoom,
    43  			},
    44  			want: false,
    45  		},
    46  		"ApplyErrorNoLog": {
    47  			args: args{
    48  				err: NewApplyFailed(nil),
    49  			},
    50  			want: true,
    51  		},
    52  		"ApplyErrorWithLog": {
    53  			args: args{
    54  				err: NewApplyFailed(errorLog),
    55  			},
    56  			want: true,
    57  		},
    58  	}
    59  	for name, tt := range tests {
    60  		t.Run(name, func(t *testing.T) {
    61  			if got := IsApplyFailed(tt.args.err); got != tt.want {
    62  				t.Errorf("IsApplyFailed() = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestIsDestroyFailed(t *testing.T) {
    69  	var nilDestroyErr *destroyFailed
    70  	type args struct {
    71  		err error
    72  	}
    73  	tests := map[string]struct {
    74  		args args
    75  		want bool
    76  	}{
    77  		"NilError": {
    78  			args: args{},
    79  			want: false,
    80  		},
    81  		"NilDestroyError": {
    82  			args: args{
    83  				err: nilDestroyErr,
    84  			},
    85  			want: true,
    86  		},
    87  		"NonDestroyError": {
    88  			args: args{
    89  				err: errorBoom,
    90  			},
    91  			want: false,
    92  		},
    93  		"DestroyErrorNoLog": {
    94  			args: args{
    95  				err: NewDestroyFailed(nil),
    96  			},
    97  			want: true,
    98  		},
    99  		"DestroyErrorWithLog": {
   100  			args: args{
   101  				err: NewDestroyFailed(errorLog),
   102  			},
   103  			want: true,
   104  		},
   105  	}
   106  	for name, tt := range tests {
   107  		t.Run(name, func(t *testing.T) {
   108  			if got := IsDestroyFailed(tt.args.err); got != tt.want {
   109  				t.Errorf("IsDestroyFailed() = %v, want %v", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestIsRefreshFailed(t *testing.T) {
   116  	var nilRefreshErr *refreshFailed
   117  	type args struct {
   118  		err error
   119  	}
   120  	tests := map[string]struct {
   121  		args args
   122  		want bool
   123  	}{
   124  		"NilError": {
   125  			args: args{},
   126  			want: false,
   127  		},
   128  		"NilRefreshError": {
   129  			args: args{
   130  				err: nilRefreshErr,
   131  			},
   132  			want: true,
   133  		},
   134  		"NonRefreshError": {
   135  			args: args{
   136  				err: errorBoom,
   137  			},
   138  			want: false,
   139  		},
   140  		"RefreshErrorNoLog": {
   141  			args: args{
   142  				err: NewRefreshFailed(nil),
   143  			},
   144  			want: true,
   145  		},
   146  		"RefreshErrorWithLog": {
   147  			args: args{
   148  				err: NewRefreshFailed(errorLog),
   149  			},
   150  			want: true,
   151  		},
   152  	}
   153  	for name, tt := range tests {
   154  		t.Run(name, func(t *testing.T) {
   155  			if got := IsRefreshFailed(tt.args.err); got != tt.want {
   156  				t.Errorf("IsRefreshFailed() = %v, want %v", got, tt.want)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func TestIsPlanFailed(t *testing.T) {
   163  	var nilPlanErr *planFailed
   164  	type args struct {
   165  		err error
   166  	}
   167  	tests := map[string]struct {
   168  		args args
   169  		want bool
   170  	}{
   171  		"NilError": {
   172  			args: args{},
   173  			want: false,
   174  		},
   175  		"NilPlanError": {
   176  			args: args{
   177  				err: nilPlanErr,
   178  			},
   179  			want: true,
   180  		},
   181  		"NonPlanError": {
   182  			args: args{
   183  				err: errorBoom,
   184  			},
   185  			want: false,
   186  		},
   187  		"PlanErrorNoLog": {
   188  			args: args{
   189  				err: NewPlanFailed(nil),
   190  			},
   191  			want: true,
   192  		},
   193  		"PlanErrorWithLog": {
   194  			args: args{
   195  				err: NewPlanFailed(errorLog),
   196  			},
   197  			want: true,
   198  		},
   199  	}
   200  	for name, tt := range tests {
   201  		t.Run(name, func(t *testing.T) {
   202  			if got := IsPlanFailed(tt.args.err); got != tt.want {
   203  				t.Errorf("IsPlanFailed() = %v, want %v", got, tt.want)
   204  			}
   205  		})
   206  	}
   207  }
   208  
   209  func TestNewApplyFailed(t *testing.T) {
   210  	type args struct {
   211  		logs []byte
   212  	}
   213  	tests := map[string]struct {
   214  		args           args
   215  		wantErrMessage string
   216  	}{
   217  		"ApplyError": {
   218  			args: args{
   219  				logs: errorLog,
   220  			},
   221  			wantErrMessage: "apply failed: Missing required argument: The argument \"location\" is required, but no definition was found.\nMissing required argument: The argument \"name\" is required, but no definition was found.",
   222  		},
   223  	}
   224  	for name, tt := range tests {
   225  		t.Run(name, func(t *testing.T) {
   226  			err := NewApplyFailed(tt.args.logs)
   227  			got := ""
   228  			if err != nil {
   229  				got = err.Error()
   230  			}
   231  			if diff := cmp.Diff(tt.wantErrMessage, got); diff != "" {
   232  				t.Errorf("\nWrapApplyFailed(...): -want message, +got message:\n%s", diff)
   233  			}
   234  		})
   235  	}
   236  }
   237  
   238  func TestNewDestroyFailed(t *testing.T) {
   239  	type args struct {
   240  		logs []byte
   241  	}
   242  	tests := map[string]struct {
   243  		args           args
   244  		wantErrMessage string
   245  	}{
   246  		"DestroyError": {
   247  			args: args{
   248  				logs: errorLog,
   249  			},
   250  			wantErrMessage: "destroy failed: Missing required argument: The argument \"location\" is required, but no definition was found.\nMissing required argument: The argument \"name\" is required, but no definition was found.",
   251  		},
   252  	}
   253  	for name, tt := range tests {
   254  		t.Run(name, func(t *testing.T) {
   255  			err := NewDestroyFailed(tt.args.logs)
   256  			got := ""
   257  			if err != nil {
   258  				got = err.Error()
   259  			}
   260  			if diff := cmp.Diff(tt.wantErrMessage, got); diff != "" {
   261  				t.Errorf("\nWrapApplyFailed(...): -want message, +got message:\n%s", diff)
   262  			}
   263  		})
   264  	}
   265  }
   266  
   267  func TestNewRefreshFailed(t *testing.T) {
   268  	type args struct {
   269  		logs []byte
   270  	}
   271  	tests := map[string]struct {
   272  		args           args
   273  		wantErrMessage string
   274  	}{
   275  		"RefreshError": {
   276  			args: args{
   277  				logs: errorLog,
   278  			},
   279  			wantErrMessage: "refresh failed: Missing required argument: The argument \"location\" is required, but no definition was found.\nMissing required argument: The argument \"name\" is required, but no definition was found.",
   280  		},
   281  	}
   282  	for name, tt := range tests {
   283  		t.Run(name, func(t *testing.T) {
   284  			err := NewRefreshFailed(tt.args.logs)
   285  			got := ""
   286  			if err != nil {
   287  				got = err.Error()
   288  			}
   289  			if diff := cmp.Diff(tt.wantErrMessage, got); diff != "" {
   290  				t.Errorf("\nWrapRefreshFailed(...): -want message, +got message:\n%s", diff)
   291  			}
   292  		})
   293  	}
   294  }
   295  
   296  func TestNewPlanFailed(t *testing.T) {
   297  	type args struct {
   298  		logs []byte
   299  	}
   300  	tests := map[string]struct {
   301  		args           args
   302  		wantErrMessage string
   303  	}{
   304  		"PlanError": {
   305  			args: args{
   306  				logs: errorLog,
   307  			},
   308  			wantErrMessage: "plan failed: Missing required argument: The argument \"location\" is required, but no definition was found.\nMissing required argument: The argument \"name\" is required, but no definition was found.",
   309  		},
   310  	}
   311  	for name, tt := range tests {
   312  		t.Run(name, func(t *testing.T) {
   313  			err := NewPlanFailed(tt.args.logs)
   314  			got := ""
   315  			if err != nil {
   316  				got = err.Error()
   317  			}
   318  			if diff := cmp.Diff(tt.wantErrMessage, got); diff != "" {
   319  				t.Errorf("\nWrapPlanFailed(...): -want message, +got message:\n%s", diff)
   320  			}
   321  		})
   322  	}
   323  }
   324  
   325  func TestNewRetryScheduleError(t *testing.T) {
   326  	type args struct {
   327  		invocationCount, ttl int
   328  	}
   329  	tests := map[string]struct {
   330  		args
   331  		wantErrMessage string
   332  	}{
   333  		"Successful": {
   334  			args: args{
   335  				invocationCount: 101,
   336  				ttl:             100,
   337  			},
   338  			wantErrMessage: "native provider reuse budget has been exceeded: invocationCount: 101, ttl: 100",
   339  		},
   340  	}
   341  	for name, tc := range tests {
   342  		t.Run(name, func(t *testing.T) {
   343  			err := NewRetryScheduleError(tc.args.invocationCount, tc.args.ttl)
   344  			got := err.Error()
   345  			if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" {
   346  				t.Errorf("\nNewRetryScheduleError(...): -want message, +got message:\n%s", diff)
   347  			}
   348  		})
   349  	}
   350  }
   351  
   352  func TestIsRetryScheduleError(t *testing.T) {
   353  	var nilErr *retrySchedule
   354  	type args struct {
   355  		err error
   356  	}
   357  	tests := map[string]struct {
   358  		args
   359  		want bool
   360  	}{
   361  		"NilError": {
   362  			args: args{},
   363  			want: false,
   364  		},
   365  		"NilRetryScheduleError": {
   366  			args: args{
   367  				err: nilErr,
   368  			},
   369  			want: true,
   370  		},
   371  		"NonRetryScheduleError": {
   372  			args: args{
   373  				err: errorBoom,
   374  			},
   375  			want: false,
   376  		},
   377  		"Successful": {
   378  			args: args{err: NewRetryScheduleError(101, 100)},
   379  			want: true,
   380  		},
   381  	}
   382  	for name, tc := range tests {
   383  		t.Run(name, func(t *testing.T) {
   384  			if got := IsRetryScheduleError(tc.args.err); got != tc.want {
   385  				t.Errorf("IsRetryScheduleError() = %v, want %v", got, tc.want)
   386  			}
   387  		})
   388  	}
   389  }
   390  
   391  func TestNewAsyncCreateFailed(t *testing.T) {
   392  	type args struct {
   393  		err error
   394  	}
   395  	tests := map[string]struct {
   396  		args
   397  		wantErrMessage string
   398  	}{
   399  		"Successful": {
   400  			args: args{
   401  				err: errors.New("already exists"),
   402  			},
   403  			wantErrMessage: "async create failed: already exists",
   404  		},
   405  		"Nil": {
   406  			args: args{
   407  				err: nil,
   408  			},
   409  			wantErrMessage: "",
   410  		},
   411  	}
   412  	for name, tc := range tests {
   413  		t.Run(name, func(t *testing.T) {
   414  			err := NewAsyncCreateFailed(tc.args.err)
   415  			got := ""
   416  			if err != nil {
   417  				got = err.Error()
   418  			}
   419  			if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" {
   420  				t.Errorf("\nNewAsyncCreateFailed(...): -want message, +got message:\n%s", diff)
   421  			}
   422  		})
   423  	}
   424  }
   425  
   426  func TestIsAsyncCreateFailed(t *testing.T) {
   427  	var nilErr *asyncCreateFailed
   428  	type args struct {
   429  		err error
   430  	}
   431  	tests := map[string]struct {
   432  		args
   433  		want bool
   434  	}{
   435  		"NilError": {
   436  			args: args{},
   437  			want: false,
   438  		},
   439  		"NilAsyncCreateError": {
   440  			args: args{
   441  				err: nilErr,
   442  			},
   443  			want: true,
   444  		},
   445  		"NonAsyncCreateError": {
   446  			args: args{
   447  				err: errorBoom,
   448  			},
   449  			want: false,
   450  		},
   451  		"Successful": {
   452  			args: args{err: NewAsyncCreateFailed(errors.New("test"))},
   453  			want: true,
   454  		},
   455  	}
   456  	for name, tc := range tests {
   457  		t.Run(name, func(t *testing.T) {
   458  			if got := IsAsyncCreateFailed(tc.args.err); got != tc.want {
   459  				t.Errorf("IsAsyncCreateFailed() = %v, want %v", got, tc.want)
   460  			}
   461  		})
   462  	}
   463  }
   464  
   465  func TestAsyncUpdateFailed(t *testing.T) {
   466  	type args struct {
   467  		err error
   468  	}
   469  	tests := map[string]struct {
   470  		args
   471  		wantErrMessage string
   472  	}{
   473  		"Successful": {
   474  			args: args{
   475  				err: errors.New("immutable field"),
   476  			},
   477  			wantErrMessage: "async update failed: immutable field",
   478  		},
   479  		"Nil": {
   480  			args: args{
   481  				err: nil,
   482  			},
   483  			wantErrMessage: "",
   484  		},
   485  	}
   486  	for name, tc := range tests {
   487  		t.Run(name, func(t *testing.T) {
   488  			err := NewAsyncUpdateFailed(tc.args.err)
   489  			got := ""
   490  			if err != nil {
   491  				got = err.Error()
   492  			}
   493  			if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" {
   494  				t.Errorf("\nAsyncUpdateFailed(...): -want message, +got message:\n%s", diff)
   495  			}
   496  		})
   497  	}
   498  }
   499  
   500  func TestIsAsyncUpdateFailed(t *testing.T) {
   501  	var nilErr *asyncUpdateFailed
   502  	type args struct {
   503  		err error
   504  	}
   505  	tests := map[string]struct {
   506  		args
   507  		want bool
   508  	}{
   509  		"NilError": {
   510  			args: args{},
   511  			want: false,
   512  		},
   513  		"NilAsyncUpdateError": {
   514  			args: args{
   515  				err: nilErr,
   516  			},
   517  			want: true,
   518  		},
   519  		"NonAsyncUpdateError": {
   520  			args: args{
   521  				err: errorBoom,
   522  			},
   523  			want: false,
   524  		},
   525  		"Successful": {
   526  			args: args{err: NewAsyncUpdateFailed(errors.New("test"))},
   527  			want: true,
   528  		},
   529  	}
   530  	for name, tc := range tests {
   531  		t.Run(name, func(t *testing.T) {
   532  			if got := IsAsyncUpdateFailed(tc.args.err); got != tc.want {
   533  				t.Errorf("IsAsyncUpdateFailed() = %v, want %v", got, tc.want)
   534  			}
   535  		})
   536  	}
   537  }
   538  
   539  func TestAsyncDeleteFailed(t *testing.T) {
   540  	type args struct {
   541  		err error
   542  	}
   543  	tests := map[string]struct {
   544  		args
   545  		wantErrMessage string
   546  	}{
   547  		"Successful": {
   548  			args: args{
   549  				err: errors.New("dependency violation"),
   550  			},
   551  			wantErrMessage: "async delete failed: dependency violation",
   552  		},
   553  		"Nil": {
   554  			args: args{
   555  				err: nil,
   556  			},
   557  			wantErrMessage: "",
   558  		},
   559  	}
   560  	for name, tc := range tests {
   561  		t.Run(name, func(t *testing.T) {
   562  			err := NewAsyncDeleteFailed(tc.args.err)
   563  			got := ""
   564  			if err != nil {
   565  				got = err.Error()
   566  			}
   567  			if diff := cmp.Diff(tc.wantErrMessage, got); diff != "" {
   568  				t.Errorf("\nAsyncDeleteFailed(...): -want message, +got message:\n%s", diff)
   569  			}
   570  		})
   571  	}
   572  }
   573  
   574  func TestIsAsyncDeleteFailed(t *testing.T) {
   575  	var nilErr *asyncDeleteFailed
   576  	type args struct {
   577  		err error
   578  	}
   579  	tests := map[string]struct {
   580  		args
   581  		want bool
   582  	}{
   583  		"NilError": {
   584  			args: args{},
   585  			want: false,
   586  		},
   587  		"NilAsyncUpdateError": {
   588  			args: args{
   589  				err: nilErr,
   590  			},
   591  			want: true,
   592  		},
   593  		"NonAsyncUpdateError": {
   594  			args: args{
   595  				err: errorBoom,
   596  			},
   597  			want: false,
   598  		},
   599  		"Successful": {
   600  			args: args{err: NewAsyncDeleteFailed(errors.New("test"))},
   601  			want: true,
   602  		},
   603  	}
   604  	for name, tc := range tests {
   605  		t.Run(name, func(t *testing.T) {
   606  			if got := IsAsyncDeleteFailed(tc.args.err); got != tc.want {
   607  				t.Errorf("IsAsyncDeleteFailed() = %v, want %v", got, tc.want)
   608  			}
   609  		})
   610  	}
   611  }