github.com/snyk/vervet/v5@v5.11.1-0.20240202085829-ad4dd7fb6101/internal/backstage/backstage_test.go (about)

     1  package backstage
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  
    10  	"github.com/snyk/vervet/v5/testdata"
    11  )
    12  
    13  func TestBackstageName(t *testing.T) {
    14  	c := qt.New(t)
    15  	tests := []struct {
    16  		in, out string
    17  	}{{
    18  		"foo bar", "foo-bar",
    19  	}, {
    20  		"foo_bar", "foo-bar",
    21  	}, {
    22  		"foo-bar", "foo-bar",
    23  	}, {
    24  		"foo~bar", "foobar",
    25  	}, {
    26  		"Foo1Bar_Baz@#$%^&*()", "Foo1Bar-Baz",
    27  	}}
    28  	for _, test := range tests {
    29  		c.Check(test.out, qt.Equals, toBackstageName(test.in))
    30  	}
    31  }
    32  
    33  func TestRoundTripCatalog(t *testing.T) {
    34  	catalogSrc := `
    35  # Important user-authored comment
    36  apiVersion: backstage.io/v1alpha1
    37  kind: Component
    38  metadata:
    39    name: some-component # inline comment
    40  ---
    41  apiVersion: backstage.io/v1alpha1
    42  kind: API
    43  # special instructions
    44  metadata:
    45    name: some-api
    46  `[1:]
    47  	vervetAPI := `
    48  ---
    49  # Generated by vervet, DO NOT EDIT
    50  apiVersion: backstage.io/v1alpha1
    51  kind: API
    52  metadata:
    53    name: vervet-api
    54    annotations:
    55      api.snyk.io/generated-by: vervet
    56    labels:
    57      api.snyk.io/version-date: "2021-06-04"
    58      api.snyk.io/version-lifecycle: sunset
    59      api.snyk.io/version-stability: experimental
    60  `[1:]
    61  	c := qt.New(t)
    62  	catalog, err := LoadCatalogInfo(bytes.NewBufferString(catalogSrc + vervetAPI))
    63  	c.Assert(err, qt.IsNil)
    64  	c.Assert(catalog.service, qt.Not(qt.IsNil))
    65  	c.Assert(catalog.components, qt.HasLen, 1)
    66  	var saveOutput bytes.Buffer
    67  	c.Assert(catalog.Save(&saveOutput), qt.IsNil)
    68  	c.Assert(saveOutput.String(), qt.Equals, catalogSrc)
    69  }
    70  
    71  func TestLoadCatalogEmpty(t *testing.T) {
    72  	c := qt.New(t)
    73  	catalog, err := LoadCatalogInfo(bytes.NewBufferString(``))
    74  	c.Assert(err, qt.IsNil)
    75  	c.Assert(catalog.service, qt.IsNil)
    76  	c.Assert(catalog.components, qt.HasLen, 0)
    77  }
    78  
    79  func TestLoadCatalogNoService(t *testing.T) {
    80  	c := qt.New(t)
    81  	catalogSrc := `
    82  apiVersion: backstage.io/v1alpha1
    83  kind: Location
    84  metadata:
    85    name: some-place
    86    tags:
    87      - things
    88  spec:
    89    type: url
    90  `[1:]
    91  	catalog, err := LoadCatalogInfo(bytes.NewBufferString(catalogSrc))
    92  	c.Assert(err, qt.IsNil)
    93  	c.Assert(catalog.service, qt.IsNil)
    94  	c.Assert(catalog.components, qt.HasLen, 1)
    95  	var saveOutput bytes.Buffer
    96  	c.Assert(catalog.Save(&saveOutput), qt.IsNil)
    97  	c.Assert(saveOutput.String(), qt.Equals, catalogSrc)
    98  }
    99  
   100  var catalogSrc = `
   101  # Important user-authored comment
   102  apiVersion: backstage.io/v1alpha1
   103  kind: Component
   104  metadata:
   105    name: some-component # inline comment
   106  spec:
   107    owner: "someone-else"
   108  `[1:]
   109  
   110  func TestLoadVersionsNoApis(t *testing.T) {
   111  	c := qt.New(t)
   112  	vervetAPIs, err := os.ReadFile(testdata.Path("catalog-vervet-apis.yaml"))
   113  	c.Assert(err, qt.IsNil)
   114  	catalog, err := LoadCatalogInfo(bytes.NewBufferString(catalogSrc))
   115  	c.Assert(err, qt.IsNil)
   116  	versionsRoot := testdata.Path("output")
   117  	err = catalog.LoadVervetAPIs(testdata.Path("."), versionsRoot)
   118  	c.Assert(err, qt.IsNil)
   119  
   120  	var saveOutput bytes.Buffer
   121  	err = catalog.Save(&saveOutput)
   122  	c.Assert(err, qt.IsNil)
   123  	c.Assert(saveOutput.String(), qt.Equals, catalogSrc+`
   124    providesApis:
   125      - Registry_2021-06-01_experimental
   126      - Registry_2021-06-04_experimental
   127      - Registry_2021-06-07_experimental
   128      - Registry_2021-06-13_beta
   129      - Registry_2021-06-13_experimental
   130      - Registry_2021-08-20_beta
   131      - Registry_2021-08-20_experimental
   132      - Registry_2023-06-01_beta
   133      - Registry_2023-06-01_experimental
   134      - Registry_2023-06-02_beta
   135      - Registry_2023-06-02_experimental
   136      - Registry_2023-06-03_beta
   137      - Registry_2023-06-03_experimental
   138  ---
   139  `[1:]+string(vervetAPIs))
   140  }
   141  
   142  func TestLoadVersionsSomeApis(t *testing.T) {
   143  	c := qt.New(t)
   144  	vervetAPIs, err := os.ReadFile(testdata.Path("catalog-vervet-apis.yaml"))
   145  	c.Assert(err, qt.IsNil)
   146  	catalog, err := LoadCatalogInfo(bytes.NewBufferString(catalogSrc + `
   147    providesApis:
   148      - someOtherApi
   149      - someOtherApi
   150  `[1:]))
   151  	c.Assert(err, qt.IsNil)
   152  	versionsRoot := testdata.Path("output")
   153  	err = catalog.LoadVervetAPIs(testdata.Path("."), versionsRoot)
   154  	c.Assert(err, qt.IsNil)
   155  
   156  	var saveOutput bytes.Buffer
   157  	err = catalog.Save(&saveOutput)
   158  	c.Assert(err, qt.IsNil)
   159  	c.Assert(saveOutput.String(), qt.Equals, catalogSrc+`
   160    providesApis:
   161      - Registry_2021-06-01_experimental
   162      - Registry_2021-06-04_experimental
   163      - Registry_2021-06-07_experimental
   164      - Registry_2021-06-13_beta
   165      - Registry_2021-06-13_experimental
   166      - Registry_2021-08-20_beta
   167      - Registry_2021-08-20_experimental
   168      - Registry_2023-06-01_beta
   169      - Registry_2023-06-01_experimental
   170      - Registry_2023-06-02_beta
   171      - Registry_2023-06-02_experimental
   172      - Registry_2023-06-03_beta
   173      - Registry_2023-06-03_experimental
   174      - someOtherApi
   175  ---
   176  `[1:]+string(vervetAPIs))
   177  }