1# Copyright (c) 2009-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 => 31;
21use File::Path;
22use strict;
23use warnings;
24
25use lib "@amperldir@";
26use Installcheck;
27use Installcheck::Mock qw( setup_mock_mtx );
28use Installcheck::Run qw( run run_get run_err );
29
30my $vtape_root = "$Installcheck::TMP/mock_mtx_vtapes";
31my $mtx_state_file = setup_mock_mtx (
32	 barcodes => 1,
33	 track_orig => 1,
34	 num_slots => 5,
35	 num_drives => 2,
36	 num_ie => 1,
37	 first_slot => 1,
38	 first_drive => 0,
39	 first_ie => 6,
40	 vtape_root => $vtape_root,
41	 loaded_slots => {
42	     1 => '023984',
43	     3 => '978344',
44         },
45       );
46
47unless(like(run_get('mock/mtx', '-f', $mtx_state_file, 'inquiry'),
48        qr/Product Type: Medium Changer/,
49        "mtx inquiry")) {
50  SKIP: {
51      skip("mock/mtx seems broken; calling off the test early",
52           Test::More->builder->expected_tests-1);
53    }
54    exit 0;
55}
56
57like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
58    qr{  Storage Changer .*:2 Drives, 6 Slots \( 1 Import/Export \)
59Data Transfer Element 0:Empty
60Data Transfer Element 1:Empty
61      Storage Element 1:Full :VolumeTag=023984
62      Storage Element 2:Empty
63      Storage Element 3:Full :VolumeTag=978344
64      Storage Element 4:Empty
65      Storage Element 5:Empty
66      Storage Element 6 IMPORT/EXPORT:Empty},
67    "mtx status (+BARCODES, +TRACK ORIG)");
68
69like(run_err('mock/mtx', '-f', $mtx_state_file, 'load', '2', '0'),
70    qr/source Element Address 2 is Empty/,
71    "mtx load 2 0 (slot empty)");
72
73like(run_err('mock/mtx', '-f', $mtx_state_file, 'unload', '5', '1'),
74    qr/Unloading Data Transfer Element into Storage Element 5...source Element Address.*/,
75    "mtx unload 5 1 (drive empty)");
76
77like(run_err('mock/mtx', '-f', $mtx_state_file, 'unload', '3', '0'),
78    qr/Storage Element 3 is Already Full/,
79    "mtx unload 5 1 (slot full)");
80
81ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '1', '0'),
82    "mtx load 1 0");
83
84ok(-d "$vtape_root/drive0/data", "fake vfs drive loaded");
85
86like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
87    qr{  Storage Changer .*:2 Drives, 6 Slots \( 1 Import/Export \)
88Data Transfer Element 0:Full \(Storage Element 1 Loaded\):VolumeTag=023984
89Data Transfer Element 1:Empty
90      Storage Element 1:Empty
91      Storage Element 2:Empty
92      Storage Element 3:Full :VolumeTag=978344
93      Storage Element 4:Empty
94      Storage Element 5:Empty
95      Storage Element 6 IMPORT/EXPORT:Empty},
96    "mtx status shows results");
97
98like(run_err('mock/mtx', '-f', $mtx_state_file, 'load', '3', '0'),
99    qr/Drive 0 Full \(Storage Element 1 Loaded\)/,
100    "mtx load 3 0 (drive full)");
101
102ok(run('mock/mtx', '-f', $mtx_state_file, 'unload', '2', '0'),
103    "mtx unload 2 0");
104
105ok(! -d "$vtape_root/drive0/data", "fake vfs drive unloaded");
106ok(-d "$vtape_root/slot2/data", "fake vfs slot re-populated");
107
108like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
109    qr{  Storage Changer .*:2 Drives, 6 Slots \( 1 Import/Export \)
110Data Transfer Element 0:Empty
111Data Transfer Element 1:Empty
112      Storage Element 1:Empty
113      Storage Element 2:Full :VolumeTag=023984
114      Storage Element 3:Full :VolumeTag=978344
115      Storage Element 4:Empty
116      Storage Element 5:Empty
117      Storage Element 6 IMPORT/EXPORT:Empty},
118    "mtx status shows results");
119
120like(run_err('mock/mtx', '-f', $mtx_state_file, 'transfer', '1', '4'),
121    qr/source Element Address 1 is Empty/,
122    "mtx transfer 1 4 (source empty)");
123
124like(run_err('mock/mtx', '-f', $mtx_state_file, 'transfer', '2', '3'),
125    qr/destination Element Address 3 is Already Full/,
126    "mtx transfer 1 4 (dest full)");
127
128ok(run('mock/mtx', '-f', $mtx_state_file, 'transfer', '2', '4'),
129    "mtx transfer 2 4");
130
131like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
132    qr{  Storage Changer .*:2 Drives, 6 Slots \( 1 Import/Export \)
133Data Transfer Element 0:Empty
134Data Transfer Element 1:Empty
135      Storage Element 1:Empty
136      Storage Element 2:Empty
137      Storage Element 3:Full :VolumeTag=978344
138      Storage Element 4:Full :VolumeTag=023984
139      Storage Element 5:Empty
140      Storage Element 6 IMPORT/EXPORT:Empty},
141    "mtx status shows results");
142
143rmtree($vtape_root);
144
145##
146# Without barcodes, with track orig
147
148$mtx_state_file = setup_mock_mtx (
149	 barcodes => 0,
150	 track_orig => 1,
151	 num_slots => 5,
152	 num_drives => 1,
153	 first_slot => 1,
154	 first_drive => 0,
155	 loaded_slots => {
156	     1 => '023984',
157	     2 => '376524',
158	     3 => '983754',
159         },
160       );
161
162like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
163    qr{  Storage Changer .*:1 Drives, 5 Slots \( 0 Import/Export \)
164Data Transfer Element 0:Empty
165      Storage Element 1:Full
166      Storage Element 2:Full
167      Storage Element 3:Full
168      Storage Element 4:Empty
169      Storage Element 5:Empty},
170    "mtx status (-BARCODES, +TRACK ORIG)");
171
172ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '1', '0'),
173    "mtx load 1 0");
174
175like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
176    qr{  Storage Changer .*:1 Drives, 5 Slots \( 0 Import/Export \)
177Data Transfer Element 0:Full \(Storage Element 1 Loaded\)
178      Storage Element 1:Empty
179      Storage Element 2:Full
180      Storage Element 3:Full
181      Storage Element 4:Empty
182      Storage Element 5:Empty},
183    "mtx status shows results");
184
185##
186# Without barcodes, without track orig
187
188$mtx_state_file = setup_mock_mtx (
189	 barcodes => 0,
190	 track_orig => 0,
191	 num_slots => 5,
192	 num_drives => 1,
193	 first_slot => 1,
194	 first_drive => 0,
195	 loaded_slots => {
196	     1 => '023984',
197	     2 => '376524',
198	     3 => '983754',
199         },
200       );
201
202ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '1', '0'),
203    "mtx load 1 0 (-BARCODES, -TRACK ORIG)");
204
205like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
206    qr{  Storage Changer .*:1 Drives, 5 Slots \( 0 Import/Export \)
207Data Transfer Element 0:Full
208      Storage Element 1:Empty
209      Storage Element 2:Full
210      Storage Element 3:Full
211      Storage Element 4:Empty
212      Storage Element 5:Empty},
213    "mtx status shows results");
214
215##
216# With barcodes, without track orig
217
218$mtx_state_file = setup_mock_mtx (
219	 barcodes => 1,
220	 track_orig => 0,
221	 num_slots => 5,
222	 num_drives => 2,
223	 first_slot => 1,
224	 first_drive => 0,
225	 loaded_slots => {
226	     2 => '023984',
227	     3 => '376524',
228	     4 => '983754',
229         },
230       );
231
232ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '2', '1'),
233    "mtx load 2 0 (+BARCODES, -TRACK ORIG)");
234
235like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
236    qr{  Storage Changer .*:2 Drives, 5 Slots \( 0 Import/Export \)
237Data Transfer Element 0:Empty
238Data Transfer Element 1:Full :VolumeTag=023984
239      Storage Element 1:Empty
240      Storage Element 2:Empty
241      Storage Element 3:Full :VolumeTag=376524
242      Storage Element 4:Full :VolumeTag=983754
243      Storage Element 5:Empty},
244    "mtx status shows results");
245
246ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '4', '0'),
247    "mtx load 2 0");
248
249like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
250    qr{  Storage Changer .*:2 Drives, 5 Slots \( 0 Import/Export \)
251Data Transfer Element 0:Full :VolumeTag=983754
252Data Transfer Element 1:Full :VolumeTag=023984
253      Storage Element 1:Empty
254      Storage Element 2:Empty
255      Storage Element 3:Full :VolumeTag=376524
256      Storage Element 4:Empty
257      Storage Element 5:Empty},
258    "mtx status shows results");
259
260##
261# With barcodes, with weird track orig behavior
262
263$mtx_state_file = setup_mock_mtx (
264	 barcodes => 1,
265	 track_orig => -1,
266	 num_slots => 5,
267	 num_drives => 2,
268	 first_slot => 1,
269	 first_drive => 0,
270	 loaded_slots => {
271	     2 => '023984',
272	     3 => '376524',
273	     4 => '983754',
274         },
275       );
276
277like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
278    qr{  Storage Changer .*:2 Drives, 5 Slots \( 0 Import/Export \)
279Data Transfer Element 0:Empty
280Data Transfer Element 1:Empty
281      Storage Element 1:Empty
282      Storage Element 2:Full :VolumeTag=023984
283      Storage Element 3:Full :VolumeTag=376524
284      Storage Element 4:Full :VolumeTag=983754
285      Storage Element 5:Empty},
286    "mtx status (+BARCODES, IMAGINED TRACK ORIG)");
287
288ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '2', '1'),
289    "mtx load 2 0");
290
291like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
292    qr{  Storage Changer .*:2 Drives, 5 Slots \( 0 Import/Export \)
293Data Transfer Element 0:Empty
294Data Transfer Element 1:Full \(Storage Element 1 Loaded\):VolumeTag=023984
295      Storage Element 1:Empty
296      Storage Element 2:Empty
297      Storage Element 3:Full :VolumeTag=376524
298      Storage Element 4:Full :VolumeTag=983754
299      Storage Element 5:Empty},
300    "mtx status shows results");
301
302ok(run('mock/mtx', '-f', $mtx_state_file, 'load', '4', '0'),
303    "mtx load 2 0");
304
305like(run_get('mock/mtx', '-f', $mtx_state_file, 'status'),
306    qr{  Storage Changer .*:2 Drives, 5 Slots \( 0 Import/Export \)
307Data Transfer Element 0:Full \(Storage Element 1 Loaded\):VolumeTag=983754
308Data Transfer Element 1:Full \(Storage Element 2 Loaded\):VolumeTag=023984
309      Storage Element 1:Empty
310      Storage Element 2:Empty
311      Storage Element 3:Full :VolumeTag=376524
312      Storage Element 4:Empty
313      Storage Element 5:Empty},
314    "mtx status shows results");
315