diff --git a/tests/viriscsitest.c b/tests/viriscsitest.c index f460124017..65de954ef2 100644 --- a/tests/viriscsitest.c +++ b/tests/viriscsitest.c @@ -51,6 +51,14 @@ static const char *iscsiadmSessionOutputNonFlash = "tcp: [6] 10.20.30.44:3260,1 iqn.2008-04.example:example1:iscsi.bar (non-flash)\n" "tcp: [7] 10.20.30.45:3260,1 iqn.2009-04.example:example1:iscsi.seven (non-flash)\n"; +const char *iscsiadmSendtargetsOutput = + "10.20.30.40:3260,1 iqn.2004-06.example:example1:iscsi.test\n" + "10.20.30.40:3260,1 iqn.2005-05.example:example1:iscsi.hello\n" + "10.20.30.40:3260,1 iqn.2006-04.example:example1:iscsi.world\n" + "10.20.30.40:3260,1 iqn.2007-04.example:example1:iscsi.foo\n" + "10.20.30.40:3260,1 iqn.2008-04.example:example1:iscsi.bar\n" + "10.20.30.40:3260,1 iqn.2009-04.example:example1:iscsi.seven\n"; + struct testSessionInfo { const char *device_path; int output_version; @@ -74,6 +82,15 @@ static void testIscsiadmCb(const char *const*args, ignore_value(VIR_STRDUP(*output, iscsiadmSessionOutputNonFlash)); else ignore_value(VIR_STRDUP(*output, iscsiadmSessionOutput)); + } else if (args[0] && STREQ(args[0], ISCSIADM) && + args[1] && STREQ(args[1], "--mode") && + args[2] && STREQ(args[2], "discovery") && + args[3] && STREQ(args[3], "--type") && + args[4] && STREQ(args[4], "sendtargets") && + args[5] && STREQ(args[5], "--portal") && + args[6] && STREQ(args[6], "10.20.30.40:3260,1") && + args[7] == NULL) { + ignore_value(VIR_STRDUP(*output, iscsiadmSendtargetsOutput)); } else { *status = -1; } @@ -107,6 +124,54 @@ cleanup: return ret; } +struct testScanTargetsInfo { + const char *fake_cmd_output; + const char *portal; + const char **expected_targets; + size_t nexpected; +}; + +static int +testISCSIScanTargets(const void *data) +{ + const struct testScanTargetsInfo *info = data; + size_t ntargets = 0; + char **targets = NULL; + int ret = -1; + size_t i; + + virCommandSetDryRun(NULL, testIscsiadmCb, NULL); + + if (virISCSIScanTargets(info->portal, NULL, + &ntargets, &targets) < 0) + goto cleanup; + + if (info->nexpected != ntargets) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected %zu targets, got %zu", + info->nexpected, ntargets); + goto cleanup; + } + + for (i = 0; i < ntargets; i++) { + if (STRNEQ(info->expected_targets[i], targets[i])) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected target '%s', got '%s'", + info->expected_targets[i], targets[i]); + goto cleanup; + } + } + + ret = 0; + +cleanup: + virCommandSetDryRun(NULL, NULL, NULL); + for (i = 0; i < ntargets; i++) + VIR_FREE(targets[i]); + VIR_FREE(targets); + return ret; +} + static int mymain(void) { @@ -128,6 +193,23 @@ mymain(void) DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.seven", "7"); DO_SESSION_TEST("iqn.2009-04.example:example1:iscsi.eight", NULL); + const char *targets[] = { + "iqn.2004-06.example:example1:iscsi.test", + "iqn.2005-05.example:example1:iscsi.hello", + "iqn.2006-04.example:example1:iscsi.world", + "iqn.2007-04.example:example1:iscsi.foo", + "iqn.2008-04.example:example1:iscsi.bar", + "iqn.2009-04.example:example1:iscsi.seven" + }; + struct testScanTargetsInfo infoTargets = { + .fake_cmd_output = "iscsiadm_sendtargets", + .portal = "10.20.30.40:3260,1", + .expected_targets = targets, + .nexpected = ARRAY_CARDINALITY(targets), + }; + if (virtTestRun("ISCSI scan targets", testISCSIScanTargets, &infoTargets) < 0) + rv = -1; + if (rv < 0) return EXIT_FAILURE; return EXIT_SUCCESS;