summaryrefslogtreecommitdiffstats
path: root/config-db/t/13-client.t
blob: 8be715184fffde182036a936066d23713edde1e2 (plain) (blame)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use Test::More qw(no_plan);

use strict;
use warnings;

use lib '/opt/openslx/lib';

# basic init
use OpenSLX::ConfigDB;

my $configDB = OpenSLX::ConfigDB->new;
$configDB->connect();

ok(
    my $client = $configDB->fetchClientByFilter, 
    'one client [default] should exist (scalar context)'
);

foreach my $requiredCol (qw(name mac)) {
    my $wrongClient = {
        'name'    => 'name',
        'mac'     => '01:02:03:04:05:06',
        'comment' => 'has column missing',
    };
    delete $wrongClient->{$requiredCol};
    ok(
        ! eval { my $clientID = $configDB->addClient($wrongClient); },
        "inserting a client without '$requiredCol' column should fail"
    );
}

is(
    my @clients = $configDB->fetchClientByFilter, 1, 
    'still just one client [default] should exist (array context)'
);

my $inClient1 = {
    'name'    => 'cli-1',
    'mac'     => '01:02:03:04:05:01',
    'comment' => '',
    'attrs'     => {
        'start_snmp' => 'no',
        'start_sshd' => 'yes',
    },
};
is(
    my $client1ID = $configDB->addClient($inClient1), 1,
    'first client has ID 1'
);

my $inClient2 = {
    'name'       => 'cli-2.0',
    'mac'        => '01:02:03:04:05:02',
    'comment'    => undef,
    'attrs' => {
        'boot_type'  => 'etherboot',
        'unbootable' => 1,
    }
};
my $fullClient = {
    'name'          => 'cli-nr-3',
    'mac'           => '01:02:03:04:05:03',
    'comment'       => 'nuff said',
    'attrs' => {
        'automnt_dir'       => 'a',
        'automnt_src'       => 'b',
        'boot_type'         => 'pxe',
        'country'           => 'c',
        'kernel_params_client' => 'debug=3 console=ttyS1',
        'scratch'           => 'q',
        'start_atd'         => 't',
        'start_cron'        => 'u',
        'start_dreshal'     => 'v',
        'start_ntp'         => 'w',
        'start_nfsv4'       => 'x',
        'start_snmp'        => 'A',
        'start_sshd'        => 'B',
        'timezone'          => 'G',
        'unbootable'        => '0',
    },
};
ok(
    my ($client2ID, $client3ID) = $configDB->addClient([
        $inClient2, $fullClient
    ]),
    'add two more clients'
);
is($client2ID, 2, 'client 2 should have ID=2');
is($client3ID, 3, 'client 3 should have ID=3');

# fetch client 3 by id and check all values
ok(my $client3 = $configDB->fetchClientByID(3), 'fetch client 3');
is($client3->{id},            '3',                     'client 3 - id');
is($client3->{name},          'cli-nr-3',              'client 3 - name');
is($client3->{mac},           '01:02:03:04:05:03',     'client 3 - mac');
is($client3->{comment},       'nuff said',             'client 3 - comment');
is($client3->{attrs}->{automnt_dir},       'a',              'client 3 - attr automnt_dir');
is($client3->{attrs}->{automnt_src},       'b',              'client 3 - attr automnt_src');
is($client3->{attrs}->{boot_type},         'pxe',            'client 3 - attr boot_type');
is($client3->{attrs}->{country},           'c',              'client 3 - attr country');
is($client3->{attrs}->{kernel_params_client}, 'debug=3 console=ttyS1', 'client 3 - attr kernel_params_client');
is($client3->{attrs}->{scratch},           'q',              'client 3 - attr scratch');
is($client3->{attrs}->{start_atd},         't',              'client 3 - attr start_atd');
is($client3->{attrs}->{start_cron},        'u',              'client 3 - attr start_cron');
is($client3->{attrs}->{start_dreshal},     'v',              'client 3 - attr start_dreshal');
is($client3->{attrs}->{start_ntp},         'w',              'client 3 - attr start_ftp');
is($client3->{attrs}->{start_nfsv4},       'x',              'client 3 - attr start_nfsv4');
is($client3->{attrs}->{start_snmp},        'A',              'client 3 - attr start_snmp');
is($client3->{attrs}->{start_sshd},        'B',              'client 3 - attr start_sshd');
is($client3->{attrs}->{timezone},          'G',              'client 3 - attr timezone');
is($client3->{attrs}->{unbootable},        '0',              'client 3 - attr unbootable');
is(keys %{$client3->{attrs}},              15,               'client 3 - attribute count');

# fetch client 2 by a filter on id and check all values
ok(
    my $client2 = $configDB->fetchClientByFilter({ id => 2 }), 
    'fetch client 2 by filter on id'
);
is($client2->{id},            2,                   'client 2 - id');
is($client2->{name},          'cli-2.0',           'client 2 - name');
is($client2->{mac},           '01:02:03:04:05:02', 'client 2 - mac');
is($client2->{comment},       undef,               'client 2 - comment');
is(keys %{$client2->{attrs}}, 2,                   'client 2 - attribute count');
is($client2->{attrs}->{boot_type},  'etherboot',   'client 2 - attr boot_type');
is($client2->{attrs}->{unbootable}, '1',           'client 2 - attr unbootable');

# fetch client 1 by filter on name and check all values
ok(
    my $client1 = $configDB->fetchClientByFilter({ name => 'cli-1' }), 
    'fetch client 1 by filter on name'
);
is($client1->{id},            1,                   'client 1 - id');
is($client1->{name},          'cli-1',             'client 1 - name');
is($client1->{mac},           '01:02:03:04:05:01', 'client 1 - mac');
is($client1->{comment},       '',                  'client 1 - comment');
is(keys %{$client1->{attrs}}, 2,                   'client 1 - attribute count');
is($client1->{attrs}->{start_snmp}, 'no',          'client 1 - attr start_snmp');
is($client1->{attrs}->{start_sshd}, 'yes',         'client 1 - attr start_sshd');

# fetch clients 3 & 1 by id
ok(
    my @clients3And1 = $configDB->fetchClientByID([3, 1]), 
    'fetch clients 3 & 1 by id'
);
is(@clients3And1, 2, 'should have got 2 clients');
# now sort by ID and check if we have really got 3 and 1
@clients3And1 = sort { $a->{id} cmp $b->{id} } @clients3And1;
is($clients3And1[0]->{id}, 1, 'first id should be 1');
is($clients3And1[1]->{id}, 3, 'second id should be 3');

# fetching clients by id without giving any should yield undef
is(
    $configDB->fetchClientByID(), undef,
    'fetch clients by id without giving any'
);

# fetching clients by filter without giving any should yield all of them
ok(
    @clients = $configDB->fetchClientByFilter(),
    'fetch clients by filter without giving any'
);
is(@clients, 4, 'should have got all four clients');

# try to fetch multiple occurrences of the same client, combined with
# some unknown IDs
ok(
    my @clients1And3 = $configDB->fetchClientByID([ 1, 21, 4-1, 1, 4, 1, 1 ]), 
    'fetch a complex set of clients by ID'
);
is(@clients1And3, 2, 'should have got 2 clients');
# now sort by ID and check if we have really got 1 and 3
@clients1And3 = sort { $a->{id} cmp $b->{id} } @clients1And3;
is($clients1And3[0]->{id}, 1, 'first id should be 1');
is($clients1And3[1]->{id}, 3, 'second id should be 3');

# filter clients by different attributes & values in combination
ok( 
    my @client1Only = $configDB->fetchClientByFilter( {}, undef, { 
        start_snmp => 'no',
    } ),
    'fetch client 1 by filter on attribute start_snmp'
);

is(@client1Only, 1, 'should have got 1 client');
is($client1Only[0]->{id}, 1, 'first id should be 1');

ok(
    @client1Only = $configDB->fetchClientByFilter( undef, 'id', { 
        start_snmp => 'no',
        tex_enable => undef,
    } ),
    'fetch client 1 by filter on attribute start_snmp + non-existing attr'
);
is(@client1Only, 1, 'should have got 1 client');
is($client1Only[0]->{id}, 1, 'first id should be 1');

is(
    $configDB->fetchClientByFilter( {
        comment => 'xxx',
    }, 'id', {
        start_snmp => 'no',
        start_dreshal => undef,
    } ),
    undef,
    'mismatch client 1 by filter with incorrect value'
);
is(
    $configDB->fetchClientByFilter( {
        name => 'cli-1',
    }, 'id', {
        start_snmp => 'yes',
        start_dreshal => undef,
    } ),
    undef,
    'mismatch client 1 by filter with incorrect attribute value'
);
is(
    $configDB->fetchClientByFilter( {
        name => 'cli-1',
    }, 'id', {
        start_sshd => undef,
    } ),
    undef,
    'mismatch client 1 by filter with attribute not being empty'
);

# fetch clients 0, 1 & 2 by filter on attribute start_dreshal not existing
ok(
    my @clients01And2 = $configDB->fetchClientByFilter( {}, undef, {
        start_dreshal => undef,
    } ), 
    'fetch clients 0,1 & 2 by filter on attribute start_dreshal not existing'
);
is(@clients01And2, 3, 'should have got 3 clients');
# now sort by ID and check if we have really got 0, 1 and 2
@clients01And2 = sort { $a->{id} cmp $b->{id} } @clients01And2;
is($clients01And2[0]->{id}, 0, 'first id should be 0');
is($clients01And2[1]->{id}, 1, 'second id should be 1');
is($clients01And2[2]->{id}, 2, 'third id should be 2');

# try to fetch a couple of non-existing clients by id
is(
    $configDB->fetchClientByID(-1), undef, 
    'client with id -1 should not exist'
);
ok($configDB->fetchClientByID(0), 'client with id 0 should exist');
is(
    $configDB->fetchClientByID(1 << 31 + 1000), undef, 
    'trying to fetch another unknown client'
);

# try to fetch a couple of non-existing clients by filter
is(
    $configDB->fetchClientByFilter({ id => 4 }), undef, 
    'fetching client with id=4 by filter should fail'
);
is(
    $configDB->fetchClientByFilter({ name => 'cli-1.x' }), undef, 
    'fetching client with name="cli-1.x" should fail'
);
is(
    $configDB->fetchClientByFilter({ mac => '01:01:01:01:01:01', id => 1 }), undef, 
    'fetching client with mac=01:01:01:01:01:01 and id=1 should fail'
);

# rename client 1 and then fetch it by its new name
ok($configDB->changeClient(1, { name => q{CLI-'1'} }), 'changing client 1');
ok(
    $client1 = $configDB->fetchClientByFilter({ name => q{CLI-'1'} }), 
    'fetching renamed client 1'
);
is($client1->{id},   1,          'really got client number 1');
is($client1->{name}, q{CLI-'1'}, q{really got client named "CLI-'1'"});

# changing nothing at all should succeed
ok($configDB->changeClient(1), 'changing nothing at all in client 1');

# adding attributes should work
$inClient1->{attrs}->{slxgrp} = 'slxgrp1';
$inClient1->{attrs}->{vmware} = 'yes';
ok($configDB->changeClient(1, $inClient1), 'adding attrs to client 1');
$client1 = $configDB->fetchClientByID(1);
is($client1->{attrs}->{slxgrp}, 'slxgrp1', 'attr slxgrp has correct value');
is($client1->{attrs}->{vmware}, 'yes', 'attr vmware has correct value');

# changing an attribute should work
$inClient1->{attrs}->{vmware} = 'no';
ok($configDB->changeClient(1, $inClient1), 'changing vmware in client 1');
$client1 = $configDB->fetchClientByID(1);
is($client1->{attrs}->{slxgrp}, 'slxgrp1', 'attr slxgrp has correct value');
is($client1->{attrs}->{vmware}, 'no', 'attr vmware has correct value');

# deleting an attribute should remove it
delete $inClient1->{attrs}->{slxgrp};
ok($configDB->changeClient(1, $inClient1), 'changing slxgrp in client 1');
$client1 = $configDB->fetchClientByID(1);
ok(!exists $client1->{attrs}->{slxgrp}, 'attr slxgrp should be gone');

# undef'ing an attribute should remove it, too
$inClient1->{attrs}->{vmware} = undef;
ok($configDB->changeClient(1, $inClient1), 'undefining vmware in client 1');
$client1 = $configDB->fetchClientByID(1);
ok(!exists $client1->{attrs}->{vmware}, 'attr vmware should be gone');

# changing a non-existing column should fail
ok(
    ! eval { $configDB->changeClient(1, { xname => "xx" }) }, 
    'changing unknown colum should fail'
);

ok(! $configDB->changeClient(1, { id => 23 }), 'changing id should fail');

# now remove an client and check if that worked
ok($configDB->removeClient(2), 'removing client 2 should be ok');
is($configDB->fetchClientByID(2, 'id'), undef, 'client 2 should be gone');
is($configDB->fetchClientByID(1)->{id}, 1, 'client 1 should still exist');
is($configDB->fetchClientByID(3)->{id}, 3, 'client 3 should still exist');

$configDB->disconnect();