Skip to content

Commit f39802f

Browse files
dwsuseigaw
authored andcommitted
test: add hostnqn lookup test
There are several places where we lookup hosnqn/hostid so ensure this code works as expected. Signed-off-by: Daniel Wagner <[email protected]>
1 parent c54a616 commit f39802f

5 files changed

Lines changed: 233 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[
2+
{
3+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:2cd2c43b-a90a-45c1-a8cd-86b33ab273b5",
4+
"hostid":"2cd2c43b-a90a-45c1-a8cd-86b33ab273b5",
5+
"subsystems":[
6+
{
7+
"nqn":"nqn.io-1",
8+
"ports":[
9+
{
10+
"transport":"tcp",
11+
"traddr":"192.168.154.144",
12+
"trsvcid":"4420",
13+
"dhchap_key":"none"
14+
},
15+
{
16+
"transport":"tcp",
17+
"traddr":"192.168.154.144",
18+
"trsvcid":"4421",
19+
"dhchap_key":"none"
20+
}
21+
]
22+
}
23+
]
24+
},
25+
{
26+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:befdec4c-2234-11b2-a85c-ca77c773af36",
27+
"hostid":"befdec4c-2234-11b2-a85c-ca77c773af36",
28+
"subsystems":[
29+
{
30+
"nqn":"nqn.io-1",
31+
"ports":[
32+
{
33+
"transport":"tcp",
34+
"traddr":"192.168.154.144",
35+
"trsvcid":"4420",
36+
"dhchap_key":"none"
37+
},
38+
{
39+
"transport":"tcp",
40+
"traddr":"192.168.154.144",
41+
"trsvcid":"4421",
42+
"dhchap_key":"none"
43+
}
44+
]
45+
}
46+
]
47+
}
48+
]

test/config/data/hostnqn-order.out

Whitespace-only changes.
11.1 KB
Binary file not shown.

test/config/hostnqn-order.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/**
3+
* This file is part of libnvme.
4+
* Copyright (c) 2024 Daniel Wagner, SUSE LLC
5+
*/
6+
7+
#include <string.h>
8+
#include <stdbool.h>
9+
#include <stdlib.h>
10+
#include <errno.h>
11+
12+
#include <libnvme.h>
13+
14+
static bool command_line(void)
15+
{
16+
bool pass = false;
17+
nvme_root_t r;
18+
int err;
19+
char *hostnqn, *hostid, *hnqn, *hid;
20+
21+
r = nvme_create_root(stderr, LOG_ERR);
22+
if (!r)
23+
return false;
24+
25+
err = nvme_scan_topology(r, NULL, NULL);
26+
if (err) {
27+
if (errno != ENOENT)
28+
goto out;
29+
}
30+
31+
hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
32+
hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";
33+
34+
err = nvme_host_get_ids(r, hostnqn, hostid, &hnqn, &hid);
35+
if (err)
36+
goto out;
37+
38+
if (strcmp(hostnqn, hnqn)) {
39+
printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
40+
goto out;
41+
}
42+
if (strcmp(hostid, hid)) {
43+
printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
44+
goto out;
45+
}
46+
47+
free(hnqn);
48+
free(hid);
49+
50+
pass = true;
51+
52+
out:
53+
nvme_free_tree(r);
54+
return pass;
55+
}
56+
57+
static bool json_config(char *file)
58+
{
59+
bool pass = false;
60+
nvme_root_t r;
61+
int err;
62+
char *hostnqn, *hostid, *hnqn, *hid;
63+
64+
setenv("LIBNVME_HOSTNQN", "", 1);
65+
setenv("LIBNVME_HOSTID", "", 1);
66+
67+
r = nvme_create_root(stderr, LOG_ERR);
68+
if (!r)
69+
return false;
70+
71+
/* We need to read the config in before we scan */
72+
err = nvme_read_config(r, file);
73+
if (err)
74+
goto out;
75+
76+
err = nvme_scan_topology(r, NULL, NULL);
77+
if (err) {
78+
if (errno != ENOENT)
79+
goto out;
80+
}
81+
82+
hostnqn = "nqn.2014-08.org.nvmexpress:uuid:2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";
83+
hostid = "2cd2c43b-a90a-45c1-a8cd-86b33ab273b5";
84+
85+
err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
86+
if (err)
87+
goto out;
88+
89+
if (strcmp(hostnqn, hnqn)) {
90+
printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
91+
goto out;
92+
}
93+
if (strcmp(hostid, hid)) {
94+
printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
95+
goto out;
96+
}
97+
98+
free(hnqn);
99+
free(hid);
100+
101+
pass = true;
102+
103+
out:
104+
nvme_free_tree(r);
105+
return pass;
106+
}
107+
108+
static bool from_file(void)
109+
{
110+
bool pass = false;
111+
nvme_root_t r;
112+
int err;
113+
char *hostnqn, *hostid, *hnqn, *hid;
114+
115+
hostnqn = "nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6";
116+
hostid = "ce4fee3e-c02c-11ee-8442-830d068a36c6";
117+
118+
setenv("LIBNVME_HOSTNQN", hostnqn, 1);
119+
setenv("LIBNVME_HOSTID", hostid, 1);
120+
121+
r = nvme_create_root(stderr, LOG_ERR);
122+
if (!r)
123+
return false;
124+
125+
err = nvme_scan_topology(r, NULL, NULL);
126+
if (err) {
127+
if (errno != ENOENT)
128+
goto out;
129+
}
130+
131+
err = nvme_host_get_ids(r, NULL, NULL, &hnqn, &hid);
132+
if (err)
133+
goto out;
134+
135+
if (strcmp(hostnqn, hnqn)) {
136+
printf("json config hostnqn '%s' does not match '%s'\n", hostnqn, hnqn);
137+
goto out;
138+
}
139+
if (strcmp(hostid, hid)) {
140+
printf("json config hostid '%s' does not match '%s'\n", hostid, hid);
141+
goto out;
142+
}
143+
144+
free(hnqn);
145+
free(hid);
146+
147+
pass = true;
148+
149+
out:
150+
nvme_free_tree(r);
151+
return pass;
152+
}
153+
154+
int main(int argc, char *argv[])
155+
{
156+
bool pass;
157+
158+
pass = command_line();
159+
pass &= json_config(argv[1]);
160+
pass &= from_file();
161+
fflush(stdout);
162+
163+
exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
164+
}

test/config/meson.build

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ if diff.found()
3535
depends : config_dump,
3636
)
3737
endforeach
38+
39+
test_hostnqn_order = executable(
40+
'test-hostnqn-order',
41+
['hostnqn-order.c'],
42+
dependencies: libnvme_dep,
43+
include_directories: [incdir],
44+
)
45+
46+
test(
47+
'hostnqn-order',
48+
config_diff,
49+
args : [
50+
meson.current_build_dir(),
51+
test_hostnqn_order.full_path(),
52+
files('data/hostnqn-order.tar.xz'),
53+
files('data/hostnqn-order.json'),
54+
files('data/hostnqn-order.out'),
55+
],
56+
depends : test_hostnqn_order,
57+
)
58+
3859
endif

0 commit comments

Comments
 (0)