From 5ffdf45ad2ac31302cf3c4b3d6ecb27f4ffffe32 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Sun, 16 Dec 2007 15:27:43 +0000 Subject: * completed regression tests for vendor-OSes * added regression tests for exports, systems and clients * fixed several bugs found by these tests * cosmetical cleanups in ConfigDB.pm git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1431 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/OpenSLX/ConfigDB.pm | 79 +++++++++++++- config-db/OpenSLX/MetaDB/DBI.pm | 19 ++-- config-db/t/10-vendor-os.t | 17 +++- config-db/t/11-export.t | 218 +++++++++++++++++++++++++++++++++++++++ config-db/t/12-system.t | 221 ++++++++++++++++++++++++++++++++++++++++ config-db/t/13-client.t | 208 +++++++++++++++++++++++++++++++++++++ config-db/t/run-all-tests.pl | 4 + 7 files changed, 754 insertions(+), 12 deletions(-) create mode 100644 config-db/t/11-export.t create mode 100644 config-db/t/12-system.t create mode 100644 config-db/t/13-client.t (limited to 'config-db') diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index 0dfaffd7..30809b08 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -115,6 +115,7 @@ sub new my $self = { }; + return bless $self, $class; } @@ -218,6 +219,7 @@ sub startTransaction my $self = shift; $self->{'meta-db'}->startTransaction(); + return 1; } @@ -233,6 +235,7 @@ sub commitTransaction my $self = shift; $self->{'meta-db'}->commitTransaction(); + return 1; } @@ -248,6 +251,7 @@ sub rollbackTransaction my $self = shift; $self->{'meta-db'}->rollbackTransaction(); + return 1; } @@ -317,6 +321,7 @@ sub fetchVendorOSByFilter my @vendorOS = $self->{'meta-db'}->fetchVendorOSByFilter($filter, $resultCols); + return wantarray() ? @vendorOS : shift @vendorOS; } @@ -349,6 +354,7 @@ sub fetchVendorOSByID my $resultCols = shift; my @vendorOS = $self->{'meta-db'}->fetchVendorOSByID($ids, $resultCols); + return wantarray() ? @vendorOS : shift @vendorOS; } @@ -383,6 +389,7 @@ sub fetchExportByFilter my $resultCols = shift; my @exports = $self->{'meta-db'}->fetchExportByFilter($filter, $resultCols); + return wantarray() ? @exports : shift @exports; } @@ -415,6 +422,7 @@ sub fetchExportByID my $resultCols = shift; my @exports = $self->{'meta-db'}->fetchExportByID($ids, $resultCols); + return wantarray() ? @exports : shift @exports; } @@ -501,6 +509,7 @@ sub fetchSystemByFilter my $resultCols = shift; my @systems = $self->{'meta-db'}->fetchSystemByFilter($filter, $resultCols); + return wantarray() ? @systems : shift @systems; } @@ -533,6 +542,7 @@ sub fetchSystemByID my $resultCols = shift; my @systems = $self->{'meta-db'}->fetchSystemByID($ids, $resultCols); + return wantarray() ? @systems : shift @systems; } @@ -646,6 +656,7 @@ sub fetchClientByFilter my $filter = shift; my @clients = $self->{'meta-db'}->fetchClientByFilter($filter); + return wantarray() ? @clients : shift @clients; } @@ -678,6 +689,7 @@ sub fetchClientByID my $resultCols = shift; my @clients = $self->{'meta-db'}->fetchClientByID($ids, $resultCols); + return wantarray() ? @clients : shift @clients; } @@ -766,6 +778,7 @@ sub fetchGroupByFilter my $resultCols = shift; my @groups = $self->{'meta-db'}->fetchGroupByFilter($filter, $resultCols); + return wantarray() ? @groups : shift @groups; } @@ -798,6 +811,7 @@ sub fetchGroupByID my $resultCols = shift; my @groups = $self->{'meta-db'}->fetchGroupByID($ids, $resultCols); + return wantarray() ? @groups : shift @groups; } @@ -884,6 +898,8 @@ sub addVendorOS my $self = shift; my $valRows = _aref(shift); + _checkCols($valRows, 'vendor_os', 'name'); + return $self->{'meta-db'}->addVendorOS($valRows); } @@ -967,6 +983,8 @@ sub addExport my $self = shift; my $valRows = _aref(shift); + _checkCols($valRows, 'export', qw(name vendor_os_id type)); + return $self->{'meta-db'}->addExport($valRows); } @@ -1114,6 +1132,8 @@ sub addSystem my $self = shift; my $valRows = _aref(shift); + _checkCols($valRows, 'system', qw(name export_id)); + foreach my $valRow (@$valRows) { if (!$valRow->{kernel}) { $valRow->{kernel} = 'vmlinuz'; @@ -1224,8 +1244,10 @@ sub setClientIDsOfSystem my $clientIDs = _aref(shift); my @uniqueClientIDs = _unique(@$clientIDs); - return $self->{'meta-db'} - ->setClientIDsOfSystem($systemID, \@uniqueClientIDs); + + return $self->{'meta-db'}->setClientIDsOfSystem( + $systemID, \@uniqueClientIDs + ); } =item C @@ -1259,6 +1281,7 @@ sub addClientIDsToSystem my @clientIDs = $self->{'meta-db'}->fetchClientIDsOfSystem($systemID); push @clientIDs, @$newClientIDs; + return $self->setClientIDsOfSystem($systemID, \@clientIDs); } @@ -1296,6 +1319,7 @@ sub removeClientIDsFromSystem my @clientIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchClientIDsOfSystem($systemID); + return $self->setClientIDsOfSystem($systemID, \@clientIDs); } @@ -1329,6 +1353,7 @@ sub setGroupIDsOfSystem my $groupIDs = _aref(shift); my @uniqueGroupIDs = _unique(@$groupIDs); + return $self->{'meta-db'}->setGroupIDsOfSystem($systemID, \@uniqueGroupIDs); } @@ -1363,6 +1388,7 @@ sub addGroupIDsToSystem my @groupIDs = $self->{'meta-db'}->fetchGroupIDsOfSystem($systemID); push @groupIDs, @$newGroupIDs; + return $self->setGroupIDsOfSystem($systemID, \@groupIDs); } @@ -1400,6 +1426,7 @@ sub removeGroupIDsFromSystem my @groupIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchGroupIDsOfSystem($systemID); + return $self->setGroupIDsOfSystem($systemID, \@groupIDs); } @@ -1426,6 +1453,8 @@ sub addClient my $self = shift; my $valRows = _aref(shift); + _checkCols($valRows, 'client', qw(name mac)); + foreach my $valRow (@$valRows) { if (!$valRow->{boot_type}) { $valRow->{boot_type} = 'pxe'; @@ -1527,8 +1556,10 @@ sub setSystemIDsOfClient my $systemIDs = _aref(shift); my @uniqueSystemIDs = _unique(@$systemIDs); - return $self->{'meta-db'} - ->setSystemIDsOfClient($clientID, \@uniqueSystemIDs); + + return $self->{'meta-db'}->setSystemIDsOfClient( + $clientID, \@uniqueSystemIDs + ); } =item C @@ -1562,6 +1593,7 @@ sub addSystemIDsToClient my @systemIDs = $self->{'meta-db'}->fetchSystemIDsOfClient($clientID); push @systemIDs, @$newSystemIDs; + return $self->setSystemIDsOfClient($clientID, \@systemIDs); } @@ -1599,6 +1631,7 @@ sub removeSystemIDsFromClient my @systemIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchSystemIDsOfClient($clientID); + return $self->setSystemIDsOfClient($clientID, \@systemIDs); } @@ -1631,6 +1664,7 @@ sub setGroupIDsOfClient my $groupIDs = _aref(shift); my @uniqueGroupIDs = _unique(@$groupIDs); + return $self->{'meta-db'}->setGroupIDsOfClient($clientID, \@uniqueGroupIDs); } @@ -1665,6 +1699,7 @@ sub addGroupIDsToClient my @groupIDs = $self->{'meta-db'}->fetchGroupIDsOfClient($clientID); push @groupIDs, @$newGroupIDs; + return $self->setGroupIDsOfClient($clientID, \@groupIDs); } @@ -1702,6 +1737,7 @@ sub removeGroupIDsFromClient my @groupIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchGroupIDsOfClient($clientID); + return $self->setGroupIDsOfClient($clientID, \@groupIDs); } @@ -1822,6 +1858,7 @@ sub setClientIDsOfGroup my $clientIDs = _aref(shift); my @uniqueClientIDs = _unique(@$clientIDs); + return $self->{'meta-db'}->setClientIDsOfGroup($groupID, \@uniqueClientIDs); } @@ -1855,6 +1892,7 @@ sub addClientIDsToGroup my @clientIDs = $self->{'meta-db'}->fetchClientIDsOfGroup($groupID); push @clientIDs, @$newClientIDs; + return $self->setClientIDsOfGroup($groupID, \@clientIDs); } @@ -1891,6 +1929,7 @@ sub removeClientIDsFromGroup my @clientIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchClientIDsOfGroup($groupID); + return $self->setClientIDsOfGroup($groupID, \@clientIDs); } @@ -1924,6 +1963,7 @@ sub setSystemIDsOfGroup my $systemIDs = _aref(shift); my @uniqueSystemIDs = _unique(@$systemIDs); + return $self->{'meta-db'}->setSystemIDsOfGroup($groupID, \@uniqueSystemIDs); } @@ -1957,6 +1997,7 @@ sub addSystemIDsToGroup my @systemIDs = $self->{'meta-db'}->fetchSystemIDsOfGroup($groupID); push @systemIDs, @$newSystemIDs; + return $self->setSystemIDsOfGroup($groupID, \@systemIDs); } @@ -1993,6 +2034,7 @@ sub removeSystemIDsFromGroup my @systemIDs = grep { !exists $toBeRemoved{$_} } $self->{'meta-db'}->fetchSystemIDsOfGroup($groupID); + return $self->setSystemIDsOfGroup($groupID, \@systemIDs); } @@ -2030,6 +2072,7 @@ sub emptyDatabase my @vendorOSIDs = map { $_->{id} } $self->fetchVendorOSByFilter(); $self->removeVendorOS(\@vendorOSIDs); + return 1; } @@ -2068,6 +2111,7 @@ sub mergeDefaultAttributesIntoSystem my $defaultClient = $self->fetchClientByFilter({name => '<<>>'}); pushAttributes($system, $defaultClient); + return 1; } @@ -2111,6 +2155,7 @@ sub mergeDefaultAndGroupAttributesIntoClient vlog(3, _tr('merging from default client...')); my $defaultClient = $self->fetchClientByFilter({name => '<<>>'}); mergeAttributes($client, $defaultClient); + return 1; } @@ -2354,6 +2399,7 @@ sub mergeAttributes $target->{$key} = $sourceVal; } } + return 1; } @@ -2391,6 +2437,7 @@ sub pushAttributes $target->{$key} = $sourceVal; } } + return 1; } @@ -2421,6 +2468,7 @@ sub externalIDForSystem my $name = $system->{name}; $name =~ tr[/][_]; + return $name; } @@ -2452,6 +2500,7 @@ sub externalIDForClient my $mac = lc($client->{mac}); # PXE seems to expect MACs being all lowercase $mac =~ tr[:][-]; + return "01-$mac"; } @@ -2482,6 +2531,7 @@ sub externalConfigNameForClient my $name = $client->{name}; $name =~ tr[/][_]; + return $name; } @@ -2507,9 +2557,11 @@ The external name of the given attribute. sub externalAttrName { my $attr = shift; + if ($attr =~ m[^attr_]) { return substr($attr, 5); } + return $attr; } @@ -2534,6 +2586,7 @@ The given variable as a placeholder string. sub generatePlaceholderFor { my $varName = shift; + return '@@@' . $varName . '@@@'; } @@ -2606,6 +2659,7 @@ sub _checkAndUpgradeDBSchemaIfNecessary } else { vlog(1, _tr('DB matches current schema version %s', $currVersion)); } + return 1; } @@ -2614,13 +2668,30 @@ sub _aref my $ref = shift; return [] unless defined $ref; $ref = [$ref] unless ref($ref) eq 'ARRAY'; + return $ref; } sub _unique { # return given array filtered to unique elements my %seenIDs; + return grep { !$seenIDs{$_}++; } @_; } +sub _checkCols +{ + my $valRows = shift; + my $table = shift; + my @colNames = @_; + + foreach my $valRow (@$valRows) { + foreach my $col (@colNames) { + die "need to set '$col' for $table!" if !$valRow->{$col}; + } + } + + return 1; +} + 1; diff --git a/config-db/OpenSLX/MetaDB/DBI.pm b/config-db/OpenSLX/MetaDB/DBI.pm index bb0a8fdc..380a1847 100644 --- a/config-db/OpenSLX/MetaDB/DBI.pm +++ b/config-db/OpenSLX/MetaDB/DBI.pm @@ -144,10 +144,11 @@ sub fetchExportByFilter $resultCols = '*' unless (defined $resultCols); my $sql = "SELECT $resultCols FROM export"; - 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); } @@ -194,10 +195,11 @@ sub fetchSystemByFilter $resultCols = '*' unless (defined $resultCols); my $sql = "SELECT $resultCols FROM system"; - 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); } @@ -256,10 +258,11 @@ sub fetchClientByFilter $resultCols = '*' unless (defined $resultCols); my $sql = "SELECT $resultCols FROM client"; - 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); } @@ -450,11 +453,13 @@ sub _doUpdate my %valData = %$valRow; delete $valData{'id'}; # filter column 'id' if present, as we don't want to update it - my $cols = join ', ', map { "$_ = " . $self->quote($valRow->{$_}) } + my @cols = map { "$_ = " . $self->quote($valRow->{$_}) } grep { $_ ne 'id' } # filter column 'id' if present, as we don't want # to update it! keys %$valRow; + return if !@cols; + my $cols = join ', ', @cols; my $sql = "UPDATE $table SET $cols"; if (defined $id) { $sql .= " WHERE id = " . $self->quote($id); diff --git a/config-db/t/10-vendor-os.t b/config-db/t/10-vendor-os.t index 9f7c9f11..b0754be7 100644 --- a/config-db/t/10-vendor-os.t +++ b/config-db/t/10-vendor-os.t @@ -12,6 +12,15 @@ is( my $vendorOS = $configDB->fetchVendorOSByFilter, undef, 'no vendor-OS yet (scalar context)' ); + +my $wrongVendorOS = { + 'comment' => 'test', +}; +ok( + ! eval { my $vendorOSID = $configDB->addVendorOS($wrongVendorOS); }, + 'trying to insert an unnamed vendor-OS should fail' +); + is( my @vendorOSes = $configDB->fetchVendorOSByFilter, 0, 'no vendor-OS yet (array context)' @@ -173,7 +182,13 @@ ok( 'changing unknown colum should fail' ); -ok($configDB->changeVendorOS(1, { id => 23 }), 'changing id should fail'); +ok(! $configDB->changeVendorOS(1, { id => 23 }), 'changing id should fail'); + +# now remove a vendor-OS and check if that worked +ok($configDB->removeVendorOS(3), 'removing vendor-OS 3 should be ok'); +is($configDB->fetchVendorOSByID(3, 'id'), undef, 'vendor-OS 3 should be gone'); +is($configDB->fetchVendorOSByID(1)->{id}, 1, 'vendor-OS 1 should still exist'); +is($configDB->fetchVendorOSByID(2)->{id}, 2, 'vendor-OS 2 should still exist'); $configDB->disconnect(); diff --git a/config-db/t/11-export.t b/config-db/t/11-export.t new file mode 100644 index 00000000..b122ffdd --- /dev/null +++ b/config-db/t/11-export.t @@ -0,0 +1,218 @@ +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 $export = $configDB->fetchExportByFilter, undef, + 'no export yet (scalar context)' +); + +foreach my $requiredCol (qw(name vendor_os_id type)) { + my $wrongExport = { + 'name' => 'name', + 'vendor_os_id' => 1, + 'type ' => 'nfs', + 'comment' => 'has column missing', + }; + delete $wrongExport->{$requiredCol}; + ok( + ! eval { my $exportID = $configDB->addExport($wrongExport); }, + "inserting an export without '$requiredCol' column should fail" + ); +} + +is( + my @exports = $configDB->fetchExportByFilter, 0, + 'no export yet (array context)' +); + +my $inExport1 = { + 'name' => 'exp-1', + 'type' => 'nfs', + 'vendor_os_id' => 1, + 'comment' => '', +}; +is( + my $export1ID = $configDB->addExport($inExport1), 1, + 'first export has ID 1' +); + +my $inExport2 = { + 'name' => 'exp-2.0', + 'type' => 'sqfs-nbd', + 'vendor_os_id' => 1, + 'comment' => undef, +}; +my $fullExport = { + 'name' => 'exp-nr-3', + 'type' => 'sqfs-nbd', + 'vendor_os_id' => 2, + 'comment' => 'nuff said', + 'server_ip' => '192.168.212.243', + 'port' => '65432', + 'uri' => 'sqfs-nbd://somehost/somepath?param=val&yes=1', +}; +ok( + my ($export2ID, $export3ID) = $configDB->addExport([ + $inExport2, $fullExport + ]), + 'add two more exports' +); +is($export2ID, 2, 'export 2 should have ID=2'); +is($export3ID, 3, 'export 3 should have ID=3'); + +# fetch export 3 by id and check all values +ok(my $export3 = $configDB->fetchExportByID(3), 'fetch export 3'); +is($export3->{id}, 3, 'export 3 - id'); +is($export3->{name}, 'exp-nr-3', 'export 3 - name'); +is($export3->{type}, 'sqfs-nbd', 'export 3 - type'); +is($export3->{vendor_os_id}, '2', 'export 3 - vendor_os_id'); +is($export3->{comment}, 'nuff said', 'export 3 - comment'); +is($export3->{server_ip}, '192.168.212.243', 'export 3 - server_ip'); +is($export3->{port}, '65432', 'export 3 - port'); +is( + $export3->{uri}, + 'sqfs-nbd://somehost/somepath?param=val&yes=1', + 'export 3 - uri' +); + +# fetch export 2 by a filter on id and check all values +ok( + my $export2 = $configDB->fetchExportByFilter({ id => 2 }), + 'fetch export 2 by filter on id' +); +is($export2->{id}, 2, 'export 2 - id'); +is($export2->{name}, 'exp-2.0', 'export 2 - name'); +is($export2->{type}, 'sqfs-nbd', 'export 2 - type'); +is($export2->{vendor_os_id}, '1', 'export 2 - vendor_os_id'); +is($export2->{comment}, undef, 'export 2 - comment'); + +# fetch export 1 by filter on name and check all values +ok( + my $export1 = $configDB->fetchExportByFilter({ name => 'exp-1' }), + 'fetch export 1 by filter on name' +); +is($export1->{id}, 1, 'export 1 - id'); +is($export1->{name}, 'exp-1', 'export 1 - name'); +is($export1->{vendor_os_id}, '1', 'export 1 - vendor_os_id'); +is($export1->{type}, 'nfs', 'export 1 - type'); +is($export1->{comment}, '', 'export 1 - comment'); +is($export1->{port}, undef, 'export 1 - port'); +is($export1->{server_ip}, undef, 'export 1 - server_ip'); +is($export1->{uri}, undef, 'export 1 - uri'); + +# fetch exports 3 & 1 by id +ok( + my @exports3And1 = $configDB->fetchExportByID([3, 1]), + 'fetch exports 3 & 1 by id' +); +is(@exports3And1, 2, 'should have got 2 exports'); +# now sort by ID and check if we have really got 3 and 1 +@exports3And1 = sort { $a->{id} cmp $b->{id} } @exports3And1; +is($exports3And1[0]->{id}, 1, 'first id should be 1'); +is($exports3And1[1]->{id}, 3, 'second id should be 3'); + +# fetching exports by id without giving any should yield undef +is( + $configDB->fetchExportByID(), undef, + 'fetch exports by id without giving any' +); + +# fetching exports by filter without giving any should yield all of them +ok( + @exports = $configDB->fetchExportByFilter(), + 'fetch exports by filter without giving any' +); +is(@exports, 3, 'should have got all three exports'); + +# fetch exports 1 & 2 by filter on vendor_os_id +ok( + my @exports1And2 = $configDB->fetchExportByFilter({ vendor_os_id => '1' }), + 'fetch exports 1 & 2 by filter on vendor_os_id' +); +is(@exports1And2, 2, 'should have got 2 exports'); +# now sort by ID and check if we have really got 1 and 2 +@exports1And2 = sort { $a->{id} cmp $b->{id} } @exports1And2; +is($exports1And2[0]->{id}, 1, 'first id should be 1'); +is($exports1And2[1]->{id}, 2, 'second id should be 2'); + +# try to fetch with multi-column filter +ok( + ($export2, $export3) + = $configDB->fetchExportByFilter({ vendor_os_id => '1', id => 2 }), + 'fetching export with vendor_os_id=1 and id=2 should work' +); +is($export2->{name}, 'exp-2.0', 'should have got exp-2.0'); +is($export3, undef, 'should not get exp-nr-3'); + +# try to fetch multiple occurrences of the same export, combined with +# some unknown IDs +ok( + my @exports1And3 = $configDB->fetchExportByID([ 1, 21, 4-1, 1, 0, 1, 1 ]), + 'fetch a complex set of exports by ID' +); +is(@exports1And3, 2, 'should have got 2 exports'); +# now sort by ID and check if we have really got 1 and 3 +@exports1And3 = sort { $a->{id} cmp $b->{id} } @exports1And3; +is($exports1And3[0]->{id}, 1, 'first id should be 1'); +is($exports1And3[1]->{id}, 3, 'second id should be 3'); + +# try to fetch a couple of non-existing exports by id +is( + $configDB->fetchExportByID(-1), undef, + 'export with id -1 should not exist' +); +is( + $configDB->fetchExportByID(0), undef, + 'export with id 0 should not exist' +); +is( + $configDB->fetchExportByID(1 << 31 + 1000), undef, + 'trying to fetch another unknown export' +); + +# try to fetch a couple of non-existing exports by filter +is( + $configDB->fetchExportByFilter({ id => 0 }), undef, + 'fetching export with id=0 by filter should fail' +); +is( + $configDB->fetchExportByFilter({ name => 'exp-1.x' }), undef, + 'fetching export with name="exp-1.x" should fail' +); +is( + $configDB->fetchExportByFilter({ vendor_os_id => '2', id => 1 }), undef, + 'fetching export with vendor_os_id=2 and id=1 should fail' +); + +# rename export 1 and then fetch it by its new name +ok($configDB->changeExport(1, { name => q{EXP-'1'} }), 'changing export 1'); +ok( + $export1 = $configDB->fetchExportByFilter({ name => q{EXP-'1'} }), + 'fetching renamed export 1' +); +is($export1->{id}, 1, 'really got export number 1'); +is($export1->{name}, q{EXP-'1'}, q{really got export named "EXP-'1'"}); + +# changing a non-existing column should fail +ok( + ! eval { $configDB->changeExport(1, { xname => "xx" }) }, + 'changing unknown colum should fail' +); + +ok(! $configDB->changeExport(1, { id => 23 }), 'changing id should fail'); + +# now remove an export and check if that worked +ok($configDB->removeExport(2), 'removing export 2 should be ok'); +is($configDB->fetchExportByID(2, 'id'), undef, 'export 2 should be gone'); +is($configDB->fetchExportByID(1)->{id}, 1, 'export 1 should still exist'); +is($configDB->fetchExportByID(3)->{id}, 3, 'export 3 should still exist'); + +$configDB->disconnect(); + diff --git a/config-db/t/12-system.t b/config-db/t/12-system.t new file mode 100644 index 00000000..ee908dba --- /dev/null +++ b/config-db/t/12-system.t @@ -0,0 +1,221 @@ +use Test::More qw(no_plan); + +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' => '', +}; +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!', + 'kernel_params' => 'debug=3 console=ttyS1', + 'hidden' => '1', + 'attr_ramfs_nicmods' => 'e1000 forcedeth', + 'attr_ramfs_fsmods' => 'squashfs', + 'attr_ramfs_miscmods' => 'tpm', + 'attr_ramfs_screen' => '1024x768', +}; +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->{kernel_params}, 'debug=3 console=ttyS1', 'system 3 - kernel_params'); +is($system3->{hidden}, '1', 'system 3 - hidden'); +is($system3->{attr_ramfs_nicmods}, 'e1000 forcedeth', 'system 3 - attr_ramfs_nicmods'); +is($system3->{attr_ramfs_fsmods}, 'squashfs', 'system 3 - attr_ramfs_fsmods'); +is($system3->{attr_ramfs_miscmods}, 'tpm', 'system 3 - attr_ramfs_miscmods'); +is($system3->{attr_ramfs_screen}, '1024x768', 'system 3 - attr_ramfs_screen'); + +# 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'); + +# 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($system1->{kernel_params}, undef, 'system 1 - kernel_params'); +is($system1->{hidden}, undef, 'system 1 - hidden'); +is($system1->{attr_ramfs_nicmods}, undef, 'system 1 - attr_ramfs_nicmods'); +is($system1->{attr_ramfs_fsmods}, undef, 'system 1 - attr_ramfs_fsmods'); +is($system1->{attr_ramfs_miscmods}, undef, 'system 1 - attr_ramfs_miscmods'); +is($system1->{attr_ramfs_screen}, undef, 'system 1 - attr_ramfs_screen'); + +# 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'); + +# 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 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(); + diff --git a/config-db/t/13-client.t b/config-db/t/13-client.t new file mode 100644 index 00000000..ff41e49b --- /dev/null +++ b/config-db/t/13-client.t @@ -0,0 +1,208 @@ +use Test::More qw(no_plan); + +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' => '', +}; +is( + my $client1ID = $configDB->addClient($inClient1), 1, + 'first client has ID 1' +); + +my $inClient2 = { + 'name' => 'cli-2.0', + 'unbootable' => 1, + 'mac' => '01:02:03:04:05:02', + 'comment' => undef, + 'boot_type' => 'etherboot', +}; +my $fullClient = { + 'name' => 'cli-nr-3', + 'mac' => '01:02:03:04:05:03', + 'comment' => 'nuff said', + 'kernel_params' => 'debug=3 console=ttyS1', + 'unbootable' => '0', + 'boot_type' => 'pxe', +}; +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->{boot_type}, 'pxe', 'client 3 - boot_type'); +is($client3->{kernel_params}, 'debug=3 console=ttyS1', 'client 3 - kernel_params'); +is($client3->{unbootable}, '0', 'client 3 - unbootable'); + +# 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->{unbootable}, '1', 'client 2 - unbootable'); +is($client2->{mac}, '01:02:03:04:05:02', 'client 2 - mac'); +is($client2->{comment}, undef, 'client 2 - comment'); +is($client2->{boot_type}, 'etherboot', 'client 2 - boot_type'); + +# 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->{unbootable}, undef, 'client 1 - unbootable'); +is($client1->{comment}, '', 'client 1 - comment'); +is($client1->{boot_type}, 'pxe', 'client 1 - boot_type'); +is($client1->{kernel_params}, undef, 'client 1 - kernel_params'); + +# 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'); + +# fetch clients 1 & 2 by filter on boot_type +ok( + my @clients1And3 = $configDB->fetchClientByFilter({ boot_type => 'pxe' }), + 'fetch clients 1 & 3 by filter on boot_type' +); +is(@clients1And3, 2, 'should have got 2 clients'); +# now sort by ID and check if we have really got 1 and 3 +@clients1And2 = 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'); + +# try to fetch with multi-column filter +ok( + ($client1, $client3) + = $configDB->fetchClientByFilter({ boot_type => 'pxe', id => 1 }), + 'fetching client with boot_type=pxe and id=1 should work' +); +is($client1->{name}, 'cli-1', 'should have got cli-1'); +is($client3, undef, 'should not get cli-nr-3'); + +# 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'); + +# 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 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(); + diff --git a/config-db/t/run-all-tests.pl b/config-db/t/run-all-tests.pl index 6cebdac1..c082052d 100755 --- a/config-db/t/run-all-tests.pl +++ b/config-db/t/run-all-tests.pl @@ -10,6 +10,8 @@ use FindBin; use lib "$FindBin::RealBin/../"; use lib "$FindBin::RealBin/../../lib"; +chdir "$FindBin::RealBin" or die "unable to chdir to $FindBin::RealBin! ($!)\n"; + use OpenSLX::Basics; use OpenSLX::MetaDB::SQLite; @@ -21,6 +23,8 @@ $cmdlineConfig{'db-type'} = $ENV{SLX_DB_TYPE} = 'SQLite'; openslxInit(); +$Test::Harness::Verbose = 1 if $openslxConfig{'verbose-level'}; + # remove the test-db if it already exists my $metaDB = OpenSLX::MetaDB::SQLite->new(); if ($metaDB->databaseExists()) { -- cgit v1.2.3-55-g7522