#!/usr/bin/perl
#
# (C) Copyright IBM Corp. 2004
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Script for testing assign, create, and delete of BBR segments.
#
# Test1: Assign the BBR plugin to the disk.
# Test2: Unassign the BBR plugin from the disk.

use strict;
use warnings;

use Evms::Common;
use Evms::Log;
use Evms::Object;
use Evms::BBR;
use Evms::DM;

# Test1
# Assign BBR to the disk.
sub Test1
{
	my $disk = $_[0];
	my @query_output;
	my $segment = $disk . "_bbr";
	my $rc;

	log_info("Test assigning the BBR plugin to a disk.\n");

	log_info("1. Assigning BBR plugin to disk $disk.\n");
	$rc = assign_bbr_plugin($disk);
	if ($rc) {
		log_error("Error assigning BBR plugin to disk $disk.\n");
		goto out;
	}

	log_info("2. Verifying assignment of plugin to disk $disk.\n");

	@query_output = query_object($segment);
	if (@query_output == 0) {
		log_error("Error getting details for $segment.\n");
		$rc = 1;
		goto out;
	}

out:
	log_result($rc);
	return $rc;
}

# Test2
# Unassign BBR from the disk.
sub Test2
{
	my $disk = $_[0];
	my $segment = $disk . "_bbr";
	my @query_output;
	my $rc;

	log_info("Testing unassigning the BBR plugin from a disk.\n");

	log_info("1. Removing the BBR plugin from disk $disk.\n");

	$rc = unassign_bbr_plugin($disk);
	if ($rc) {
		log_error("Error removing BBR plugin from disk $disk.\n");
		goto out;
	}

	log_info("2. Verifying removal of BBR plugin.\n");

	@query_output = query_object($segment);
	if (@query_output != 0) {
		log_error("Error: BBR plugin was not removed from disk $disk.\n");
		$rc = 1;
		goto out;
	}

out:
	log_result($rc);
	return $rc;
}

MAIN:
{
	my $disk;
	my $rc;

	# Only use the first disk specified.
	$disk = $ARGV[0];
	$disk || die("USAGE: $0 <disk>\n");

	# Check for minimum-sized disk.
	$rc = check_minimum_object_size($disk, "1GB");
	if ($rc) {
		die("Disk $disk isn't large enough for this test.\n");
	}

	# No setup required, as long as $disk is already clean.

	$rc = Test1($disk);
	if ($rc) {
		goto finish;
	}

	$rc = Test2($disk);
	if ($rc) {
		goto finish;
	}

finish:
}

