github.com/GoogleCloudPlatform/deploystack@v1.12.8/tui/pre_processor_test.go (about) 1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tui 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/GoogleCloudPlatform/deploystack/config" 22 "github.com/charmbracelet/bubbles/list" 23 tea "github.com/charmbracelet/bubbletea" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestPreprocessors(t *testing.T) { 28 testdata := "" 29 tests := map[string]struct { 30 f func(q *Queue) tea.Cmd 31 count int 32 label1st string 33 value1st string 34 settings map[string]string 35 cache map[string]interface{} 36 throw bool 37 errmsg errMsg 38 }{ 39 "getDiskTypes": { 40 f: getDiskTypes, 41 count: 3, 42 label1st: "Standard", 43 value1st: "pd-standard", 44 }, 45 "getYesOrNo": { 46 f: getYesOrNo, 47 count: 2, 48 label1st: "Yes", 49 value1st: "y", 50 }, 51 "getNoOrYes": { 52 f: getNoOrYes, 53 count: 2, 54 label1st: "No", 55 value1st: "n", 56 }, 57 58 "getBillingAccounts": { 59 f: getBillingAccounts, 60 count: 2, 61 label1st: "Very Limted Funds", 62 value1st: "000000-000000-00000Y", 63 }, 64 "getBillingAccountsError": { 65 f: getBillingAccounts, 66 label1st: "Very Limted Funds", 67 value1st: "000000-000000-00000Y", 68 throw: true, 69 errmsg: errMsg{err: errForced}, 70 }, 71 72 "getProjects": { 73 f: getProjects, 74 count: 86, 75 label1st: "aiab-test-project", 76 value1st: "aiab-test-project", 77 }, 78 "getProjectsError": { 79 f: getProjects, 80 count: 86, 81 label1st: "aiab-test-project", 82 value1st: "aiab-test-project", 83 throw: true, 84 errmsg: errMsg{err: errForced}, 85 }, 86 87 "getRegions": { 88 f: getRegions, 89 count: 35, 90 label1st: "asia-east1", 91 value1st: "asia-east1", 92 }, 93 "getRegionsError": { 94 f: getRegions, 95 count: 35, 96 label1st: "asia-east1", 97 value1st: "asia-east1", 98 throw: true, 99 errmsg: errMsg{err: errForced}, 100 }, 101 102 "getZones": { 103 f: getZones, 104 count: 3, 105 label1st: "asia-east1-a", 106 value1st: "asia-east1-a", 107 settings: map[string]string{"region": "asia-east1"}, 108 }, 109 "getZonesError": { 110 f: getZones, 111 count: 3, 112 label1st: "asia-east1-a", 113 value1st: "asia-east1-a", 114 throw: true, 115 errmsg: errMsg{err: errForced}, 116 }, 117 118 "getMachineTypeFamilies": { 119 f: getMachineTypeFamilies, 120 count: 34, 121 label1st: "a2 highgpu", 122 value1st: "a2-highgpu", 123 settings: map[string]string{"zone": "asia-east1-b"}, 124 }, 125 126 "getMachineTypeFamiliesError": { 127 f: getMachineTypeFamilies, 128 count: 34, 129 label1st: "a2 highgpu", 130 value1st: "a2-highgpu", 131 throw: true, 132 errmsg: errMsg{err: errForced}, 133 }, 134 135 "getMachineTypes": { 136 f: getMachineTypes, 137 count: 4, 138 label1st: "a2-highgpu-1g", 139 value1st: "a2-highgpu-1g", 140 settings: map[string]string{ 141 "zone": "asia-east1-b", 142 "instance-machine-type-family": "a2-highgpu", 143 }, 144 }, 145 "getMachineTypesError": { 146 f: getMachineTypes, 147 count: 4, 148 label1st: "a2-highgpu-1g", 149 value1st: "a2-highgpu-1g", 150 throw: true, 151 errmsg: errMsg{err: errForced}, 152 }, 153 154 "getDiskProjects": { 155 f: getDiskProjects, 156 count: 14, 157 label1st: "CentOS", 158 value1st: "centos-cloud", 159 }, 160 161 "getImageFamilies": { 162 f: getImageFamilies, 163 count: 3, 164 label1st: "centos-7", 165 value1st: "centos-7", 166 settings: map[string]string{ 167 "instance-image-project": "centos-cloud", 168 }, 169 }, 170 "getImageFamiliesError": { 171 f: getImageFamilies, 172 count: 3, 173 label1st: "centos-7", 174 value1st: "centos-7", 175 throw: true, 176 errmsg: errMsg{err: errForced}, 177 }, 178 179 "getImageDisks": { 180 f: getImageDisks, 181 count: 1, 182 label1st: "centos-7-v20230203 (Latest)", 183 value1st: "centos-cloud/centos-7-v20230203", 184 settings: map[string]string{ 185 "instance-image-project": "centos-cloud", 186 "instance-image-family": "centos-7", 187 }, 188 }, 189 190 "getImageDisksError": { 191 f: getImageDisks, 192 count: 1, 193 label1st: "centos-7-v20230203 (Latest)", 194 value1st: "centos-cloud/centos-7-v20230203", 195 throw: true, 196 errmsg: errMsg{err: errForced}, 197 }, 198 199 "handleReports": { 200 f: handleReports, 201 count: 2, 202 settings: map[string]string{}, 203 label1st: "Minimal JSON", 204 value1st: "/minimaljson", 205 cache: map[string]interface{}{ 206 "reports": []config.Report{ 207 { 208 WD: fmt.Sprintf("%s/minimaljson", testdata), 209 Path: fmt.Sprintf("%s/minimaljson/.deploystack/deploystack.json", testdata), 210 Config: config.Config{Title: "Minimal JSON"}, 211 }, 212 { 213 WD: fmt.Sprintf("%s/minimalyaml", testdata), 214 Path: fmt.Sprintf("%s/minimalyaml/.deploystack/deploystack.yaml", testdata), 215 Config: config.Config{Title: "Minimal YAML"}, 216 }, 217 }, 218 }, 219 }, 220 } 221 222 for name, tc := range tests { 223 t.Run(name, func(t *testing.T) { 224 q := getTestQueue(appTitle, "test") 225 226 if tc.throw { 227 m := GetMock(0) 228 m.forceErr = true 229 q.client = m 230 } 231 232 if tc.settings != nil { 233 for i, v := range tc.settings { 234 q.stack.AddSetting(i, v) 235 } 236 } 237 238 if tc.cache != nil { 239 for i, v := range tc.cache { 240 q.Save(i, v) 241 } 242 } 243 244 cmd := tc.f(&q) 245 raw := cmd() 246 247 switch got := raw.(type) { 248 case []list.Item: 249 250 assert.Equal(t, tc.count, len(got)) 251 252 i := got[0].(item) 253 254 assert.Equal(t, tc.label1st, i.label) 255 assert.Equal(t, tc.value1st, i.value) 256 257 case errMsg: 258 assert.Equal(t, got, tc.errmsg) 259 } 260 261 }) 262 } 263 } 264 265 func TestCleanUp(t *testing.T) { 266 267 tests := map[string]struct { 268 setting string 269 filler string 270 want string 271 }{ 272 "domain_consent": { 273 setting: "domain_consent", 274 filler: "fillerdata", 275 want: "", 276 }, 277 278 "billing": { 279 setting: "form1_new_billing_selector", 280 filler: "fillerdata", 281 want: "", 282 }, 283 } 284 285 for name, tc := range tests { 286 t.Run(name, func(t *testing.T) { 287 q := getTestQueue(appTitle, "test") 288 q.stack.AddSetting(tc.setting, tc.filler) 289 assert.Equal(t, tc.filler, q.stack.GetSetting(tc.setting)) 290 f := cleanUp(&q) 291 f() 292 assert.Equal(t, "", q.stack.GetSetting(tc.setting)) 293 }) 294 } 295 }