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

Revision 1426, 48.0 kB (checked in by kindlund, 9 months ago)

Updated Agent unit tests.

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 # Generate a notice, to clarify our assumptions.
18 diag("About to run basic unit tests.");
19 diag("Note: These tests *expect* VMware Server or VMware GSX to be installed and running on this system beforehand.");
20
21 my $question;
22 $question = prompt("# Do you want to run basic tests?", "yes");
23 if ($question !~ /^y.*/i) {
24     exit;
25 }
26
27 # Make sure Log::Log4perl loads
28 BEGIN { use_ok('Log::Log4perl', qw(:nowarn))
29         or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path.");
30        
31         # Suppress all logging messages, since we need clean output for unit testing.
32         Log::Log4perl->init({
33             "log4perl.rootLogger"                               => "DEBUG, Buffer",
34             "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
35             "log4perl.appender.Buffer.min_level"                => "fatal",
36             "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
37             "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
38         });
39 }
40 require_ok('Log::Log4perl');
41 use Log::Log4perl qw(:easy);
42
43 # Make sure HoneyClient::Util::Config loads.
44 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar))
45         or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path.");
46
47         # Suppress all logging messages, since we need clean output for unit testing.
48         Log::Log4perl->init({
49             "log4perl.rootLogger"                               => "DEBUG, Buffer",
50             "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
51             "log4perl.appender.Buffer.min_level"                => "fatal",
52             "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
53             "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
54         });
55 }
56 require_ok('HoneyClient::Util::Config');
57 can_ok('HoneyClient::Util::Config', 'getVar');
58 use HoneyClient::Util::Config qw(getVar);
59
60 # Suppress all logging messages, since we need clean output for unit testing.
61 Log::Log4perl->init({
62     "log4perl.rootLogger"                               => "DEBUG, Buffer",
63     "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
64     "log4perl.appender.Buffer.min_level"                => "fatal",
65     "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
66     "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
67 });
68
69 # Make sure the module loads properly, with the exportable
70 # functions shared.
71 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."); }
72 require_ok('HoneyClient::Manager::VM');
73 can_ok('HoneyClient::Manager::VM', 'init');
74 can_ok('HoneyClient::Manager::VM', 'destroy');
75 use HoneyClient::Manager::VM;
76
77 # Make sure HoneyClient::Util::SOAP loads.
78 BEGIN { use_ok('HoneyClient::Util::SOAP', qw(getServerHandle getClientHandle)) or diag("Can't load HoneyClient::Util::SOAP package.  Check to make sure the package library is correctly listed within the path."); }
79 require_ok('HoneyClient::Util::SOAP');
80 can_ok('HoneyClient::Util::SOAP', 'getServerHandle');
81 can_ok('HoneyClient::Util::SOAP', 'getClientHandle');
82 use HoneyClient::Util::SOAP qw(getServerHandle getClientHandle);
83
84 # Make sure File::Basename loads.
85 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."); }
86 require_ok('File::Basename');
87 can_ok('File::Basename', 'dirname');
88 can_ok('File::Basename', 'basename');
89 use File::Basename qw(dirname basename);
90
91 # Make sure File::Copy::Recursive loads.
92 BEGIN { use_ok('File::Copy::Recursive', qw(dircopy pathrmdir)) or diag("Can't load File::Copy::Recursive package.  Check to make sure the package library is correctly listed within the path."); }
93 require_ok('File::Copy::Recursive');
94 can_ok('File::Copy::Recursive', 'dircopy');
95 can_ok('File::Copy::Recursive', 'pathrmdir');
96 use File::Copy::Recursive qw(dircopy pathrmdir);
97
98 # Make sure Data::Dumper loads.
99 BEGIN { use_ok('Data::Dumper') or diag("Can't load Data::Dumper package.  Check to make sure the package library is correctly listed within the path."); }
100 require_ok('Data::Dumper');
101 use Data::Dumper;
102
103 # Make sure File::stat loads.
104 BEGIN { use_ok('File::stat') or diag("Can't load File::stat package.  Check to make sure the package library is correctly listed within the path."); }
105 require_ok('File::stat');
106 use File::stat;
107
108 # Make sure Digest::MD5 loads.
109 BEGIN { use_ok('Digest::MD5', qw(md5_hex)) or diag("Can't load Digest::MD5 package.  Check to make sure the package library is correctly listed within the path."); }
110 require_ok('Digest::MD5');
111 can_ok('Digest::MD5', 'md5_hex');
112 use Digest::MD5 qw(md5_hex);
113
114 # Make sure DateTime::HiRes loads.
115 BEGIN { use_ok('DateTime::HiRes') or diag("Can't load DateTime::HiRes package.  Check to make sure the package library is correctly listed within the path."); }
116 require_ok('DateTime::HiRes');
117 use DateTime::HiRes;
118
119 # Make sure Fcntl loads.
120 BEGIN { use_ok('Fcntl', qw(O_RDONLY)) or diag("Can't load Fcntl package.  Check to make sure the package library is correctly listed within the path."); }
121 require_ok('Fcntl');
122 use Fcntl qw(O_RDONLY);
123
124 # Make sure VMware::VmPerl loads.
125 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."); }
126 require_ok('VMware::VmPerl');
127 use VMware::VmPerl qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED);
128
129 # Make sure VMware::VmPerl::Server loads.
130 BEGIN { use_ok('VMware::VmPerl::Server') or diag("Can't load VMware::VmPerl::Server package.  Check to make sure the package library is correctly listed within the path."); }
131 require_ok('VMware::VmPerl::Server');
132 use VMware::VmPerl::Server;
133
134 # Make sure VMware::VmPerl::ConnectParams loads.
135 BEGIN { use_ok('VMware::VmPerl::ConnectParams') or diag("Can't load VMware::VmPerl::ConnectParams package.  Check to make sure the package library is correctly listed within the path."); }
136 require_ok('VMware::VmPerl::ConnectParams');
137 use VMware::VmPerl::ConnectParams;
138
139 # Make sure VMware::VmPerl::VM loads.
140 BEGIN { use_ok('VMware::VmPerl::VM') or diag("Can't load VMware::VmPerl::VM package.  Check to make sure the package library is correctly listed within the path."); }
141 require_ok('VMware::VmPerl::VM');
142 use VMware::VmPerl::VM;
143
144 # Make sure VMware::VmPerl::VM loads.
145 BEGIN { use_ok('VMware::VmPerl::Question') or diag("Can't load VMware::VmPerl::Question package.  Check to make sure the package library is correctly listed within the path."); }
146 require_ok('VMware::VmPerl::Question');
147 use VMware::VmPerl::Question;
148
149 # Make sure threads loads.
150 BEGIN { use_ok('threads') or diag("Can't load threads package.  Check to make sure the package library is correctly listed within the path."); }
151 require_ok('threads');
152 use threads;
153
154 # Make sure threads::shared loads.
155 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."); }
156 require_ok('threads::shared');
157 use threads::shared;
158
159 # Make sure Thread::Queue loads.
160 BEGIN { use_ok('Thread::Queue') or diag("Can't load Thread::Queue package.  Check to make sure the package library is correctly listed within the path."); }
161 require_ok('Thread::Queue');
162 use Thread::Queue;
163
164 # Make sure Thread::Semaphore loads.
165 BEGIN { use_ok('Thread::Semaphore') or diag("Can't load Thread::Semaphore package.  Check to make sure the package library is correctly listed within the path."); }
166 require_ok('Thread::Semaphore');
167 use Thread::Semaphore;
168
169 diag("About to run extended tests.");
170 # Generate a notice, to inform the tester that these tests are not
171 # exactly quick.
172 diag("Note: These extended tests will take *significant* time to complete (10-30 minutes).");
173
174 $question = prompt("# Do you want to run extended tests?", "no");
175 if ($question !~ /^y.*/i) {
176     exit;
177 }
178 }
179
180
181
182 # =begin testing
183 {
184 # Shared test variables.
185 my $PORT = getVar(name      => "port",
186                   namespace => "HoneyClient::Manager::VM");
187 my ($stub, $som, $URL);
188 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
189                                       namespace => "HoneyClient::Manager::VM::Test");
190
191 # Test init() method.
192 $URL = HoneyClient::Manager::VM->init();
193 is($URL, "http://localhost:$PORT/HoneyClient/Manager/VM", "init()") or diag("Failed to start up the VM SOAP server.  Check to see if any other daemon is listening on TCP port $PORT.");
194 }
195
196
197
198 # =begin testing
199 {
200 # Shared test variables.
201 my $PORT = getVar(name      => "port",
202                   namespace => "HoneyClient::Manager::VM");
203 my ($stub, $som, $URL);
204 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
205                                       namespace => "HoneyClient::Manager::VM::Test");
206
207 # Test destroy() method.
208 is(HoneyClient::Manager::VM->destroy(), 1, "destroy()") or diag("Unable to terminate VM SOAP server.  Be sure to check for any stale or lingering processes.");
209 }
210
211
212
213 # =begin testing
214 {
215 # Shared test variables.
216 my $PORT = getVar(name      => "port",
217                   namespace => "HoneyClient::Manager::VM");
218 my ($stub, $som, $URL);
219 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
220                                       namespace => "HoneyClient::Manager::VM::Test");
221
222 # Catch all errors, in order to make sure child processes are
223 # properly killed.
224 eval {
225
226     $URL = HoneyClient::Manager::VM->init();
227    
228     # Connect to daemon as a client.
229     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
230
231     # Make sure the test VM is stopped.
232     $som = $stub->stopVM(config => $testVM);
233    
234     # Test isRegisteredVM() method.
235     $som = $stub->isRegisteredVM(config => $testVM);
236    
237     # The test VM should be registered.
238     ok($som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
239
240     # Make sure the test VM is unregistered.
241     $som = $stub->unregisterVM(config => $testVM);
242
243     # Test isRegisteredVM() method.
244     $som = $stub->isRegisteredVM(config => $testVM);
245
246     # The test VM should not be registered.
247     ok(!$som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
248 };
249
250 # Kill the child daemon, if it still exists.
251 HoneyClient::Manager::VM->destroy();
252
253 # Report any failure found.
254 if ($@) {
255     fail($@);
256 }
257 }
258
259
260
261 # =begin testing
262 {
263 # Shared test variables.
264 my $PORT = getVar(name      => "port",
265                   namespace => "HoneyClient::Manager::VM");
266 my ($stub, $som, $URL);
267 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
268                                       namespace => "HoneyClient::Manager::VM::Test");
269
270 # Catch all errors, in order to make sure child processes are
271 # properly killed.
272 eval {
273
274     $URL = HoneyClient::Manager::VM->init();
275    
276     # Connect to daemon as a client.
277     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
278
279     # Register the test VM.
280     $som = $stub->registerVM(config => $testVM);
281
282     # Test enumerate() method.
283     $som = $stub->enumerate();
284
285     # The test VM should be listed in the output.
286     my @list = $som->paramsall;
287     like(join(' ', @list), "/$testVM/", "enumerate()") or diag("The enumerate() call failed.  Attempted to register VM ($testVM), but the VM was not listed in the output of enumerate().");
288    
289     # Unregister the test VM.
290     $som = $stub->unregisterVM(config => $testVM);
291 };
292
293 # Kill the child daemon, if it still exists.
294 HoneyClient::Manager::VM->destroy();
295
296 # Report any failure found.
297 if ($@) {
298     fail($@);
299 }
300 }
301
302
303
304 # =begin testing
305 {
306 # Shared test variables.
307 my $PORT = getVar(name      => "port",
308                   namespace => "HoneyClient::Manager::VM");
309 my ($stub, $som, $URL);
310 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
311                                       namespace => "HoneyClient::Manager::VM::Test");
312
313 # Catch all errors, in order to make sure child processes are
314 # properly killed.
315 eval {
316
317     $URL = HoneyClient::Manager::VM->init();
318    
319     # Connect to daemon as a client.
320     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
321
322     # Register the test VM.
323     $som = $stub->registerVM(config => $testVM);
324
325     # Test getStateVM() method.
326     $som = $stub->getStateVM(config => $testVM);
327
328     # The test VM should be off.
329     is($som->result, VM_EXECUTION_STATE_OFF, "getStateVM(config => '$testVM')") or diag("The getStateVM() call failed.  Attempted to register VM ($testVM), but the VM state was not reported as OFF.");
330    
331     # Unregister the test VM.
332     $som = $stub->unregisterVM(config => $testVM);
333 };
334
335 # Kill the child daemon, if it still exists.
336 HoneyClient::Manager::VM->destroy();
337
338 # Report any failure found.
339 if ($@) {
340     fail($@);
341 }
342 }
343
344
345
346 # =begin testing
347 {
348 # Shared test variables.
349 my $PORT = getVar(name      => "port",
350                   namespace => "HoneyClient::Manager::VM");
351 my ($stub, $som, $URL);
352 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
353                                       namespace => "HoneyClient::Manager::VM::Test");
354
355 # Catch all errors, in order to make sure child processes are
356 # properly killed.
357 eval {
358
359     $URL = HoneyClient::Manager::VM->init();
360    
361     # Connect to daemon as a client.
362     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
363
364     # Register the test VM.
365     $som = $stub->registerVM(config => $testVM);
366
367     # Test startVM() method.
368     $som = $stub->startVM(config => $testVM);
369     ok($som->result, "startVM(config => '$testVM')");
370
371     # The test VM should be on.
372     $som = $stub->getStateVM(config => $testVM);
373
374     # Since the test VM doesn't have an OS installed on it,
375     # the VM may be considered stuck.  Go ahead and answer
376     # this question, if need be.
377     if ($som->result == VM_EXECUTION_STATE_STUCK) {
378         $som = $stub->answerVM(config => $testVM);
379         # Fetch the state again, to see if it's now ON.
380         $som = $stub->getStateVM(config => $testVM);
381     }
382     is($som->result, VM_EXECUTION_STATE_ON, "startVM(config => '$testVM')") or diag("The startVM() call failed.  Attempted to start VM ($testVM), but the VM state was not reported as ON.");
383    
384     # Stop and unregister the test VM.
385     $som = $stub->stopVM(config => $testVM);
386     $som = $stub->unregisterVM(config => $testVM);
387 };
388
389 # Kill the child daemon, if it still exists.
390 HoneyClient::Manager::VM->destroy();
391
392 # Report any failure found.
393 if ($@) {
394     fail($@);
395 }
396 }
397
398
399
400 # =begin testing
401 {
402 # Shared test variables.
403 my $PORT = getVar(name      => "port",
404                   namespace => "HoneyClient::Manager::VM");
405 my ($stub, $som, $URL);
406 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
407                                       namespace => "HoneyClient::Manager::VM::Test");
408
409 # Catch all errors, in order to make sure child processes are
410 # properly killed.
411 eval {
412
413     $URL = HoneyClient::Manager::VM->init();
414    
415     # Connect to daemon as a client.
416     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
417
418     # Register and start the test VM.
419     $som = $stub->registerVM(config => $testVM);
420     $som = $stub->startVM(config => $testVM);
421
422     # Test stopVM() method.
423     $som = $stub->stopVM(config => $testVM);
424     ok($som->result, "stopVM(config => '$testVM')");
425
426     # The test VM should be on.
427     $som = $stub->getStateVM(config => $testVM);
428     is($som->result, VM_EXECUTION_STATE_OFF, "stopVM(config => '$testVM')") or diag("The stopVM() call failed.  Attempted to stop VM ($testVM), but the VM state was not reported as OFF.");
429    
430     # Unregister the test VM.
431     $som = $stub->unregisterVM(config => $testVM);
432 };
433
434 # Kill the child daemon, if it still exists.
435 HoneyClient::Manager::VM->destroy();
436
437 # Report any failure found.
438 if ($@) {
439     fail($@);
440 }
441 }
442
443
444
445 # =begin testing
446 {
447 # Shared test variables.
448 my $PORT = getVar(name      => "port",
449                   namespace => "HoneyClient::Manager::VM");
450 my ($stub, $som, $URL);
451 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
452                                       namespace => "HoneyClient::Manager::VM::Test");
453
454 # Catch all errors, in order to make sure child processes are
455 # properly killed.
456 eval {
457
458     $URL = HoneyClient::Manager::VM->init();
459    
460     # Connect to daemon as a client.
461     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
462
463     # Register and start the test VM.
464     $som = $stub->registerVM(config => $testVM);
465     $som = $stub->startVM(config => $testVM);
466
467     # Test rebootVM() method.
468     $som = $stub->rebootVM(config => $testVM);
469     ok($som->result, "rebootVM(config => '$testVM')");
470
471     # The test VM should be on.
472     $som = $stub->getStateVM(config => $testVM);
473
474     # Since the test VM doesn't have an OS installed on it,
475     # the VM may be considered stuck.  Go ahead and answer
476     # this question, if need be.
477     if ($som->result == VM_EXECUTION_STATE_STUCK) {
478         $som = $stub->answerVM(config => $testVM);
479         # Fetch the state again, to see if it's now ON.
480         $som = $stub->getStateVM(config => $testVM);
481     }
482     is($som->result, VM_EXECUTION_STATE_ON, "rebootVM(config => '$testVM')") or diag("The rebootVM() call failed.  Attempted to reboot VM ($testVM), but the VM state was not reported as ON.");
483    
484     # Stop and unregister the test VM.
485     $som = $stub->stopVM(config => $testVM);
486     $som = $stub->unregisterVM(config => $testVM);
487 };
488
489 # Kill the child daemon, if it still exists.
490 HoneyClient::Manager::VM->destroy();
491
492 # Report any failure found.
493 if ($@) {
494     fail($@);
495 }
496 }
497
498
499
500 # =begin testing
501 {
502 # Shared test variables.
503 my $PORT = getVar(name      => "port",
504                   namespace => "HoneyClient::Manager::VM");
505 my ($stub, $som, $URL);
506 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
507                                       namespace => "HoneyClient::Manager::VM::Test");
508
509 # Catch all errors, in order to make sure child processes are
510 # properly killed.
511 eval {
512
513     $URL = HoneyClient::Manager::VM->init();
514    
515     # Connect to daemon as a client.
516     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
517
518     # Register and start the test VM.
519     $som = $stub->registerVM(config => $testVM);
520     $som = $stub->startVM(config => $testVM);
521
522     # Test suspendVM() method.
523     $som = $stub->suspendVM(config => $testVM);
524     ok($som->result, "suspendVM(config => '$testVM')");
525
526     # The test VM should be suspended.
527     $som = $stub->getStateVM(config => $testVM);
528     is($som->result, VM_EXECUTION_STATE_SUSPENDED, "suspendVM(config => '$testVM')") or diag("The suspendVM() call failed.  Attempted to suspend VM ($testVM), but the VM state was not reported as SUSPENDED.");
529
530     # Wake, stop, and unregister the test VM.
531     $som = $stub->startVM(config => $testVM);
532     $som = $stub->stopVM(config => $testVM);
533     $som = $stub->unregisterVM(config => $testVM);
534 };
535
536 # Kill the child daemon, if it still exists.
537 HoneyClient::Manager::VM->destroy();
538
539 # Report any failure found.
540 if ($@) {
541     fail($@);
542 }
543 }
544
545
546
547 # =begin testing
548 {
549 # Shared test variables.
550 my $PORT = getVar(name      => "port",
551                   namespace => "HoneyClient::Manager::VM");
552 my ($stub, $som, $URL);
553 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
554                                       namespace => "HoneyClient::Manager::VM::Test");
555
556 # Catch all errors, in order to make sure child processes are
557 # properly killed.
558 eval {
559
560     $URL = HoneyClient::Manager::VM->init();
561    
562     # Connect to daemon as a client.
563     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
564
565     # Register the test VM.
566     $som = $stub->registerVM(config => $testVM);
567
568     # Get the test VM's parent directory,
569     # in order to create a temporary clone VM.
570     my $testVMDir = dirname($testVM);
571     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
572     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
573
574     # Test fullCloneVM() method.
575     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
576     # Check to see if the clone's absolute file path is returned.
577     is($som->result, $cloneVM, "fullCloneVM(src_config => '$testVM', dest_dir => '$cloneVMDir')");
578
579     # Wait a small amount of time for the asynchronous clone
580     # to complete.
581     sleep (15);
582
583     # The clone VM should be on.
584     $som = $stub->getStateVM(config => $cloneVM);
585
586     # Since the clone VM doesn't have an OS installed on it,
587     # the VM may be considered stuck.  Go ahead and answer
588     # this question, if need be.
589     if ($som->result == VM_EXECUTION_STATE_STUCK) {
590         $som = $stub->answerVM(config => $cloneVM);
591         # Fetch the state again, to see if it's now ON.
592         $som = $stub->getStateVM(config => $cloneVM);
593     }
594     is($som->result, VM_EXECUTION_STATE_ON, "fullCloneVM(src_config => '$testVM', dest_dir => '$cloneVMDir')") or diag("The fullCloneVM() call failed.  Attempted to fully clone VM ($testVM) at ($cloneVM), but the cloned VM state was not reported as ON.");
595  
596     # Destroy the clone VM.
597     $som = $stub->destroyVM(config => $cloneVM);
598
599     # Stop and unregister the test VM.
600     $som = $stub->stopVM(config => $testVM);
601     $som = $stub->unregisterVM(config => $testVM);
602 };
603
604 # Kill the child daemon, if it still exists.
605 HoneyClient::Manager::VM->destroy();
606
607 # Report any failure found.
608 if ($@) {
609     fail($@);
610 }
611 }
612
613
614
615 # =begin testing
616 {
617 # Shared test variables.
618 my $PORT = getVar(name      => "port",
619                   namespace => "HoneyClient::Manager::VM");
620 my ($stub, $som, $URL);
621 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
622                                       namespace => "HoneyClient::Manager::VM::Test");
623
624 # Catch all errors, in order to make sure child processes are
625 # properly killed.
626 eval {
627
628     $URL = HoneyClient::Manager::VM->init();
629    
630     # Connect to daemon as a client.
631     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
632
633     # Make sure the test VM is registered.
634     $som = $stub->registerVM(config => $testVM);
635
636     # Test getNameVM() method.
637     $som = $stub->getNameVM(config => $testVM);
638
639     # The test VM should not be registered.
640     is($som->result, "testVM", "getNameVM(config => '$testVM')") or diag("The getNameVM() call failed.  Expected VM ($testVM) to have the name \"testVM\".");
641    
642     # Unregister the test VM.
643     $som = $stub->unregisterVM(config => $testVM);
644 };
645
646 # Kill the child daemon, if it still exists.
647 HoneyClient::Manager::VM->destroy();
648
649 # Report any failure found.
650 if ($@) {
651     fail($@);
652 }
653 }
654
655
656
657 # =begin testing
658 {
659 # Shared test variables.
660 my $PORT = getVar(name      => "port",
661                   namespace => "HoneyClient::Manager::VM");
662 my ($stub, $som, $URL);
663 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
664                                       namespace => "HoneyClient::Manager::VM::Test");
665
666 # Catch all errors, in order to make sure child processes are
667 # properly killed.
668 eval {
669
670     $URL = HoneyClient::Manager::VM->init();
671    
672     # Connect to daemon as a client.
673     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
674
675     # Register the test VM.
676     $som = $stub->registerVM(config => $testVM);
677
678     # Get the current name of the test VM.
679     $som = $stub->getNameVM(config => $testVM);
680     my $oldName = $som->result;
681
682     # Set the new name of the test VM.
683     my $newName = "newVM";
684     # Test setNameVM() method.
685     $som = $stub->setNameVM(config => $testVM, name => $newName);
686     is($som->result, $newName, "setNameVM(config => '$testVM', name => '$newName')") or diag("The setNameVM() call failed.  Attempted to change the test VM ($testVM) name of \"$oldName\" to \"$newName\".");
687
688     # Check to make sure the new name is set.
689     $som = $stub->getNameVM(config => $testVM);
690     is($som->result, $newName, "setNameVM(config => '$testVM', name => '$newName')") or diag("The setNameVM() call failed.  Attempted to change the test VM ($testVM) name of \"$oldName\" to \"$newName\".");
691
692     # Restore the old test VM name and unregister the test VM.
693     $som = $stub->setNameVM(config => $testVM, name => $oldName);
694     $som = $stub->unregisterVM(config => $testVM);
695 };
696
697 # Kill the child daemon, if it still exists.
698 HoneyClient::Manager::VM->destroy();
699
700 # Report any failure found.
701 if ($@) {
702     fail($@);
703 }
704 }
705
706
707
708 # =begin testing
709 {
710 # Shared test variables.
711 my $PORT = getVar(name      => "port",
712                   namespace => "HoneyClient::Manager::VM");
713 my ($stub, $som, $URL);
714 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
715                                       namespace => "HoneyClient::Manager::VM::Test");
716
717 # Catch all errors, in order to make sure child processes are
718 # properly killed.
719 eval {
720
721     $URL = HoneyClient::Manager::VM->init();
722    
723     # Connect to daemon as a client.
724     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
725
726     # Register the test VM.
727     $som = $stub->registerVM(config => $testVM);
728
729     # Get the MAC address of the test VM.
730     # Test getMACaddrVM() method.
731     $som = $stub->getMACaddrVM(config => $testVM);
732
733     # The exact MAC address of the VM will change from system to system,
734     # so we check to make sure the result looks like a valid MAC address.
735     like($som->result, "/[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]/", "getMACaddrVM(config => '$testVM')") or diag("The getMACaddrVM() call failed.  Attempted to retrieve the MAC address of test VM ($testVM).");
736
737     # Unregister the test VM.
738     $som = $stub->unregisterVM(config => $testVM);
739 };
740
741 # Kill the child daemon, if it still exists.
742 HoneyClient::Manager::VM->destroy();
743
744 # Report any failure found.
745 if ($@) {
746     fail($@);
747 }
748 }
749
750
751
752 # =begin testing
753 {
754 # Shared test variables.
755 my $PORT = getVar(name      => "port",
756                   namespace => "HoneyClient::Manager::VM");
757 my ($stub, $som, $URL);
758 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
759                                       namespace => "HoneyClient::Manager::VM::Test");
760
761 # Catch all errors, in order to make sure child processes are
762 # properly killed.
763 eval {
764
765     $URL = HoneyClient::Manager::VM->init();
766    
767     # Connect to daemon as a client.
768     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
769
770     # Register and start the test VM.
771     $som = $stub->registerVM(config => $testVM);
772     $som = $stub->startVM(config => $testVM);
773
774     # Wait 10 seconds, for the DHCP server to give the testVM
775     # a DHCP lease.
776     sleep (10);
777
778     # Get the IP address of the test VM.
779     # Test getIPaddrVM() method.
780     $som = $stub->getIPaddrVM(config => $testVM);
781
782     # The exact IP address of the VM will change from system to system,
783     # so we check to make sure the result looks like a valid IP address.
784     like($som->result, "/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", "getIPaddrVM(config => '$testVM')") or diag("The getIPaddrVM() call failed.  Attempted to retrieve the IP address of test VM ($testVM).");
785
786     # Stop and unregister the test VM.
787     $som = $stub->stopVM(config => $testVM);
788     $som = $stub->unregisterVM(config => $testVM);
789 };
790
791 # Kill the child daemon, if it still exists.
792 HoneyClient::Manager::VM->destroy();
793
794 # Report any failure found.
795 if ($@) {
796     fail($@);
797 }
798 }
799
800
801
802 # =begin testing
803 {
804 # Shared test variables.
805 my $PORT = getVar(name      => "port",
806                   namespace => "HoneyClient::Manager::VM");
807 my ($stub, $som, $URL);
808 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
809                                       namespace => "HoneyClient::Manager::VM::Test");
810
811 # Catch all errors, in order to make sure child processes are
812 # properly killed.
813 eval {
814
815     $URL = HoneyClient::Manager::VM->init();
816    
817     # Connect to daemon as a client.
818     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
819
820     # Make sure the test VM is stopped and unregistered.
821     $som = $stub->stopVM(config => $testVM);
822     $som = $stub->unregisterVM(config => $testVM);
823
824     # Test registerVM() method.
825     $som = $stub->registerVM(config => $testVM);
826     ok($som->result, "registerVM(config => '$testVM')") or diag("The registerVM() call failed.");
827
828     # The test VM should be registered.
829     $som = $stub->isRegisteredVM(config => $testVM);
830     ok($som->result, "registerVM(config => '$testVM')") or diag("The registerVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
831
832     # Unregister the test VM.
833     $som = $stub->unregisterVM(config => $testVM);
834 };
835
836 # Kill the child daemon, if it still exists.
837 HoneyClient::Manager::VM->destroy();
838
839 # Report any failure found.
840 if ($@) {
841     fail($@);
842 }
843 }
844
845
846
847 # =begin testing
848 {
849 # Shared test variables.
850 my $PORT = getVar(name      => "port",
851                   namespace => "HoneyClient::Manager::VM");
852 my ($stub, $som, $URL);
853 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
854                                       namespace => "HoneyClient::Manager::VM::Test");
855
856 # Catch all errors, in order to make sure child processes are
857 # properly killed.
858 eval {
859
860     $URL = HoneyClient::Manager::VM->init();
861    
862     # Connect to daemon as a client.
863     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
864
865     # Make sure the test VM is stopped and registered.
866     $som = $stub->stopVM(config => $testVM);
867     $som = $stub->registerVM(config => $testVM);
868
869     # Test unregisterVM() method.
870     $som = $stub->unregisterVM(config => $testVM);
871     ok($som->result, "unregisterVM(config => '$testVM')") or diag("The unregisterVM() call failed.");
872
873     # The test VM should be registered.
874     $som = $stub->isRegisteredVM(config => $testVM);
875     ok(!$som->result, "unregisterVM(config => '$testVM')") or diag("The unregisterVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
876 };
877
878 # Kill the child daemon, if it still exists.
879 HoneyClient::Manager::VM->destroy();
880
881 # Report any failure found.
882 if ($@) {
883     fail($@);
884 }
885 }
886
887
888
889 # =begin testing
890 {
891 # Shared test variables.
892 my $PORT = getVar(name      => "port",
893                   namespace => "HoneyClient::Manager::VM");
894 my ($stub, $som, $URL);
895 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
896                                       namespace => "HoneyClient::Manager::VM::Test");
897
898 # Catch all errors, in order to make sure child processes are
899 # properly killed.
900 eval {
901
902     $URL = HoneyClient::Manager::VM->init();
903    
904     # Connect to daemon as a client.
905     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
906
907     # Make sure the test VM is stopped and unregistered.
908     $som = $stub->stopVM(config => $testVM);
909     $som = $stub->unregisterVM(config => $testVM);
910
911     # The only consistent way to get a VM into a stuck state,
912     # is to manually copy a VM into a new directory, register it,
913     # and then proceed to start it.  VMware Server / GSX will immediately
914     # ask if we'd like to create a new identifier before
915     # moving on.
916
917     # Get the test VM's parent directory,
918     # in order to create a temporary clone VM.
919     my $testVMDir = dirname($testVM);
920     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
921     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
922
923     # Make the destDir.
924     if (!dircopy($testVMDir, $cloneVMDir)) {
925         fail("answerVM()");
926         diag("Could not copy test VM directory ($testVMDir) for testing answerVM() method.");
927     } else {
928         # Update clone VM data permissions...
929         chmod(oct(700), $cloneVM);
930         chmod(oct(700), glob($cloneVMDir . "/*.nvram"));
931         chmod(oct(600), glob($cloneVMDir . "/*.vms*"));
932         chmod(oct(600), glob($cloneVMDir . "/*REDO*"));
933     }
934
935     # Register the clone VM.
936     $som = $stub->registerVM(config => $cloneVM);
937
938     # Start the clone VM.
939     # Test answerVM() method.
940     $som = $stub->startVM(config => $cloneVM);
941     ok($som->result, "answerVM(config => '$cloneVM')") or diag("The answerVM() call failed.");
942
943     # Destroy the clone VM.
944     $som = $stub->destroyVM(config => $cloneVM);
945 };
946
947 # Kill the child daemon, if it still exists.
948 HoneyClient::Manager::VM->destroy();
949
950 # Report any failure found.
951 if ($@) {
952     fail($@);
953 }
954 }
955
956
957
958 # =begin testing
959 {
960 # Shared test variables.
961 my $PORT = getVar(name      => "port",
962                   namespace => "HoneyClient::Manager::VM");
963 my ($stub, $som, $URL);
964 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
965                                       namespace => "HoneyClient::Manager::VM::Test");
966
967 # Catch all errors, in order to make sure child processes are
968 # properly killed.
969 eval {
970
971     $URL = HoneyClient::Manager::VM->init();
972    
973     # Connect to daemon as a client.
974     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
975
976     # Register the test VM.
977     $som = $stub->registerVM(config => $testVM);
978
979     # Get the test VM's parent directory,
980     # in order to create a temporary clone VM.
981     my $testVMDir = dirname($testVM);
982     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
983     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
984
985     # Clone the test VM.
986     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
987
988     # Wait a small amount of time for the asynchronous clone
989     # to complete.
990     sleep (15);
991
992     # The clone VM should be on.
993     $som = $stub->getStateVM(config => $cloneVM);
994
995     # Since the clone VM doesn't have an OS installed on it,
996     # the VM may be considered stuck.  Go ahead and answer
997     # this question, if need be.
998     if ($som->result == VM_EXECUTION_STATE_STUCK) {
999         $som = $stub->answerVM(config => $cloneVM);
1000     }
1001
1002     # Destroy the clone VM.
1003     $som = $stub->destroyVM(config => $cloneVM);
1004
1005     # Test destroyVM() method.
1006     ok($som->result, "destroyVM(config => '$cloneVM')") or diag("The destroyVM() call failed.");
1007
1008     # Check to make sure the clone VM is unregistered.
1009     $som = $stub->isRegisteredVM(config => $cloneVM);
1010     ok(!$som->result, "destroyVM(config => '$cloneVM')") or diag("The destroyVM() call failed.  If ($cloneVM) is still registered, be sure to unregister it manually.");
1011
1012     # Stop and unregister the test VM.
1013     $som = $stub->stopVM(config => $testVM);
1014     $som = $stub->unregisterVM(config => $testVM);
1015 };
1016
1017 # Kill the child daemon, if it still exists.
1018 HoneyClient::Manager::VM->destroy();
1019
1020 # Report any failure found.
1021 if ($@) {
1022     fail($@);
1023 }
1024 }
1025
1026
1027
1028 # =begin testing
1029 {
1030 # Shared test variables.
1031 my $PORT = getVar(name      => "port",
1032                   namespace => "HoneyClient::Manager::VM");
1033 my ($stub, $som, $URL);
1034 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
1035                                       namespace => "HoneyClient::Manager::VM::Test");
1036
1037 # Catch all errors, in order to make sure child processes are
1038 # properly killed.
1039 eval {
1040
1041     $URL = HoneyClient::Manager::VM->init();
1042    
1043     # Connect to daemon as a client.
1044     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
1045
1046     # Register the test VM.
1047     $som = $stub->registerVM(config => $testVM);
1048
1049     # In order to test the setMasterVM() method,
1050     # we're going to clone the test VM, then set the clone
1051     # as a master VM, and finally, check to make sure
1052     # the corresponding permissions were set in the
1053     # clone, as per master VM specification.
1054
1055     # Get the test VM's parent directory,
1056     # in order to create a temporary clone VM.
1057     my $testVMDir = dirname($testVM);
1058     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
1059     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
1060
1061     # Create the clone VM.
1062     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
1063
1064     # Wait a small amount of time for the asynchronous clone
1065     # to complete.
1066     sleep (15);
1067
1068     # The clone VM should be on.
1069     $som = $stub->getStateVM(config => $cloneVM);
1070
1071     # Since the clone VM doesn't have an OS installed on it,
1072     # the VM may be considered stuck.  Go ahead and answer
1073     # this question, if need be.
1074     if ($som->result == VM_EXECUTION_STATE_STUCK) {
1075         $som = $stub->answerVM(config => $cloneVM);
1076     }
1077
1078     # Set the clone as a master VM.
1079     $som = $stub->setMasterVM(config => $cloneVM);
1080
1081     # Test setMasterVM() method.
1082     ok($som->result, "setMasterVM(config => '$cloneVM')") or diag("The setMasterVM() call failed.");
1083
1084     my $mode = undef;
1085     foreach (glob($cloneVMDir . "/*.vmdk*"),
1086              glob($cloneVMDir . "/*.vms*"),
1087              glob($cloneVMDir . "/*.vme*")) {
1088         $mode = sprintf("%04o", stat($_)->mode & 07777);
1089         is($mode, "0440", "setMasterVM(config => '$cloneVM')") or diag("The setMasterVM() call failed.  Expected file ($_) to be mode 0440, but it was mode $mode instead.");
1090     }
1091
1092     # Destroy the clone VM.
1093     $som = $stub->destroyVM(config => $cloneVM);
1094
1095     # Stop and unregister the test VM.
1096     $som = $stub->stopVM(config => $testVM);
1097     $som = $stub->unregisterVM(config => $testVM);
1098 };
1099
1100 # Kill the child daemon, if it still exists.
1101 HoneyClient::Manager::VM->destroy();
1102
1103 # Report any failure found.
1104 if ($@) {
1105     fail($@);
1106 }
1107 }
1108
1109
1110
1111 # =begin testing
1112 {
1113 # Shared test variables.
1114 my $PORT = getVar(name      => "port",
1115                   namespace => "HoneyClient::Manager::VM");
1116 my ($stub, $som, $URL);
1117 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
1118                                       namespace => "HoneyClient::Manager::VM::Test");
1119
1120 # Catch all errors, in order to make sure child processes are
1121 # properly killed.
1122 eval {
1123
1124     $URL = HoneyClient::Manager::VM->init();
1125    
1126     # Connect to daemon as a client.
1127     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
1128
1129     # Register the test VM.
1130     $som = $stub->registerVM(config => $testVM);
1131
1132     # In order to test the quickCloneVM() method,
1133     # we're going to fully clone the test VM, then set the
1134     # newly created clone as a master VM, and finally,
1135     # create a secondary quick clone from the master VM.
1136
1137     # Get the test VM's parent directory,
1138     # in order to create a temporary master and clone VM.
1139     my $testVMDir = dirname($testVM);
1140     my $masterVMDir = dirname($testVMDir) . "/test_vm_master";
1141     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
1142     my $masterVM = $masterVMDir . "/" . basename($testVM);
1143     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
1144
1145     # Create the master VM.
1146     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $masterVMDir);
1147
1148     # Wait a small amount of time for the asynchronous clone
1149     # to complete.
1150     sleep (60);
1151
1152     # The master VM should be on.
1153     $som = $stub->getStateVM(config => $masterVM);
1154
1155     # Since the master VM doesn't have an OS installed on it,
1156     # the VM may be considered stuck.  Go ahead and answer
1157     # this question, if need be.
1158     if ($som->result == VM_EXECUTION_STATE_STUCK) {
1159         $som = $stub->answerVM(config => $masterVM);
1160     }
1161
1162     # Set the master VM as a true master.
1163     $som = $stub->quickCloneVM(src_config => $masterVM, dest_dir => $cloneVMDir);
1164    
1165     # Test quickCloneVM() method.
1166     is($som->result, $cloneVM, "quickCloneVM(src_config => '$masterVM', dest_dir => '$cloneVMDir')") or diag("The quickCloneVM() call failed.");
1167    
1168     # Wait a small amount of time for the asynchronous clone
1169     # to complete