github.com/crossplane/upjet@v1.3.0/pkg/migration/filesystem_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package migration
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/afero"
    14  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    15  )
    16  
    17  var (
    18  	unstructuredAwsVpc = map[string]interface{}{
    19  		"apiVersion": "ec2.aws.crossplane.io/v1beta1",
    20  		"kind":       "VPC",
    21  		"metadata": map[string]interface{}{
    22  			"name": "sample-vpc",
    23  		},
    24  		"spec": map[string]interface{}{
    25  			"forProvider": map[string]interface{}{
    26  				"region":    "us-west-1",
    27  				"cidrBlock": "172.16.0.0/16",
    28  			},
    29  		},
    30  	}
    31  
    32  	unstructuredResourceGroup = map[string]interface{}{
    33  		"apiVersion": "azure.crossplane.io/v1beta1",
    34  		"kind":       "ResourceGroup",
    35  		"metadata": map[string]interface{}{
    36  			"name": "example-resources",
    37  		},
    38  		"spec": map[string]interface{}{
    39  			"forProvider": map[string]interface{}{
    40  				"location": "West Europe",
    41  			},
    42  		},
    43  	}
    44  )
    45  
    46  func TestNewFileSystemSource(t *testing.T) {
    47  	type args struct {
    48  		dir string
    49  		a   func() afero.Afero
    50  	}
    51  	type want struct {
    52  		fs  *FileSystemSource
    53  		err error
    54  	}
    55  
    56  	cases := map[string]struct {
    57  		args
    58  		want
    59  	}{
    60  		"Successful": {
    61  			args: args{
    62  				dir: "testdata",
    63  				a: func() afero.Afero {
    64  					fss := afero.Afero{Fs: afero.NewMemMapFs()}
    65  					_ = fss.WriteFile("testdata/source/awsvpc.yaml",
    66  						[]byte("apiVersion: ec2.aws.crossplane.io/v1beta1\nkind: VPC\nmetadata:\n  name: sample-vpc\nspec:\n  forProvider:\n    cidrBlock: 172.16.0.0/16\n    region: us-west-1\n"),
    67  						0600)
    68  					_ = fss.WriteFile("testdata/source/resourcegroup.yaml",
    69  						[]byte("apiVersion: azure.crossplane.io/v1beta1\nkind: ResourceGroup\nmetadata:\n  name: example-resources\nspec:\n  forProvider:\n    location: West Europe\n"),
    70  						0600)
    71  					return fss
    72  				},
    73  			},
    74  			want: want{
    75  				fs: &FileSystemSource{
    76  					index: 0,
    77  					items: []UnstructuredWithMetadata{
    78  						{
    79  							Object: unstructured.Unstructured{
    80  								Object: unstructuredAwsVpc,
    81  							},
    82  							Metadata: Metadata{
    83  								Path: "testdata/source/awsvpc.yaml",
    84  							},
    85  						},
    86  						{
    87  							Object: unstructured.Unstructured{
    88  								Object: unstructuredResourceGroup,
    89  							},
    90  							Metadata: Metadata{
    91  								Path: "testdata/source/resourcegroup.yaml",
    92  							},
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  	for name, tc := range cases {
   100  		t.Run(name, func(t *testing.T) {
   101  			files := tc.args.a()
   102  			fs, err := NewFileSystemSource("testdata/source", FsWithFileSystem(files))
   103  			if err != nil {
   104  				t.Fatalf("Failed to initialize a new FileSystemSource: %v", err)
   105  			}
   106  			if diff := cmp.Diff(tc.want.err, err); diff != "" {
   107  				t.Errorf("\nNext(...): -want, +got:\n%s", diff)
   108  			}
   109  			if diff := cmp.Diff(tc.want.fs.items, fs.items); diff != "" {
   110  				t.Errorf("\nNext(...): -want, +got:\n%s", diff)
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestFileSystemTarget_Put(t *testing.T) {
   117  	type args struct {
   118  		o UnstructuredWithMetadata
   119  		a func() afero.Afero
   120  	}
   121  	type want struct {
   122  		data string
   123  		err  error
   124  	}
   125  
   126  	cases := map[string]struct {
   127  		args
   128  		want
   129  	}{
   130  		"Write": {
   131  			args: args{
   132  				o: UnstructuredWithMetadata{
   133  					Object: unstructured.Unstructured{
   134  						Object: map[string]interface{}{
   135  							"apiVersion": "ec2.aws.upbound.io/v1beta1",
   136  							"kind":       "VPC",
   137  							"metadata": map[string]interface{}{
   138  								"name": "sample-vpc",
   139  							},
   140  							"spec": map[string]interface{}{
   141  								"forProvider": map[string]interface{}{
   142  									"region":    "us-west-1",
   143  									"cidrBlock": "172.16.0.0/16",
   144  								},
   145  							},
   146  						},
   147  					},
   148  					Metadata: Metadata{
   149  						Path: "testdata/source/awsvpc.yaml",
   150  					},
   151  				},
   152  				a: func() afero.Afero {
   153  					return afero.Afero{Fs: afero.NewMemMapFs()}
   154  				},
   155  			},
   156  			want: want{
   157  				data: "apiVersion: ec2.aws.upbound.io/v1beta1\nkind: VPC\nmetadata:\n  name: sample-vpc\nspec:\n  forProvider:\n    cidrBlock: 172.16.0.0/16\n    region: us-west-1\n",
   158  				err:  nil,
   159  			},
   160  		},
   161  		"Append": {
   162  			args: args{
   163  				o: UnstructuredWithMetadata{
   164  					Object: unstructured.Unstructured{
   165  						Object: map[string]interface{}{
   166  							"apiVersion": "azure.crossplane.io/v1beta1",
   167  							"kind":       "ResourceGroup",
   168  							"metadata": map[string]interface{}{
   169  								"name": "example-resources",
   170  							},
   171  							"spec": map[string]interface{}{
   172  								"forProvider": map[string]interface{}{
   173  									"location": "West Europe",
   174  								},
   175  							},
   176  						},
   177  					},
   178  					Metadata: Metadata{
   179  						Path:    "testdata/source/awsvpc.yaml",
   180  						Parents: "parent metadata",
   181  					},
   182  				},
   183  				a: func() afero.Afero {
   184  					fss := afero.Afero{Fs: afero.NewMemMapFs()}
   185  					_ = fss.WriteFile("testdata/source/awsvpc.yaml",
   186  						[]byte("apiVersion: ec2.aws.upbound.io/v1beta1\nkind: VPC\nmetadata:\n  name: sample-vpc\nspec:\n  forProvider:\n    cidrBlock: 172.16.0.0/16\n    region: us-west-1\n"),
   187  						0600)
   188  					return fss
   189  				},
   190  			},
   191  			want: want{
   192  				data: "apiVersion: ec2.aws.upbound.io/v1beta1\nkind: VPC\nmetadata:\n  name: sample-vpc\nspec:\n  forProvider:\n    cidrBlock: 172.16.0.0/16\n    region: us-west-1\n\n---\n\napiVersion: azure.crossplane.io/v1beta1\nkind: ResourceGroup\nmetadata:\n  name: example-resources\nspec:\n  forProvider:\n    location: West Europe\n",
   193  				err:  nil,
   194  			},
   195  		},
   196  	}
   197  	for name, tc := range cases {
   198  		t.Run(name, func(t *testing.T) {
   199  			files := tc.args.a()
   200  			ft := NewFileSystemTarget(FtWithFileSystem(files))
   201  			if err := ft.Put(tc.args.o); err != nil {
   202  				t.Error(err)
   203  			}
   204  			b, err := ft.afero.ReadFile("testdata/source/awsvpc.yaml")
   205  			if diff := cmp.Diff(tc.want.err, err); diff != "" {
   206  				t.Errorf("\nNext(...): -want, +got:\n%s", diff)
   207  			}
   208  			if diff := cmp.Diff(tc.want.data, string(b)); diff != "" {
   209  				t.Errorf("\nNext(...): -want, +got:\n%s", diff)
   210  			}
   211  		})
   212  	}
   213  }
   214  
   215  func TestFileSystemTarget_Delete(t *testing.T) {
   216  	type args struct {
   217  		o UnstructuredWithMetadata
   218  		a func() afero.Afero
   219  	}
   220  	type want struct {
   221  		err error
   222  	}
   223  	cases := map[string]struct {
   224  		args
   225  		want
   226  	}{
   227  		"Successful": {
   228  			args: args{
   229  				o: UnstructuredWithMetadata{
   230  					Metadata: Metadata{
   231  						Path: "testdata/source/awsvpc.yaml",
   232  					},
   233  				},
   234  				a: func() afero.Afero {
   235  					fss := afero.Afero{Fs: afero.NewMemMapFs()}
   236  					_ = fss.WriteFile("testdata/source/awsvpc.yaml",
   237  						[]byte("apiVersion: ec2.aws.upbound.io/v1beta1\nkind: VPC\nmetadata:\n  name: sample-vpc\nspec:\n  forProvider:\n    cidrBlock: 172.16.0.0/16\n    region: us-west-1\n"),
   238  						0600)
   239  					return fss
   240  				},
   241  			},
   242  			want: want{
   243  				err: errors.New(fmt.Sprintf("%s: %s", "open testdata/source/awsvpc.yaml", afero.ErrFileNotFound)),
   244  			},
   245  		},
   246  	}
   247  	for name, tc := range cases {
   248  		t.Run(name, func(t *testing.T) {
   249  			files := tc.args.a()
   250  			ft := NewFileSystemTarget(FtWithFileSystem(files))
   251  			if err := ft.Delete(tc.args.o); err != nil {
   252  				t.Error(err)
   253  			}
   254  			_, err := ft.afero.ReadFile("testdata/source/awsvpc.yaml")
   255  			if diff := cmp.Diff(tc.want.err.Error(), err.Error()); diff != "" {
   256  				t.Errorf("\nNext(...): -want, +got:\n%s", diff)
   257  			}
   258  		})
   259  	}
   260  }