root/honeyclient/tags/exp/DOWN1-kindlund-simpler_agent/t/honeyclient_manager_vm_clone.t

Revision 1495, 15.5 kB (checked in by kindlund, 9 months ago)

Verified unit tests now work perfectly with these changes.

Line 
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 # Make sure ExtUtils::MakeMaker loads.
12 BEGIN { use_ok('ExtUtils::MakeMaker', qw(prompt)) or diag("Can't load ExtUtils::MakeMaker package.  Check to make sure the package library is correctly listed within the path."); }
13 require_ok('ExtUtils::MakeMaker');
14 can_ok('ExtUtils::MakeMaker', 'prompt');
15 use ExtUtils::MakeMaker qw(prompt);
16
17 # Make sure Log::Log4perl loads
18 BEGIN { use_ok('Log::Log4perl', qw(:nowarn))
19         or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path.");
20        
21         # Suppress all logging messages, since we need clean output for unit testing.
22         Log::Log4perl->init({
23             "log4perl.rootLogger"                               => "DEBUG, Buffer",
24             "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
25             "log4perl.appender.Buffer.min_level"                => "fatal",
26             "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
27             "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
28         });
29 }
30 require_ok('Log::Log4perl');
31 use Log::Log4perl qw(:easy);
32
33 # Make sure HoneyClient::Util::Config loads.
34 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar))
35         or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path.");
36
37         # Suppress all logging messages, since we need clean output for unit testing.
38         Log::Log4perl->init({
39             "log4perl.rootLogger"                               => "DEBUG, Buffer",
40             "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
41             "log4perl.appender.Buffer.min_level"                => "fatal",
42             "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
43             "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
44         });
45        
46 }
47 require_ok('HoneyClient::Util::Config');
48 can_ok('HoneyClient::Util::Config', 'getVar');
49 use HoneyClient::Util::Config qw(getVar);
50
51 # Make sure HoneyClient::Util::SOAP loads.
52 BEGIN { use_ok('HoneyClient::Util::SOAP', qw(getClientHandle)) or diag("Can't load HoneyClient::Util::SOAP package.  Check to make sure the package library is correctly listed within the path."); }
53 require_ok('HoneyClient::Util::SOAP');
54 can_ok('HoneyClient::Util::SOAP', 'getClientHandle');
55 use HoneyClient::Util::SOAP qw(getClientHandle);
56
57 # Make sure HoneyClient::Manager::VM loads.
58 BEGIN { use_ok('HoneyClient::Manager::VM') or diag("Can't load HoneyClient::Manager::VM package.  Check to make sure the package library is correctly listed within the path."); }
59 require_ok('HoneyClient::Manager::VM');
60 use HoneyClient::Manager::VM;
61
62 # Make sure HoneyClient::Manager::Database loads.
63 BEGIN { use_ok('HoneyClient::Manager::Database') or diag("Can't load HoneyClient::Manager::Database package.  Check to make sure the package library is correctly listed within the path."); }
64 require_ok('HoneyClient::Manager::Database');
65 use HoneyClient::Manager::Database;
66
67 # Make sure VMware::VmPerl loads.
68 BEGIN { use_ok('VMware::VmPerl', qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED)) or diag("Can't load VMware::VmPerl package.  Check to make sure the package library is correctly listed within the path."); }
69 require_ok('VMware::VmPerl');
70 use VMware::VmPerl qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED);
71
72 # Make sure the module loads properly, with the exportable
73 # functions shared.
74 BEGIN { use_ok('HoneyClient::Manager::VM::Clone') or diag("Can't load HoneyClient::Manager::VM::Clone package.  Check to make sure the package library is correctly listed within the path."); }
75 require_ok('HoneyClient::Manager::VM::Clone');
76 use HoneyClient::Manager::VM::Clone;
77
78 # Suppress all logging messages, since we need clean output for unit testing.
79 Log::Log4perl->init({
80     "log4perl.rootLogger"                               => "DEBUG, Buffer",
81     "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
82     "log4perl.appender.Buffer.min_level"                => "fatal",
83     "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
84     "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
85 });
86
87 # Make sure Storable loads.
88 BEGIN { use_ok('Storable', qw(dclone thaw)) or diag("Can't load Storable package.  Check to make sure the package library is correctly listed within the path."); }
89 require_ok('Storable');
90 can_ok('Storable', 'dclone');
91 can_ok('Storable', 'thaw');
92 use Storable qw(dclone thaw);
93
94 # Make sure MIME::Base64 loads.
95 BEGIN { use_ok('MIME::Base64', qw(encode_base64 decode_base64)) or diag("Can't load MIME::Base64 package.  Check to make sure the package library is correctly listed within the path."); }
96 require_ok('MIME::Base64');
97 can_ok('MIME::Base64', 'encode_base64');
98 can_ok('MIME::Base64', 'decode_base64');
99 use MIME::Base64 qw(encode_base64 decode_base64);
100
101 # Make sure Data::Dumper loads
102 BEGIN { use_ok('Data::Dumper')
103         or diag("Can't load Data::Dumper package. Check to make sure the package library is correctly listed within the path."); }
104 require_ok('Data::Dumper');
105 use Data::Dumper;
106
107 # Make sure threads loads.
108 BEGIN { use_ok('threads') or diag("Can't load threads package.  Check to make sure the package library is correctly listed within the path."); }
109 require_ok('threads');
110 use threads;
111
112 # Make sure threads::shared loads.
113 BEGIN { use_ok('threads::shared') or diag("Can't load threads::shared package.  Check to make sure the package library is correctly listed within the path."); }
114 require_ok('threads::shared');
115 use threads::shared;
116
117 # Make sure File::Basename loads.
118 BEGIN { use_ok('File::Basename', qw(dirname basename)) or diag("Can't load File::Basename package.  Check to make sure the package library is correctly listed within the path."); }
119 require_ok('File::Basename');
120 can_ok('File::Basename', 'dirname');
121 can_ok('File::Basename', 'basename');
122 use File::Basename qw(dirname basename);
123
124 # Make sure Sys::Hostname loads.
125 BEGIN { use_ok('Sys::Hostname') or diag("Can't load Sys::Hostname package.  Check to make sure the package library is correctly listed within the path."); }
126 require_ok('Sys::Hostname');
127 use Sys::Hostname;
128
129 # Make sure Sys::HostIP loads.
130 BEGIN { use_ok('Sys::HostIP') or diag("Can't load Sys::HostIP package.  Check to make sure the package library is correctly listed within the path."); }
131 require_ok('Sys::HostIP');
132 use Sys::HostIP;
133
134 # Make sure DateTime::HiRes loads.
135 BEGIN { use_ok('DateTime::HiRes') or diag("Can't load Sys::HostIP package.  Check to make sure the package library is correctly listed within the path."); }
136 require_ok('DateTime::HiRes');
137 use DateTime::HiRes;
138
139 # Make sure IO::File loads.
140 BEGIN { use_ok('IO::File') or diag("Can't load IO::File package.  Check to make sure the package library is correctly listed within the path."); }
141 require_ok('IO::File');
142 use IO::File;
143
144 # Make sure Filesys::DfPortable loads
145 BEGIN { use_ok('Filesys::DfPortable')
146         or diag("Can't load Filesys::DfPortable package. Check to make sure the package library is correctly listed within the path."); }
147 require_ok('Filesys::DfPortable');
148 use Filesys::DfPortable;
149 }
150
151
152
153 # =begin testing
154 {
155 # Shared test variables.
156 my ($stub, $som, $URL);
157 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
158                                       namespace => "HoneyClient::Manager::VM::Test");
159
160 # Include notice, to clarify our assumptions.
161 diag("About to run basic unit tests; these may take some time.");
162 diag("Note: These tests *expect* VMware Server or VMware GSX to be installed and running on this system beforehand.");
163
164 # Catch all errors, in order to make sure child processes are
165 # properly killed.
166 eval {
167
168     $URL = HoneyClient::Manager::VM->init();
169
170     # Connect to daemon as a client.
171     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
172
173     # In order to test setMasterVM(), we're going to fully clone
174     # the testVM, then set the newly created clone as a master VM.
175
176     # Get the test VM's parent directory,
177     # in order to create a temporary master VM.
178     my $testVMDir = dirname($testVM);
179     my $masterVMDir = dirname($testVMDir) . "/test_vm_master";
180     my $masterVM = $masterVMDir . "/" . basename($testVM);
181
182     # Create the master VM.
183     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $masterVMDir);
184
185     # Wait a small amount of time for the asynchronous clone
186     # to complete.
187     sleep (10);
188
189     # The master VM should be on.
190     $som = $stub->getStateVM(config => $masterVM);
191    
192     # Since the master VM doesn't have an OS installed on it,
193     # the VM may be considered stuck.  Go ahead and answer
194     # this question, if need be.
195     if ($som->result == VM_EXECUTION_STATE_STUCK) {
196         $som = $stub->answerVM(config => $masterVM);
197     }
198
199     # Turn off the master VM.
200     $som = $stub->stopVM(config => $masterVM);
201
202     # Now, kill the VM daemon.
203     HoneyClient::Manager::VM->destroy();
204
205     # Create a generic empty clone, with test state data.
206     my $clone = HoneyClient::Manager::VM::Clone->new(test => 1, master_vm_config => $masterVM, _dont_init => 1, _bypass_firewall => 1);
207     is($clone->{test}, 1, "new(test => 1, master_vm_config => '$masterVM', _dont_init => 1, _bypass_firewall => 1)") or diag("The new() call failed.");
208     isa_ok($clone, 'HoneyClient::Manager::VM::Clone', "new(test => 1, master_vm_config => '$masterVM', _dont_init => 1, _bypass_firewall => 1)") or diag("The new() call failed.");
209     $clone = undef;
210
211     # Destroy the master VM.
212     $som = $stub->destroyVM(config => $masterVM);
213
214     my $question;
215     $question = prompt("#\n" .
216                        "# Note: Testing real clone operations will *ONLY* work\n" .
217                        "# with a fully functional master VM that has the HoneyClient code\n" .
218                        "# loaded upon boot-up.\n" .
219                        "#\n" .
220                        "# This test also requires that the firewall VM is registered,\n" .
221                        "# powered on, and operational.\n" .
222                        "#\n" .
223                        "# Your master VM is: " . getVar(name => "master_vm_config", namespace => "HoneyClient::Manager::VM") . "\n" .
224                        "#\n" .
225                        "# Do you want to test cloning this master VM?", "no");
226     if ($question =~ /^y.*/i) {
227         $clone = HoneyClient::Manager::VM::Clone->new(test => 1);
228         is($clone->{test}, 1, "new(test => 1)") or diag("The new() call failed.");
229         isa_ok($clone, 'HoneyClient::Manager::VM::Clone', "new(test => 1)") or diag("The new() call failed.");
230         my $cloneConfig = $clone->{config};
231         $clone = undef;
232    
233         # Destroy the clone VM.
234         $som = $stub->destroyVM(config => $cloneConfig);
235     }
236 };
237
238 # Kill the child daemon, if it still exists.
239 HoneyClient::Manager::VM->destroy();
240
241 # Report any failure found.
242 if ($@) {
243     fail($@);
244 }
245 }
246
247
248
249 # =begin testing
250 {
251 # Shared test variables.
252 my ($stub, $som, $URL);
253 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
254                                       namespace => "HoneyClient::Manager::VM::Test");
255
256 # Catch all errors, in order to make sure child processes are
257 # properly killed.
258 eval {
259
260     my $testVMDir = dirname($testVM);
261
262     # Specify where the snapshot should be created.
263     my $snapshot = dirname($testVMDir) . "/test_vm_clone.tar.gz";
264
265     # Pretend as though no other Clone objects have been created prior
266     # to this point.
267     $HoneyClient::Manager::VM::Clone::OBJECT_COUNT = -1;
268    
269     my $question;
270     $question = prompt("#\n" .
271                        "# Note: Testing real suspend/archive operations will *ONLY* work\n" .
272                        "# with a fully functional master VM that has the HoneyClient code\n" .
273                        "# loaded upon boot-up.\n" .
274                        "#\n" .
275                        "# This test also requires that the firewall VM is registered,\n" .
276                        "# powered on, and operational.\n" .
277                        "#\n" .
278                        "# Your master VM is: " . getVar(name => "master_vm_config", namespace => "HoneyClient::Manager::VM") . "\n" .
279                        "#\n" .
280                        "# Do you want to test cloning and archiving this master VM?", "no");
281     if ($question =~ /^y.*/i) {
282
283         # Create a generic empty clone, with test state data.
284         my $clone = HoneyClient::Manager::VM::Clone->new(_bypass_firewall => 1);
285         my $cloneConfig = $clone->{config};
286
287         # Archive the clone.
288         $clone->suspend(perform_archive => 1, snapshot_file => $snapshot);
289
290         # Wait for the suspend/archive to complete.
291         sleep (45);
292    
293         # Test if the operations worked.
294         is(-f $snapshot, 1, "suspend(perform_archive => 1, snapshot_file => '$snapshot')") or diag("The suspend() call failed.");
295    
296         unlink $snapshot;
297         $clone = undef;
298    
299         # Connect to daemon as a client.
300         $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
301    
302         # Destroy the clone VM.
303         $som = $stub->destroyVM(config => $cloneConfig);
304     }
305 };
306
307 # Kill the child daemon, if it still exists.
308 HoneyClient::Manager::VM->destroy();
309
310 # Report any failure found.
311 if ($@) {
312     fail($@);
313 }
314 }
315
316
317
318 # =begin testing
319 {
320 # Shared test variables.
321 my ($stub, $som, $URL);
322 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
323                                       namespace => "HoneyClient::Manager::VM::Test");
324
325 # Catch all errors, in order to make sure child processes are
326 # properly killed.
327 eval {
328
329     # Pretend as though no other Clone objects have been created prior
330     # to this point.
331     $HoneyClient::Manager::VM::Clone::OBJECT_COUNT = -1;
332    
333     my $question;
334     $question = prompt("#\n" .
335                        "# Note: Testing real drive operations will *ONLY* work\n" .
336                        "# with a fully functional master VM that has the HoneyClient code\n" .
337                        "# loaded upon boot-up.\n" .
338                        "#\n" .
339                        "# This test also requires that the firewall VM is registered,\n" .
340                        "# powered on, and operational.\n" .
341                        "#\n" .
342                        "# Your master VM is: " . getVar(name => "master_vm_config", namespace => "HoneyClient::Manager::VM") . "\n" .
343                        "#\n" .
344                        "# Do you want to test cloning and driving this master VM?", "no");
345     if ($question =~ /^y.*/i) {
346
347         # Create a generic empty clone, with test state data.
348         my $clone = HoneyClient::Manager::VM::Clone->new(_bypass_firewall => 1);
349         my $cloneConfig = $clone->{config};
350
351         $clone = $clone->drive(work => { 'http://www.google.com/' => 1 });
352         isa_ok($clone, 'HoneyClient::Manager::VM::Clone', "drive(work => { 'http://www.google.com/' => 1})") or diag("The drive() call failed.");
353         $clone = undef;
354
355         # Connect to daemon as a client.
356         $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
357    
358         # Destroy the clone VM.
359         $som = $stub->destroyVM(config => $cloneConfig);
360     }
361 };
362
363 # Kill the child daemon, if it still exists.
364 HoneyClient::Manager::VM->destroy();
365
366 # Report any failure found.
367 if ($@) {
368     fail($@);
369 }
370 }
371
372
373
374
375 1;
Note: See TracBrowser for help on using the browser.