summaryrefslogtreecommitdiffstats
path: root/config-db/t/12-system.t
blob: 0c3a3042c6fa7b144747fc02f76ed076fd74bac8 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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 $system = $configDB->fetchSystemByFilter, 
    'one system [default] should exist (scalar context)'
);

foreach my $requiredCol (qw(name export_id)) {
    my $wrongSystem = {
        'name'      => 'name',
        'export_id' => 1,
        'comment'   => 'has column missing',
    };
    delete $wrongSystem->{$requiredCol};
    ok(
        ! eval { my $systemID = $configDB->addSystem($wrongSystem); },
        "inserting a system without '$requiredCol' column should fail"
    );
}

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

my $inSystem1 = {
    'name'      => 'sys-1',
    'export_id' => 1,
    'comment'   => '',
    'attrs'     => {
        'ramfs_fsmods'  => 'squashfs',
        'ramfs_nicmods' => 'e1000 forcedeth r8169',
        'start_sshd'    => 'yes',
    },
};
is(
    my $system1ID = $configDB->addSystem($inSystem1), 1,
    'first system has ID 1'
);

my $inSystem2 = {
    'name'      => 'sys-2.0',
    'kernel'    => 'vmlinuz',
    'export_id' => 1,
    'comment'   => undef,
};
my $fullSystem = {
    'name'                => 'sys-nr-3',
    'kernel'              => 'vmlinuz-2.6.22.13-0.3-default',
    'export_id'           => 3,
    'comment'             => 'nuff said',
    'label'               => 'BlingBling System - really kuul!',
    'attrs' => {
        'automnt_dir'       => 'a',
        'automnt_src'       => 'b',
        'country'           => 'c',
        'hidden'            => '1',
        'kernel_params'     => 'debug=3 console=ttyS1',
        'ramfs_fsmods'      => 'l',
        'ramfs_miscmods'    => 'm',
        'ramfs_nicmods'     => 'n',
        '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',
    },
};
ok(
    my ($system2ID, $system3ID) = $configDB->addSystem([
        $inSystem2, $fullSystem
    ]),
    'add two more systems'
);
is($system2ID, 2, 'system 2 should have ID=2');
is($system3ID, 3, 'system 3 should have ID=3');

# fetch system 3 by id and check all values
ok(my $system3 = $configDB->fetchSystemByID(3), 'fetch system 3');
is($system3->{id},                  '3',                     'system 3 - id');
is($system3->{name},                'sys-nr-3',              'system 3 - name');
is($system3->{kernel},              'vmlinuz-2.6.22.13-0.3-default',        'system 3 - type');
is($system3->{export_id},           '3',                     'system 3 - export_id');
is($system3->{comment},             'nuff said',             'system 3 - comment');
is($system3->{label},               'BlingBling System - really kuul!', 'system 3 - label');
is($system3->{attrs}->{automnt_dir},       'a',              'system 3 - attr automnt_dir');
is($system3->{attrs}->{automnt_src},       'b',              'system 3 - attr automnt_src');
is($system3->{attrs}->{country},           'c',              'system 3 - attr country');
is($system3->{attrs}->{hidden},            '1',              'system 3 - attr hidden');
is($system3->{attrs}->{kernel_params},     'debug=3 console=ttyS1', 'system 3 - attr kernel_params');
is($system3->{attrs}->{ramfs_fsmods},      'l',              'system 3 - attr ramfs_fsmods');
is($system3->{attrs}->{ramfs_miscmods},    'm',              'system 3 - attr ramfs_miscmods');
is($system3->{attrs}->{ramfs_nicmods},     'n',              'system 3 - attr ramfs_nicmods');
is($system3->{attrs}->{scratch},           'q',              'system 3 - attr scratch');
is($system3->{attrs}->{start_atd},         't',              'system 3 - attr start_atd');
is($system3->{attrs}->{start_cron},        'u',              'system 3 - attr start_cron');
is($system3->{attrs}->{start_dreshal},     'v',              'system 3 - attr start_dreshal');
is($system3->{attrs}->{start_ntp},         'w',              'system 3 - attr start_ftp');
is($system3->{attrs}->{start_nfsv4},       'x',              'system 3 - attr start_nfsv4');
is($system3->{attrs}->{start_snmp},        'A',              'system 3 - attr start_snmp');
is($system3->{attrs}->{start_sshd},        'B',              'system 3 - attr start_sshd');
is($system3->{attrs}->{timezone},          'G',              'system 3 - attr timezone');
is(keys %{$system3->{attrs}},              17,               'system 3 - attribute count');

# fetch system 2 by a filter on id and check all values
ok(
    my $system2 = $configDB->fetchSystemByFilter({ id => 2 }), 
    'fetch system 2 by filter on id'
);
is($system2->{id},            2,         'system 2 - id');
is($system2->{name},          'sys-2.0', 'system 2 - name');
is($system2->{kernel},        'vmlinuz', 'system 2 - kernel');
is($system2->{export_id},     '1',       'system 2 - export_id');
is($system2->{comment},       undef,     'system 2 - comment');
is(keys %{$system2->{attrs}}, 0,         'system 2 - attribute count');

# fetch system 1 by filter on name and check all values
ok(
    my $system1 = $configDB->fetchSystemByFilter({ name => 'sys-1' }), 
    'fetch system 1 by filter on name'
);
is($system1->{id},            1,         'system 1 - id');
is($system1->{name},          'sys-1',   'system 1 - name');
is($system1->{export_id},     '1',       'system 1 - export_id');
is($system1->{kernel},        'vmlinuz', 'system 1 - kernel');
is($system1->{comment},       '',        'system 1 - comment');
is($system1->{label},         'sys-1',   'system 1 - label');
is(keys %{$system1->{attrs}}, 3,         'system 1 - attribute count');
is($system1->{attrs}->{ramfs_fsmods},   'squashfs',              'system 1 - attr ramfs_fsmods');
is($system1->{attrs}->{ramfs_nicmods},  'e1000 forcedeth r8169', 'system 1 - attr ramfs_nicmods');
is($system1->{attrs}->{start_sshd},     'yes',                   'system 1 - attr start_sshd');

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

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

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

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

# try to fetch with multi-column filter
ok(
    ($system2, $system3)
        = $configDB->fetchSystemByFilter({ export_id => '1', id => 2 }), 
    'fetching system with export_id=1 and id=2 should work'
);
is($system2->{name}, 'sys-2.0', 'should have got sys-2.0');
is($system3, undef, 'should not get sys-nr-3');

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

# filter systems by different attributes & values in combination
ok( 
    my @system1Only = $configDB->fetchSystemByFilter( {}, undef, { 
        ramfs_nicmods => 'e1000 forcedeth r8169'
    } ),
    'fetch system 1 by filter on attribute ramfs_nicmods'
);

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

ok(
    @system1Only = $configDB->fetchSystemByFilter( undef, 'id', { 
        ramfs_nicmods => 'e1000 forcedeth r8169',
        slxgrp        => undef,
    } ),
    'fetch system 1 by filter on attribute ramfs_nicmods'
);
is(@system1Only, 1, 'should have got 1 system');
is($system1Only[0]->{id}, 1, 'first id should be 1');

ok(
    @system1Only = $configDB->fetchSystemByFilter( {
        export_id => 1,
        comment   => '',
    }, 'id', {
        ramfs_nicmods => 'e1000 forcedeth r8169',
        slxgrp        => undef,
    } ),
    'fetch system 1 by multiple filter on values and attributes'
);
is(@system1Only, 1, 'should have got 1 system');
is($system1Only[0]->{id}, 1, 'first id should be 1');

is(
    $configDB->fetchSystemByFilter( {
        export_id => 2,
    }, 'id', {
        ramfs_nicmods => 'e1000 forcedeth r8169',
        slxgrp        => undef,
    } ),
    undef,
    'mismatch system 1 by filter with incorrect value'
);
is(
    $configDB->fetchSystemByFilter( {
        export_id => 1,
    }, 'id', {
        ramfs_nicmods => 'xxxx',
        slxgrp        => undef,
    } ),
    undef,
    'mismatch system 1 by filter with incorrect attribute value'
);
is(
    $configDB->fetchSystemByFilter( {
        name      => 'sys-1',
    }, 'id', {
        start_sshd => undef,
    } ),
    undef,
    'mismatch system 1 by filter with attribute not being empty'
);

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

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

# try to fetch a couple of non-existing systems by filter
is(
    $configDB->fetchSystemByFilter({ id => 4 }), undef, 
    'fetching system with id=4 by filter should fail'
);
is(
    $configDB->fetchSystemByFilter({ name => 'sys-1.x' }), undef, 
    'fetching system with name="sys-1.x" should fail'
);
is(
    $configDB->fetchSystemByFilter({ export_id => '2', id => 1 }), undef, 
    'fetching system with export_id=2 and id=1 should fail'
);

# rename system 1 and then fetch it by its new name
ok($configDB->changeSystem(1, { name => q{SYS-'1'} }), 'changing system 1');
ok(
    $system1 = $configDB->fetchSystemByFilter({ name => q{SYS-'1'} }), 
    'fetching renamed system 1'
);
is($system1->{id},   1,          'really got system number 1');
is($system1->{name}, q{SYS-'1'}, q{really got system named "SYS-'1'"});

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

# adding attributes should work
$inSystem1->{attrs}->{slxgrp} = 'slxgrp1';
$inSystem1->{attrs}->{vmware} = 'yes';
ok($configDB->changeSystem(1, $inSystem1), 'adding attrs to system 1');
$system1 = $configDB->fetchSystemByID(1);
is($system1->{attrs}->{slxgrp}, 'slxgrp1', 'attr slxgrp has correct value');
is($system1->{attrs}->{vmware}, 'yes', 'attr vmware has correct value');

# changing an attribute should work
$inSystem1->{attrs}->{vmware} = 'no';
ok($configDB->changeSystem(1, $inSystem1), 'changing vmware in system 1');
$system1 = $configDB->fetchSystemByID(1);
is($system1->{attrs}->{slxgrp}, 'slxgrp1', 'attr slxgrp has correct value');
is($system1->{attrs}->{vmware}, 'no', 'attr vmware has correct value');

# deleting an attribute should remove it
delete $inSystem1->{attrs}->{slxgrp};
ok($configDB->changeSystem(1, $inSystem1), 'changing slxgrp in system 1');
$system1 = $configDB->fetchSystemByID(1);
ok(!exists $system1->{attrs}->{slxgrp}, 'attr slxgrp should be gone');

# undef'ing an attribute should remove it, too
$inSystem1->{attrs}->{vmware} = undef;
ok($configDB->changeSystem(1, $inSystem1), 'undefining vmware in system 1');
$system1 = $configDB->fetchSystemByID(1);
ok(!exists $system1->{attrs}->{vmware}, 'attr vmware should be gone');

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

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

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

$configDB->disconnect();