forked from opensvc/multipath-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwtable.c
1784 lines (1539 loc) · 52 KB
/
hwtable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Set BROKEN to 1 to treat broken behavior as success */
#define BROKEN 1
#define VERBOSITY 2
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <cmocka.h>
#include <libudev.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <sys/sysmacros.h>
#include "structs.h"
#include "structs_vec.h"
#include "config.h"
#include "debug.h"
#include "defaults.h"
#include "pgpolicies.h"
#include "test-lib.h"
#include "print.h"
#include "util.h"
#include "foreign.h"
#define N_CONF_FILES 2
static const char template[] = "/tmp/hwtable-XXXXXX";
struct key_value {
const char *key;
const char *value;
};
struct hwt_state {
char *tmpname;
char *dirname;
FILE *config_file;
FILE *conf_dir_file[N_CONF_FILES];
struct vectors *vecs;
void (*test)(const struct hwt_state *);
const char *test_name;
};
#define SET_TEST_FUNC(hwt, func) do { \
hwt->test = func; \
hwt->test_name = #func; \
} while (0)
static struct config *_conf;
struct udev *udev;
int logsink = LOGSINK_STDERR_WITHOUT_TIME;
struct config *get_multipath_config(void)
{
return _conf;
}
void put_multipath_config(void *arg)
{}
void make_config_file_path(char *buf, int buflen,
const struct hwt_state *hwt, int i)
{
static const char fn_template[] = "%s/test-%02d.conf";
if (i == -1)
/* main config file */
snprintf(buf, buflen, fn_template, hwt->tmpname, 0);
else
snprintf(buf, buflen, fn_template, hwt->dirname, i);
}
static void reset_vecs(struct vectors *vecs)
{
remove_maps(vecs);
free_pathvec(vecs->pathvec, FREE_PATHS);
vecs->pathvec = vector_alloc();
assert_ptr_not_equal(vecs->pathvec, NULL);
vecs->mpvec = vector_alloc();
assert_ptr_not_equal(vecs->mpvec, NULL);
}
static void free_hwt(struct hwt_state *hwt)
{
char buf[PATH_MAX];
int i;
if (hwt->config_file != NULL)
fclose(hwt->config_file);
for (i = 0; i < N_CONF_FILES; i++) {
if (hwt->conf_dir_file[i] != NULL)
fclose(hwt->conf_dir_file[i]);
}
if (hwt->tmpname != NULL) {
make_config_file_path(buf, sizeof(buf), hwt, -1);
unlink(buf);
rmdir(hwt->tmpname);
free(hwt->tmpname);
}
if (hwt->dirname != NULL) {
for (i = 0; i < N_CONF_FILES; i++) {
make_config_file_path(buf, sizeof(buf), hwt, i);
unlink(buf);
}
rmdir(hwt->dirname);
free(hwt->dirname);
}
if (hwt->vecs != NULL) {
if (hwt->vecs->mpvec != NULL)
remove_maps(hwt->vecs);
if (hwt->vecs->pathvec != NULL)
free_pathvec(hwt->vecs->pathvec, FREE_PATHS);
pthread_mutex_destroy(&hwt->vecs->lock.mutex);
free(hwt->vecs);
}
free(hwt);
}
static int setup(void **state)
{
struct hwt_state *hwt;
char buf[PATH_MAX];
int i;
*state = NULL;
hwt = calloc(1, sizeof(*hwt));
if (hwt == NULL)
return -1;
snprintf(buf, sizeof(buf), "%s", template);
if (mkdtemp(buf) == NULL) {
condlog(0, "mkdtemp: %s", strerror(errno));
goto err;
}
hwt->tmpname = strdup(buf);
hwt->dirname = strdup(TESTCONFDIR);
if (mkdir(hwt->dirname, 0744) != 0) {
condlog(0, "mkdir %s: %s", hwt->dirname, strerror(errno));
goto err;
}
make_config_file_path(buf, sizeof(buf), hwt, -1);
hwt->config_file = fopen(buf, "w+");
if (hwt->config_file == NULL)
goto err;
for (i = 0; i < N_CONF_FILES; i++) {
make_config_file_path(buf, sizeof(buf), hwt, i);
hwt->conf_dir_file[i] = fopen(buf, "w+");
if (hwt->conf_dir_file[i] == NULL)
goto err;
}
hwt->vecs = calloc(1, sizeof(*hwt->vecs));
if (hwt->vecs == NULL)
goto err;
pthread_mutex_init(&hwt->vecs->lock.mutex, NULL);
hwt->vecs->pathvec = vector_alloc();
hwt->vecs->mpvec = vector_alloc();
if (hwt->vecs->pathvec == NULL || hwt->vecs->mpvec == NULL)
goto err;
*state = hwt;
return 0;
err:
free_hwt(hwt);
return -1;
}
static int teardown(void **state)
{
if (state == NULL || *state == NULL)
return -1;
free_hwt(*state);
*state = NULL;
cleanup_prio();
cleanup_checkers();
cleanup_foreign();
return 0;
}
/*
* Helpers for creating the config file(s)
*/
static void reset_config(FILE *ff)
{
if (ff == NULL)
return;
rewind(ff);
if (ftruncate(fileno(ff), 0) == -1)
condlog(1, "ftruncate: %s", strerror(errno));
}
static void reset_configs(const struct hwt_state *hwt)
{
int i;
reset_config(hwt->config_file);
for (i = 0; i < N_CONF_FILES; i++)
reset_config(hwt->conf_dir_file[i]);
}
static void write_key_values(FILE *ff, int nkv, const struct key_value *kv)
{
int i;
for (i = 0; i < nkv; i++) {
if (strchr(kv[i].value, ' ') == NULL &&
strchr(kv[i].value, '\"') == NULL)
fprintf(ff, "\t%s %s\n", kv[i].key, kv[i].value);
else
fprintf(ff, "\t%s \"%s\"\n", kv[i].key, kv[i].value);
}
}
static void begin_section(FILE *ff, const char *section)
{
fprintf(ff, "%s {\n", section);
}
static void end_section(FILE *ff)
{
fprintf(ff, "}\n");
}
static void write_section(FILE *ff, const char *section,
int nkv, const struct key_value *kv)
{
begin_section(ff, section);
write_key_values(ff, nkv, kv);
end_section(ff);
}
static void write_defaults(const struct hwt_state *hwt)
{
static const char bindings_name[] = "bindings";
static struct key_value defaults[] = {
{ "config_dir", NULL },
{ "bindings_file", NULL },
{ "multipath_dir", NULL },
{ "detect_prio", "no" },
{ "detect_checker", "no" },
};
char buf[sizeof(template) + sizeof(bindings_name)];
char dirbuf[PATH_MAX];
snprintf(buf, sizeof(buf), "%s/%s", hwt->tmpname, bindings_name);
defaults[0].value = hwt->dirname;
defaults[1].value = buf;
assert_ptr_not_equal(getcwd(dirbuf, sizeof(dirbuf)), NULL);
strncat(dirbuf, "/lib", sizeof(dirbuf) - 5);
defaults[2].value = dirbuf;
write_section(hwt->config_file, "defaults",
ARRAY_SIZE(defaults), defaults);
}
static void begin_config(const struct hwt_state *hwt)
{
reset_configs(hwt);
write_defaults(hwt);
}
static void begin_section_all(const struct hwt_state *hwt, const char *section)
{
int i;
begin_section(hwt->config_file, section);
for (i = 0; i < N_CONF_FILES; i++)
begin_section(hwt->conf_dir_file[i], section);
}
static void end_section_all(const struct hwt_state *hwt)
{
int i;
end_section(hwt->config_file);
for (i = 0; i < N_CONF_FILES; i++)
end_section(hwt->conf_dir_file[i]);
}
static void finish_config(const struct hwt_state *hwt)
{
int i;
fflush(hwt->config_file);
for (i = 0; i < N_CONF_FILES; i++) {
fflush(hwt->conf_dir_file[i]);
}
}
static void write_device(FILE *ff, int nkv, const struct key_value *kv)
{
write_section(ff, "device", nkv, kv);
}
/*
* Some macros to avoid boilerplate code
*/
#define CHECK_STATE(state) ({ \
assert_ptr_not_equal(state, NULL); \
assert_ptr_not_equal(*(state), NULL); \
*state; })
#define WRITE_EMPTY_CONF(hwt) do { \
begin_config(hwt); \
finish_config(hwt); \
} while (0)
#define WRITE_ONE_DEVICE(hwt, kv) do { \
begin_config(hwt); \
begin_section_all(hwt, "devices"); \
write_device(hwt->config_file, ARRAY_SIZE(kv), kv); \
end_section_all(hwt); \
finish_config(hwt); \
} while (0)
#define WRITE_TWO_DEVICES(hwt, kv1, kv2) do { \
begin_config(hwt); \
begin_section_all(hwt, "devices"); \
write_device(hwt->config_file, ARRAY_SIZE(kv1), kv1); \
write_device(hwt->config_file, ARRAY_SIZE(kv2), kv2); \
end_section_all(hwt); \
finish_config(hwt); \
} while (0)
#define WRITE_TWO_DEVICES_W_DIR(hwt, kv1, kv2) do { \
begin_config(hwt); \
begin_section_all(hwt, "devices"); \
write_device(hwt->config_file, ARRAY_SIZE(kv1), kv1); \
write_device(hwt->conf_dir_file[0], \
ARRAY_SIZE(kv2), kv2); \
end_section_all(hwt); \
finish_config(hwt); \
} while (0)
#define LOAD_CONFIG(hwt) ({ \
char buf[PATH_MAX]; \
struct config *__cf; \
\
make_config_file_path(buf, sizeof(buf), hwt, -1); \
__cf = load_config(buf); \
assert_ptr_not_equal(__cf, NULL); \
assert_ptr_not_equal(__cf->hwtable, NULL); \
__cf->verbosity = VERBOSITY; \
__cf; })
#define FREE_CONFIG(conf) do { \
free_config(conf); \
conf = NULL; \
} while (0)
static void replace_config(const struct hwt_state *hwt,
const char *conf_str)
{
FREE_CONFIG(_conf);
reset_configs(hwt);
fprintf(hwt->config_file, "%s", conf_str);
fflush(hwt->config_file);
_conf = LOAD_CONFIG(hwt);
}
#define TEST_PROP(prop, val) do { \
if (val == NULL) \
assert_ptr_equal(prop, NULL); \
else { \
assert_ptr_not_equal(prop, NULL); \
assert_string_equal(prop, val); \
} \
} while (0)
#if BROKEN
#define TEST_PROP_BROKEN(name, prop, bad, good) do { \
condlog(1, "%s: WARNING: Broken test for %s == \"%s\" on line %d, should be \"%s\"", \
__func__, name, bad ? bad : "NULL", \
__LINE__, good ? good : "NULL"); \
TEST_PROP(prop, bad); \
} while (0)
#else
#define TEST_PROP_BROKEN(name, prop, bad, good) TEST_PROP(prop, good)
#endif
/*
* Some predefined key/value pairs
*/
static const char _wwid[] = "wwid";
static const char _vendor[] = "vendor";
static const char _product[] = "product";
static const char _prio[] = "prio";
static const char _checker[] = "path_checker";
static const char _vpd_vnd[] = "vpd_vendor";
static const char _uid_attr[] = "uid_attribute";
static const char _bl_product[] = "product_blacklist";
static const char _minio[] = "rr_min_io_rq";
static const char _no_path_retry[] = "no_path_retry";
/* Device identifiers */
static const struct key_value vnd_foo = { _vendor, "foo" };
static const struct key_value prd_bar = { _product, "bar" };
static const struct key_value prd_bam = { _product, "bam" };
static const struct key_value prd_baq = { _product, "\"bar\"" };
static const struct key_value prd_baqq = { _product, "\"\"bar\"\"" };
static const struct key_value prd_barz = { _product, "barz" };
static const struct key_value vnd_boo = { _vendor, "boo" };
static const struct key_value prd_baz = { _product, "baz" };
static const struct key_value wwid_test = { _wwid, default_wwid };
/* Regular expressions */
static const struct key_value vnd__oo = { _vendor, ".oo" };
static const struct key_value vnd_t_oo = { _vendor, "^.oo" };
static const struct key_value prd_ba_ = { _product, "ba." };
static const struct key_value prd_ba_s = { _product, "(bar|baz|ba\\.)$" };
/* Pathological cases, see below */
static const struct key_value prd_barx = { _product, "ba[[rxy]" };
static const struct key_value prd_bazy = { _product, "ba[zy]" };
static const struct key_value prd_bazy1 = { _product, "ba(z|y)" };
/* Properties */
static const struct key_value prio_emc = { _prio, "emc" };
static const struct key_value prio_hds = { _prio, "hds" };
static const struct key_value prio_rdac = { _prio, "rdac" };
static const struct key_value chk_hp = { _checker, "hp_sw" };
static const struct key_value vpd_hp3par = { _vpd_vnd, "hp3par" };
static const struct key_value uid_baz = { _uid_attr, "BAZ_ATTR" };
static const struct key_value bl_bar = { _bl_product, "bar" };
static const struct key_value bl_baz = { _bl_product, "baz" };
static const struct key_value bl_barx = { _bl_product, "ba[[rxy]" };
static const struct key_value bl_bazy = { _bl_product, "ba[zy]" };
static const struct key_value minio_99 = { _minio, "99" };
static const struct key_value npr_37 = { _no_path_retry, "37" };
static const struct key_value npr_queue = { _no_path_retry, "queue" };
/***** BEGIN TESTS SECTION *****/
/*
* Dump the configuration, substitute the dumped configuration
* for the current one, and verify that the result is identical.
*/
static void replicate_config(const struct hwt_state *hwt, bool local)
{
char *cfg1, *cfg2;
vector hwtable;
struct config *conf;
condlog(3, "--- %s: replicating %s configuration", __func__,
local ? "local" : "full");
conf = get_multipath_config();
if (!local)
/* "full" configuration */
cfg1 = snprint_config(conf, NULL, NULL, NULL);
else {
/* "local" configuration */
hwtable = get_used_hwes(hwt->vecs->pathvec);
cfg1 = snprint_config(conf, NULL, hwtable, hwt->vecs->mpvec);
vector_free(hwtable);
}
assert_non_null(cfg1);
put_multipath_config(conf);
replace_config(hwt, cfg1);
/*
* The local configuration adds multipath entries, and may move device
* entries for local devices to the end of the list. Identical config
* strings therefore can't be expected in the "local" case.
* That doesn't matter. The important thing is that, with the reloaded
* configuration, the test case still passes.
*/
if (local) {
free(cfg1);
return;
}
conf = get_multipath_config();
cfg2 = snprint_config(conf, NULL, NULL, NULL);
assert_non_null(cfg2);
put_multipath_config(conf);
// #define DBG_CONFIG 1
#ifdef DBG_CONFIG
#define DUMP_CFG_STR(x) do { \
FILE *tmp = fopen("/tmp/hwtable-" #x ".txt", "w"); \
fprintf(tmp, "%s", x); \
fclose(tmp); \
} while (0)
DUMP_CFG_STR(cfg1);
DUMP_CFG_STR(cfg2);
#endif
assert_int_equal(strlen(cfg2), strlen(cfg1));
assert_string_equal(cfg2, cfg1);
free(cfg1);
free(cfg2);
}
/*
* Run hwt->test three times; once with the constructed configuration,
* once after re-reading the full dumped configuration, and once with the
* dumped local configuration.
*
* Expected: test passes every time.
*/
static void test_driver(void **state)
{
const struct hwt_state *hwt;
hwt = CHECK_STATE(state);
_conf = LOAD_CONFIG(hwt);
hwt->test(hwt);
replicate_config(hwt, false);
reset_vecs(hwt->vecs);
hwt->test(hwt);
replicate_config(hwt, true);
reset_vecs(hwt->vecs);
hwt->test(hwt);
reset_vecs(hwt->vecs);
FREE_CONFIG(_conf);
}
/*
* Sanity check for the test itself, because defaults may be changed
* in libmultipath.
*
* Our checking for match or non-match relies on the defaults being
* different from what our device sections contain.
*/
static void test_sanity_globals(void **state)
{
assert_string_not_equal(prio_emc.value, DEFAULT_PRIO);
assert_string_not_equal(prio_hds.value, DEFAULT_PRIO);
assert_string_not_equal(chk_hp.value, DEFAULT_CHECKER);
assert_int_not_equal(MULTIBUS, DEFAULT_PGPOLICY);
assert_int_not_equal(NO_PATH_RETRY_QUEUE, DEFAULT_NO_PATH_RETRY);
assert_int_not_equal(atoi(minio_99.value), DEFAULT_MINIO_RQ);
assert_int_not_equal(atoi(npr_37.value), DEFAULT_NO_PATH_RETRY);
}
/*
* Regression test for internal hwtable. NVME is an example of two entries
* in the built-in hwtable, one if which matches a subset of the other.
*/
static void test_internal_nvme(const struct hwt_state *hwt)
{
struct path *pp;
struct multipath *mp;
/*
* Generic NVMe: expect defaults for pgpolicy and no_path_retry
*/
pp = mock_path("NVME", "NoName");
mp = mock_multipath(pp);
assert_ptr_not_equal(mp, NULL);
TEST_PROP(checker_name(&pp->checker), NONE);
TEST_PROP(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE);
assert_int_equal(mp->pgpolicy, GROUP_BY_PRIO);
assert_int_equal(mp->no_path_retry, DEFAULT_NO_PATH_RETRY);
assert_int_equal(mp->retain_hwhandler, RETAIN_HWHANDLER_OFF);
/*
* NetApp NVMe: expect special values for pgpolicy and no_path_retry
*/
pp = mock_path_wwid("NVME", "NetApp ONTAP Controller",
default_wwid_1);
mp = mock_multipath(pp);
assert_ptr_not_equal(mp, NULL);
TEST_PROP(checker_name(&pp->checker), NONE);
TEST_PROP(pp->uid_attribute, "ID_WWN");
assert_int_equal(mp->pgpolicy, GROUP_BY_PRIO);
assert_int_equal(mp->no_path_retry, NO_PATH_RETRY_QUEUE);
assert_int_equal(mp->retain_hwhandler, RETAIN_HWHANDLER_OFF);
}
static int setup_internal_nvme(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
WRITE_EMPTY_CONF(hwt);
SET_TEST_FUNC(hwt, test_internal_nvme);
return 0;
}
/*
* Device section with a simple entry with double quotes ('foo:"bar"')
*/
static void test_quoted_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:"bar" matches */
pp = mock_path(vnd_foo.value, prd_baq.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
/* foo:bar doesn't match */
pp = mock_path(vnd_foo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
}
static int setup_quoted_hwe(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv[] = { vnd_foo, prd_baqq, prio_emc };
WRITE_ONE_DEVICE(hwt, kv);
SET_TEST_FUNC(hwt, test_quoted_hwe);
return 0;
}
/*
* Device section with a single simple entry ("foo:bar")
*/
static void test_string_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:bar matches */
pp = mock_path(vnd_foo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
/* foo:baz doesn't match */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
/* boo:bar doesn't match */
pp = mock_path(vnd_boo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
}
static int setup_string_hwe(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv[] = { vnd_foo, prd_bar, prio_emc };
WRITE_ONE_DEVICE(hwt, kv);
SET_TEST_FUNC(hwt, test_string_hwe);
return 0;
}
/*
* Device section with a broken entry (no product)
* It should be ignored.
*/
static void test_broken_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:bar doesn't match, as hwentry is ignored */
pp = mock_path(vnd_foo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
/* boo:bar doesn't match */
pp = mock_path(vnd_boo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
}
static int setup_broken_hwe(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv[] = { vnd_foo, prio_emc };
WRITE_ONE_DEVICE(hwt, kv);
SET_TEST_FUNC(hwt, test_broken_hwe);
return 0;
}
/*
* Like test_broken_hwe, but in config_dir file.
*/
static int setup_broken_hwe_dir(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv[] = { vnd_foo, prio_emc };
begin_config(hwt);
begin_section_all(hwt, "devices");
write_device(hwt->conf_dir_file[0], ARRAY_SIZE(kv), kv);
end_section_all(hwt);
finish_config(hwt);
hwt->test = test_broken_hwe;
hwt->test_name = "test_broken_hwe_dir";
return 0;
}
/*
* Device section with a single regex entry ("^.foo:(bar|baz|ba\.)$")
*/
static void test_regex_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:bar matches */
pp = mock_path(vnd_foo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
/* foo:baz matches */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
/* boo:baz matches */
pp = mock_path(vnd_boo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
/* foo:BAR doesn't match */
pp = mock_path(vnd_foo.value, "BAR");
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
/* bboo:bar doesn't match */
pp = mock_path("bboo", prd_bar.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
}
static int setup_regex_hwe(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv[] = { vnd_t_oo, prd_ba_s, prio_emc };
WRITE_ONE_DEVICE(hwt, kv);
SET_TEST_FUNC(hwt, test_regex_hwe);
return 0;
}
/*
* Two device entries, kv1 is a regex match ("^.foo:(bar|baz|ba\.)$"),
* kv2 a string match (foo:bar) which matches a subset of the regex.
* Both are added to the main config file.
*
* Expected: Devices matching both get properties from both, kv2 taking
* precedence. Devices matching kv1 only just get props from kv1.
*/
static void test_regex_string_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:baz matches kv1 */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* boo:baz matches kv1 */
pp = mock_path(vnd_boo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .oo:ba. matches kv1 */
pp = mock_path(vnd__oo.value, prd_ba_.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .foo:(bar|baz|ba\.) doesn't match */
pp = mock_path(vnd__oo.value, prd_ba_s.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), DEFAULT_CHECKER);
/* foo:bar matches kv2 and kv1 */
pp = mock_path_flags(vnd_foo.value, prd_bar.value, USE_VPD_VND);
TEST_PROP(prio_name(&pp->prio), prio_hds.value);
assert_int_equal(pp->vpd_vendor_id, VPD_VP_HP3PAR);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
}
static int setup_regex_string_hwe(void **state)
{
struct hwt_state *hwt = CHECK_STATE(state);
const struct key_value kv1[] = { vnd_t_oo, prd_ba_s, prio_emc, chk_hp };
const struct key_value kv2[] = { vnd_foo, prd_bar, prio_hds, vpd_hp3par };
WRITE_TWO_DEVICES(hwt, kv1, kv2);
SET_TEST_FUNC(hwt, test_regex_string_hwe);
return 0;
}
/*
* Two device entries, kv1 is a regex match ("^.foo:(bar|baz|ba\.)$"),
* kv2 a string match (foo:bar) which matches a subset of the regex.
* kv1 is added to the main config file, kv2 to a config_dir file.
* This case is more important as you may think, because it's equivalent
* to kv1 being in the built-in hwtable and kv2 in multipath.conf.
*
* Expected: Devices matching kv2 (and thus, both) get properties
* from both, kv2 taking precedence.
* Devices matching kv1 only just get props from kv1.
*/
static void test_regex_string_hwe_dir(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:baz matches kv1 */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* boo:baz matches kv1 */
pp = mock_path(vnd_boo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .oo:ba. matches kv1 */
pp = mock_path(vnd__oo.value, prd_ba_.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .oo:(bar|baz|ba\.)$ doesn't match */
pp = mock_path(vnd__oo.value, prd_ba_s.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), DEFAULT_CHECKER);
/* foo:bar matches kv2 */
pp = mock_path_flags(vnd_foo.value, prd_bar.value, USE_VPD_VND);
/* Later match takes prio */
TEST_PROP(prio_name(&pp->prio), prio_hds.value);
assert_int_equal(pp->vpd_vendor_id, VPD_VP_HP3PAR);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
}
static int setup_regex_string_hwe_dir(void **state)
{
const struct key_value kv1[] = { vnd_t_oo, prd_ba_s, prio_emc, chk_hp };
const struct key_value kv2[] = { vnd_foo, prd_bar, prio_hds, vpd_hp3par };
struct hwt_state *hwt = CHECK_STATE(state);
WRITE_TWO_DEVICES_W_DIR(hwt, kv1, kv2);
SET_TEST_FUNC(hwt, test_regex_string_hwe_dir);
return 0;
}
/*
* Three device entries, kv1 is a regex match and kv2 and kv3 string
* matches, where kv3 is a substring of kv2. All in different config
* files.
*
* Expected: Devices matching kv3 get props from all, devices matching
* kv2 from kv2 and kv1, and devices matching kv1 only just from kv1.
*/
static void test_regex_2_strings_hwe_dir(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:baz matches kv1 */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* boo:baz doesn't match */
pp = mock_path(vnd_boo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE);
TEST_PROP(checker_name(&pp->checker), DEFAULT_CHECKER);
/* foo:bar matches kv2 and kv1 */
pp = mock_path(vnd_foo.value, prd_bar.value);
TEST_PROP(prio_name(&pp->prio), prio_hds.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(pp->uid_attribute, uid_baz.value);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* foo:barz matches kv3 and kv2 and kv1 */
pp = mock_path_flags(vnd_foo.value, prd_barz.value, USE_VPD_VND);
TEST_PROP(prio_name(&pp->prio), prio_rdac.value);
assert_int_equal(pp->vpd_vendor_id, VPD_VP_HP3PAR);
TEST_PROP(pp->uid_attribute, uid_baz.value);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
}
static int setup_regex_2_strings_hwe_dir(void **state)
{
const struct key_value kv1[] = { vnd_foo, prd_ba_, prio_emc, chk_hp };
const struct key_value kv2[] = { vnd_foo, prd_bar, prio_hds, uid_baz };
const struct key_value kv3[] = { vnd_foo, prd_barz,
prio_rdac, vpd_hp3par };
struct hwt_state *hwt = CHECK_STATE(state);
begin_config(hwt);
begin_section_all(hwt, "devices");
write_device(hwt->config_file, ARRAY_SIZE(kv1), kv1);
write_device(hwt->conf_dir_file[0], ARRAY_SIZE(kv2), kv2);
write_device(hwt->conf_dir_file[1], ARRAY_SIZE(kv3), kv3);
end_section_all(hwt);
finish_config(hwt);
SET_TEST_FUNC(hwt, test_regex_2_strings_hwe_dir);
return 0;
}
/*
* Like test_regex_string_hwe_dir, but the order of kv1 and kv2 is exchanged.
*
* Expected: Devices matching kv1 (and thus, both) get properties
* from both, kv1 taking precedence.
* Devices matching kv1 only just get props from kv1.
*/
static void test_string_regex_hwe_dir(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:bar matches kv2 and kv1 */
pp = mock_path_flags(vnd_foo.value, prd_bar.value, USE_VPD_VND);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, VPD_VP_HP3PAR);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* foo:baz matches kv1 */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* boo:baz matches kv1 */
pp = mock_path(vnd_boo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .oo:ba. matches kv1 */
pp = mock_path(vnd__oo.value, prd_ba_.value);
TEST_PROP(prio_name(&pp->prio), prio_emc.value);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
/* .oo:(bar|baz|ba\.)$ doesn't match */
pp = mock_path(vnd__oo.value, prd_ba_s.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), DEFAULT_CHECKER);
}
static int setup_string_regex_hwe_dir(void **state)
{
const struct key_value kv1[] = { vnd_t_oo, prd_ba_s, prio_emc, chk_hp };
const struct key_value kv2[] = { vnd_foo, prd_bar, prio_hds, vpd_hp3par };
struct hwt_state *hwt = CHECK_STATE(state);
WRITE_TWO_DEVICES_W_DIR(hwt, kv2, kv1);
SET_TEST_FUNC(hwt, test_string_regex_hwe_dir);
return 0;
}
/*
* Two identical device entries kv1 and kv2, trivial regex ("string").
* Both are added to the main config file.
* These entries are NOT merged.
* This could happen in a large multipath.conf file.
*
* Expected: matching devices get props from both, kv2 taking precedence.
*/
static void test_2_ident_strings_hwe(const struct hwt_state *hwt)
{
struct path *pp;
/* foo:baz doesn't match */
pp = mock_path(vnd_foo.value, prd_baz.value);
TEST_PROP(prio_name(&pp->prio), DEFAULT_PRIO);
assert_int_equal(pp->vpd_vendor_id, 0);
TEST_PROP(checker_name(&pp->checker), DEFAULT_CHECKER);
/* foo:bar matches both */
pp = mock_path_flags(vnd_foo.value, prd_bar.value, USE_VPD_VND);
TEST_PROP(prio_name(&pp->prio), prio_hds.value);
assert_int_equal(pp->vpd_vendor_id, VPD_VP_HP3PAR);
TEST_PROP(checker_name(&pp->checker), chk_hp.value);
}
static int setup_2_ident_strings_hwe(void **state)
{
const struct key_value kv1[] = { vnd_foo, prd_bar, prio_emc, chk_hp };
const struct key_value kv2[] = { vnd_foo, prd_bar, prio_hds, vpd_hp3par };
struct hwt_state *hwt = CHECK_STATE(state);
WRITE_TWO_DEVICES(hwt, kv1, kv2);
SET_TEST_FUNC(hwt, test_2_ident_strings_hwe);