| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use Test::More 'no_plan'; |
|---|
| 5 |
$| = 1; |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
# =begin testing |
|---|
| 10 |
{ |
|---|
| 11 |
# Make sure ExtUtils::MakeMaker loads. |
|---|
| 12 |
BEGIN { use_ok('ExtUtils::MakeMaker', qw(prompt)) or diag("Can't load ExtUtils::MakeMaker package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 13 |
require_ok('ExtUtils::MakeMaker'); |
|---|
| 14 |
can_ok('ExtUtils::MakeMaker', 'prompt'); |
|---|
| 15 |
use ExtUtils::MakeMaker qw(prompt); |
|---|
| 16 |
|
|---|
| 17 |
# Make sure Log::Log4perl loads |
|---|
| 18 |
BEGIN { use_ok('Log::Log4perl', qw(:nowarn)) |
|---|
| 19 |
or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path."); |
|---|
| 20 |
|
|---|
| 21 |
# Suppress all logging messages, since we need clean output for unit testing. |
|---|
| 22 |
Log::Log4perl->init({ |
|---|
| 23 |
"log4perl.rootLogger" => "DEBUG, Buffer", |
|---|
| 24 |
"log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer", |
|---|
| 25 |
"log4perl.appender.Buffer.min_level" => "fatal", |
|---|
| 26 |
"log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout", |
|---|
| 27 |
"log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", |
|---|
| 28 |
}); |
|---|
| 29 |
} |
|---|
| 30 |
require_ok('Log::Log4perl'); |
|---|
| 31 |
use Log::Log4perl qw(:easy); |
|---|
| 32 |
|
|---|
| 33 |
# Make sure HoneyClient::Util::Config loads. |
|---|
| 34 |
BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) |
|---|
| 35 |
or diag("Can't load HoneyClient::Util::Config package. Check to make sure the package library is correctly listed within the path."); |
|---|
| 36 |
|
|---|
| 37 |
# Suppress all logging messages, since we need clean output for unit testing. |
|---|
| 38 |
Log::Log4perl->init({ |
|---|
| 39 |
"log4perl.rootLogger" => "DEBUG, Buffer", |
|---|
| 40 |
"log4perl.appender.Buffer" => "Log::Log4perl::Appender::TestBuffer", |
|---|
| 41 |
"log4perl.appender.Buffer.min_level" => "fatal", |
|---|
| 42 |
"log4perl.appender.Buffer.layout" => "Log::Log4perl::Layout::PatternLayout", |
|---|
| 43 |
"log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", |
|---|
| 44 |
}); |
|---|
| 45 |
|
|---|
| 46 |
} |
|---|
| 47 |
require_ok('HoneyClient::Util::Config'); |
|---|
| 48 |
can_ok('HoneyClient::Util::Config', 'getVar'); |
|---|
| 49 |
use HoneyClient::Util::Config qw(getVar); |
|---|
| 50 |
|
|---|
| 51 |
# Make sure HoneyClient::Util::SOAP loads. |
|---|
| 52 |
BEGIN { use_ok('HoneyClient::Util::SOAP', qw(getClientHandle)) or diag("Can't load HoneyClient::Util::SOAP package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 53 |
require_ok('HoneyClient::Util::SOAP'); |
|---|
| 54 |
can_ok('HoneyClient::Util::SOAP', 'getClientHandle'); |
|---|
| 55 |
use HoneyClient::Util::SOAP qw(getClientHandle); |
|---|
| 56 |
|
|---|
| 57 |
# Make sure HoneyClient::Manager::VM loads. |
|---|
| 58 |
BEGIN { use_ok('HoneyClient::Manager::VM') or diag("Can't load HoneyClient::Manager::VM package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 59 |
require_ok('HoneyClient::Manager::VM'); |
|---|
| 60 |
use HoneyClient::Manager::VM; |
|---|
| 61 |
|
|---|
| 62 |
# Make sure VMware::VmPerl loads. |
|---|
| 63 |
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."); } |
|---|
| 64 |
require_ok('VMware::VmPerl'); |
|---|
| 65 |
use VMware::VmPerl qw(VM_EXECUTION_STATE_ON VM_EXECUTION_STATE_OFF VM_EXECUTION_STATE_STUCK VM_EXECUTION_STATE_SUSPENDED); |
|---|
| 66 |
|
|---|
| 67 |
# Make sure the module loads properly, with the exportable |
|---|
| 68 |
# functions shared. |
|---|
| 69 |
BEGIN { use_ok('HoneyClient::Manager::VM::Clone') or diag("Can't load HoneyClient::Manager::VM::Clone package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 70 |
require_ok('HoneyClient::Manager::VM::Clone'); |
|---|
| 71 |
use HoneyClient::Manager::VM::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)) 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 |
use Storable qw(dclone); |
|---|
| 87 |
|
|---|
| 88 |
# Make sure Data::Dumper loads |
|---|
| 89 |
BEGIN { use_ok('Data::Dumper') |
|---|
| 90 |
or diag("Can't load Data::Dumper package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 91 |
require_ok('Data::Dumper'); |
|---|
| 92 |
use Data::Dumper; |
|---|
| 93 |
|
|---|
| 94 |
# Make sure threads loads. |
|---|
| 95 |
BEGIN { use_ok('threads') or diag("Can't load threads package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 96 |
require_ok('threads'); |
|---|
| 97 |
use threads; |
|---|
| 98 |
|
|---|
| 99 |
# Make sure threads::shared loads. |
|---|
| 100 |
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."); } |
|---|
| 101 |
require_ok('threads::shared'); |
|---|
| 102 |
use threads::shared; |
|---|
| 103 |
|
|---|
| 104 |
# Make sure File::Basename loads. |
|---|
| 105 |
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."); } |
|---|
| 106 |
require_ok('File::Basename'); |
|---|
| 107 |
can_ok('File::Basename', 'dirname'); |
|---|
| 108 |
can_ok('File::Basename', 'basename'); |
|---|
| 109 |
use File::Basename qw(dirname basename); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
# =begin testing |
|---|
| 115 |
{ |
|---|
| 116 |
# Shared test variables. |
|---|
| 117 |
my ($stub, $som, $URL); |
|---|
| 118 |
my $testVM = $ENV{PWD} . "/" . getVar(name => "test_vm_config", |
|---|
| 119 |
namespace => "HoneyClient::Manager::VM::Test"); |
|---|
| 120 |
|
|---|
| 121 |
# Include notice, to clarify our assumptions. |
|---|
| 122 |
diag("About to run basic unit tests; these may take some time."); |
|---|
| 123 |
diag("Note: These tests *expect* VMware Server or VMware GSX to be installed and running on this system beforehand."); |
|---|
| 124 |
|
|---|
| 125 |
# Catch all errors, in order to make sure child processes are |
|---|
| 126 |
# properly killed. |
|---|
| 127 |
eval { |
|---|
| 128 |
|
|---|
| 129 |
$URL = HoneyClient::Manager::VM->init(); |
|---|
| 130 |
|
|---|
| 131 |
# Connect to daemon as a client. |
|---|
| 132 |
$stub = getClientHandle(namespace => "HoneyClient::Manager::VM"); |
|---|
| 133 |
|
|---|
| 134 |
# In order to test setMasterVM(), we're going to fully clone |
|---|
| 135 |
# the testVM, then set the newly created clone as a master VM. |
|---|
| 136 |
|
|---|
| 137 |
# Get the test VM's parent directory, |
|---|
| 138 |
# in order to create a temporary master VM. |
|---|
| 139 |
my $testVMDir = dirname($testVM); |
|---|
| 140 |
my $masterVMDir = dirname($testVMDir) . "/test_vm_master"; |
|---|
| 141 |
my $masterVM = $masterVMDir . "/" . basename($testVM); |
|---|
| 142 |
|
|---|
| 143 |
# Create the master VM. |
|---|
| 144 |
$som = $stub->fullCloneVM(src_config => $testVM, dest_dir => $masterVMDir); |
|---|
| 145 |
|
|---|
| 146 |
# Wait a small amount of time for the asynchronous clone |
|---|
| 147 |
# to complete. |
|---|
| 148 |
sleep (10); |
|---|
| 149 |
|
|---|
| 150 |
# The master VM should be on. |
|---|
| 151 |
$som = $stub->getStateVM(config => $masterVM); |
|---|
| 152 |
|
|---|
| 153 |
# Since the master VM doesn't have an OS installed on it, |
|---|
| 154 |
# the VM may be considered stuck. Go ahead and answer |
|---|
| 155 |
# this question, if need be. |
|---|
| 156 |
if ($som->result == VM_EXECUTION_STATE_STUCK) { |
|---|
| 157 |
$som = $stub->answerVM(config => $masterVM); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
# Turn off the master VM. |
|---|
| 161 |
$som = $stub->stopVM(config => $masterVM); |
|---|
| 162 |
|
|---|
| 163 |
# Now, kill the VM daemon. |
|---|
| 164 |
HoneyClient::Manager::VM->destroy(); |
|---|
| 165 |
# XXX: See if this is still needed. |
|---|
| 166 |
#sleep (10); |
|---|
| 167 |
|
|---|
| 168 |
# Create a generic empty clone, with test state data. |
|---|
| 169 |
my $clone = HoneyClient::Manager::VM::Clone->new(test => 1, master_vm_config => $masterVM, _dont_init => 1); |
|---|
| 170 |
is($clone->{test}, 1, "new(test => 1, master_vm_config => '$masterVM', _dont_init => 1)") or diag("The new() call failed."); |
|---|
| 171 |
isa_ok($clone, 'HoneyClient::Manager::VM::Clone', "new(test => 1, master_vm_config => '$masterVM', _dont_init => 1)") or diag("The new() call failed."); |
|---|
| 172 |
$clone = undef; |
|---|
| 173 |
|
|---|
| 174 |
# Destroy the master VM. |
|---|
| 175 |
$som = $stub->destroyVM(config => $masterVM); |
|---|
| 176 |
|
|---|
| 177 |
my $question; |
|---|
| 178 |
$question = prompt("#\n" . |
|---|
| 179 |
"# Note: Testing real clone operations will *ONLY* work\n" . |
|---|
| 180 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 181 |
"# loaded upon boot-up.\n" . |
|---|
| 182 |
"#\n" . |
|---|
| 183 |
"# Your master VM is: " . getVar(name => "master_vm_config", namespace => "HoneyClient::Manager::VM") . "\n" . |
|---|
| 184 |
"#\n" . |
|---|
| 185 |
"# Do you want to test cloning this master VM?", "no"); |
|---|
| 186 |
if ($question =~ /^y.*/i) { |
|---|
| 187 |
$clone = HoneyClient::Manager::VM::Clone->new(test => 1); |
|---|
| 188 |
is($clone->{test}, 1, "new(test => 1)") or diag("The new() call failed."); |
|---|
| 189 |
isa_ok($clone, 'HoneyClient::Manager::VM::Clone', "new(test => 1)") or diag("The new() call failed."); |
|---|
| 190 |
my $cloneConfig = $clone->{config}; |
|---|
| 191 |
$clone = undef; |
|---|
| 192 |
|
|---|
| 193 |
# Destroy the clone VM. |
|---|
| 194 |
$som = $stub->destroyVM(config => $cloneConfig); |
|---|
| 195 |
} |
|---|
| 196 |
}; |
|---|
| 197 |
|
|---|
| 198 |
# Kill the child daemon, if it still exists. |
|---|
| 199 |
HoneyClient::Manager::VM->destroy(); |
|---|
| 200 |
# XXX: See if this is still needed. |
|---|
| 201 |
#sleep (1); |
|---|
| 202 |
|
|---|
| 203 |
# Report any failure found. |
|---|
| 204 |
if ($@) { |
|---|
| 205 |
fail($@); |
|---|
| 206 |
} |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
# =begin testing |
|---|
| 212 |
{ |
|---|
| 213 |
# Shared test variables. |
|---|
| 214 |
my ($stub, $som, $URL); |
|---|
| 215 |
my $testVM = $ENV{PWD} . "/" . getVar(name => "test_vm_config", |
|---|
| 216 |
namespace => "HoneyClient::Manager::VM::Test"); |
|---|
| 217 |
|
|---|
| 218 |
# Catch all errors, in order to make sure child processes are |
|---|
| 219 |
# properly killed. |
|---|
| 220 |
eval { |
|---|
| 221 |
|
|---|
| 222 |
my $testVMDir = dirname($testVM); |
|---|
| 223 |
|
|---|
| 224 |
# Specify where the snapshot should be created. |
|---|
| 225 |
my $snapshot = dirname($testVMDir) . "/test_vm_clone.tar.gz"; |
|---|
| 226 |
|
|---|
| 227 |
# Pretend as though no other Clone objects have been created prior |
|---|
| 228 |
# to this point. |
|---|
| 229 |
$HoneyClient::Manager::VM::Clone::OBJECT_COUNT = -1; |
|---|
| 230 |
|
|---|
| 231 |
my $question; |
|---|
| 232 |
$question = prompt("#\n" . |
|---|
| 233 |
"# Note: Testing real archive operations will *ONLY* work\n" . |
|---|
| 234 |
"# with a fully functional master VM that has the HoneyClient code\n" . |
|---|
| 235 |
"# loaded upon boot-up.\n" . |
|---|
| 236 |
"#\n" . |
|---|
| 237 |
"# Your master VM is: " . getVar(name => "master_vm_config", namespace => "HoneyClient::Manager::VM") . "\n" . |
|---|
| 238 |
"#\n" . |
|---|
| 239 |
"# Do you want to test cloning this master VM?", "no"); |
|---|
| 240 |
if ($question =~ /^y.*/i) { |
|---|
| 241 |
|
|---|
| 242 |
# Create a generic empty clone, with test state data. |
|---|
| 243 |
my $clone = HoneyClient::Manager::VM::Clone->new(); |
|---|
| 244 |
my $cloneConfig = $clone->{config}; |
|---|
| 245 |
|
|---|
| 246 |
# Archive the clone. |
|---|
| 247 |
$clone->archive(snapshot_file => $snapshot); |
|---|
| 248 |
|
|---|
| 249 |
# Wait for the archive to complete. |
|---|
| 250 |
sleep (45); |
|---|
| 251 |
|
|---|
| 252 |
# Test if the archive worked. |
|---|
| 253 |
is(-f $snapshot, 1, "archive(snapshot_file => '$snapshot')") or diag("The archive() call failed."); |
|---|
| 254 |
|
|---|
| 255 |
unlink $snapshot; |
|---|
| 256 |
$clone = undef; |
|---|
| 257 |
|
|---|
| 258 |
# Connect to daemon as a client. |
|---|
| 259 |
$stub = getClientHandle(namespace => "HoneyClient::Manager::VM"); |
|---|
| 260 |
|
|---|
| 261 |
# Destroy the clone VM. |
|---|
| 262 |
$som = $stub->destroyVM(config => $cloneConfig); |
|---|
| 263 |
} |
|---|
| 264 |
}; |
|---|
| 265 |
|
|---|
| 266 |
# Kill the child daemon, if it still exists. |
|---|
| 267 |
HoneyClient::Manager::VM->destroy(); |
|---|
| 268 |
# XXX: See if this is still needed. |
|---|
| 269 |
#sleep (1); |
|---|
| 270 |
|
|---|
| 271 |
# Report any failure found. |
|---|
| 272 |
if ($@) { |
|---|
| 273 |
fail($@); |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
1; |
|---|