diff options
author | Oliver Tappe | 2007-12-27 23:35:57 +0100 |
---|---|---|
committer | Oliver Tappe | 2007-12-27 23:35:57 +0100 |
commit | 230200926c8332c8b6d16e5f4146b34ed8c403bb (patch) | |
tree | a3ebbb270e815b5125028fe94072511f94d1c2b8 /config-db | |
parent | * Changed naming scheme for exports from <vendor-os>:<type> to (diff) | |
download | core-230200926c8332c8b6d16e5f4146b34ed8c403bb.tar.gz core-230200926c8332c8b6d16e5f4146b34ed8c403bb.tar.xz core-230200926c8332c8b6d16e5f4146b34ed8c403bb.zip |
* added more tests for basic DB-features: group, global_info and
client_system_ref
git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1435 95ad53e4-c205-0410-b2fa-d234c58c8868
Diffstat (limited to 'config-db')
-rw-r--r-- | config-db/OpenSLX/ConfigDB.pm | 12 | ||||
-rw-r--r-- | config-db/OpenSLX/MetaDB/DBI.pm | 51 | ||||
-rw-r--r-- | config-db/t/11-export.t | 23 | ||||
-rw-r--r-- | config-db/t/14-group.t | 191 | ||||
-rw-r--r-- | config-db/t/15-global_info.t | 40 | ||||
-rw-r--r-- | config-db/t/20-client_system_ref.t | 138 |
6 files changed, 435 insertions, 20 deletions
diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index 30809b08..9030311d 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -1106,6 +1106,8 @@ sub changeGlobalInfo my $id = shift; my $value = shift; + return if !defined $self->{'meta-db'}->fetchGlobalInfo($id); + return $self->{'meta-db'}->changeGlobalInfo($id, $value); } @@ -1137,7 +1139,8 @@ sub addSystem foreach my $valRow (@$valRows) { if (!$valRow->{kernel}) { $valRow->{kernel} = 'vmlinuz'; - warn( + vlog( + 1, _tr( "setting kernel of system '%s' to 'vmlinuz'!", $valRow->{name} @@ -1764,6 +1767,13 @@ sub addGroup my $self = shift; my $valRows = _aref(shift); + _checkCols($valRows, 'group', qw(name)); + + foreach my $valRow (@$valRows) { + if (!defined $valRow->{priority}) { + $valRow->{priority} = '50'; + } + } return $self->{'meta-db'}->addGroup($valRows); } diff --git a/config-db/OpenSLX/MetaDB/DBI.pm b/config-db/OpenSLX/MetaDB/DBI.pm index 380a1847..a1b782c1 100644 --- a/config-db/OpenSLX/MetaDB/DBI.pm +++ b/config-db/OpenSLX/MetaDB/DBI.pm @@ -103,6 +103,10 @@ sub _doSelect push @vals, $row; } } + + # return undef if there's no result in scalar context + return if !wantarray(); + return @vals; } @@ -183,7 +187,7 @@ sub fetchGlobalInfo my $id = shift; return if !length($id); - my $sql = "SELECT * FROM global_info WHERE id = " . $self->quote($id); + my $sql = "SELECT value FROM global_info WHERE id = " . $self->quote($id); return $self->_doSelect($sql, 'value'); } @@ -310,10 +314,11 @@ sub fetchGroupByFilter $resultCols = '*' unless (defined $resultCols); my $sql = "SELECT $resultCols FROM groups"; - my $connector; + my ($connector, $quotedVal); foreach my $col (keys %$filter) { $connector = !defined $connector ? 'WHERE' : 'AND'; - $sql .= " $connector $col = '$filter->{$col}'"; + $quotedVal = $self->{dbh}->quote($filter->{$col}); + $sql .= " $connector $col = $quotedVal"; } return $self->_doSelect($sql); } @@ -628,8 +633,10 @@ sub setClientIDsOfSystem my $clientIDs = shift; my @currClients = $self->fetchClientIDsOfSystem($systemID); - return $self->_updateRefTable('client_system_ref', $systemID, $clientIDs, - 'system_id', 'client_id', \@currClients); + return $self->_updateRefTable( + 'client_system_ref', $systemID, $clientIDs, 'system_id', 'client_id', + \@currClients + ); } sub setGroupIDsOfSystem @@ -639,8 +646,10 @@ sub setGroupIDsOfSystem my $groupIDs = shift; my @currGroups = $self->fetchGroupIDsOfSystem($systemID); - return $self->_updateRefTable('group_system_ref', $systemID, $groupIDs, - 'system_id', 'group_id', \@currGroups); + return $self->_updateRefTable( + 'group_system_ref', $systemID, $groupIDs, 'system_id', 'group_id', + \@currGroups + ); } sub addClient @@ -675,9 +684,10 @@ sub setSystemIDsOfClient my $systemIDs = shift; my @currSystems = $self->fetchSystemIDsOfClient($clientID); - $self->_updateRefTable('client_system_ref', $clientID, $systemIDs, - 'client_id', 'system_id', \@currSystems); - return; + return $self->_updateRefTable( + 'client_system_ref', $clientID, $systemIDs, 'client_id', 'system_id', + \@currSystems + ); } sub setGroupIDsOfClient @@ -687,9 +697,10 @@ sub setGroupIDsOfClient my $groupIDs = shift; my @currGroups = $self->fetchGroupIDsOfClient($clientID); - $self->_updateRefTable('group_client_ref', $clientID, $groupIDs, - 'client_id', 'group_id', \@currGroups); - return; + return $self->_updateRefTable( + 'group_client_ref', $clientID, $groupIDs, 'client_id', 'group_id', + \@currGroups + ); } sub addGroup @@ -724,9 +735,10 @@ sub setClientIDsOfGroup my $clientIDs = shift; my @currClients = $self->fetchClientIDsOfGroup($groupID); - $self->_updateRefTable('group_client_ref', $groupID, $clientIDs, 'group_id', - 'client_id', \@currClients); - return; + return $self->_updateRefTable( + 'group_client_ref', $groupID, $clientIDs, 'group_id', 'client_id', + \@currClients + ); } sub setSystemIDsOfGroup @@ -736,9 +748,10 @@ sub setSystemIDsOfGroup my $systemIDs = shift; my @currSystems = $self->fetchSystemIDsOfGroup($groupID); - $self->_updateRefTable('group_system_ref', $groupID, $systemIDs, 'group_id', - 'system_id', \@currSystems); - return; + return $self->_updateRefTable( + 'group_system_ref', $groupID, $systemIDs, 'group_id', 'system_id', + \@currSystems + ); } ################################################################################ diff --git a/config-db/t/11-export.t b/config-db/t/11-export.t index b122ffdd..96fad00b 100644 --- a/config-db/t/11-export.t +++ b/config-db/t/11-export.t @@ -32,6 +32,16 @@ is( 'no export yet (array context)' ); +is( + my @exportIDs = $configDB->fetchExportIDsOfVendorOS(1), 0, + 'vendor-OS 1 has no export IDs yet' +); + +is( + @exportIDs = $configDB->fetchExportIDsOfVendorOS(2), 0, + 'vendor-OS 2 has no export IDs yet' +); + my $inExport1 = { 'name' => 'exp-1', 'type' => 'nfs', @@ -107,6 +117,19 @@ is($export1->{port}, undef, 'export 1 - port'); is($export1->{server_ip}, undef, 'export 1 - server_ip'); is($export1->{uri}, undef, 'export 1 - uri'); +is( + @exportIDs = sort( { $a <=> $b } $configDB->fetchExportIDsOfVendorOS(1)), + 2, 'vendor-OS 1 has two export IDs' +); +is($exportIDs[0], 1, 'first export ID of vendor-OS 1 (1)'); +is($exportIDs[1], 2, 'second export ID of vendor-OS 1 (2)'); + +is( + @exportIDs = sort( { $a <=> $b } $configDB->fetchExportIDsOfVendorOS(2)), + 1, 'vendor-OS 2 has one export IDs' +); +is($exportIDs[0], 3, 'first export ID of vendor-OS 2 (3)'); + # fetch exports 3 & 1 by id ok( my @exports3And1 = $configDB->fetchExportByID([3, 1]), diff --git a/config-db/t/14-group.t b/config-db/t/14-group.t new file mode 100644 index 00000000..70831a5e --- /dev/null +++ b/config-db/t/14-group.t @@ -0,0 +1,191 @@ +use Test::More qw(no_plan); + +use lib '/opt/openslx/lib'; + +# basic init +use OpenSLX::ConfigDB; + +my $configDB = OpenSLX::ConfigDB->new; +$configDB->connect(); + +is( + my $group = $configDB->fetchGroupByFilter, undef, + 'no group should exist (scalar context)' +); + +foreach my $requiredCol (qw(name)) { + my $wrongGroup = { + 'name' => 'name', + 'priority' => 41, + 'comment' => 'has column missing', + }; + delete $wrongGroup->{$requiredCol}; + ok( + ! eval { my $groupID = $configDB->addGroup($wrongGroup); }, + "inserting a group without '$requiredCol' column should fail" + ); +} + +is( + my @groups = $configDB->fetchGroupByFilter, 0, + 'still no group should exist (array context)' +); + +my $inGroup1 = { + 'name' => 'grp-1', + 'comment' => '', +}; +is( + my $group1ID = $configDB->addGroup($inGroup1), 1, + 'first group has ID 1' +); + +my $inGroup2 = { + 'name' => 'grp-2.0', + 'priority' => 30, + 'comment' => undef, +}; +my $fullGroup = { + 'name' => 'grp-nr-3', + 'priority' => 50, + 'comment' => 'nuff said', +}; +ok( + my ($group2ID, $group3ID) = $configDB->addGroup([ + $inGroup2, $fullGroup + ]), + 'add two more groups' +); +is($group2ID, 2, 'group 2 should have ID=2'); +is($group3ID, 3, 'group 3 should have ID=3'); + +# fetch group 3 by id and check all values +ok(my $group3 = $configDB->fetchGroupByID(3), 'fetch group 3'); +is($group3->{id}, '3', 'group 3 - id'); +is($group3->{name}, 'grp-nr-3', 'group 3 - name'); +is($group3->{priority}, 50, 'group 3 - priority'); +is($group3->{comment}, 'nuff said', 'group 3 - comment'); + +# fetch group 2 by a filter on id and check all values +ok( + my $group2 = $configDB->fetchGroupByFilter({ id => 2 }), + 'fetch group 2 by filter on id' +); +is($group2->{id}, 2, 'group 2 - id'); +is($group2->{name}, 'grp-2.0', 'group 2 - name'); +is($group2->{priority}, 30, 'group 2 - priority'); +is($group2->{comment}, undef, 'group 2 - comment'); + +# fetch group 1 by filter on name and check all values +ok( + my $group1 = $configDB->fetchGroupByFilter({ name => 'grp-1' }), + 'fetch group 1 by filter on name' +); +is($group1->{id}, 1, 'group 1 - id'); +is($group1->{name}, 'grp-1', 'group 1 - name'); +is($group1->{priority}, 50, 'group 1 - priority'); +is($group1->{comment}, '', 'group 1 - comment'); + +# fetch groups 3 & 1 by id +ok( + my @groups3And1 = $configDB->fetchGroupByID([3, 1]), + 'fetch groups 3 & 1 by id' +); +is(@groups3And1, 2, 'should have got 2 groups'); +# now sort by ID and check if we have really got 3 and 1 +@groups3And1 = sort { $a->{id} cmp $b->{id} } @groups3And1; +is($groups3And1[0]->{id}, 1, 'first id should be 1'); +is($groups3And1[1]->{id}, 3, 'second id should be 3'); + +# fetching groups by id without giving any should yield undef +is( + $configDB->fetchGroupByID(), undef, + 'fetch groups by id without giving any' +); + +# fetching groups by filter without giving any should yield all of them +ok( + @groups = $configDB->fetchGroupByFilter(), + 'fetch groups by filter without giving any' +); +is(@groups, 3, 'should have got all three groups'); + +# fetch groups 1 & 2 by filter on priority +ok( + my @groups1And3 = $configDB->fetchGroupByFilter({ priority => 50 }), + 'fetch groups 1 & 3 by filter on priority' +); +is(@groups1And3, 2, 'should have got 2 groups'); +# now sort by ID and check if we have really got 1 and 3 +@groups1And3 = sort { $a->{id} cmp $b->{id} } @groups1And3; +is($groups1And3[0]->{id}, 1, 'first id should be 1'); +is($groups1And3[1]->{id}, 3, 'second id should be 3'); + +# try to fetch with multi-column filter +ok( + ($group1, $group3) + = $configDB->fetchGroupByFilter({ priority => '50', id => 1 }), + 'fetching group with priority=50 and id=1 should work' +); +is($group1->{name}, 'grp-1', 'should have got grp-1'); +is($group3, undef, 'should not get grp-nr-3'); + +# try to fetch multiple occurrences of the same group, combined with +# some unknown IDs +ok( + @groups1And3 = $configDB->fetchGroupByID([ 1, 21, 4-1, 1, 4, 1, 1 ]), + 'fetch a complex set of groups by ID' +); +is(@groups1And3, 2, 'should have got 2 groups'); +# now sort by ID and check if we have really got 1 and 3 +@groups1And3 = sort { $a->{id} cmp $b->{id} } @groups1And3; +is($groups1And3[0]->{id}, 1, 'first id should be 1'); +is($groups1And3[1]->{id}, 3, 'second id should be 3'); + +# try to fetch a couple of non-existing groups by id +is($configDB->fetchGroupByID(-1), undef, 'group with id -1 should not exist'); +is($configDB->fetchGroupByID(0), undef, 'group with id 0 should not exist'); +is( + $configDB->fetchGroupByID(1 << 31 + 1000), undef, + 'trying to fetch another unknown group' +); + +# try to fetch a couple of non-existing groups by filter +is( + $configDB->fetchGroupByFilter({ id => 4 }), undef, + 'fetching group with id=4 by filter should fail' +); +is( + $configDB->fetchGroupByFilter({ name => 'grp-1.x' }), undef, + 'fetching group with name="grp-1.x" should fail' +); +is( + $configDB->fetchGroupByFilter({ priority => '22', id => 1 }), undef, + 'fetching group with priority=22 and id=1 should fail' +); + +# rename group 1 and then fetch it by its new name +ok($configDB->changeGroup(1, { name => q{GRP-'1'} }), 'changing group 1'); +ok( + $group1 = $configDB->fetchGroupByFilter({ name => q{GRP-'1'} }), + 'fetching renamed group 1' +); +is($group1->{id}, 1, 'really got group number 1'); +is($group1->{name}, q{GRP-'1'}, q{really got group named "GRP-'1'"}); + +# changing a non-existing column should fail +ok( + ! eval { $configDB->changeGroup(1, { xname => "xx" }) }, + 'changing unknown colum should fail' +); + +ok(! $configDB->changeGroup(1, { id => 23 }), 'changing id should fail'); + +# now remove an group and check if that worked +ok($configDB->removeGroup(2), 'removing group 2 should be ok'); +is($configDB->fetchGroupByID(2, 'id'), undef, 'group 2 should be gone'); +is($configDB->fetchGroupByID(1)->{id}, 1, 'group 1 should still exist'); +is($configDB->fetchGroupByID(3)->{id}, 3, 'group 3 should still exist'); + +$configDB->disconnect(); + diff --git a/config-db/t/15-global_info.t b/config-db/t/15-global_info.t new file mode 100644 index 00000000..82c04997 --- /dev/null +++ b/config-db/t/15-global_info.t @@ -0,0 +1,40 @@ +use Test::More qw(no_plan); + +use lib '/opt/openslx/lib'; + +# basic init +use OpenSLX::ConfigDB; + +my $configDB = OpenSLX::ConfigDB->new; +$configDB->connect(); + +# fetch global-info 'next-nbd-server-port' +ok( + $globalInfo = $configDB->fetchGlobalInfo('next-nbd-server-port'), + 'fetch global-info' +); +is($globalInfo, '5000', 'global-info - value'); + +# try to fetch a couple of non-existing global-infos +is( + $configDB->fetchGlobalInfo(-1), undef, + 'global-info with id -1 should not exist' +); +is($configDB->fetchGlobalInfo('xxx'), undef, + 'global-info with id xxx should not exist'); + +# change value of global-info and then fetch and check the new value +ok($configDB->changeGlobalInfo('next-nbd-server-port', '5050'), 'changing global-info'); +is( + $configDB->fetchGlobalInfo('next-nbd-server-port'), '5050', + 'fetching changed global-info' +); + +# changing a non-existing global-info should fail +ok( + ! eval { $configDB->changeGlobalInfo('xxx', 'new-value') }, + 'changing unknown global-info should fail' +); + +$configDB->disconnect(); + diff --git a/config-db/t/20-client_system_ref.t b/config-db/t/20-client_system_ref.t new file mode 100644 index 00000000..ab7a3b2a --- /dev/null +++ b/config-db/t/20-client_system_ref.t @@ -0,0 +1,138 @@ +use Test::More qw(no_plan); + +use lib '/opt/openslx/lib'; + +# basic init +use OpenSLX::ConfigDB; + +my $configDB = OpenSLX::ConfigDB->new; +$configDB->connect(); + +# fetch clients & systems +my @clients = sort { $a->{id} <=> $b->{id} } $configDB->fetchClientByFilter(); +is(@clients, 3, 'should have got 3 clients (default, 1 and 3)'); +my $defaultClient = shift @clients; +my $client1 = shift @clients; +my $client3 = shift @clients; + +my @systems = sort { $a->{id} <=> $b->{id} } $configDB->fetchSystemByFilter(); +is(@systems, 3, 'should have got 3 systems (default, 1 and 3)'); +my $defaultSystem = shift @systems; +my $system1 = shift @systems; +my $system3 = shift @systems; + +foreach my $client ($defaultClient, $client1, $client3) { + is( + my @systemIDs = $configDB->fetchSystemIDsOfClient($client->{id}), + 0, "client $client->{id} has no system-IDs yet" + ); +} + +foreach my $system ($defaultSystem, $system1, $system3) { + is( + my @clientIDs = $configDB->fetchClientIDsOfSystem($system->{id}), + 0, "system $system->{id} has no client-IDs yet" + ); +} + +ok( + $configDB->addSystemIDsToClient(1, [3]), + 'system-ID 3 has been associated to client 1' +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(0)), + 0, "default client should have no system-ID" +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(1)), + 1, "client 1 should have one system-ID" +); +is($systemIDs[0], 3, "first system of client 1 should have ID 3"); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(3)), + 0, "client 3 should have no system-ID" +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(0)), + 0, "default system should have no client-IDs" +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(1)), + 0, "system 1 should have no client-IDs" +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(3)), + 1, "system 3 should have one client-ID" +); +is($clientIDs[0], 1, "first client of system 3 should have ID 1"); + +ok( + $configDB->addSystemIDsToClient(3, [1,3,3,1,3]), + 'system-IDs 1 and 3 have been associated to client 3' +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(0)), + 0, "default client should have no system-IDs" +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(1)), + 1, "client 1 should have one system-ID" +); +is($systemIDs[0], 3, "first system of client 1 should have ID 3"); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(3)), + 2, "client 3 should have two system-IDs" +); +is($systemIDs[0], 1, "first system of client 3 should have ID 1"); +is($systemIDs[1], 3, "second system of client 3 should have ID 3"); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(0)), + 0, "default system should have no client-ID" +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(1)), + 1, "system 1 should have one client-ID" +); +is($clientIDs[0], 3, "first client of system 1 should have ID 3"); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(3)), + 2, "system 3 should have two client-IDs" +); +is($clientIDs[0], 1, "first client of system 3 should have ID 1"); +is($clientIDs[1], 3, "second client of system 3 should have ID 3"); + +ok( + $configDB->setClientIDsOfSystem(3, []), + 'client-IDs of system 3 have been set to empty array' +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(3)), + 0, "system 3 should have no client-IDs" +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(1)), + 0, "client 1 should have no more system-IDs" +); +is( + @systemIDs = sort($configDB->fetchSystemIDsOfClient(3)), + 1, "client 3 should have one system-ID" +); +is($systemIDs[0], 1, "first system of client 3 should have ID 1"); + +ok( + $configDB->removeClientIDsFromSystem(1, [1]), + 'removing an unassociated client-ID should have no effect' +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(1)), + 1, "system 1 should have one client-ID" +); +ok( + $configDB->removeClientIDsFromSystem(1, [3]), + 'removing an associated client-ID should work' +); +is( + @clientIDs = sort($configDB->fetchClientIDsOfSystem(1)), + 0, "system 1 should have no more client-ID" +); +$configDB->disconnect(); |