github.com/crossplane/upjet@v1.3.0/pkg/config/resource_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package config
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/crossplane/crossplane-runtime/pkg/errors"
    13  	"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
    14  	xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
    15  	"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
    16  	"github.com/crossplane/crossplane-runtime/pkg/test"
    17  	"github.com/google/go-cmp/cmp"
    18  	"sigs.k8s.io/controller-runtime/pkg/client"
    19  )
    20  
    21  const (
    22  	kind     = "ACoolService"
    23  	name     = "example-service"
    24  	provider = "ACoolProvider"
    25  )
    26  
    27  func TestTagger_Initialize(t *testing.T) {
    28  	errBoom := errors.New("boom")
    29  
    30  	type args struct {
    31  		mg   xpresource.Managed
    32  		kube client.Client
    33  	}
    34  	type want struct {
    35  		err error
    36  	}
    37  	cases := map[string]struct {
    38  		args
    39  		want
    40  	}{
    41  		"Successful": {
    42  			args: args{
    43  				mg:   &fake.Managed{},
    44  				kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)},
    45  			},
    46  			want: want{
    47  				err: nil,
    48  			},
    49  		},
    50  		"Failure": {
    51  			args: args{
    52  				mg:   &fake.Managed{},
    53  				kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)},
    54  			},
    55  			want: want{
    56  				err: errBoom,
    57  			},
    58  		},
    59  	}
    60  	for n, tc := range cases {
    61  		t.Run(n, func(t *testing.T) {
    62  			tagger := NewTagger(tc.kube, "tags")
    63  			gotErr := tagger.Initialize(context.TODO(), tc.mg)
    64  			if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" {
    65  				t.Fatalf("generateTypeName(...): -want error, +got error: %s", diff)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestSetExternalTagsWithPaved(t *testing.T) {
    72  	type args struct {
    73  		externalTags map[string]string
    74  		paved        *fieldpath.Paved
    75  		fieldName    string
    76  	}
    77  	type want struct {
    78  		pavedString string
    79  		err         error
    80  	}
    81  	cases := map[string]struct {
    82  		args
    83  		want
    84  	}{
    85  		"Successful": {
    86  			args: args{
    87  				externalTags: map[string]string{
    88  					xpresource.ExternalResourceTagKeyKind:     kind,
    89  					xpresource.ExternalResourceTagKeyName:     name,
    90  					xpresource.ExternalResourceTagKeyProvider: provider,
    91  				},
    92  				paved:     fieldpath.Pave(map[string]any{}),
    93  				fieldName: "tags",
    94  			},
    95  			want: want{
    96  				pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s"}}}}`,
    97  					xpresource.ExternalResourceTagKeyKind, kind,
    98  					xpresource.ExternalResourceTagKeyName, name,
    99  					xpresource.ExternalResourceTagKeyProvider, provider),
   100  			},
   101  		},
   102  	}
   103  	for n, tc := range cases {
   104  		t.Run(n, func(t *testing.T) {
   105  			gotByte, gotErr := setExternalTagsWithPaved(tc.externalTags, tc.paved, tc.fieldName)
   106  			if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" {
   107  				t.Fatalf("generateTypeName(...): -want error, +got error: %s", diff)
   108  			}
   109  			if diff := cmp.Diff(tc.want.pavedString, string(gotByte), test.EquateErrors()); diff != "" {
   110  				t.Fatalf("generateTypeName(...): -want gotByte, +got gotByte: %s", diff)
   111  			}
   112  		})
   113  	}
   114  }