Files
libiscsi/test-tool/1130_persistent_reserve_simple.c
2012-12-22 14:12:10 -08:00

110 lines
2.8 KiB
C

/*
Copyright (C) 2013 by Lee Duncan <leeman.duncan@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
#include "iscsi-test.h"
int T1130_persistent_reserve_simple(const char *initiator,
const char *url, int data_loss, int show_info)
{
struct iscsi_context *iscsi;
int ret;
int lun;
const unsigned long long key = rand_key();
struct resvn_type_info *rtip;
printf("1130_persistent_reserve_simple:\n");
printf("=========================================\n");
if (show_info) {
int idx = 1;
printf("Test that we can using each type of Persistent Reservation,\n");
printf(" and that we can release each type, as well\n");
printf("%d, We can register a key\n", idx++);
for (rtip = reservation_types;
rtip->pr_type_str != NULL;
rtip++) {
printf("%d, Can register %s\n", idx++,
rtip->pr_type_str);
printf("%d, Can read reservation\n", idx++);
printf("%d, Can release reservation\n", idx++);
}
printf("%d, Can unregister\n", idx++);
return 0;
}
iscsi = iscsi_context_login(initiator, url, &lun);
if (iscsi == NULL) {
printf("Failed to login to target\n");
return -1;
}
if (!data_loss) {
printf("--dataloss flag is not set. Skipping test\n");
ret = -2;
goto finished;
}
ret = 0;
/* register our reservation key with the target */
ret = register_and_ignore(iscsi, lun, key);
if (ret != 0)
goto finished;
/* test each reservatoin type */
for (rtip = reservation_types;
rtip->pr_type_str != NULL;
rtip++) {
/* reserve the target */
ret = reserve(iscsi, lun, key, rtip);
if (ret != 0)
goto finished;
/* verify target reservation */
ret = verify_reserved_as(iscsi, lun,
pr_type_is_all_registrants(rtip->pr_type) ? 0 : key,
rtip);
if (ret != 0)
goto finished;
/* release our reservation */
ret = release(iscsi, lun, key, rtip);
if (ret != 0)
goto finished;
}
/* remove our key from the target */
ret = register_key(iscsi, lun, 0, key);
if (ret != 0)
goto finished;
finished:
/* XXX should we clean up key if needed? */
iscsi_logout_sync(iscsi);
iscsi_destroy_context(iscsi);
return ret;
}