github.com/crossplane/upjet@v1.3.0/pkg/registry/meta_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package registry
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
    12  	xptest "github.com/crossplane/crossplane-runtime/pkg/test"
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/google/go-cmp/cmp/cmpopts"
    15  	"gopkg.in/yaml.v3"
    16  )
    17  
    18  func TestScrapeRepo(t *testing.T) {
    19  	type args struct {
    20  		config *ScrapeConfiguration
    21  	}
    22  	type want struct {
    23  		err    error
    24  		pmPath string
    25  	}
    26  	tests := map[string]struct {
    27  		reason string
    28  		args   args
    29  		want   want
    30  	}{
    31  		"ScrapeAWSResources": {
    32  			reason: "Should successfully scrape AWS resource metadata",
    33  			args: args{
    34  				config: &ScrapeConfiguration{
    35  					RepoPath:       "testdata/aws/r",
    36  					CodeXPath:      `//code[@class="language-terraform" or @class="language-hcl"]/text()`,
    37  					PreludeXPath:   `//text()[contains(., "description") and contains(., "subcategory")]`,
    38  					FieldDocXPath:  `//ul/li//code[1]/text()`,
    39  					ImportXPath:    `//code[@class="language-shell"]/text()`,
    40  					FileExtensions: []string{".markdown"},
    41  				},
    42  			},
    43  			want: want{
    44  				pmPath: "testdata/aws/pm.yaml",
    45  			},
    46  		},
    47  		"ScrapeAzureResources": {
    48  			reason: "Should successfully scrape Azure resource metadata",
    49  			args: args{
    50  				config: &ScrapeConfiguration{
    51  					RepoPath:       "testdata/azure/r",
    52  					CodeXPath:      `//code[@class="language-terraform" or @class="language-hcl"]/text()`,
    53  					PreludeXPath:   `//text()[contains(., "description") and contains(., "subcategory")]`,
    54  					FieldDocXPath:  `//ul/li//code[1]/text()`,
    55  					ImportXPath:    `//code[@class="language-shell"]/text()`,
    56  					FileExtensions: []string{".markdown"},
    57  				},
    58  			},
    59  			want: want{
    60  				pmPath: "testdata/azure/pm.yaml",
    61  			},
    62  		},
    63  		"ScrapeGCPResources": {
    64  			reason: "Should successfully scrape GCP resource metadata",
    65  			args: args{
    66  				config: &ScrapeConfiguration{
    67  					RepoPath:       "testdata/gcp/r",
    68  					CodeXPath:      `//code[@class="language-terraform" or @class="language-hcl"]/text()`,
    69  					PreludeXPath:   `//text()[contains(., "description") and contains(., "subcategory")]`,
    70  					FieldDocXPath:  `//ul/li//code[1]/text()`,
    71  					ImportXPath:    `//code[@class="language-shell"]/text()`,
    72  					FileExtensions: []string{".markdown"},
    73  				},
    74  			},
    75  			want: want{
    76  				pmPath: "testdata/gcp/pm.yaml",
    77  			},
    78  		},
    79  	}
    80  	for name, tc := range tests {
    81  		t.Run(name, func(t *testing.T) {
    82  			pm := NewProviderMetadata("test-provider")
    83  			err := pm.ScrapeRepo(tc.args.config)
    84  			if diff := cmp.Diff(tc.want.err, err, xptest.EquateErrors()); diff != "" {
    85  				t.Errorf("\n%s\nScrapeRepo(error): -want, +got:\n%s", tc.reason, diff)
    86  			}
    87  			if err != nil {
    88  				return
    89  			}
    90  			pmExpected := ProviderMetadata{}
    91  			buff, err := os.ReadFile(tc.want.pmPath)
    92  			if err != nil {
    93  				t.Errorf("Failed to load expected ProviderMetadata from file: %s", tc.want.pmPath)
    94  			}
    95  			if err := yaml.Unmarshal(buff, &pmExpected); err != nil {
    96  				t.Errorf("Failed to unmarshal expected ProviderMetadata from file: %s", tc.want.pmPath)
    97  			}
    98  			// upcoming cmp.Diff fails if
    99  			// resources[*].examples[*].dependencies or
   100  			// resources[*].examples[*].references is not present in the expected
   101  			// metadata document (and is thus nil when decoded). One way to handle
   102  			// this would be not to initialize them to empty maps/slices while
   103  			// populating the `ProviderMetadata` struct but this is good to eliminate
   104  			// nil checks elsewhere. Thus, for the test cases, instead of having to manually
   105  			// initialize them in the testcase YAML documents, we do so programmatically below
   106  			for _, r := range pmExpected.Resources {
   107  				for eKey, e := range r.Examples {
   108  					if e.Dependencies == nil {
   109  						e.Dependencies = make(Dependencies)
   110  					}
   111  					if e.References == nil {
   112  						e.References = make(map[string]string)
   113  					}
   114  					r.Examples[eKey] = e
   115  				}
   116  				if len(r.ImportStatements) == 0 {
   117  					r.ImportStatements = nil
   118  				}
   119  			}
   120  			if diff := cmp.Diff(&pmExpected, pm, cmpopts.IgnoreUnexported(fieldpath.Paved{})); diff != "" {
   121  				t.Errorf("\n%s\nScrapeRepo(ProviderConfig): -want, +got:\n%s", tc.reason, diff)
   122  			}
   123  		})
   124  	}
   125  }