1# Copyright (c) 2008-2013 Zmanda, Inc.  All Rights Reserved.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11# for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16#
17# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20use Test::More tests => 15;
21use strict;
22use warnings;
23
24use lib "@amperldir@";
25use Amanda::Config qw( :init :getconf );
26use Amanda::Disklist;
27use Installcheck::Config;
28
29my $testconf;
30
31$testconf = Installcheck::Config->new();
32$testconf->add_dumptype("mytype", [
33    "compress" => "server",
34    "starttime" => "1830",
35    "amandad_path" => "\"/path/to/amandad\"",
36]);
37$testconf->add_interface("eth1", []);
38$testconf->add_dle("otherbox /home mytype");
39$testconf->add_dle("otherbox /disk1 mytype");
40$testconf->add_dle("otherbox /disk2 mytype");
41$testconf->add_dle(<<EOF);
42myhost /mydisk {
43    auth "bsd"
44} -1 eth1
45EOF
46$testconf->write();
47
48if (config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF") != $CFGERR_OK) {
49    config_print_errors();
50    die("config errors");
51}
52
53is(Amanda::Disklist::read_disklist(), $CFGERR_OK,
54    "read_disklist returns CFGERR_OK")
55    or die("Error loading disklist");
56
57my ($x, $d, @list);
58
59$x = Amanda::Disklist::get_host("otherbox");
60ok($x, "get_host returns a host");
61is($x->{'auth'}, 'BSDTCP', "..host has correct auth");
62is_deeply([ sort @{$x->{'disks'}} ],
63	  [ sort "/disk1", "/disk2", "/home" ],
64	  "..and three disks");
65is(interface_name($x->{'interface'}->{'config'}), "default", "..and correct interface");
66
67$d = $x->get_disk("/home");
68is($d->{'name'}, "/home", "host->get_disk() works");
69
70@list = $x->all_disks();
71is_deeply([ sort map { $_->{'name'} } @list ],
72	  [ sort "/disk1", "/disk2", "/home" ],
73	  "host->all_disks returns all disk objects");
74
75@list = Amanda::Disklist::all_hosts();
76is_deeply([ sort( map { $_->{'hostname'} } @list ) ],
77	[ sort qw(myhost otherbox) ],
78	"all_hosts returns correct hosts");
79
80$x = Amanda::Disklist::get_disk("myhost", "/mydisk");
81ok($x, "get_disk returns a disk");
82is($x->{'name'}, "/mydisk", "..and it's the right one");
83
84@list = Amanda::Disklist::all_disks();
85is(scalar @list, 4, "all_lists returns 4 disks");
86
87$x = Amanda::Disklist::get_interface("eth1");
88is(interface_name($x->{'config'}), "eth1", "get_interface returns an interface");
89
90@list = Amanda::Disklist::all_interfaces();
91is(scalar @list, 2, "all_interfaces returns two interfaces");
92
93Amanda::Disklist::unload_disklist();
94is(Amanda::Disklist::read_disklist(), $CFGERR_OK,
95    "read_disklist returns CFGERR_OK after unload_disklist")
96    or die("Error loading disklist");
97
98is(Amanda::Disklist::read_disklist(), $CFGERR_ERRORS,
99    "read_disklist returns CFGERR_ERRORS for second read");
100
101