github.com/oam-dev/kubevela@v1.9.11/references/cli/show_test.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/crossplane/crossplane-runtime/pkg/test" 26 "github.com/google/go-cmp/cmp" 27 "github.com/stretchr/testify/assert" 28 29 "github.com/oam-dev/kubevela/apis/types" 30 ) 31 32 const BaseDir = "testdata" 33 34 var TestDir = filepath.Join(BaseDir, "show") 35 36 func TestCreateTestDir(t *testing.T) { 37 if _, err := os.Stat(TestDir); err != nil && os.IsNotExist(err) { 38 err := os.MkdirAll(TestDir, 0750) 39 assert.NoError(t, err) 40 } 41 } 42 43 func TestGenerateSideBar(t *testing.T) { 44 workloadName := "component1" 45 traitName := "trait1" 46 47 cases := map[string]struct { 48 reason string 49 capabilities []types.Capability 50 want error 51 }{ 52 "ComponentTypeAndTraitCapability": { 53 reason: "valid capabilities", 54 capabilities: []types.Capability{ 55 { 56 Name: workloadName, 57 Type: types.TypeComponentDefinition, 58 }, 59 { 60 Name: traitName, 61 Type: types.TypeTrait, 62 }, 63 }, 64 want: nil, 65 }, 66 } 67 68 for name, tc := range cases { 69 t.Run(name, func(t *testing.T) { 70 got := generateSideBar(tc.capabilities, TestDir) 71 if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" { 72 t.Errorf("\n%s\ngenerateSideBar(...): -want error, +got error:\n%s", tc.reason, diff) 73 } 74 data, err := os.ReadFile(filepath.Clean(filepath.Join(TestDir, SideBar))) 75 assert.NoError(t, err) 76 for _, c := range tc.capabilities { 77 assert.Contains(t, string(data), c.Name) 78 } 79 }) 80 } 81 } 82 83 func TestGenerateNavBar(t *testing.T) { 84 assert.NoError(t, generateNavBar(TestDir)) 85 _, err := os.Stat(filepath.Clean(filepath.Join(TestDir, NavBar))) 86 assert.NoError(t, err) 87 } 88 89 func TestGenerateIndexHTML(t *testing.T) { 90 assert.NoError(t, generateIndexHTML(TestDir)) 91 _, err := os.Stat(filepath.Clean(filepath.Join(TestDir, IndexHTML))) 92 assert.NoError(t, err) 93 } 94 95 func TestGenerateCustomCSS(t *testing.T) { 96 assert.NoError(t, generateCustomCSS(TestDir)) 97 _, err := os.Stat(filepath.Clean(filepath.Join(TestDir, CSS))) 98 assert.NoError(t, err) 99 } 100 101 func TestGenerateREADME(t *testing.T) { 102 workloadName := "component1" 103 traitName := "trait1" 104 105 cases := map[string]struct { 106 reason string 107 capabilities []types.Capability 108 want error 109 }{ 110 "ComponentTypeAndTraitCapability": { 111 reason: "valid capabilities", 112 capabilities: []types.Capability{ 113 { 114 Name: workloadName, 115 Type: types.TypeComponentDefinition, 116 }, 117 { 118 Name: traitName, 119 Type: types.TypeTrait, 120 }, 121 }, 122 want: nil, 123 }, 124 } 125 for name, tc := range cases { 126 t.Run(name, func(t *testing.T) { 127 got := generateREADME(tc.capabilities, TestDir) 128 if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" { 129 t.Errorf("\n%s\ngenerateREADME(...): -want error, +got error:\n%s", tc.reason, diff) 130 } 131 data, err := os.ReadFile(filepath.Clean(filepath.Join(TestDir, README))) 132 assert.NoError(t, err) 133 for _, c := range tc.capabilities { 134 switch c.Type { 135 case types.TypeComponentDefinition: 136 assert.Contains(t, string(data), fmt.Sprintf(" - [%s](%s/%s.md)\n", c.Name, types.TypeComponentDefinition, c.Name)) 137 case types.TypeTrait: 138 assert.Contains(t, string(data), fmt.Sprintf(" - [%s](%s/%s.md)\n", c.Name, types.TypeTrait, c.Name)) 139 } 140 } 141 }) 142 } 143 } 144 145 func TestGetWorkloadAndTraits(t *testing.T) { 146 type want struct { 147 workloads []string 148 traits []string 149 policies []string 150 } 151 152 var ( 153 workloadName = "component1" 154 traitName = "trait1" 155 policyName = "policy1" 156 ) 157 158 cases := map[string]struct { 159 reason string 160 capabilities []types.Capability 161 want want 162 }{ 163 "ComponentTypeAndTraitCapability": { 164 reason: "valid capabilities", 165 capabilities: []types.Capability{ 166 { 167 Name: workloadName, 168 Type: types.TypeComponentDefinition, 169 }, 170 { 171 Name: traitName, 172 Type: types.TypeTrait, 173 }, 174 }, 175 want: want{ 176 workloads: []string{workloadName}, 177 traits: []string{traitName}, 178 }, 179 }, 180 "PolicyTypeCapability": { 181 capabilities: []types.Capability{ 182 { 183 Name: policyName, 184 Type: types.TypePolicy, 185 }, 186 }, 187 want: want{ 188 policies: []string{policyName}, 189 }, 190 }, 191 } 192 for name, tc := range cases { 193 t.Run(name, func(t *testing.T) { 194 gotWorkloads, gotTraits, _, gotPolicies := getDefinitions(tc.capabilities) 195 assert.Equal(t, tc.want, want{workloads: gotWorkloads, traits: gotTraits, policies: gotPolicies}) 196 }) 197 } 198 } 199 200 func TestDeleteTestDir(t *testing.T) { 201 if _, err := os.Stat(BaseDir); err == nil { 202 err := os.RemoveAll(BaseDir) 203 assert.NoError(t, err) 204 } 205 }