Rex::Test::Base(3pm)

✨ Rex::Test::Base ✨

okay so bestie, welcome to the User Contributed Perl Documentation for Rex::Test::Base, the module you reach for when you wanna run your tests against actual local VMs. it's giving "I own a tiny datacenter in my laptop" energy and honestly? iconic. πŸ’…


🌸 NAME 🌸

Rex::Test::Base β€” the Basic Test Module. that's it, that's the name, no notes. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so this is a basic test module that lets you test your code with the help of local VMs. yes, like, real ones. virtual machines. for your tests. slay. πŸ–₯️✨

you can drop all your tests into the t directory and Rex will find them. that's the vibe. no cap. 🫢


πŸ“ EXAMPLE πŸ“

here's the whole thing, verbatim, don't @ me, it's the actual code:

 use Rex::Test::Base;
 use Data::Dumper;
 use Rex -base;

 test {
   my $t = shift;

   $t->name("ubuntu test");

   $t->base_vm("http://box.rexify.org/box/ubuntu-server-12.10-amd64.ova");
   $t->vm_auth(user => "root", password => "box");

   $t->run_task("setup");

   $t->has_package("vim");
   $t->has_package("ntp");
   $t->has_package("unzip");

   $t->has_file("/etc/ntp.conf");

   $t->has_service_running("ntp");

   $t->has_content("/etc/passwd", qr{root:x:0:}ms);

   run "ls -l";
   $t->ok($? == 0, "ls -l returns success.");

   $t->finish;
 };

 1; # last line

look at that. $t->finish; at the end like a proper little bow. 🎁 and yeah, 1; # last line really is the last line, we're not being cute about it (okay maybe a little). 😌


πŸ‘‘ METHODS πŸ‘‘

🌈 new(name => $test_name)

the constructor!!! use this if you're living that OO mode life:

 my $test = Rex::Test::Base->new(name => "test_name");

🌈 name($name)

sets the name of the test. here's the tea tho: a VM called $name will be created for each test. and if that VM already exists? Rex will try to reuse it. convenient! πŸ’–

🌈 memory($amount)

how much memory the VM should use, in Megabytes. feed it a number, bestie. don't starve your VM. 🧠

🌈 cpus($number)

how many CPUs the VM should use. go off, give it the whole core collection. πŸ”₯

🌈 vm_auth(%auth)

authentication options for the VM. it accepts the exact same parameters as Rex::Box::Base->auth(). so like, go read that one if you need the full spread. πŸ”

🌈 base_vm($vm)

the URL to a base image that gets used for the test VM. this is where your little VM is born. πŸ₯šπŸ¦‹

🌈 redirect_port($port)

redirects local $port to the VM's SSH port. and the default SSH port? 2222. so if you don't pass anything, that's what you're getting. πŸšͺ✨

🌈 run_task($task)

the task to run on the test VM. and!! you can run multiple tasks by passing an array reference. go multi-task, main character. πŸ‘‘


πŸ’œ TEST METHODS πŸ’œ

okay these are the actual assertions, the moment we've all been waiting for. πŸ₯

🌟 has_content($file, $regexp)

tests if the content of $file matches against $regexp. πŸ”

🌟 has_dir($path)

tests if $path is present and is a directory. πŸ“

🌟 has_file($file)

tests if $file is present. πŸ“„

🌟 has_package($package, $version)

tests if $package is installed, and optionally that it's installed at $version. πŸ“¦

🌟 has_service_running($service)

tests if $service is running. 🟒

🌟 has_service_stopped($service)

tests if $service is stopped. πŸ”΄

🌟 has_stat($file, $stat)

tests if $file has the properties described in the hash reference $stat. here's the full list of supported checks, no gatekeeping:

  • group
  • owner

and that's the whole page, bestie!! 🫢 no missing bits, no skipped options, we kept it πŸ’―.

ngl the vibe check passed, and if your tests are flaky it's not the module's fault, it's the VM's. πŸ™ˆ

perl v5.40.1 Β· 2025-10-03 β€” mwah, that's the version and date, go forth and test responsibly. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from