Skip to content

Commit 154c374

Browse files
dwsuseigaw
authored andcommitted
test: read and dump sysfs tar file
The library is able to scan the topology from a simple copy of sysfs. There is no need to do any IOCTL, thus we can start test to tree code. This provides just the basic building blocks. Signed-off-by: Daniel Wagner <[email protected]>
1 parent dfb421a commit 154c374

7 files changed

Lines changed: 187 additions & 0 deletions

File tree

scripts/collect-sysfs.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
filename=nvme-sysfs-$(hostname)-$(uname -r).tar.xz
5+
6+
declare -a dirs=(
7+
"/sys/class/nvme"
8+
"/sys/class/nvme-fabrics"
9+
"/sys/class/nvme-generic"
10+
"/sys/class/nvme-subsystem"
11+
"/sys/bus/pci/slots"
12+
)
13+
14+
files=""
15+
for d in "${dirs[@]}"; do
16+
files+="${d} "
17+
for l in "${d}"/*; do
18+
files+="$(readlink -f $l) "
19+
done
20+
done
21+
22+
tar -c -J -p -f "${filename}" ${files} 2> /dev/null

test/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ endif
9797

9898
subdir('ioctl')
9999
subdir('nbft')
100+
101+
if json_c_dep.found()
102+
subdir('sysfs')
103+
endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"hosts":[
3+
{
4+
"hostnqn":"nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6",
5+
"hostid":"ce4fee3e-c02c-11ee-8442-830d068a36c6",
6+
"subsystems":[
7+
{
8+
"name":"nvme-subsys1",
9+
"nqn":"nqn.2019-08.org.qemu:nvme-0",
10+
"controllers":[
11+
{
12+
"name":"nvme1",
13+
"transport":"pcie",
14+
"traddr":"0000:00:05.0"
15+
}
16+
]
17+
},
18+
{
19+
"name":"nvme-subsys0",
20+
"nqn":"nqn.2019-08.org.qemu:subsys1",
21+
"controllers":[
22+
{
23+
"name":"nvme0",
24+
"transport":"pcie",
25+
"traddr":"0000:0f:00.0"
26+
}
27+
]
28+
}
29+
]
30+
}
31+
]
32+
}
19.3 KB
Binary file not shown.

test/sysfs/meson.build

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-License-Identifier: LGPL-2.1-or-later
2+
#
3+
# This file is part of libnvme.
4+
# Copyright (c) 2024 SUSE LLC.
5+
#
6+
# Authors: Daniel Wagner <[email protected]>
7+
8+
9+
sysfs = executable(
10+
'test-sysfs',
11+
['sysfs.c'],
12+
dependencies: libnvme_dep,
13+
include_directories: [incdir, internal_incdir]
14+
)
15+
16+
sysfs_files= [
17+
'nvme-sysfs-tw-carbon-6.8.0-rc1+'
18+
]
19+
20+
setup = find_program('setup.sh')
21+
22+
foreach t_file : sysfs_files
23+
r = run_command(setup, files('data'/t_file + '.tar.xz'), meson.current_build_dir(), check: true)
24+
i = r.stdout().strip()
25+
e0 = 'LIBNVME_SYSFS_PATH=' + i
26+
e1 = 'LIBNVME_HOSTNQN=nqn.2014-08.org.nvmexpress:uuid:ce4fee3e-c02c-11ee-8442-830d068a36c6'
27+
e2 = 'LIBNVME_HOSTID=ce4fee3e-c02c-11ee-8442-830d068a36c6'
28+
test('sysfs', sysfs, args : [ i, t_file + '.out', files('data'/t_file + '.out') ], env : [ e0, e1, e2 ])
29+
endforeach

test/sysfs/setup.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
TARFILE=$1
5+
BASEDIR=$2
6+
TESTDIR="$BASEDIR/$(basename -s .tar.xz ${TARFILE})"
7+
8+
mkdir -p "${TESTDIR}"
9+
tar -x -f "${TARFILE}" -C "${TESTDIR}" || exit 1
10+
11+
echo "${TESTDIR}"

test/sysfs/sysfs.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 "nvme/tree.h"
8+
#include <assert.h>
9+
#include <errno.h>
10+
#include <string.h>
11+
#include <stdbool.h>
12+
#include <stdlib.h>
13+
#include <arpa/inet.h>
14+
15+
#include <ccan/array_size/array_size.h>
16+
17+
#include <libnvme.h>
18+
#include <nvme/private.h>
19+
20+
static bool test_sysfs(const char *path, const char *filename)
21+
{
22+
FILE *f;
23+
nvme_root_t r;
24+
int err;
25+
26+
f = fopen(filename, "w");
27+
if (!f)
28+
return false;
29+
30+
r = nvme_create_root(f, LOG_ERR);
31+
assert(r);
32+
33+
err = nvme_scan_topology(r, NULL, NULL);
34+
if (!err)
35+
nvme_dump_tree(r);
36+
fprintf(f, "\n");
37+
38+
nvme_free_tree(r);
39+
fclose(f);
40+
41+
return err == 0;
42+
}
43+
44+
static bool compare_content(const char *filename1, const char *filename2)
45+
{
46+
FILE *f1, *f2;
47+
char c1, c2;
48+
bool pass = false;
49+
50+
f1 = fopen(filename1, "r");
51+
if (!f1)
52+
return false;
53+
54+
f2 = fopen(filename2, "r");
55+
if (!f2) {
56+
fclose(f1);
57+
return false;
58+
}
59+
60+
do {
61+
c1 = getc(f1);
62+
c2 = getc(f2);
63+
if (c1 != c2)
64+
goto out;
65+
} while (c1 != EOF || c2 != EOF);
66+
67+
if (c1 == c2)
68+
pass = true;
69+
out:
70+
fclose(f1);
71+
fclose(f2);
72+
73+
return pass;
74+
}
75+
76+
int main(int argc, char *argv[])
77+
{
78+
bool pass = true;
79+
80+
if (argc < 4) {
81+
fprintf(stderr, "usage: test-sysfs SYSFS_DIR OUTPUT_FILE COMPARE_FILE\n");
82+
return EXIT_FAILURE;
83+
}
84+
85+
pass &= test_sysfs(argv[1], argv[2]);
86+
pass &= compare_content(argv[2], argv[3]);
87+
88+
exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
89+
}

0 commit comments

Comments
 (0)