2 # Copyright (c) 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
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
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
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 package Installcheck::Mock;
24 Installcheck::Mock - utilities for the installcheck mocks
28 use Installcheck::Mock;
30 my $statefile = Installcheck::Mock::setup_mock_mtx(
48 C<setup_mock_mtx> sets up a state file for C<mock/mtx> with the given config
49 hash, and returns the filename of the state file. This function must be run
50 with the current dirctory pointing to 'installcheck/'.
54 Ndmjob can be used to provide an NDMP server, complete with two tape drives and
55 a 10-tape changer. Start it with:
57 my $ndmpserver = Installcheck::Mock::NdmpServer->new();
59 All keyword arguments are optional, and include:
61 tape_limit size limit for simulated tapes
63 The resulting object has a number of useful attributes:
65 $n->{'port'} port the NDMP server is listening on
66 $n->{'changer'} device name for the NDMP changer
67 $n->{'drive0'} device name for changer's drive 0
68 $n->{'drive1'} device name for changer's drive 1
69 $n->{'drive'} device name for a drive not attached to the changer,
70 and already loaded with a volume
72 The constructor takes the following keyword arguments:
74 no_reset => 1 do not empty out the changer of any existing state or data
76 The C<config> method, given an L<Installcheck::Config> object, will add the
77 necessary config to use C<chg-ndmp>:
79 $ndmp->config($testconf);
82 The C<edit_config> method is intended for use with NDMP dumps in
83 L<Installcheck::Dumpcache>. It edits an existing, on-disk configuration that
84 was created by the C<config> method to reflect the NDMP port in use by this
87 Installcheck:Dumpcache::load("ndmp")
90 The C<cleanup> method will clean up any data files, and should be called when a
91 test suite finishes. The C<reset> method resets the changer to its initial
92 state, without restarting ndmjob.
98 use File::Path qw( mkpath rmtree );
106 @EXPORT_OK = qw( setup_mock_mtx $mock_mtx_path );
110 my $state_filename = "$Installcheck::TMP/mtx_state";
111 open (my $fh, ">", $state_filename) or die $!;
112 print $fh (Data::Dumper->Dump([
113 { config => \%config }
117 return $state_filename;
120 our $mock_mtx_path = abs_path("mock") . "/mtx"; # abs_path only does dirs in perl-5.6
122 package Installcheck::Mock::NdmpServer;
124 use File::Path qw( mkpath rmtree );
131 my $self = bless {}, $class;
133 no warnings; # don't complain that OUTFH is used only once
135 # note that we put this under /vtapes/ so that Dumpcache will pick it up
136 $self->{'simdir'} = "$Installcheck::TMP/vtapes/ndmjob";
137 $self->reset() unless ($params{'no_reset'});
140 my $port = Installcheck::get_unused_port();
142 if ($params{'tape_limit'}) {
143 push @extra_args, "-o", "tape-limit=".$params{'tape_limit'};
145 my $ndmjob_pid = IPC::Open3::open3("INFH", "OUTFH", "ERRFH",
146 "$amlibexecdir/ndmjob", "-d8", "-o", "test-daemon",
147 "-p", $port, @extra_args);
149 # wait for it to start up
150 unless (<OUTFH> =~ /READY/) {
154 die "ndmjob did not start up";
157 # and fill in the various attributes
158 $self->{'port'} = $port;
159 $self->{'changer'} = $self->{'simdir'};
160 $self->{'drive0'} = $self->{'simdir'} . '/drive0';
161 $self->{'drive1'} = $self->{'simdir'} . '/drive1';
162 $self->{'drive'} = $self->{'simdir'} . '/solo-drive';
163 $self->{'pid'} = $ndmjob_pid;
172 my $port = $self->{'port'};
173 my $chg = $self->{'changer'};
174 my $drive0 = $self->{'drive0'};
175 my $drive1 = $self->{'drive1'};
177 $testconf->remove_param('tapedev');
178 $testconf->remove_param('tpchanger');
179 $testconf->remove_param('changerfile');
180 $testconf->add_param('tpchanger', '"ndmp_server"');
181 $testconf->add_changer('ndmp_server', [
182 tpchanger => "\"chg-ndmp:127.0.0.1:$port\@$chg\"",
183 property => "\"tape-device\" \"0=ndmp:127.0.0.1:$port\@$drive0\"",
184 property => "append \"tape-device\" \"1=ndmp:127.0.0.1:$port\@$drive1\"",
185 changerfile => "\"$chg-state\"",
192 # this is a bit sneaky, but is useful for dumpcache'd ndmp runs
193 my $amanda_conf_filename = "$CONFIG_DIR/TESTCONF/amanda.conf";
195 # slurp the whole file
196 open(my $fh, "<", $amanda_conf_filename) or die("open $amanda_conf_filename: $!");
197 my $amanda_conf = do { local $/; <$fh> };
200 # replace all existing ndmp changers and devices with the new port
201 # note that we assume that the remaining paths are correct
202 my $port = $self->{'port'};
203 $amanda_conf =~ s/ndmp:127.0.0.1:\d+/ndmp:127.0.0.1:$port/g;
205 open($fh, ">", $amanda_conf_filename) or die("open $amanda_conf_filename for writing: $!");
206 print $fh $amanda_conf;
214 mkpath($self->{'simdir'})
215 or die("cannot create " . $self->{'simdir'});
217 # non-changer drive must exist
218 my $drive = $self->{'simdir'} . '/solo-drive';
219 open(my $fd, ">", $drive)
220 or die("Could not open '$drive': $!");
227 rmtree($self->{'simdir'}) if -d $self->{'simdir'};