|
18 | 18 | package cli |
19 | 19 |
|
20 | 20 | import ( |
| 21 | + "reflect" |
| 22 | + "strings" |
21 | 23 | "testing" |
22 | 24 |
|
23 | 25 | "github.com/apache/cloudstack-cloudmonkey/config" |
@@ -221,3 +223,169 @@ func TestFindAutocompleteAPIHeuristicWinsOverRelated(t *testing.T) { |
221 | 223 | t.Fatalf("expected listProjects, got %s", result.Name) |
222 | 224 | } |
223 | 225 | } |
| 226 | + |
| 227 | +func TestMapFieldsFromDescription(t *testing.T) { |
| 228 | + description := "Example: tags[0].key=value&tags[0].value=value" |
| 229 | + |
| 230 | + fields := mapFieldsFromDescription(description) |
| 231 | + |
| 232 | + expected := []string{ |
| 233 | + "tags[0].key=", |
| 234 | + "tags[0].value=", |
| 235 | + } |
| 236 | + |
| 237 | + if len(fields) != len(expected) { |
| 238 | + t.Fatalf("expected %d fields, got %d: %v", len(expected), len(fields), fields) |
| 239 | + } |
| 240 | + |
| 241 | + for i := range expected { |
| 242 | + if fields[i] != expected[i] { |
| 243 | + t.Errorf("expected %q, got %q", expected[i], fields[i]) |
| 244 | + } |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +func TestMapFieldsFromDescriptionWithRealMap(t *testing.T) { |
| 249 | + description := "VMware only: used to specify network mapping of a vApp VMware template registered \"as-is\". Example nicnetworklist[0].ip=Nic-101&nicnetworklist[0].network=uuid" |
| 250 | + |
| 251 | + fields := mapFieldsFromDescription(description) |
| 252 | + |
| 253 | + expected := []string{ |
| 254 | + "nicnetworklist[0].ip=", |
| 255 | + "nicnetworklist[0].network=", |
| 256 | + } |
| 257 | + |
| 258 | + if len(fields) != len(expected) { |
| 259 | + t.Fatalf("expected %d fields, got %d: %v", len(expected), len(fields), fields) |
| 260 | + } |
| 261 | + |
| 262 | + for i := range expected { |
| 263 | + if fields[i] != expected[i] { |
| 264 | + t.Errorf("expected %q, got %q", expected[i], fields[i]) |
| 265 | + } |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +func TestMapFieldsFromDescriptionWithMultipleFields(t *testing.T) { |
| 270 | + description := "Disk offering details. Example: datadisksdetails[0].diskofferingid=id&datadisksdetails[0].size=10&datadisksdetails[0].miniops=100&datadisksdetails[0].maxiops=200" |
| 271 | + |
| 272 | + fields := mapFieldsFromDescription(description) |
| 273 | + |
| 274 | + expected := []string{ |
| 275 | + "datadisksdetails[0].diskofferingid=", |
| 276 | + "datadisksdetails[0].size=", |
| 277 | + "datadisksdetails[0].miniops=", |
| 278 | + "datadisksdetails[0].maxiops=", |
| 279 | + } |
| 280 | + |
| 281 | + if len(fields) != len(expected) { |
| 282 | + t.Fatalf("expected %d fields, got %d: %v", len(expected), len(fields), fields) |
| 283 | + } |
| 284 | + |
| 285 | + for i := range expected { |
| 286 | + if fields[i] != expected[i] { |
| 287 | + t.Errorf("expected %q, got %q", expected[i], fields[i]) |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +func TestMapFieldSuggestions(t *testing.T) { |
| 293 | + arg := &config.APIArg{ |
| 294 | + Name: "tags=", |
| 295 | + Type: "map", |
| 296 | + Description: "Example: tags[0].key=value&tags[0].value=value", |
| 297 | + } |
| 298 | + |
| 299 | + suggestions := mapFieldSuggestions(arg) |
| 300 | + |
| 301 | + expected := []string{ |
| 302 | + "tags[0].key=", |
| 303 | + "tags[0].value=", |
| 304 | + } |
| 305 | + |
| 306 | + if len(suggestions) != len(expected) { |
| 307 | + t.Fatalf("expected %d suggestions, got %d: %v", len(expected), len(suggestions), suggestions) |
| 308 | + } |
| 309 | + |
| 310 | + for i := range expected { |
| 311 | + if suggestions[i] != expected[i] { |
| 312 | + t.Errorf("expected %q, got %q", expected[i], suggestions[i]) |
| 313 | + } |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 317 | +func TestMapFieldSuggestionsWithGenericMap(t *testing.T) { |
| 318 | + arg := &config.APIArg{ |
| 319 | + Name: "tags", |
| 320 | + Type: "map", |
| 321 | + Description: "List resources by tags (key/value pairs)", |
| 322 | + } |
| 323 | + |
| 324 | + expected := []string{ |
| 325 | + "tags[0].key=", |
| 326 | + "tags[0].value=", |
| 327 | + } |
| 328 | + |
| 329 | + got := mapFieldSuggestions(arg) |
| 330 | + |
| 331 | + if !reflect.DeepEqual(got, expected) { |
| 332 | + t.Fatalf("expected %v, got %v", expected, got) |
| 333 | + } |
| 334 | +} |
| 335 | + |
| 336 | +func TestMapAutocompleteOptions(t *testing.T) { |
| 337 | + arg := &config.APIArg{ |
| 338 | + Name: "tags", |
| 339 | + Type: "map", |
| 340 | + Description: "List resources by tags (key/value pairs)", |
| 341 | + } |
| 342 | + |
| 343 | + line := "tags" |
| 344 | + |
| 345 | + suggestions := mapFieldSuggestions(arg) |
| 346 | + var options [][]rune |
| 347 | + |
| 348 | + for _, suggestion := range suggestions { |
| 349 | + if strings.HasPrefix(suggestion, line) { |
| 350 | + options = append(options, []rune(suggestion[len(line):])) |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + expected := [][]rune{ |
| 355 | + []rune("[0].key="), |
| 356 | + []rune("[0].value="), |
| 357 | + } |
| 358 | + |
| 359 | + if !reflect.DeepEqual(options, expected) { |
| 360 | + t.Fatalf("expected %v, got %v", expected, options) |
| 361 | + } |
| 362 | +} |
| 363 | + |
| 364 | +func TestMapFieldsFromDescriptionWithMultipleIndexes(t *testing.T) { |
| 365 | + description := "Example: details[0].key=accesskey&details[0].value=value&details[1].key=secretkey&details[1].value=value" |
| 366 | + |
| 367 | + expected := []string{ |
| 368 | + "details[0].key=", |
| 369 | + "details[0].value=", |
| 370 | + "details[1].key=", |
| 371 | + "details[1].value=", |
| 372 | + } |
| 373 | + |
| 374 | + fields := mapFieldsFromDescription(description) |
| 375 | + |
| 376 | + if !reflect.DeepEqual(fields, expected) { |
| 377 | + t.Fatalf("expected %v, got %v", expected, fields) |
| 378 | + } |
| 379 | +} |
| 380 | + |
| 381 | +func TestMapFieldSuggestionsWithNonKeyValueMap(t *testing.T) { |
| 382 | + arg := &config.APIArg{ |
| 383 | + Name: "storagecapabilities", |
| 384 | + Type: "map", |
| 385 | + Description: "the storage pool capabilities", |
| 386 | + } |
| 387 | + |
| 388 | + if got := mapFieldSuggestions(arg); got != nil { |
| 389 | + t.Fatalf("expected no suggestions, got %v", got) |
| 390 | + } |
| 391 | +} |
0 commit comments