more extensive testing for Users probe with external placeholder binaries
[scpubgit/System-Introspector.git] / t / users.t
CommitLineData
a2598396 1use strictures 1;
2use Test::More;
970fe8fe 3use FindBin;
a2598396 4
afd7c030 5use System::Introspector::Probe::Users;
a2598396 6
970fe8fe 7my $passwd_file = "$FindBin::Bin/test-passwd";
8my $home = "$FindBin::Bin/data/home-testfoo";
9my $nohome = "$FindBin::Bin/data/home-doesnotexist";
a2598396 10
970fe8fe 11open my $passwd_fh, '>', $passwd_file
12 or die "Unable to write $passwd_file: $!\n";
13printf $passwd_fh "%s\n", join ':', @$_
14 for [qw( testfoo x 23 42 comment ), $home, '/bin/false'],
15 [qw( testbar x 24 43 comment ), $nohome, '/bin/false'];
16close $passwd_fh;
a2598396 17
970fe8fe 18local $ENV{PATH} = join ':',
19 "$FindBin::Bin/bin",
20 $ENV{PATH};
21
22my $probe = System::Introspector::Probe::Users->new(
23 passwd_file => $passwd_file,
24);
25my $data = $probe->gather;
26
27do {
28 my $user = $data->{users}{testfoo};
29 ok $user, 'found first user';
30
31 my $keys = $user->{ssh}{keys};
32 ok $keys, 'found ssh keys structure';
33
34 is_deeply $keys, {
35 files => {
36 'first.pub' => {
37 file_name => "$home/.ssh/first.pub",
38 body => "pubkey\n",
39 },
40 },
41 authorized => {
42 file_name => "$home/.ssh/authorized_keys",
43 body => "keyA\nkeyB\n",
44 },
45 }, 'ssh key data';
46
47 is_deeply $user->{groups}, {
48 list => [qw( testfoo_group_A testfoo_group_B testfoo_group_C )],
49 }, 'groups list';
50
51 my $tab = $user->{crontab};
52 ok $tab, 'got crontab results';
53 like $tab->{body}, qr{-u\s*testfoo}, 'crontab called with user option';
54 like $tab->{body}, qr{-l}, 'crontab asked for list';
55};
56
57do {
58 my $user = $data->{users}{testbar};
59 ok $user, 'found second user';
60
61 my $keys = $user->{ssh}{keys};
62 ok $keys, 'found ssh keys structure';
63
64 is_deeply $keys, {
65 files => {},
66 authorized => {
67 file_name => "$nohome/.ssh/authorized_keys",
68 body => '',
69 },
70 }, 'ssh key data';
71};
72
73#ok((my $user = $data->{users}{ +getlogin }), 'found own user');
74#ok(defined($user->{ $_ }), "$_ is defined")
75# for qw( comment crontab gid groups home shell ssh uid username );
76#ok(not(exists $user->{crontab}{error}), 'no crontab error');
77#is($user->{ssh}{keys}{error}, undef, 'no ssh keys error');
78
79
80unlink $passwd_file;
a2598396 81done_testing;