github.com/w3security/vervet/v5@v5.3.1-0.20230618081846-5bd9b5d799dc/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/w3security/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.w3security.io/generated-by: vervet 56 labels: 57 api.w3security.io/version-date: "2021-06-04" 58 api.w3security.io/version-lifecycle: sunset 59 api.w3security.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 --- 133 `[1:]+string(vervetAPIs)) 134 } 135 136 func TestLoadVersionsSomeApis(t *testing.T) { 137 c := qt.New(t) 138 vervetAPIs, err := os.ReadFile(testdata.Path("catalog-vervet-apis.yaml")) 139 c.Assert(err, qt.IsNil) 140 catalog, err := LoadCatalogInfo(bytes.NewBufferString(catalogSrc + ` 141 providesApis: 142 - someOtherApi 143 - someOtherApi 144 `[1:])) 145 c.Assert(err, qt.IsNil) 146 versionsRoot := testdata.Path("output") 147 err = catalog.LoadVervetAPIs(testdata.Path("."), versionsRoot) 148 c.Assert(err, qt.IsNil) 149 150 var saveOutput bytes.Buffer 151 err = catalog.Save(&saveOutput) 152 c.Assert(err, qt.IsNil) 153 c.Assert(saveOutput.String(), qt.Equals, catalogSrc+` 154 providesApis: 155 - Registry_2021-06-01_experimental 156 - Registry_2021-06-04_experimental 157 - Registry_2021-06-07_experimental 158 - Registry_2021-06-13_beta 159 - Registry_2021-06-13_experimental 160 - Registry_2021-08-20_beta 161 - Registry_2021-08-20_experimental 162 - someOtherApi 163 --- 164 `[1:]+string(vervetAPIs)) 165 }