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

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package terraform
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/crossplane/crossplane-runtime/pkg/errors"
    12  	"github.com/crossplane/crossplane-runtime/pkg/test"
    13  	"github.com/google/go-cmp/cmp"
    14  )
    15  
    16  func TestTimeoutsAsParameter(t *testing.T) {
    17  	type args struct {
    18  		to timeouts
    19  	}
    20  	type want struct {
    21  		out map[string]string
    22  	}
    23  	cases := map[string]struct {
    24  		args
    25  		want
    26  	}{
    27  		"NoTimeouts": {
    28  			want: want{
    29  				out: map[string]string{},
    30  			},
    31  		},
    32  		"SomeTimeout": {
    33  			args: args{
    34  				to: timeouts{
    35  					Read: 3 * time.Minute,
    36  				},
    37  			},
    38  			want: want{
    39  				out: map[string]string{
    40  					"read": "3m0s",
    41  				},
    42  			},
    43  		},
    44  		"AllTimeouts": {
    45  			args: args{
    46  				to: timeouts{
    47  					Create: time.Minute,
    48  					Update: 2 * time.Minute,
    49  					Read:   3 * time.Minute,
    50  					Delete: 4 * time.Minute,
    51  				},
    52  			},
    53  			want: want{
    54  				out: map[string]string{
    55  					"create": "1m0s",
    56  					"update": "2m0s",
    57  					"read":   "3m0s",
    58  					"delete": "4m0s",
    59  				},
    60  			},
    61  		},
    62  	}
    63  	for name, tc := range cases {
    64  		t.Run(name, func(t *testing.T) {
    65  			got := tc.args.to.asParameter()
    66  			if diff := cmp.Diff(tc.want.out, got); diff != "" {
    67  				t.Errorf("\n%s\nasParameter(...): -want out, +got out:\n%s", name, diff)
    68  			}
    69  		})
    70  	}
    71  }
    72  func TestTimeoutsAsMetadata(t *testing.T) {
    73  	type args struct {
    74  		to timeouts
    75  	}
    76  	type want struct {
    77  		out map[string]any
    78  	}
    79  	cases := map[string]struct {
    80  		args
    81  		want
    82  	}{
    83  		"NoTimeouts": {
    84  			want: want{
    85  				out: map[string]any{},
    86  			},
    87  		},
    88  		"SomeTimeout": {
    89  			args: args{
    90  				to: timeouts{
    91  					Read: 3 * time.Minute,
    92  				},
    93  			},
    94  			want: want{
    95  				out: map[string]any{
    96  					"read": int64(180000000000),
    97  				},
    98  			},
    99  		},
   100  		"AllTimeouts": {
   101  			args: args{
   102  				to: timeouts{
   103  					Create: time.Minute,
   104  					Update: 2 * time.Minute,
   105  					Read:   3 * time.Minute,
   106  					Delete: 4 * time.Minute,
   107  				},
   108  			},
   109  			want: want{
   110  				out: map[string]any{
   111  					"create": int64(60000000000),
   112  					"update": int64(120000000000),
   113  					"read":   int64(180000000000),
   114  					"delete": int64(240000000000),
   115  				},
   116  			},
   117  		},
   118  	}
   119  	for name, tc := range cases {
   120  		t.Run(name, func(t *testing.T) {
   121  			got := tc.args.to.asMetadata()
   122  			if diff := cmp.Diff(tc.want.out, got); diff != "" {
   123  				t.Errorf("\n%s\nasParameter(...): -want out, +got out:\n%s", name, diff)
   124  			}
   125  		})
   126  	}
   127  }
   128  func TestInsertTimeoutsMeta(t *testing.T) {
   129  	type args struct {
   130  		rawMeta []byte
   131  		to      timeouts
   132  	}
   133  	type want struct {
   134  		out []byte
   135  		err error
   136  	}
   137  	cases := map[string]struct {
   138  		args
   139  		want
   140  	}{
   141  		"NoTimeoutNoMeta": {},
   142  		"NoMetaButTimeout": {
   143  			args: args{
   144  				to: timeouts{
   145  					Read: 2 * time.Minute,
   146  				},
   147  			},
   148  			want: want{
   149  				out: []byte(`{"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0":{"read":120000000000}}`),
   150  			},
   151  		},
   152  		"NonNilMetaButTimeout": {
   153  			args: args{
   154  				rawMeta: []byte(`{}`),
   155  				to: timeouts{
   156  					Read: 2 * time.Minute,
   157  				},
   158  			},
   159  			want: want{
   160  				out: []byte(`{"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0":{"read":120000000000}}`),
   161  			},
   162  		},
   163  		"CannotParseExistingMeta": {
   164  			args: args{
   165  				rawMeta: []byte(`{malformed}`),
   166  				to: timeouts{
   167  					Read: 2 * time.Minute,
   168  				},
   169  			},
   170  			want: want{
   171  				err: errors.Wrap(errors.New(`ReadString: expects " or n, but found m, error found in #2 byte of ...|{malformed}|..., bigger context ...|{malformed}|...`), `cannot parse existing metadata`), //nolint: golint
   172  			},
   173  		},
   174  		"ExistingMetaAndTimeout": {
   175  			args: args{
   176  				rawMeta: []byte(`{"some-key":"some-value"}`),
   177  				to: timeouts{
   178  					Read: 2 * time.Minute,
   179  				},
   180  			},
   181  			want: want{
   182  				out: []byte(`{"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0":{"read":120000000000},"some-key":"some-value"}`),
   183  			},
   184  		},
   185  		"ExistingMetaNoTimeout": {
   186  			args: args{
   187  				rawMeta: []byte(`{"some-key":"some-value"}`),
   188  			},
   189  			want: want{
   190  				out: []byte(`{"some-key":"some-value"}`),
   191  			},
   192  		},
   193  		"ExistingMetaOverridesSomeTimeout": {
   194  			args: args{
   195  				rawMeta: []byte(`{"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0":{"create":240000000000,"read":120000000000},"some-key":"some-value"}`),
   196  				to: timeouts{
   197  					Read: 1 * time.Minute,
   198  				},
   199  			},
   200  			want: want{
   201  				out: []byte(`{"e2bfb730-ecaa-11e6-8f88-34363bc7c4c0":{"create":240000000000,"read":60000000000},"some-key":"some-value"}`),
   202  			},
   203  		},
   204  	}
   205  	for name, tc := range cases {
   206  		t.Run(name, func(t *testing.T) {
   207  			got, err := insertTimeoutsMeta(tc.args.rawMeta, tc.args.to)
   208  			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
   209  				t.Errorf("\n%s\ninsertTimeoutsMeta(...): -want error, +got error:\n%s", name, diff)
   210  			}
   211  			if diff := cmp.Diff(tc.want.out, got); diff != "" {
   212  				t.Errorf("\n%s\ninsertTimeoutsMeta(...): -want out, +got out:\n%s", name, diff)
   213  			}
   214  		})
   215  	}
   216  }