#!/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
#
# LVM2 script for testing region and container renames.
#
# Setup: Create one 200MB disk segment, and create an LVM2 container from
#        this segment. Create two regions in this container.
# Test1: Check that the regions exist in the kernel. Then rename one of the
#        regions. Check that the region was renamed in the kernel.
# Test2: Rename the container. Check that both regions were renamed in the
#        kernel.

use strict;
use warnings;

use Evms::Common;
use Evms::Log;
use Evms::Object;
use Evms::DM;
use Evms::Dos;
use Evms::Lvm2;

my $container_name = "News";
my $container_name_new = "Network";
my $region1_name = "ABC";
my $region2_name = "CBS";
my $region1_name_new = "NBC";
my $container_pe_size = "1MB";

# Setup
# Create an LVM2 container with one DOS segment.
sub Setup
{
	my $disk = $_[0];
	my %options;
	my @pvs;
	my $rc;

	log_info("1. Creating one DOS segments.\n");

	$rc = create_dos_segments($disk, 1, "200MB");
	if ($rc) {
		log_error("Error creating DOS segments.\n");
		goto out;
	}

	# The CLI reverses the order of the objects when creating the container.
	@pvs = ($disk."5");
	$options{"name"} = $container_name;
	$options{"extent_size"} = $container_pe_size;

	log_info("2. Creating LVM2 container $container_name\n");

	$rc = create_lvm2_container(\%options, @pvs);
	if ($rc) {
		log_error("Error creating LVM container.\n");
		goto out;
	}

	log_info("2. Creating LVM2 region $container_name/$region1_name\n");

	$options{"name"} = $region1_name;
	$options{"size"} = "50MB";
	$rc = create_lvm2_region($container_name, \%options);
	if ($rc) {
		log_error("Error creating LVM2 region.\n");
		goto out;
	}

	log_info("3. Creating LVM2 region $container_name/$region2_name\n");

	$options{"name"} = $region2_name;
	$options{"size"} = "75MB";
	$rc = create_lvm2_region($container_name, \%options);
	if ($rc) {
		log_error("Error creating LVM2 region.\n");
		goto out;
	}

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

# Test1
# Check that the regions exist in the kernel. Then rename one of the
# regions. Check that the region was renamed in the kernel.
sub Test1
{
	my @output;
	my $rc;

	log_info("Test renaming an LVM2 region.\n");

	log_info("1. Checking that LVM2 regions $region1_name and $region2_name\n");
	log_info("   are active in the kernel.\n");

	@output = dm_info("lvm2|" . $container_name . "|" . $region1_name);
	if (@output == 0) {
		log_error("Region $region1_name not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	@output = dm_info("lvm2|" . $container_name . "|" . $region2_name);
	if (@output == 0) {
		log_error("Region $region2_name not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	log_info("2. Renaming LVM2 region $region1_name to $region1_name_new.\n");

	$rc = rename_lvm2_region("$container_name/$region1_name",
				 $region1_name_new);
	if ($rc) {
		log_error("Error renaming LVM2 region.\n");
		goto out;
	}

	log_info("3. Checking that region $region1_name_new is active in the kernel.\n");

	@output = dm_info("lvm2|" . $container_name . "|" . $region1_name_new);
	if (@output == 0) {
		log_error("Region $region1_name_new not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	log_info("4. Checking that region $region1_name is no longer active in the kernel.\n");

	@output = dm_info("lvm2|" . $container_name . "|" . $region1_name);
	if (@output != 0) {
		log_error("Region $region1_name is still active in the kernel.\n");
		$rc = 1;
		goto out;
	}

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

# Test2
# Rename the container. Check that both regions were renamed in the kernel.
sub Test2
{
	my @output;
	my $rc;

	log_info("Test renaming an LVM2 container.\n");

	log_info("1. Checking that LVM2 regions $region1_name_new and " .
	         "$region2_name are active in the kernel.\n");

	@output = dm_info("lvm2|" . $container_name . "|" . $region1_name_new);
	if (@output == 0) {
		log_error("Region $region1_name_new not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	@output = dm_info("lvm2|" . $container_name . "|" . $region2_name);
	if (@output == 0) {
		log_error("Region $region2_name not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	log_info("2. Renaming LVM2 container $container_name to " .
		 "$container_name_new.\n");

	$rc = rename_lvm2_container("$container_name", $container_name_new);
	if ($rc) {
		log_error("Error renaming LVM2 container.\n");
		goto out;
	}

	log_info("3. Checking that both regions were renamed in the kernel.\n");

	@output = dm_info("lvm2|" . $container_name_new . "|" . $region1_name_new);
	if (@output == 0) {
		log_error("Region $region1_name_new not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	@output = dm_info("lvm2|" . $container_name_new . "|" . $region2_name);
	if (@output == 0) {
		log_error("Region $region2_name not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	log_info("4. Checking that old region names are no longer active.\n");

	@output = dm_info("lvm2|" . $container_name . "|" . $region1_name_new);
	if (@output != 0) {
		log_error("Region $region1_name_new not active in the kernel.\n");
		$rc = 1;
		goto out;
	}

	@output = dm_info("lvm2|" . $container_name . "|" . $region2_name);
	if (@output != 0) {
		log_error("Region $region2_name not active in the kernel.\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, "200MB");
	if ($rc) {
		goto finish;
	}

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

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

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

finish:
}

