File: config.t

package info (click to toggle)
fusioninventory-agent 1%3A2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,492 kB
  • sloc: perl: 120,896; xml: 9,459; sh: 760; python: 26; makefile: 13
file content (184 lines) | stat: -rwxr-xr-x 5,609 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;

use English qw(-no_match_vars);
use Test::Deep;
use Test::More;
use Storable;
use UNIVERSAL;

use FusionInventory::Agent::Config;
use lib 't/lib';
use FusionInventory::Test::Utils;

my %config = (
    sample1 => {
        'no-task'     => ['snmpquery', 'wakeonlan'],
        'no-category' => [],
        'httpd-trust' => [],
        'tasks'       => ['inventory', 'deploy', 'inventory'],
        'conf-reload-interval' => 0
    },
    sample2 => {
        'no-task'     => [],
        'no-category' => ['printer'],
        'httpd-trust' => ['example', '127.0.0.1', 'foobar', '123.0.0.0/10'],
        'conf-reload-interval' => 0
    },
    sample3 => {
        'no-task'     => [],
        'no-category' => [],
        'httpd-trust' => [],
        'conf-reload-interval' => 3600
    },
    sample4 => {
        'no-task'     => ['snmpquery','wakeonlan','inventory'],
        'no-category' => [],
        'httpd-trust' => [],
        'tasks'       => ['inventory', 'deploy', 'inventory'],
        'conf-reload-interval' => 60
    },
    include7 => {
        'tag'         => "include7",
        'logfile'     => $OSNAME eq 'MSWin32' ? "C:\\tmp\\logfile.txt" : "/tmp/logfile.txt",
        'timeout'     => 16,
        'no-task'     => [],
        'no-category' => [],
        'httpd-trust' => [],
        'conf-reload-interval' => 0
    },
    include8 => {
        'tag'     => "include8",
        'logfile' => "",
        'timeout' => 16,
        'no-task'     => [],
        'no-category' => [],
        'httpd-trust' => [],
        'conf-reload-interval' => 0
    }
);

my %include = (
    include1 => {
        'tag'       => 'include2',
        'timeout'   => 12
    },
    include2 => {
        'tag'       => 'txt-include',
        'timeout'   => 99
    },
    include3 => {
        'tag'       => 'include3',
        'timeout'   => 15
    },
    include4 => {
        'tag'       => 'loop',
        'timeout'   => 77
    },
    include5 => {
        'tag'       => 'include5',
        'timeout'   => 1
    },
    include6 => {
        'tag'       => 'include2',
        'timeout'   => 16
    }
);

plan tests => (scalar keys %config) * 4 + (scalar keys %include) * 2 + 40;

foreach my $test (keys %config) {
    my $c = FusionInventory::Agent::Config->new(options => {
        'conf-file' => "resources/config/$test"
    });

    foreach my $k (qw/ no-task no-category httpd-trust conf-reload-interval logfile /) {
        cmp_deeply($c->{$k}, $config{$test}->{$k}, $test." ".$k);
    }

    if ($test eq 'sample1') {
        ok ($c->isParamArrayAndFilled('no-task'));
        ok (! $c->isParamArrayAndFilled('no-category'));
        ok (! $c->isParamArrayAndFilled('httpd-trust'));
        ok ($c->isParamArrayAndFilled('tasks'));
    } elsif ($test eq 'sample2') {
        ok (! $c->isParamArrayAndFilled('no-task'));
        ok ($c->isParamArrayAndFilled('no-category'));
        ok ($c->isParamArrayAndFilled('httpd-trust'));
        ok (! $c->isParamArrayAndFilled('tasks'));
    } elsif ($test eq 'sample3') {
        ok (! $c->isParamArrayAndFilled('no-task'));
        ok (! $c->isParamArrayAndFilled('no-category'));
        ok (! $c->isParamArrayAndFilled('httpd-trust'));
        ok (! $c->isParamArrayAndFilled('tasks'));
    } elsif ($test eq 'sample4') {
        ok ($c->isParamArrayAndFilled('no-task'));
        ok (! $c->isParamArrayAndFilled('no-category'));
        ok (! $c->isParamArrayAndFilled('httpd-trust'));
        ok ($c->isParamArrayAndFilled('tasks'));
    }
}

foreach my $test (keys %include) {
    my $cfg = FusionInventory::Agent::Config->new(
        options => {
            'conf-file' => "resources/config/$test"
        }
    );
    # Reload cfg to validate loadedConfs has been reset between loads
    $cfg->reload();

    foreach my $k (qw/ tag timeout /) {
        is($cfg->{$k}, $include{$test}->{$k}, $test." ".$k);
    }
}

my $c = FusionInventory::Agent::Config->new(options => {
        'conf-file' => "resources/config/sample1"
    });
ok (ref($c->{'no-task'}) eq 'ARRAY');
ok (scalar(@{$c->{'no-task'}}) == 2);

$c->reload();
ok (ref($c->{'no-task'}) eq 'ARRAY');
ok (scalar(@{$c->{'no-task'}}) == 2);

$c->{'conf-file'} = "resources/config/sample2";
$c->reload();
my %cNoCategory = map {$_ => 1} @{$c->{'no-category'}};
ok (defined($cNoCategory{'printer'}));
ok (scalar(@{$c->{'no-category'}}) == 1, 'structure size is ' . scalar(@{$c->{'no-category'}}));
#httpd-trust=example,127.0.0.1,foobar,123.0.0.0/10
my %cHttpdTrust = map {$_ => 1} @{$c->{'httpd-trust'}};
ok (defined($cHttpdTrust{'example'}));
ok (defined($cHttpdTrust{'127.0.0.1'}));
ok (defined($cHttpdTrust{'foobar'}));
ok (defined($cHttpdTrust{'123.0.0.0/10'}));
ok (scalar(@{$c->{'httpd-trust'}}) == 4);

SKIP: {
    skip ('test for Windows only', 7) if ($OSNAME ne 'MSWin32');
    my $settings = FusionInventory::Test::Utils::openWin32Registry();
    ok (defined $settings);
    my $testValue = time;
    $settings->{'TEST_KEY'} = $testValue;

    my $settingsRead = FusionInventory::Test::Utils::openWin32Registry();
    ok (defined $settingsRead);
    ok (defined $settingsRead->{'TEST_KEY'});
    ok ($settingsRead->{'TEST_KEY'} eq $testValue);

    # reset conf in registry
    my $deleted;
    if (defined $settings && defined $settings->{'TEST_KEY'}) {
        $deleted = delete $settings->{'TEST_KEY'};
    }
    ok (!(defined($settings->{'TEST_KEY'})));

    $settingsRead = undef;
    $settingsRead = FusionInventory::Test::Utils::openWin32Registry();
    ok (defined $settingsRead);
    ok (!(defined $settingsRead->{'TEST_KEY'}));
}