| 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::ESX loads. |
|---|
| 58 |
BEGIN { use_ok('HoneyClient::Manager::ESX') or diag("Can't load HoneyClient::Manager::ESX package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 59 |
require_ok('HoneyClient::Manager::ESX'); |
|---|
| 60 |
use HoneyClient::Manager::ESX; |
|---|
| 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 the module loads properly, with the exportable |
|---|
| 68 |
# functions shared. |
|---|
| 69 |
BEGIN { use_ok('HoneyClient::Manager::ESX::Clone') or diag("Can't load HoneyClient::Manager::ESX::Clone package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 70 |
require_ok('HoneyClient::Manager::ESX::Clone'); |
|---|
| 71 |
use HoneyClient::Manager::ESX::Clone; |
|---|
| 72 |
|
|---|
| 73 |
# Suppress all logging messages, since we need clean output for unit testing. |
|---|
| 74 |
Log::Log4perl->init({ |
|---|
| 75 |
"log4perl.rootLogger" => "DEBUG, Buffer", |
|---|
| 76 |
"log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer", |
|---|
| 77 |
"log4perl.appender.Buffer.min_level" => "fatal", |
|---|
| 78 |
"log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout", |
|---|
| 79 |
"log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", |
|---|
| 80 |
}); |
|---|
| 81 |
|
|---|
| 82 |
# Make sure Storable loads. |
|---|
| 83 |
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."); } |
|---|
| 84 |
require_ok('Storable'); |
|---|
| 85 |
can_ok('Storable', 'dclone'); |
|---|
| 86 |
can_ok('Storable', 'thaw'); |
|---|
| 87 |
use Storable qw(dclone thaw); |
|---|
| 88 |
|
|---|
| 89 |
# Make sure MIME::Base64 loads. |
|---|
| 90 |
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."); } |
|---|
| 91 |
require_ok('MIME::Base64'); |
|---|
| 92 |
can_ok('MIME::Base64', 'encode_base64'); |
|---|
| 93 |
can_ok('MIME::Base64', 'decode_base64'); |
|---|
| 94 |
use MIME::Base64 qw(encode_base64 decode_base64); |
|---|
| 95 |
|
|---|
| 96 |
# Make sure Data::Dumper loads |
|---|
| 97 |
BEGIN { use_ok('Data::Dumper') |
|---|
| 98 |
or diag("Can't load Data::Dumper package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 99 |
require_ok('Data::Dumper'); |
|---|
| 100 |
use Data::Dumper; |
|---|
| 101 |
|
|---|
| 102 |
# Make sure threads loads. |
|---|
| 103 |
BEGIN { use_ok('threads') or diag("Can't load threads package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 104 |
require_ok('threads'); |
|---|
| 105 |
use threads; |
|---|
| 106 |
|
|---|
| 107 |
# Make sure threads::shared loads. |
|---|
| 108 |
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."); } |
|---|
| 109 |
require_ok('threads::shared'); |
|---|
| 110 |
use threads::shared; |
|---|
| 111 |
|
|---|
| 112 |
# Make sure Thread::Semaphore loads. |
|---|
| 113 |
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."); } |
|---|
| 114 |
require_ok('Thread::Semaphore'); |
|---|
| 115 |
use Thread::Semaphore; |
|---|
| 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 DateTime::HiRes loads. |
|---|
| 125 |
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."); } |
|---|
| 126 |
require_ok('DateTime::HiRes'); |
|---|
| 127 |
use DateTime::HiRes; |
|---|
| 128 |
|
|---|
| 129 |
# Make sure IO::File loads. |
|---|
| 130 |
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."); } |
|---|
| 131 |
require_ok('IO::File'); |
|---|
| 132 |
use IO::File; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
# =begin testing |
|---|
| 138 |
{ |
|---|
| 139 |
# Shared test variables. |
|---|
| 140 |
my $testVM = getVar(name => "test_vm_name", |
|---|
| 141 |
namespace => "HoneyClient::Manager::ESX::Test"); |
|---|
| 142 |
|
|---|
| 143 |
# Include notice, to clarify our assumptions. |
|---|
| 144 |
diag("About to run basic unit tests; these may take some time."); |
|---|
| 145 |
diag("Note: These tests *expect* VMware ESX Server to be accessible and running beforehand."); |
|---|
| 146 |
|
|---|
| 147 |
# Catch all errors, in order to make sure child processes are |
|---|
| 148 |
# properly killed. |
|---|
| 149 |
eval { |
|---|
| 150 |
|
|---|
| 151 |
# Create a generic empty clone object, with test state data. |
|---|
| 152 |
my $clone = HoneyClient::Manager::ESX::Clone->new(test => 1, master_vm_name => $testVM, _dont_init => 1, _bypass_firewall => 1); |
|---|
| 153 |
is($clone->{test}, 1, "new(test => 1, master_vm_name => '$testVM', _dont_init => 1, _bypass_firewall => 1)") or diag("The new() call failed."); |
|---|
| 154 |
isa_ok($clone, 'HoneyClient::Manager::ESX::Clone', "new(test => 1, master_vm_name => '$testVM', _dont_init => 1, _bypass_firewall => 1)") or diag("The new() call failed."); |
|---|
| 155 |
$clone = undef; |
|---|
| 156 |
|
|---|
| 157 |
my $question; |
|---|
| 158 |
$question = prompt("#\n" . |
|---|
| 159 |
"# Note: Testing real clone operations will *ONLY* work\n" . |
|---|
| 160 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 161 |
"# loaded upon boot-up.\n" . |
|---|
| 162 |
"#\n" . |
|---|
| 163 |
"# This test also requires that the firewall VM is registered,\n" . |
|---|
| 164 |
"# powered on, and operational.\n" . |
|---|
| 165 |
"#\n" . |
|---|
| 166 |
"# Your master VM is: " . getVar(name => "master_vm_name", namespace => "HoneyClient::Manager::ESX") . "\n" . |
|---|
| 167 |
"#\n" . |
|---|
| 168 |
"# Do you want to test cloning this master VM?", "no"); |
|---|
| 169 |
if ($question =~ /^y.*/i) { |
|---|
| 170 |
$clone = HoneyClient::Manager::ESX::Clone->new(test => 1); |
|---|
| 171 |
is($clone->{test}, 1, "new(test => 1)") or diag("The new() call failed."); |
|---|
| 172 |
isa_ok($clone, 'HoneyClient::Manager::ESX::Clone', "new(test => 1)") or diag("The new() call failed."); |
|---|
| 173 |
my $quick_clone_vm_name = $clone->{quick_clone_vm_name}; |
|---|
| 174 |
$clone = undef; |
|---|
| 175 |
|
|---|
| 176 |
# Destroy the clone VM. |
|---|
| 177 |
my $session = HoneyClient::Manager::ESX->login(); |
|---|
| 178 |
HoneyClient::Manager::ESX->destroyVM(session => $session, name => $quick_clone_vm_name); |
|---|
| 179 |
HoneyClient::Manager::ESX->logout(session => $session); |
|---|
| 180 |
} |
|---|
| 181 |
}; |
|---|
| 182 |
|
|---|
| 183 |
# Report any failure found. |
|---|
| 184 |
if ($@) { |
|---|
| 185 |
fail($@); |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
# =begin testing |
|---|
| 192 |
{ |
|---|
| 193 |
# Shared test variables. |
|---|
| 194 |
my $testVM = getVar(name => "test_vm_name", |
|---|
| 195 |
namespace => "HoneyClient::Manager::ESX::Test"); |
|---|
| 196 |
|
|---|
| 197 |
# Catch all errors, in order to make sure child processes are |
|---|
| 198 |
# properly killed. |
|---|
| 199 |
eval { |
|---|
| 200 |
|
|---|
| 201 |
my $question; |
|---|
| 202 |
$question = prompt("#\n" . |
|---|
| 203 |
"# Note: Testing real suspend/snapshot operations will *ONLY* work\n" . |
|---|
| 204 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 205 |
"# loaded upon boot-up.\n" . |
|---|
| 206 |
"#\n" . |
|---|
| 207 |
"# This test also requires that the firewall VM is registered,\n" . |
|---|
| 208 |
"# powered on, and operational.\n" . |
|---|
| 209 |
"#\n" . |
|---|
| 210 |
"# Your master VM is: " . getVar(name => "master_vm_name", namespace => "HoneyClient::Manager::ESX") . "\n" . |
|---|
| 211 |
"#\n" . |
|---|
| 212 |
"# Do you want to test cloning this master VM and suspending/snapshotting a subsequent clone?", "no"); |
|---|
| 213 |
if ($question =~ /^y.*/i) { |
|---|
| 214 |
|
|---|
| 215 |
# Create a generic empty clone, with test state data. |
|---|
| 216 |
my $clone = HoneyClient::Manager::ESX::Clone->new(_bypass_firewall => 1); |
|---|
| 217 |
my $quick_clone_vm_name = $clone->{quick_clone_vm_name}; |
|---|
| 218 |
my $name = $clone->{name}; |
|---|
| 219 |
|
|---|
| 220 |
# Snapshot and suspend the clone. |
|---|
| 221 |
$clone->suspend(perform_snapshot => 1); |
|---|
| 222 |
|
|---|
| 223 |
# Destroy the clone object. |
|---|
| 224 |
$clone->destroy(); |
|---|
| 225 |
|
|---|
| 226 |
# Wait for the suspend/snapshot to complete. |
|---|
| 227 |
sleep (5); |
|---|
| 228 |
|
|---|
| 229 |
# Test if the operations worked. |
|---|
| 230 |
my $result = HoneyClient::Manager::ESX::_findSnapshot($name, HoneyClient::Manager::ESX::_getViewVMSnapshotTrees(session => $clone->{_vm_session})); |
|---|
| 231 |
is(defined($result), 1, "suspend()") or diag("The suspend() call failed."); |
|---|
| 232 |
|
|---|
| 233 |
$clone = undef; |
|---|
| 234 |
|
|---|
| 235 |
# Now, destroy the backing cloned VM. |
|---|
| 236 |
my $session = HoneyClient::Manager::ESX->login(); |
|---|
| 237 |
HoneyClient::Manager::ESX->destroyVM(session => $session, name => $quick_clone_vm_name); |
|---|
| 238 |
HoneyClient::Manager::ESX->logout(session => $session); |
|---|
| 239 |
} |
|---|
| 240 |
}; |
|---|
| 241 |
|
|---|
| 242 |
# Report any failure found. |
|---|
| 243 |
if ($@) { |
|---|
| 244 |
fail($@); |
|---|
| 245 |
} |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
# =begin testing |
|---|
| 251 |
{ |
|---|
| 252 |
# Shared test variables. |
|---|
| 253 |
my $testVM = getVar(name => "test_vm_name", |
|---|
| 254 |
namespace => "HoneyClient::Manager::ESX::Test"); |
|---|
| 255 |
|
|---|
| 256 |
# Catch all errors, in order to make sure child processes are |
|---|
| 257 |
# properly killed. |
|---|
| 258 |
eval { |
|---|
| 259 |
|
|---|
| 260 |
my $question; |
|---|
| 261 |
$question = prompt("#\n" . |
|---|
| 262 |
"# Note: Testing real destroy operations will *ONLY* work\n" . |
|---|
| 263 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 264 |
"# loaded upon boot-up.\n" . |
|---|
| 265 |
"#\n" . |
|---|
| 266 |
"# This test also requires that the firewall VM is registered,\n" . |
|---|
| 267 |
"# powered on, and operational.\n" . |
|---|
| 268 |
"#\n" . |
|---|
| 269 |
"# Your master VM is: " . getVar(name => "master_vm_name", namespace => "HoneyClient::Manager::ESX") . "\n" . |
|---|
| 270 |
"#\n" . |
|---|
| 271 |
"# Do you want to test cloning this master VM and destroying a subsequent clone?", "no"); |
|---|
| 272 |
if ($question =~ /^y.*/i) { |
|---|
| 273 |
|
|---|
| 274 |
# Create a generic empty clone, with test state data. |
|---|
| 275 |
my $clone = HoneyClient::Manager::ESX::Clone->new(_bypass_firewall => 1); |
|---|
| 276 |
my $quick_clone_vm_name = $clone->{quick_clone_vm_name}; |
|---|
| 277 |
my $name = $clone->{name}; |
|---|
| 278 |
|
|---|
| 279 |
# Destroy the clone object. |
|---|
| 280 |
$clone->destroy(); |
|---|
| 281 |
|
|---|
| 282 |
# Wait for the destroy to complete. |
|---|
| 283 |
sleep (5); |
|---|
| 284 |
|
|---|
| 285 |
# Test if the operations worked. |
|---|
| 286 |
my $result = HoneyClient::Manager::ESX::_findSnapshot($name, HoneyClient::Manager::ESX::_getViewVMSnapshotTrees(session => $clone->{_vm_session})); |
|---|
| 287 |
is(!defined($result), 1, "destroy()") or diag("The destroy() call failed."); |
|---|
| 288 |
|
|---|
| 289 |
$clone = undef; |
|---|
| 290 |
|
|---|
| 291 |
# Now, destroy the backing cloned VM. |
|---|
| 292 |
my $session = HoneyClient::Manager::ESX->login(); |
|---|
| 293 |
HoneyClient::Manager::ESX->destroyVM(session => $session, name => $quick_clone_vm_name); |
|---|
| 294 |
HoneyClient::Manager::ESX->logout(session => $session); |
|---|
| 295 |
} |
|---|
| 296 |
}; |
|---|
| 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 $testVM = getVar(name => "test_vm_name", |
|---|
| 310 |
namespace => "HoneyClient::Manager::ESX::Test"); |
|---|
| 311 |
|
|---|
| 312 |
# Catch all errors, in order to make sure child processes are |
|---|
| 313 |
# properly killed. |
|---|
| 314 |
eval { |
|---|
| 315 |
|
|---|
| 316 |
my $question; |
|---|
| 317 |
$question = prompt("#\n" . |
|---|
| 318 |
"# Note: Testing real drive operations will *ONLY* work\n" . |
|---|
| 319 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 320 |
"# loaded upon boot-up.\n" . |
|---|
| 321 |
"#\n" . |
|---|
| 322 |
"# This test also requires that the firewall VM is registered,\n" . |
|---|
| 323 |
"# powered on, and operational.\n" . |
|---|
| 324 |
"#\n" . |
|---|
| 325 |
"# Your master VM is: " . getVar(name => "master_vm_name", namespace => "HoneyClient::Manager::ESX") . "\n" . |
|---|
| 326 |
"#\n" . |
|---|
| 327 |
"# Do you want to test cloning and driving this master VM?", "no"); |
|---|
| 328 |
if ($question =~ /^y.*/i) { |
|---|
| 329 |
|
|---|
| 330 |
# Create a generic empty clone, with test state data. |
|---|
| 331 |
my $clone = HoneyClient::Manager::ESX::Clone->new(_bypass_firewall => 1); |
|---|
| 332 |
my $quick_clone_vm_name = $clone->{quick_clone_vm_name}; |
|---|
| 333 |
|
|---|
| 334 |
$clone = $clone->drive(work => { 'http://www.google.com/' => 1 }); |
|---|
| 335 |
isa_ok($clone, 'HoneyClient::Manager::ESX::Clone', "drive(work => { 'http://www.google.com/' => 1})") or diag("The drive() call failed."); |
|---|
| 336 |
$clone = undef; |
|---|
| 337 |
|
|---|
| 338 |
# Now, destroy the backing cloned VM. |
|---|
| 339 |
my $session = HoneyClient::Manager::ESX->login(); |
|---|
| 340 |
HoneyClient::Manager::ESX->destroyVM(session => $session, name => $quick_clone_vm_name); |
|---|
| 341 |
HoneyClient::Manager::ESX->logout(session => $session); |
|---|
| 342 |
} |
|---|
| 343 |
}; |
|---|
| 344 |
|
|---|
| 345 |
# Report any failure found. |
|---|
| 346 |
if ($@) { |
|---|
| 347 |
fail($@); |
|---|
| 348 |
} |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
1; |
|---|