diff --git a/tests/cli/netapp/meson.build b/tests/cli/netapp/meson.build index 0e37b2e5b4..0352089f47 100644 --- a/tests/cli/netapp/meson.build +++ b/tests/cli/netapp/meson.build @@ -8,4 +8,12 @@ if have_mock_nvme env: cli_test_env, depends: nvme_runtime + [mock_nvme_lib], suite: 'mock') + + test('netapp - device-info', + python3, + args: [files('netapp_device_info_mock_test.py'), nvme_exe, + mock_nvme_lib], + env: cli_test_env, + depends: nvme_runtime + [mock_nvme_lib], + suite: 'mock') endif diff --git a/tests/cli/netapp/netapp_device_info_mock_test.py b/tests/cli/netapp/netapp_device_info_mock_test.py new file mode 100644 index 0000000000..043f6a26b2 --- /dev/null +++ b/tests/cli/netapp/netapp_device_info_mock_test.py @@ -0,0 +1,368 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +"""Tests that the NetApp commands decode device data correctly. + +These exercise the smdevices/ontapdevices printers end-to-end with +realistic vendor-specific Identify data and a proper ONTAP C2 nsinfo +log, so the label and TLV parsing is covered, not just reached. + +Usage: python3 netapp_device_info_mock_test.py +""" + +import errno +import json +import struct +import uuid + +from tests.cli.nvme_mock import (CNS_CTRL, CNS_NS, CNS_NS_DESC_LIST, + HAVE_JSON, ID_CTRL_SIZE, NVME_IOCTL_ID, + SC_INTERNAL, MockTestCase, Reply, id_ctrl, + id_ns, main) + + +_DEVICE = '/dev/nvme0n1' +_NSID = 7 +_ONTAP_LOG_ID = 0xc2 +_ONTAP_C2_LOG_SIZE = 4096 + +# Offsets of the vendor-specific areas the NetApp plugin reads. +_CTRL_VS = 3072 # nvme_id_ctrl.vs +_NS_VS = 384 # nvme_id_ns.vs +_NGUID_OFF = 104 # nvme_id_ns.nguid +_SUBNQN_OFFSET = 768 +_SUBNQN_SIZE = 256 + +# The nsinfo log layout the plugin parses. +_LSP_OFFSET = 16 # low nibble holds the log specific field +_LSP_NSINFO = 0x1 +_TLV_START = 32 +_TLV_VSERVER = 0x11 +_TLV_VOLUME = 0x12 +_TLV_NS_NAME = 0x13 +_TLV_NS_PATH = 0x14 + +_UUID = bytes(range(16)) +_UUID_STR = str(uuid.UUID(bytes=_UUID)) # 00010203-0405-...-0e0f +_NGUID = bytes(range(0x10, 0x20)) +_NGUID_STR = _NGUID.hex() # lower case, no separators + + +def _ucs2(text): + """The plugin squashes UCS-2 labels to ASCII by taking every second + byte (src[i * 2 + 1]), so encode the ASCII into those odd bytes.""" + out = bytearray() + for byte in text.encode('ascii'): + out += bytes((0, byte)) + return bytes(out) + + +def sm_controller(array_name, primary): + """An E-Series Identify Controller: the array label lives in vs[20], + and bit 0 of vs[0] is the single-LUN-target-access (A vs B) flag.""" + data = id_ctrl(mn='NetApp E-Series', fr='TESTFW') + data[_CTRL_VS] = 0x1 if primary else 0x0 + label = _ucs2(array_name) + data[_CTRL_VS + 20:_CTRL_VS + 20 + len(label)] = label + return data + + +def sm_namespace(volume_name): + """An Identify Namespace with the volume label in vs and an NGUID.""" + data = id_ns(lbaf=[(0, 12, 0)], nsze=100, nuse=25) + label = _ucs2(volume_name) + data[_NS_VS:_NS_VS + len(label)] = label + data[_NGUID_OFF:_NGUID_OFF + 16] = _NGUID + return data + + +def ontap_controller(subnqn=b'nqn.2014-08.org.nvmexpress:mock.netapp-subsystem'): + """An ONTAP Identify Controller with a subsystem NQN to parse.""" + data = id_ctrl(mn='NetApp ONTAP Controller', fr='TESTFW') + data[_SUBNQN_OFFSET:_SUBNQN_OFFSET + _SUBNQN_SIZE] = \ + subnqn.ljust(_SUBNQN_SIZE, b' ') + return data + + +def ns_desc_list(): + """An Identify Namespace descriptor list; the plugin copies the 16 + bytes right after the first descriptor header as the UUID.""" + buf = bytearray(ID_CTRL_SIZE) + buf[0] = 0x03 # NVME_NIDT_UUID + buf[1] = 16 # NIDL + buf[4:4 + 16] = _UUID + return buf + + +def _tlv(tlv_type, value): + """One nsinfo TLV: type, reserved, little-endian u16 length in units + of four bytes, then the value. The value is NUL padded to a multiple + of four; a NUL also terminates the decoded label.""" + data = bytearray(value.encode('ascii')) + data += b'\x00' * (-len(data) % 4) + return bytes((tlv_type, 0)) + struct.pack('