-
Notifications
You must be signed in to change notification settings - Fork 125
/
agent.t
executable file
·215 lines (172 loc) · 4.54 KB
/
agent.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
use strict;
use warnings;
use English qw(-no_match_vars);
use File::Temp;
use IPC::Run qw(run);
use XML::TreePP;
use FusionInventory::Agent::Tools;
use Test::More tests => 27;
my ($out, $err, $rc);
($out, $err, $rc) = run_agent('--help');
ok($rc == 2, '--help exit status');
like(
$err,
qr/^Usage:/,
'--help stderr'
);
is($out, '', '--help stdin');
($out, $err, $rc) = run_agent('--version');
ok($rc == 0, '--version exit status');
is($err, '', '--version stderr');
like(
$out,
qr/^FusionInventory unified agent for UNIX, Linux and MacOSX/,
'--version stdin'
);
($out, $err, $rc) = run_agent();
ok($rc == 1, 'no target exit status');
like(
$err,
qr/No target defined/,
'no target stderr'
);
is($out, '', 'no target stdin');
my $base_options = "--stdout --debug --no-task ocsdeploy,wakeonlan,snmpquery,netdiscovery";
my $content;
# first inventory
($out, $err, $rc) = run_agent(
"$base_options --no-category printer"
);
subtest "first inventory" => sub {
check_execution_ok($out, $err, $rc);
};
ok(
exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
'inventory has software'
);
ok(
exists $content->{REQUEST}->{CONTENT}->{ENVS},
'inventory has environment variables'
);
# second inventory, without software
($out, $err, $rc) = run_agent(
"$base_options --no-category printer,software"
);
subtest "first inventory" => sub {
check_execution_ok($out, $err, $rc);
};
ok(
!exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
"output doesn't have any software"
);
ok(
exists $content->{REQUEST}->{CONTENT}->{ENVS},
'inventory has environment variables'
);
# third inventory, without software, but additional content
my $file = File::Temp->new(UNLINK => $ENV{TEST_DEBUG} ? 0 : 1, SUFFIX => '.xml');
print $file <<EOF;
<?xml version="1.0" encoding="UTF-8" ?>
<REQUEST>
<CONTENT>
<SOFTWARES>
<NAME>foo</NAME>
<VERSION>bar</VERSION>
</SOFTWARES>
</CONTENT>
</REQUEST>
EOF
close($file);
($out, $err, $rc) = run_agent(
"$base_options --no-category printer,software --additional-content $file"
);
subtest "first inventory" => sub {
check_execution_ok($out, $err, $rc);
};
ok(
exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
'inventory has softwares'
);
ok(
ref $content->{REQUEST}->{CONTENT}->{SOFTWARES} eq 'HASH',
'inventory contains only one software'
);
ok(
$content->{REQUEST}->{CONTENT}->{SOFTWARES}->{NAME} eq 'foo' &&
$content->{REQUEST}->{CONTENT}->{SOFTWARES}->{VERSION} eq 'bar',
'inventory contains the expected software'
);
ok(
exists $content->{REQUEST}->{CONTENT}->{ENVS},
'inventory has environment variables'
);
# PATH through WMI appears with %SystemRoot% templates, preventing direct
# comparaison with %ENV content
my $name = $OSNAME eq 'MSWin32' ? 'PATHEXT' : 'PATH';
my $value = $ENV{$name};
($out, $err, $rc) = run_agent(
"$base_options --no-category printer,software"
);
subtest "first inventory" => sub {
check_execution_ok($out, $err, $rc);
};
ok(
!exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
"inventory doesn't have any software"
);
ok(
exists $content->{REQUEST}->{CONTENT}->{ENVS},
'inventory has environment variables'
);
ok(
(any
{ $_->{KEY} eq $name && $_->{VAL} eq $value }
@{$content->{REQUEST}->{CONTENT}->{ENVS}}
),
'inventory has expected environment variable value'
);
($out, $err, $rc) = run_agent(
"$base_options --no-category printer,software,environment"
);
subtest "first inventory" => sub {
check_execution_ok($out, $err, $rc);
};
ok(
!exists $content->{REQUEST}->{CONTENT}->{SOFTWARES},
"inventory doesn't have any software"
);
ok(
!exists $content->{REQUEST}->{CONTENT}->{ENVS},
"inventory doesn't have any environment variables"
);
sub run_agent {
my ($args) = @_;
my @args = $args ? split(/\s+/, $args) : ();
run(
[ $EXECUTABLE_NAME, 'fusioninventory-agent', @args ],
\my ($in, $out, $err)
);
return ($out, $err, $CHILD_ERROR >> 8);
}
sub check_execution_ok {
my ($client, $url) = @_;
ok($rc == 0, 'exit status');
unlike(
$err,
qr/module \S+ disabled: failure to load/,
'no broken module (loading)'
);
unlike(
$err,
qr/unexpected error in \S+/,
'no broken module (execution)'
);
like(
$out,
qr/^<\?xml version="1.0" encoding="UTF-8" \?>/,
'output has correct encoding'
);
$content = XML::TreePP->new()->parse($out);
ok($content, 'output is valid XML');
}