more extensive testing for Users probe with external placeholder binaries
Robert 'phaylon' Sedlacek [Tue, 29 May 2012 19:11:16 +0000 (19:11 +0000)]
t/bin/crontab [new file with mode: 0755]
t/bin/groups [new file with mode: 0755]
t/data/home-testfoo/.ssh/authorized_keys [new file with mode: 0644]
t/data/home-testfoo/.ssh/first.pub [new file with mode: 0644]
t/users.t

diff --git a/t/bin/crontab b/t/bin/crontab
new file mode 100755 (executable)
index 0000000..fee8547
--- /dev/null
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use strictures 1;
+my $arg = join ' ', @ARGV;
+print "* * * * * test $arg\n";
diff --git a/t/bin/groups b/t/bin/groups
new file mode 100755 (executable)
index 0000000..432a363
--- /dev/null
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use strictures 1;
+my $user = shift @ARGV;
+printf "%s\n", join ' ', map { "${user}_group_${_}" } qw( A B C );
diff --git a/t/data/home-testfoo/.ssh/authorized_keys b/t/data/home-testfoo/.ssh/authorized_keys
new file mode 100644 (file)
index 0000000..3d4e849
--- /dev/null
@@ -0,0 +1,2 @@
+keyA
+keyB
diff --git a/t/data/home-testfoo/.ssh/first.pub b/t/data/home-testfoo/.ssh/first.pub
new file mode 100644 (file)
index 0000000..803985a
--- /dev/null
@@ -0,0 +1 @@
+pubkey
index 101affd..ccd6826 100644 (file)
--- a/t/users.t
+++ b/t/users.t
@@ -1,15 +1,81 @@
 use strictures 1;
 use Test::More;
+use FindBin;
 
 use System::Introspector::Probe::Users;
 
-my $probe = System::Introspector::Probe::Users->new;
-my $data  = $probe->gather;
+my $passwd_file = "$FindBin::Bin/test-passwd";
+my $home = "$FindBin::Bin/data/home-testfoo";
+my $nohome = "$FindBin::Bin/data/home-doesnotexist";
 
-ok((my $user = $data->{users}{ +getlogin }), 'found own user');
-ok(defined($user->{ $_ }), "$_ is defined")
-    for qw( comment crontab gid groups home shell ssh uid username );
-ok(not(exists $user->{crontab}{error}), 'no crontab error');
-is($user->{ssh}{keys}{error}, undef, 'no ssh keys error');
+open my $passwd_fh, '>', $passwd_file
+    or die "Unable to write $passwd_file: $!\n";
+printf $passwd_fh "%s\n", join ':', @$_
+    for [qw( testfoo x 23 42 comment ), $home, '/bin/false'],
+        [qw( testbar x 24 43 comment ), $nohome, '/bin/false'];
+close $passwd_fh;
 
+local $ENV{PATH} = join ':',
+    "$FindBin::Bin/bin",
+    $ENV{PATH};
+
+my $probe = System::Introspector::Probe::Users->new(
+    passwd_file => $passwd_file,
+);
+my $data = $probe->gather;
+
+do {
+    my $user = $data->{users}{testfoo};
+    ok $user, 'found first user';
+
+    my $keys = $user->{ssh}{keys};
+    ok $keys, 'found ssh keys structure';
+
+    is_deeply $keys, {
+        files => {
+            'first.pub' => {
+                file_name => "$home/.ssh/first.pub",
+                body => "pubkey\n",
+            },
+        },
+        authorized => {
+            file_name => "$home/.ssh/authorized_keys",
+            body => "keyA\nkeyB\n",
+        },
+    }, 'ssh key data';
+
+    is_deeply $user->{groups}, {
+        list => [qw( testfoo_group_A testfoo_group_B testfoo_group_C )],
+    }, 'groups list';
+
+    my $tab = $user->{crontab};
+    ok $tab, 'got crontab results';
+    like $tab->{body}, qr{-u\s*testfoo}, 'crontab called with user option';
+    like $tab->{body}, qr{-l}, 'crontab asked for list';
+};
+
+do {
+    my $user = $data->{users}{testbar};
+    ok $user, 'found second user';
+
+    my $keys = $user->{ssh}{keys};
+    ok $keys, 'found ssh keys structure';
+
+    is_deeply $keys, {
+        files => {},
+        authorized => {
+            file_name => "$nohome/.ssh/authorized_keys",
+            body => '',
+        },
+    }, 'ssh key data';
+};
+
+#ok((my $user = $data->{users}{ +getlogin }), 'found own user');
+#ok(defined($user->{ $_ }), "$_ is defined")
+#    for qw( comment crontab gid groups home shell ssh uid username );
+#ok(not(exists $user->{crontab}{error}), 'no crontab error');
+#is($user->{ssh}{keys}{error}, undef, 'no ssh keys error');
+
+
+unlink $passwd_file;
 done_testing;