Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kselftest/alsa: Use card name rather than number in test names

Currently for the PCM and mixer tests we report test names which identify
the card being tested with the card number. This ensures we have unique
names but since card numbers are dynamically assigned at runtime the names
we end up with will often not be stable on systems with multiple cards
especially where those cards are provided by separate modules loeaded at
runtime. This makes it difficult for automated systems and UIs to relate
test results between runs on affected platforms.

Address this by replacing our use of card numbers with card names which are
more likely to be stable across runs. We use the card ID since it is
guaranteed to be unique by default, unlike the long name. There is still
some vulnerability to ordering issues if multiple cards with the same base
ID are present in the system but have separate dependencies but not all
drivers put distinguishing information in their long names.

Signed-off-by: Mark Brown <broonie@kernel.org>
Link: https://patch.msgid.link/20240716-alsa-kselftest-board-name-v2-1-60f1acdde096@kernel.org
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by

Mark Brown and committed by
Takashi Iwai
b1a7b97a d7063c08

+104 -58
+59 -39
tools/testing/selftests/alsa/mixer-test.c
··· 33 33 struct card_data { 34 34 snd_ctl_t *handle; 35 35 int card; 36 + snd_ctl_card_info_t *info; 37 + const char *card_name; 36 38 struct pollfd pollfd; 37 39 int num_ctls; 38 40 snd_ctl_elem_list_t *ctls; ··· 93 91 err = snd_card_get_longname(card, &card_longname); 94 92 if (err != 0) 95 93 card_longname = "Unknown"; 96 - ksft_print_msg("Card %d - %s (%s)\n", card, 97 - card_name, card_longname); 94 + 95 + err = snd_ctl_card_info_malloc(&card_data->info); 96 + if (err != 0) 97 + ksft_exit_fail_msg("Failed to allocate card info: %d\n", 98 + err); 99 + 100 + err = snd_ctl_card_info(card_data->handle, card_data->info); 101 + if (err == 0) { 102 + card_data->card_name = snd_ctl_card_info_get_id(card_data->info); 103 + if (!card_data->card_name) 104 + ksft_print_msg("Failed to get card ID\n"); 105 + } else { 106 + ksft_print_msg("Failed to get card info: %d\n", err); 107 + } 108 + 109 + if (!card_data->card_name) 110 + card_data->card_name = "Unknown"; 111 + 112 + ksft_print_msg("Card %d/%s - %s (%s)\n", card, 113 + card_data->card_name, card_name, card_longname); 98 114 99 115 /* Count controls */ 100 116 snd_ctl_elem_list_malloc(&card_data->ctls); ··· 409 389 /* If the control is turned off let's be polite */ 410 390 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 411 391 ksft_print_msg("%s is inactive\n", ctl->name); 412 - ksft_test_result_skip("get_value.%d.%d\n", 413 - ctl->card->card, ctl->elem); 392 + ksft_test_result_skip("get_value.%s.%d\n", 393 + ctl->card->card_name, ctl->elem); 414 394 return; 415 395 } 416 396 417 397 /* Can't test reading on an unreadable control */ 418 398 if (!snd_ctl_elem_info_is_readable(ctl->info)) { 419 399 ksft_print_msg("%s is not readable\n", ctl->name); 420 - ksft_test_result_skip("get_value.%d.%d\n", 421 - ctl->card->card, ctl->elem); 400 + ksft_test_result_skip("get_value.%s.%d\n", 401 + ctl->card->card_name, ctl->elem); 422 402 return; 423 403 } 424 404 ··· 433 413 err = -EINVAL; 434 414 435 415 out: 436 - ksft_test_result(err >= 0, "get_value.%d.%d\n", 437 - ctl->card->card, ctl->elem); 416 + ksft_test_result(err >= 0, "get_value.%s.%d\n", 417 + ctl->card->card_name, ctl->elem); 438 418 } 439 419 440 420 static bool strend(const char *haystack, const char *needle) ··· 451 431 { 452 432 bool name_ok = true; 453 433 454 - ksft_print_msg("%d.%d %s\n", ctl->card->card, ctl->elem, 434 + ksft_print_msg("%s.%d %s\n", ctl->card->card_name, ctl->elem, 455 435 ctl->name); 456 436 457 437 /* Only boolean controls should end in Switch */ ··· 473 453 } 474 454 } 475 455 476 - ksft_test_result(name_ok, "name.%d.%d\n", 477 - ctl->card->card, ctl->elem); 456 + ksft_test_result(name_ok, "name.%s.%d\n", 457 + ctl->card->card_name, ctl->elem); 478 458 } 479 459 480 460 static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val, ··· 715 695 /* If the control is turned off let's be polite */ 716 696 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 717 697 ksft_print_msg("%s is inactive\n", ctl->name); 718 - ksft_test_result_skip("write_default.%d.%d\n", 719 - ctl->card->card, ctl->elem); 698 + ksft_test_result_skip("write_default.%s.%d\n", 699 + ctl->card->card_name, ctl->elem); 720 700 return; 721 701 } 722 702 723 703 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 724 704 ksft_print_msg("%s is not writeable\n", ctl->name); 725 - ksft_test_result_skip("write_default.%d.%d\n", 726 - ctl->card->card, ctl->elem); 705 + ksft_test_result_skip("write_default.%s.%d\n", 706 + ctl->card->card_name, ctl->elem); 727 707 return; 728 708 } 729 709 730 710 /* No idea what the default was for unreadable controls */ 731 711 if (!snd_ctl_elem_info_is_readable(ctl->info)) { 732 712 ksft_print_msg("%s couldn't read default\n", ctl->name); 733 - ksft_test_result_skip("write_default.%d.%d\n", 734 - ctl->card->card, ctl->elem); 713 + ksft_test_result_skip("write_default.%s.%d\n", 714 + ctl->card->card_name, ctl->elem); 735 715 return; 736 716 } 737 717 738 718 err = write_and_verify(ctl, ctl->def_val, NULL); 739 719 740 - ksft_test_result(err >= 0, "write_default.%d.%d\n", 741 - ctl->card->card, ctl->elem); 720 + ksft_test_result(err >= 0, "write_default.%s.%d\n", 721 + ctl->card->card_name, ctl->elem); 742 722 } 743 723 744 724 static bool test_ctl_write_valid_boolean(struct ctl_data *ctl) ··· 848 828 /* If the control is turned off let's be polite */ 849 829 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 850 830 ksft_print_msg("%s is inactive\n", ctl->name); 851 - ksft_test_result_skip("write_valid.%d.%d\n", 852 - ctl->card->card, ctl->elem); 831 + ksft_test_result_skip("write_valid.%s.%d\n", 832 + ctl->card->card_name, ctl->elem); 853 833 return; 854 834 } 855 835 856 836 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 857 837 ksft_print_msg("%s is not writeable\n", ctl->name); 858 - ksft_test_result_skip("write_valid.%d.%d\n", 859 - ctl->card->card, ctl->elem); 838 + ksft_test_result_skip("write_valid.%s.%d\n", 839 + ctl->card->card_name, ctl->elem); 860 840 return; 861 841 } 862 842 ··· 879 859 880 860 default: 881 861 /* No tests for this yet */ 882 - ksft_test_result_skip("write_valid.%d.%d\n", 883 - ctl->card->card, ctl->elem); 862 + ksft_test_result_skip("write_valid.%s.%d\n", 863 + ctl->card->card_name, ctl->elem); 884 864 return; 885 865 } 886 866 887 867 /* Restore the default value to minimise disruption */ 888 868 write_and_verify(ctl, ctl->def_val, NULL); 889 869 890 - ksft_test_result(pass, "write_valid.%d.%d\n", 891 - ctl->card->card, ctl->elem); 870 + ksft_test_result(pass, "write_valid.%s.%d\n", 871 + ctl->card->card_name, ctl->elem); 892 872 } 893 873 894 874 static bool test_ctl_write_invalid_value(struct ctl_data *ctl, ··· 1060 1040 /* If the control is turned off let's be polite */ 1061 1041 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 1062 1042 ksft_print_msg("%s is inactive\n", ctl->name); 1063 - ksft_test_result_skip("write_invalid.%d.%d\n", 1064 - ctl->card->card, ctl->elem); 1043 + ksft_test_result_skip("write_invalid.%s.%d\n", 1044 + ctl->card->card_name, ctl->elem); 1065 1045 return; 1066 1046 } 1067 1047 1068 1048 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 1069 1049 ksft_print_msg("%s is not writeable\n", ctl->name); 1070 - ksft_test_result_skip("write_invalid.%d.%d\n", 1071 - ctl->card->card, ctl->elem); 1050 + ksft_test_result_skip("write_invalid.%s.%d\n", 1051 + ctl->card->card_name, ctl->elem); 1072 1052 return; 1073 1053 } 1074 1054 ··· 1091 1071 1092 1072 default: 1093 1073 /* No tests for this yet */ 1094 - ksft_test_result_skip("write_invalid.%d.%d\n", 1095 - ctl->card->card, ctl->elem); 1074 + ksft_test_result_skip("write_invalid.%s.%d\n", 1075 + ctl->card->card_name, ctl->elem); 1096 1076 return; 1097 1077 } 1098 1078 1099 1079 /* Restore the default value to minimise disruption */ 1100 1080 write_and_verify(ctl, ctl->def_val, NULL); 1101 1081 1102 - ksft_test_result(pass, "write_invalid.%d.%d\n", 1103 - ctl->card->card, ctl->elem); 1082 + ksft_test_result(pass, "write_invalid.%s.%d\n", 1083 + ctl->card->card_name, ctl->elem); 1104 1084 } 1105 1085 1106 1086 static void test_ctl_event_missing(struct ctl_data *ctl) 1107 1087 { 1108 - ksft_test_result(!ctl->event_missing, "event_missing.%d.%d\n", 1109 - ctl->card->card, ctl->elem); 1088 + ksft_test_result(!ctl->event_missing, "event_missing.%s.%d\n", 1089 + ctl->card->card_name, ctl->elem); 1110 1090 } 1111 1091 1112 1092 static void test_ctl_event_spurious(struct ctl_data *ctl) 1113 1093 { 1114 - ksft_test_result(!ctl->event_spurious, "event_spurious.%d.%d\n", 1115 - ctl->card->card, ctl->elem); 1094 + ksft_test_result(!ctl->event_spurious, "event_spurious.%s.%d\n", 1095 + ctl->card->card_name, ctl->elem); 1116 1096 } 1117 1097 1118 1098 int main(void)
+45 -19
tools/testing/selftests/alsa/pcm-test.c
··· 24 24 25 25 struct card_data { 26 26 int card; 27 + snd_ctl_card_info_t *info; 28 + const char *name; 27 29 pthread_t thread; 28 30 struct card_data *next; 29 31 }; ··· 37 35 int card; 38 36 int device; 39 37 int subdevice; 38 + const char *card_name; 40 39 snd_pcm_stream_t stream; 41 40 snd_config_t *pcm_config; 42 41 struct pcm_data *next; ··· 170 167 config = get_alsalib_config(); 171 168 172 169 while (card >= 0) { 170 + card_data = calloc(1, sizeof(*card_data)); 171 + if (!card_data) 172 + ksft_exit_fail_msg("Out of memory\n"); 173 + 173 174 sprintf(name, "hw:%d", card); 174 175 175 176 err = snd_ctl_open_lconf(&handle, name, 0, config); ··· 189 182 err = snd_card_get_longname(card, &card_longname); 190 183 if (err != 0) 191 184 card_longname = "Unknown"; 192 - ksft_print_msg("Card %d - %s (%s)\n", card, 193 - card_name, card_longname); 185 + 186 + err = snd_ctl_card_info_malloc(&card_data->info); 187 + if (err != 0) 188 + ksft_exit_fail_msg("Failed to allocate card info: %d\n", 189 + err); 190 + 191 + err = snd_ctl_card_info(handle, card_data->info); 192 + if (err == 0) { 193 + card_data->name = snd_ctl_card_info_get_id(card_data->info); 194 + if (!card_data->name) 195 + ksft_print_msg("Failed to get card ID\n"); 196 + } else { 197 + ksft_print_msg("Failed to get card info: %d\n", err); 198 + } 199 + 200 + if (!card_data->name) 201 + card_data->name = "Unknown"; 202 + 203 + ksft_print_msg("Card %d/%s - %s (%s)\n", card, 204 + card_data->name, card_name, card_longname); 194 205 195 206 card_config = conf_by_card(card); 196 207 197 - card_data = calloc(1, sizeof(*card_data)); 198 - if (!card_data) 199 - ksft_exit_fail_msg("Out of memory\n"); 200 208 card_data->card = card; 201 209 card_data->next = card_list; 202 210 card_list = card_data; ··· 254 232 pcm_data->card = card; 255 233 pcm_data->device = dev; 256 234 pcm_data->subdevice = subdev; 235 + pcm_data->card_name = card_data->name; 257 236 pcm_data->stream = stream; 258 237 pcm_data->pcm_config = conf_get_subtree(card_config, key, NULL); 259 238 pcm_data->next = pcm_list; ··· 317 294 318 295 desc = conf_get_string(pcm_cfg, "description", NULL, NULL); 319 296 if (desc) 320 - ksft_print_msg("%s.%s.%d.%d.%d.%s - %s\n", 297 + ksft_print_msg("%s.%s.%s.%d.%d.%s - %s\n", 321 298 test_class_name, test_name, 322 - data->card, data->device, data->subdevice, 299 + data->card_name, data->device, data->subdevice, 323 300 snd_pcm_stream_name(data->stream), 324 301 desc); 325 302 ··· 375 352 old_format = format; 376 353 format = snd_pcm_format_value(alt_formats[i]); 377 354 if (format != SND_PCM_FORMAT_UNKNOWN) { 378 - ksft_print_msg("%s.%d.%d.%d.%s.%s format %s -> %s\n", 355 + ksft_print_msg("%s.%s.%d.%d.%s.%s format %s -> %s\n", 379 356 test_name, 380 - data->card, data->device, data->subdevice, 357 + data->card_name, data->device, data->subdevice, 381 358 snd_pcm_stream_name(data->stream), 382 359 snd_pcm_access_name(access), 383 360 snd_pcm_format_name(old_format), ··· 453 430 goto __close; 454 431 } 455 432 456 - ksft_print_msg("%s.%s.%d.%d.%d.%s hw_params.%s.%s.%ld.%ld.%ld.%ld sw_params.%ld\n", 433 + ksft_print_msg("%s.%s.%s.%d.%d.%s hw_params.%s.%s.%ld.%ld.%ld.%ld sw_params.%ld\n", 457 434 test_class_name, test_name, 458 - data->card, data->device, data->subdevice, 435 + data->card_name, data->device, data->subdevice, 459 436 snd_pcm_stream_name(data->stream), 460 437 snd_pcm_access_name(access), 461 438 snd_pcm_format_name(format), ··· 514 491 * Anything specified as specific to this system 515 492 * should always be supported. 516 493 */ 517 - ksft_test_result(!skip, "%s.%s.%d.%d.%d.%s.params\n", 494 + ksft_test_result(!skip, "%s.%s.%s.%d.%d.%s.params\n", 518 495 test_class_name, test_name, 519 - data->card, data->device, data->subdevice, 496 + data->card_name, data->device, 497 + data->subdevice, 520 498 snd_pcm_stream_name(data->stream)); 521 499 break; 522 500 default: ··· 525 501 } 526 502 527 503 if (!skip) 528 - ksft_test_result(pass, "%s.%s.%d.%d.%d.%s\n", 504 + ksft_test_result(pass, "%s.%s.%s.%d.%d.%s\n", 529 505 test_class_name, test_name, 530 - data->card, data->device, data->subdevice, 506 + data->card_name, data->device, 507 + data->subdevice, 531 508 snd_pcm_stream_name(data->stream)); 532 509 else 533 - ksft_test_result_skip("%s.%s.%d.%d.%d.%s\n", 510 + ksft_test_result_skip("%s.%s.%s.%d.%d.%s\n", 534 511 test_class_name, test_name, 535 - data->card, data->device, data->subdevice, 512 + data->card_name, data->device, 513 + data->subdevice, 536 514 snd_pcm_stream_name(data->stream)); 537 515 538 516 if (msg[0]) ··· 635 609 conf->filename, conf->config_id); 636 610 637 611 for (pcm = pcm_missing; pcm != NULL; pcm = pcm->next) { 638 - ksft_test_result(false, "test.missing.%d.%d.%d.%s\n", 639 - pcm->card, pcm->device, pcm->subdevice, 612 + ksft_test_result(false, "test.missing.%s.%d.%d.%s\n", 613 + pcm->card_name, pcm->device, pcm->subdevice, 640 614 snd_pcm_stream_name(pcm->stream)); 641 615 } 642 616