root/honeyclient/branches/rel/1.0/t/honeyclient_manager_vm.t

Revision 788, 48.3 kB (checked in by kindlund, 1 year ago)

Fixed permissions issue in 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 sleep (1);
253
254 # Report any failure found.
255 if ($@) {
256     fail($@);
257 }
258 }
259
260
261
262 # =begin testing
263 {
264 # Shared test variables.
265 my $PORT = getVar(name      => "port",
266                   namespace => "HoneyClient::Manager::VM");
267 my ($stub, $som, $URL);
268 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
269                                       namespace => "HoneyClient::Manager::VM::Test");
270
271 # Catch all errors, in order to make sure child processes are
272 # properly killed.
273 eval {
274
275     $URL = HoneyClient::Manager::VM->init();
276    
277     # Connect to daemon as a client.
278     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
279
280     # Register the test VM.
281     $som = $stub->registerVM(config => $testVM);
282
283     # Test enumerate() method.
284     $som = $stub->enumerate();
285
286     # The test VM should be listed in the output.
287     my @list = $som->paramsall;
288     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().");
289    
290     # Unregister the test VM.
291     $som = $stub->unregisterVM(config => $testVM);
292 };
293
294 # Kill the child daemon, if it still exists.
295 HoneyClient::Manager::VM->destroy();
296 sleep (1);
297
298 # Report any failure found.
299 if ($@) {
300     fail($@);
301 }
302 }
303
304
305
306 # =begin testing
307 {
308 # Shared test variables.
309 my $PORT = getVar(name      => "port",
310                   namespace => "HoneyClient::Manager::VM");
311 my ($stub, $som, $URL);
312 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
313                                       namespace => "HoneyClient::Manager::VM::Test");
314
315 # Catch all errors, in order to make sure child processes are
316 # properly killed.
317 eval {
318
319     $URL = HoneyClient::Manager::VM->init();
320    
321     # Connect to daemon as a client.
322     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
323
324     # Register the test VM.
325     $som = $stub->registerVM(config => $testVM);
326
327     # Test getStateVM() method.
328     $som = $stub->getStateVM(config => $testVM);
329
330     # The test VM should be off.
331     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.");
332    
333     # Unregister the test VM.
334     $som = $stub->unregisterVM(config => $testVM);
335 };
336
337 # Kill the child daemon, if it still exists.
338 HoneyClient::Manager::VM->destroy();
339 sleep (1);
340
341 # Report any failure found.
342 if ($@) {
343     fail($@);
344 }
345 }
346
347
348
349 # =begin testing
350 {
351 # Shared test variables.
352 my $PORT = getVar(name      => "port",
353                   namespace => "HoneyClient::Manager::VM");
354 my ($stub, $som, $URL);
355 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
356                                       namespace => "HoneyClient::Manager::VM::Test");
357
358 # Catch all errors, in order to make sure child processes are
359 # properly killed.
360 eval {
361
362     $URL = HoneyClient::Manager::VM->init();
363    
364     # Connect to daemon as a client.
365     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
366
367     # Register the test VM.
368     $som = $stub->registerVM(config => $testVM);
369
370     # Test startVM() method.
371     $som = $stub->startVM(config => $testVM);
372     ok($som->result, "startVM(config => '$testVM')");
373
374     # The test VM should be on.
375     $som = $stub->getStateVM(config => $testVM);
376
377     # Since the test VM doesn't have an OS installed on it,
378     # the VM may be considered stuck.  Go ahead and answer
379     # this question, if need be.
380     if ($som->result == VM_EXECUTION_STATE_STUCK) {
381         $som = $stub->answerVM(config => $testVM);
382         # Fetch the state again, to see if it's now ON.
383         $som = $stub->getStateVM(config => $testVM);
384     }
385     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.");
386    
387     # Stop and unregister the test VM.
388     $som = $stub->stopVM(config => $testVM);
389     $som = $stub->unregisterVM(config => $testVM);
390 };
391
392 # Kill the child daemon, if it still exists.
393 HoneyClient::Manager::VM->destroy();
394 sleep (1);
395
396 # Report any failure found.
397 if ($@) {
398     fail($@);
399 }
400 }
401
402
403
404 # =begin testing
405 {
406 # Shared test variables.
407 my $PORT = getVar(name      => "port",
408                   namespace => "HoneyClient::Manager::VM");
409 my ($stub, $som, $URL);
410 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
411                                       namespace => "HoneyClient::Manager::VM::Test");
412
413 # Catch all errors, in order to make sure child processes are
414 # properly killed.
415 eval {
416
417     $URL = HoneyClient::Manager::VM->init();
418    
419     # Connect to daemon as a client.
420     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
421
422     # Register and start the test VM.
423     $som = $stub->registerVM(config => $testVM);
424     $som = $stub->startVM(config => $testVM);
425
426     # Test stopVM() method.
427     $som = $stub->stopVM(config => $testVM);
428     ok($som->result, "stopVM(config => '$testVM')");
429
430     # The test VM should be on.
431     $som = $stub->getStateVM(config => $testVM);
432     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.");
433    
434     # Unregister the test VM.
435     $som = $stub->unregisterVM(config => $testVM);
436 };
437
438 # Kill the child daemon, if it still exists.
439 HoneyClient::Manager::VM->destroy();
440 sleep (1);
441
442 # Report any failure found.
443 if ($@) {
444     fail($@);
445 }
446 }
447
448
449
450 # =begin testing
451 {
452 # Shared test variables.
453 my $PORT = getVar(name      => "port",
454                   namespace => "HoneyClient::Manager::VM");
455 my ($stub, $som, $URL);
456 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
457                                       namespace => "HoneyClient::Manager::VM::Test");
458
459 # Catch all errors, in order to make sure child processes are
460 # properly killed.
461 eval {
462
463     $URL = HoneyClient::Manager::VM->init();
464    
465     # Connect to daemon as a client.
466     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
467
468     # Register and start the test VM.
469     $som = $stub->registerVM(config => $testVM);
470     $som = $stub->startVM(config => $testVM);
471
472     # Test rebootVM() method.
473     $som = $stub->rebootVM(config => $testVM);
474     ok($som->result, "rebootVM(config => '$testVM')");
475
476     # The test VM should be on.
477     $som = $stub->getStateVM(config => $testVM);
478
479     # Since the test VM doesn't have an OS installed on it,
480     # the VM may be considered stuck.  Go ahead and answer
481     # this question, if need be.
482     if ($som->result == VM_EXECUTION_STATE_STUCK) {
483         $som = $stub->answerVM(config => $testVM);
484         # Fetch the state again, to see if it's now ON.
485         $som = $stub->getStateVM(config => $testVM);
486     }
487     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.");
488    
489     # Stop and unregister the test VM.
490     $som = $stub->stopVM(config => $testVM);
491     $som = $stub->unregisterVM(config => $testVM);
492 };
493
494 # Kill the child daemon, if it still exists.
495 HoneyClient::Manager::VM->destroy();
496 sleep (1);
497
498 # Report any failure found.
499 if ($@) {
500     fail($@);
501 }
502 }
503
504
505
506 # =begin testing
507 {
508 # Shared test variables.
509 my $PORT = getVar(name      => "port",
510                   namespace => "HoneyClient::Manager::VM");
511 my ($stub, $som, $URL);
512 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
513                                       namespace => "HoneyClient::Manager::VM::Test");
514
515 # Catch all errors, in order to make sure child processes are
516 # properly killed.
517 eval {
518
519     $URL = HoneyClient::Manager::VM->init();
520    
521     # Connect to daemon as a client.
522     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
523
524     # Register and start the test VM.
525     $som = $stub->registerVM(config => $testVM);
526     $som = $stub->startVM(config => $testVM);
527
528     # Test suspendVM() method.
529     $som = $stub->suspendVM(config => $testVM);
530     ok($som->result, "suspendVM(config => '$testVM')");
531
532     # The test VM should be suspended.
533     $som = $stub->getStateVM(config => $testVM);
534     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.");
535
536     # Wake, stop, and unregister the test VM.
537     $som = $stub->startVM(config => $testVM);
538     $som = $stub->stopVM(config => $testVM);
539     $som = $stub->unregisterVM(config => $testVM);
540 };
541
542 # Kill the child daemon, if it still exists.
543 HoneyClient::Manager::VM->destroy();
544 sleep (1);
545
546 # Report any failure found.
547 if ($@) {
548     fail($@);
549 }
550 }
551
552
553
554 # =begin testing
555 {
556 # Shared test variables.
557 my $PORT = getVar(name      => "port",
558                   namespace => "HoneyClient::Manager::VM");
559 my ($stub, $som, $URL);
560 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
561                                       namespace => "HoneyClient::Manager::VM::Test");
562
563 # Catch all errors, in order to make sure child processes are
564 # properly killed.
565 eval {
566
567     $URL = HoneyClient::Manager::VM->init();
568    
569     # Connect to daemon as a client.
570     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
571
572     # Register the test VM.
573     $som = $stub->registerVM(config => $testVM);
574
575     # Get the test VM's parent directory,
576     # in order to create a temporary clone VM.
577     my $testVMDir = dirname($testVM);
578     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
579     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
580
581     # Test fullCloneVM() method.
582     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
583     # Check to see if the clone's absolute file path is returned.
584     is($som->result, $cloneVM, "fullCloneVM(src_config => '$testVM', dest_dir => '$cloneVMDir')");
585
586     # Wait a small amount of time for the asynchronous clone
587     # to complete.
588     sleep (15);
589
590     # The clone VM should be on.
591     $som = $stub->getStateVM(config => $cloneVM);
592
593     # Since the clone VM doesn't have an OS installed on it,
594     # the VM may be considered stuck.  Go ahead and answer
595     # this question, if need be.
596     if ($som->result == VM_EXECUTION_STATE_STUCK) {
597         $som = $stub->answerVM(config => $cloneVM);
598         # Fetch the state again, to see if it's now ON.
599         $som = $stub->getStateVM(config => $cloneVM);
600     }
601     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.");
602  
603     # Destroy the clone VM.
604     $som = $stub->destroyVM(config => $cloneVM);
605
606     # Stop and unregister the test VM.
607     $som = $stub->stopVM(config => $testVM);
608     $som = $stub->unregisterVM(config => $testVM);
609 };
610
611 # Kill the child daemon, if it still exists.
612 HoneyClient::Manager::VM->destroy();
613 sleep (1);
614
615 # Report any failure found.
616 if ($@) {
617     fail($@);
618 }
619 }
620
621
622
623 # =begin testing
624 {
625 # Shared test variables.
626 my $PORT = getVar(name      => "port",
627                   namespace => "HoneyClient::Manager::VM");
628 my ($stub, $som, $URL);
629 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
630                                       namespace => "HoneyClient::Manager::VM::Test");
631
632 # Catch all errors, in order to make sure child processes are
633 # properly killed.
634 eval {
635
636     $URL = HoneyClient::Manager::VM->init();
637    
638     # Connect to daemon as a client.
639     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
640
641     # Make sure the test VM is registered.
642     $som = $stub->registerVM(config => $testVM);
643
644     # Test getNameVM() method.
645     $som = $stub->getNameVM(config => $testVM);
646
647     # The test VM should not be registered.
648     is($som->result, "testVM", "getNameVM(config => '$testVM')") or diag("The getNameVM() call failed.  Expected VM ($testVM) to have the name \"testVM\".");
649    
650     # Unregister the test VM.
651     $som = $stub->unregisterVM(config => $testVM);
652 };
653
654 # Kill the child daemon, if it still exists.
655 HoneyClient::Manager::VM->destroy();
656 sleep (1);
657
658 # Report any failure found.
659 if ($@) {
660     fail($@);
661 }
662 }
663
664
665
666 # =begin testing
667 {
668 # Shared test variables.
669 my $PORT = getVar(name      => "port",
670                   namespace => "HoneyClient::Manager::VM");
671 my ($stub, $som, $URL);
672 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
673                                       namespace => "HoneyClient::Manager::VM::Test");
674
675 # Catch all errors, in order to make sure child processes are
676 # properly killed.
677 eval {
678
679     $URL = HoneyClient::Manager::VM->init();
680    
681     # Connect to daemon as a client.
682     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
683
684     # Register the test VM.
685     $som = $stub->registerVM(config => $testVM);
686
687     # Get the current name of the test VM.
688     $som = $stub->getNameVM(config => $testVM);
689     my $oldName = $som->result;
690
691     # Set the new name of the test VM.
692     my $newName = "newVM";
693     # Test setNameVM() method.
694     $som = $stub->setNameVM(config => $testVM, name => $newName);
695     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\".");
696
697     # Check to make sure the new name is set.
698     $som = $stub->getNameVM(config => $testVM);
699     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\".");
700
701     # Restore the old test VM name and unregister the test VM.
702     $som = $stub->setNameVM(config => $testVM, name => $oldName);
703     $som = $stub->unregisterVM(config => $testVM);
704 };
705
706 # Kill the child daemon, if it still exists.
707 HoneyClient::Manager::VM->destroy();
708 sleep (1);
709
710 # Report any failure found.
711 if ($@) {
712     fail($@);
713 }
714 }
715
716
717
718 # =begin testing
719 {
720 # Shared test variables.
721 my $PORT = getVar(name      => "port",
722                   namespace => "HoneyClient::Manager::VM");
723 my ($stub, $som, $URL);
724 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
725                                       namespace => "HoneyClient::Manager::VM::Test");
726
727 # Catch all errors, in order to make sure child processes are
728 # properly killed.
729 eval {
730
731     $URL = HoneyClient::Manager::VM->init();
732    
733     # Connect to daemon as a client.
734     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
735
736     # Register the test VM.
737     $som = $stub->registerVM(config => $testVM);
738
739     # Get the MAC address of the test VM.
740     # Test getMACaddrVM() method.
741     $som = $stub->getMACaddrVM(config => $testVM);
742
743     # The exact MAC address of the VM will change from system to system,
744     # so we check to make sure the result looks like a valid MAC address.
745     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).");
746
747     # Unregister the test VM.
748     $som = $stub->unregisterVM(config => $testVM);
749 };
750
751 # Kill the child daemon, if it still exists.
752 HoneyClient::Manager::VM->destroy();
753 sleep (1);
754
755 # Report any failure found.
756 if ($@) {
757     fail($@);
758 }
759 }
760
761
762
763 # =begin testing
764 {
765 # Shared test variables.
766 my $PORT = getVar(name      => "port",
767                   namespace => "HoneyClient::Manager::VM");
768 my ($stub, $som, $URL);
769 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
770                                       namespace => "HoneyClient::Manager::VM::Test");
771
772 # Catch all errors, in order to make sure child processes are
773 # properly killed.
774 eval {
775
776     $URL = HoneyClient::Manager::VM->init();
777    
778     # Connect to daemon as a client.
779     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
780
781     # Register and start the test VM.
782     $som = $stub->registerVM(config => $testVM);
783     $som = $stub->startVM(config => $testVM);
784
785     # Wait 10 seconds, for the DHCP server to give the testVM
786     # a DHCP lease.
787     sleep (10);
788
789     # Get the IP address of the test VM.
790     # Test getIPaddrVM() method.
791     $som = $stub->getIPaddrVM(config => $testVM);
792
793     # The exact IP address of the VM will change from system to system,
794     # so we check to make sure the result looks like a valid IP address.
795     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).");
796
797     # Stop and unregister the test VM.
798     $som = $stub->stopVM(config => $testVM);
799     $som = $stub->unregisterVM(config => $testVM);
800 };
801
802 # Kill the child daemon, if it still exists.
803 HoneyClient::Manager::VM->destroy();
804 sleep (1);
805
806 # Report any failure found.
807 if ($@) {
808     fail($@);
809 }
810 }
811
812
813
814 # =begin testing
815 {
816 # Shared test variables.
817 my $PORT = getVar(name      => "port",
818                   namespace => "HoneyClient::Manager::VM");
819 my ($stub, $som, $URL);
820 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
821                                       namespace => "HoneyClient::Manager::VM::Test");
822
823 # Catch all errors, in order to make sure child processes are
824 # properly killed.
825 eval {
826
827     $URL = HoneyClient::Manager::VM->init();
828    
829     # Connect to daemon as a client.
830     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
831
832     # Make sure the test VM is stopped and unregistered.
833     $som = $stub->stopVM(config => $testVM);
834     $som = $stub->unregisterVM(config => $testVM);
835
836     # Test registerVM() method.
837     $som = $stub->registerVM(config => $testVM);
838     ok($som->result, "registerVM(config => '$testVM')") or diag("The registerVM() call failed.");
839
840     # The test VM should be registered.
841     $som = $stub->isRegisteredVM(config => $testVM);
842     ok($som->result, "registerVM(config => '$testVM')") or diag("The registerVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
843
844     # Unregister the test VM.
845     $som = $stub->unregisterVM(config => $testVM);
846 };
847
848 # Kill the child daemon, if it still exists.
849 HoneyClient::Manager::VM->destroy();
850 sleep (1);
851
852 # Report any failure found.
853 if ($@) {
854     fail($@);
855 }
856 }
857
858
859
860 # =begin testing
861 {
862 # Shared test variables.
863 my $PORT = getVar(name      => "port",
864                   namespace => "HoneyClient::Manager::VM");
865 my ($stub, $som, $URL);
866 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
867                                       namespace => "HoneyClient::Manager::VM::Test");
868
869 # Catch all errors, in order to make sure child processes are
870 # properly killed.
871 eval {
872
873     $URL = HoneyClient::Manager::VM->init();
874    
875     # Connect to daemon as a client.
876     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
877
878     # Make sure the test VM is stopped and registered.
879     $som = $stub->stopVM(config => $testVM);
880     $som = $stub->registerVM(config => $testVM);
881
882     # Test unregisterVM() method.
883     $som = $stub->unregisterVM(config => $testVM);
884     ok($som->result, "unregisterVM(config => '$testVM')") or diag("The unregisterVM() call failed.");
885
886     # The test VM should be registered.
887     $som = $stub->isRegisteredVM(config => $testVM);
888     ok(!$som->result, "unregisterVM(config => '$testVM')") or diag("The unregisterVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually.");
889 };
890
891 # Kill the child daemon, if it still exists.
892 HoneyClient::Manager::VM->destroy();
893 sleep (1);
894
895 # Report any failure found.
896 if ($@) {
897     fail($@);
898 }
899 }
900
901
902
903 # =begin testing
904 {
905 # Shared test variables.
906 my $PORT = getVar(name      => "port",
907                   namespace => "HoneyClient::Manager::VM");
908 my ($stub, $som, $URL);
909 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
910                                       namespace => "HoneyClient::Manager::VM::Test");
911
912 # Catch all errors, in order to make sure child processes are
913 # properly killed.
914 eval {
915
916     $URL = HoneyClient::Manager::VM->init();
917    
918     # Connect to daemon as a client.
919     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
920
921     # Make sure the test VM is stopped and unregistered.
922     $som = $stub->stopVM(config => $testVM);
923     $som = $stub->unregisterVM(config => $testVM);
924
925     # The only consistent way to get a VM into a stuck state,
926     # is to manually copy a VM into a new directory, register it,
927     # and then proceed to start it.  VMware Server / GSX will immediately
928     # ask if we'd like to create a new identifier before
929     # moving on.
930
931     # Get the test VM's parent directory,
932     # in order to create a temporary clone VM.
933     my $testVMDir = dirname($testVM);
934     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
935     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
936
937     # Make the destDir.
938     if (!dircopy($testVMDir, $cloneVMDir)) {
939         fail("answerVM()");
940         diag("Could not copy test VM directory ($testVMDir) for testing answerVM() method.");
941     } else {
942         # Update clone VM data permissions...
943         chmod(oct(700), $cloneVM);
944         chmod(oct(700), glob($cloneVMDir . "/*.nvram"));
945         chmod(oct(600), glob($cloneVMDir . "/*.vms*"));
946         chmod(oct(600), glob($cloneVMDir . "/*REDO*"));
947     }
948
949     # Register the clone VM.
950     $som = $stub->registerVM(config => $cloneVM);
951
952     # Start the clone VM.
953     # Test answerVM() method.
954     $som = $stub->startVM(config => $cloneVM);
955     ok($som->result, "answerVM(config => '$cloneVM')") or diag("The answerVM() call failed.");
956
957     # Destroy the clone VM.
958     $som = $stub->destroyVM(config => $cloneVM);
959 };
960
961 # Kill the child daemon, if it still exists.
962 HoneyClient::Manager::VM->destroy();
963 sleep (1);
964
965 # Report any failure found.
966 if ($@) {
967     fail($@);
968 }
969 }
970
971
972
973 # =begin testing
974 {
975 # Shared test variables.
976 my $PORT = getVar(name      => "port",
977                   namespace => "HoneyClient::Manager::VM");
978 my ($stub, $som, $URL);
979 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
980                                       namespace => "HoneyClient::Manager::VM::Test");
981
982 # Catch all errors, in order to make sure child processes are
983 # properly killed.
984 eval {
985
986     $URL = HoneyClient::Manager::VM->init();
987    
988     # Connect to daemon as a client.
989     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
990
991     # Register the test VM.
992     $som = $stub->registerVM(config => $testVM);
993
994     # Get the test VM's parent directory,
995     # in order to create a temporary clone VM.
996     my $testVMDir = dirname($testVM);
997     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
998     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
999
1000     # Clone the test VM.
1001     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
1002
1003     # Wait a small amount of time for the asynchronous clone
1004     # to complete.
1005     sleep (15);
1006
1007     # The clone VM should be on.
1008     $som = $stub->getStateVM(config => $cloneVM);
1009
1010     # Since the clone VM doesn't have an OS installed on it,
1011     # the VM may be considered stuck.  Go ahead and answer
1012     # this question, if need be.
1013     if ($som->result == VM_EXECUTION_STATE_STUCK) {
1014         $som = $stub->answerVM(config => $cloneVM);
1015     }
1016
1017     # Destroy the clone VM.
1018     $som = $stub->destroyVM(config => $cloneVM);
1019
1020     # Test destroyVM() method.
1021     ok($som->result, "destroyVM(config => '$cloneVM')") or diag("The destroyVM() call failed.");
1022
1023     # Check to make sure the clone VM is unregistered.
1024     $som = $stub->isRegisteredVM(config => $cloneVM);
1025     ok(!$som->result, "destroyVM(config => '$cloneVM')") or diag("The destroyVM() call failed.  If ($cloneVM) is still registered, be sure to unregister it manually.");
1026
1027     # Stop and unregister the test VM.
1028     $som = $stub->stopVM(config => $testVM);
1029     $som = $stub->unregisterVM(config => $testVM);
1030 };
1031
1032 # Kill the child daemon, if it still exists.
1033 HoneyClient::Manager::VM->destroy();
1034 sleep (1);
1035
1036 # Report any failure found.
1037 if ($@) {
1038     fail($@);
1039 }
1040 }
1041
1042
1043
1044 # =begin testing
1045 {
1046 # Shared test variables.
1047 my $PORT = getVar(name      => "port",
1048                   namespace => "HoneyClient::Manager::VM");
1049 my ($stub, $som, $URL);
1050 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
1051                                       namespace => "HoneyClient::Manager::VM::Test");
1052
1053 # Catch all errors, in order to make sure child processes are
1054 # properly killed.
1055 eval {
1056
1057     $URL = HoneyClient::Manager::VM->init();
1058    
1059     # Connect to daemon as a client.
1060     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
1061
1062     # Register the test VM.
1063     $som = $stub->registerVM(config => $testVM);
1064
1065     # In order to test the setMasterVM() method,
1066     # we're going to clone the test VM, then set the clone
1067     # as a master VM, and finally, check to make sure
1068     # the corresponding permissions were set in the
1069     # clone, as per master VM specification.
1070
1071     # Get the test VM's parent directory,
1072     # in order to create a temporary clone VM.
1073     my $testVMDir = dirname($testVM);
1074     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
1075     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
1076
1077     # Create the clone VM.
1078     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $cloneVMDir);
1079
1080     # Wait a small amount of time for the asynchronous clone
1081     # to complete.
1082     sleep (15);
1083
1084     # The clone VM should be on.
1085     $som = $stub->getStateVM(config => $cloneVM);
1086
1087     # Since the clone VM doesn't have an OS installed on it,
1088     # the VM may be considered stuck.  Go ahead and answer
1089     # this question, if need be.
1090     if ($som->result == VM_EXECUTION_STATE_STUCK) {
1091         $som = $stub->answerVM(config => $cloneVM);
1092     }
1093
1094     # Set the clone as a master VM.
1095     $som = $stub->setMasterVM(config => $cloneVM);
1096
1097     # Test setMasterVM() method.
1098     ok($som->result, "setMasterVM(config => '$cloneVM')") or diag("The setMasterVM() call failed.");
1099
1100     my $mode = undef;
1101     foreach (glob($cloneVMDir . "/*.vmdk*"),
1102              glob($cloneVMDir . "/*.vms*"),
1103              glob($cloneVMDir . "/*.vme*")) {
1104         $mode = sprintf("%04o", stat($_)->mode & 07777);
1105         is($mode, "0440", "setMasterVM(config => '$cloneVM')") or diag("The setMasterVM() call failed.  Expected file ($_) to be mode 0440, but it was mode $mode instead.");
1106     }
1107
1108     # Destroy the clone VM.
1109     $som = $stub->destroyVM(config => $cloneVM);
1110
1111     # Stop and unregister the test VM.
1112     $som = $stub->stopVM(config => $testVM);
1113     $som = $stub->unregisterVM(config => $testVM);
1114 };
1115
1116 # Kill the child daemon, if it still exists.
1117 HoneyClient::Manager::VM->destroy();
1118 sleep (1);
1119
1120 # Report any failure found.
1121 if ($@) {
1122     fail($@);
1123 }
1124 }
1125
1126
1127
1128 # =begin testing
1129 {
1130 # Shared test variables.
1131 my $PORT = getVar(name      => "port",
1132                   namespace => "HoneyClient::Manager::VM");
1133 my ($stub, $som, $URL);
1134 my $testVM = $ENV{PWD} . "/" . getVar(name      => "test_vm_config",
1135                                       namespace => "HoneyClient::Manager::VM::Test");
1136
1137 # Catch all errors, in order to make sure child processes are
1138 # properly killed.
1139 eval {
1140
1141     $URL = HoneyClient::Manager::VM->init();
1142    
1143     # Connect to daemon as a client.
1144     $stub = getClientHandle(namespace => "HoneyClient::Manager::VM");
1145
1146     # Register the test VM.
1147     $som = $stub->registerVM(config => $testVM);
1148
1149     # In order to test the quickCloneVM() method,
1150     # we're going to fully clone the test VM, then set the
1151     # newly created clone as a master VM, and finally,
1152     # create a secondary quick clone from the master VM.
1153
1154     # Get the test VM's parent directory,
1155     # in order to create a temporary master and clone VM.
1156     my $testVMDir = dirname($testVM);
1157     my $masterVMDir = dirname($testVMDir) . "/test_vm_master";
1158     my $cloneVMDir = dirname($testVMDir) . "/test_vm_clone";
1159     my $masterVM = $masterVMDir . "/" . basename($testVM);
1160     my $cloneVM = $cloneVMDir . "/" . basename($testVM);
1161
1162     # Create the master VM.
1163     $som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $masterVMDir);
1164
1165     # Wait a small amount of time for the asynchronous clone
1166     # to complete.
1167     sleep (60);
1168
1169     # The master VM should be on.
1170     $som = $stub->getStateVM(config => $masterVM);
1171
1172     # Since the master VM doesn't have an OS installed on it,
1173     # the VM may be considered stuck.  Go ahead and answer
1174     # this question, if need be.
1175     if ($som->result == VM_EXECUTION_STATE_STUCK) {
1176         $som = $stub->answerVM(config => $masterVM);
1177     }