Rex meet Rspec~

As an DevOps, I use Puppet and Rex, and like automatic testing for my infrastructure. So when I saw serverspec, I thought: maybe I can do such things in Perl?

I know there are already so many testing frameworks at CPAN. But I like the syntax used by serverspec( yes, evoloved from Rspec!) so DIY begin~

Here is my work:Rex::Test::Spec.

We can use it as follow:

    use Rex::Test::Spec;
    describe "Nginx Test", sub {
        context run("nginx -t"), "nginx.conf testing", sub {
            like its('stdout'), qr/ok/;
        };
        context file("~/.ssh/id_rsa"), sub {
            is its('ensure'), 'file';
            is its('mode'), '0600';
            like its('content'), qr/name\@email\.com/;
        };
        context file("/data"), sub {
            is its('ensure'), 'directory';
            is its('owner'), 'www';
            is its('mounted_on'), '/dev/sdb1';
            isnt its('writable');
        };
        context service("nginx"), sub {
            is its('ensure'), 'running';
        };
        context pkg("nginx"), sub {
            is its('ensure'), 'present';
            is its('version'), '1.5.8';
        };
        context cron, sub {
            like its('www'), 'logrotate';
        };
        context gateway, sub {
            is it, '192.168.0.1';
        };
        context group('www'), sub {
            ok its('ensure');
        };
        context iptables, sub {
        };
        context port(80), sub {
            is its('bind'), '0.0.0.0';
            is its('proto'), 'tcp';
            is its('command'), 'nginx';
        };
        context process('nginx'), sub {
            like its('command'), qr(nginx -c /etc/nginx.conf);
            ok its('mem') > 1024;
        };
        context routes, sub {
            is_deeply its(1), {
                destination => $dest,
                gateway => $gw,
                genmask => $genmask,
                flags => $flags,
                mss => $mss,
                irtt => $irtt,
                iface => $iface,
            };
        };
        context sysctl, sub {
            is its('vm.swapiness'), 1;
        };
        context user('www'), sub {
            ok its('ensure');
            is its('home'), '/var/www/html';
            is its('shell'), '/sbin/nologin';
            is_deeply its('belong_to'), ['www', 'nogroup'];
        };
    };
    done_testing;

I override the functions Test::More normally export. so I can pass the resource type name as the testing message to every test example belong to themself.

BTW, I wrote these at MBA, but Rex don't support Darwin==! And I have no LInux now... So, I only run one example, the run example. Happly for help, especially test on SSH interface.

2 Comments

Hi Chenryn,

this looks really awesome. I wonder if we can integrate it into the base Rex distribution?

Keep up the good work,

Jan

Leave a comment

About chenryn

user-pic a DevOps who love Perl and Poetry.