]> xenbits.xensource.com Git - people/royger/osstest.git/commitdiff
ResourceCondition: Break out PropCompareBase
authorIan Jackson <ian.jackson@eu.citrix.com>
Mon, 11 Jun 2018 16:45:08 +0000 (17:45 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Tue, 12 Jun 2018 11:17:42 +0000 (12:17 +0100)
Make a base class so we can easily invent other kinds of resource
property condition.  The child is responsible only for stringification
and the actual comparison.

These classes does not need Exporter, so drop all that.  Use `use
parent' to import the base class.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Osstest/ResourceCondition/PropCompareBase.pm [new file with mode: 0644]
Osstest/ResourceCondition/PropMinVer.pm

diff --git a/Osstest/ResourceCondition/PropCompareBase.pm b/Osstest/ResourceCondition/PropCompareBase.pm
new file mode 100644 (file)
index 0000000..5306bb8
--- /dev/null
@@ -0,0 +1,66 @@
+# This is part of "osstest", an automated testing framework for Xen.
+# Copyright (C) 2015 Citrix Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package Osstest::ResourceCondition::PropCompareBase;
+
+use strict;
+use warnings;
+
+use Osstest;
+use Osstest::TestSupport;
+
+BEGIN {
+    use Exporter ();
+    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
+    $VERSION     = 1.00;
+    @ISA         = qw(Exporter);
+    @EXPORT      = qw();
+    %EXPORT_TAGS = ( );
+
+    @EXPORT_OK   = qw();
+}
+
+sub new {
+    my ($class, $name, $prop, $val) = @_;
+
+    die "propname: $prop?" unless propname_check($prop);
+
+    return bless {
+       Prop => $prop,
+       Val => $val
+    }, $class;
+}
+
+sub check {
+    my ($pc, $restype, $resname) = @_;
+
+    # Using _cached avoids needing to worry about $dbh_tests being
+    # closed/reopened between invocations
+    my $hpropq = $dbh_tests->prepare_cached(<<END);
+       SELECT val FROM resource_properties
+       WHERE restype = ? AND resname = ? AND name = ?
+END
+    $hpropq->execute($restype, $resname, $pc->{Prop});
+
+    my $row= $hpropq->fetchrow_arrayref();
+    $hpropq->finish();
+
+    my $propval = $row->[0];
+
+    return $pc->_check($propval);
+}
+
+1;
index 155aefb43e0b92065f8c2c0cabffe41d85a93800..1880d7ce0281089b49662a495ad29df4786fb4e6 100644 (file)
@@ -19,6 +19,8 @@ package Osstest::ResourceCondition::PropMinVer;
 use strict;
 use warnings;
 
+use parent 'Osstest::ResourceCondition::PropCompareBase';
+
 use Osstest;
 use Osstest::TestSupport;
 
@@ -26,52 +28,19 @@ use Sort::Versions;
 
 use overload '""' => 'stringify';
 
-BEGIN {
-    use Exporter ();
-    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
-    $VERSION     = 1.00;
-    @ISA         = qw(Exporter);
-    @EXPORT      = qw();
-    %EXPORT_TAGS = ( );
-
-    @EXPORT_OK   = qw();
-}
-
-sub new {
-    my ($class, $name, $prop, $val) = @_;
-
-    die "propname: $prop?" unless propname_check($prop);
-
-    return bless {
-       Prop => $prop,
-       MinVal => $val
-    }, $class;
-}
-
 sub stringify {
     my ($pmv) = @_;
-    return "$pmv->{MinVal} >=(v) property $pmv->{Prop}";
+    return "$pmv->{Val} >=(v) property $pmv->{Prop}";
 }
 
-sub check {
-    my ($pmv, $restype, $resname) = @_;
-
-    # Using _cached avoids needing to worry about $dbh_tests being
-    # closed/reopened between invocations
-    my $hpropq = $dbh_tests->prepare_cached(<<END);
-       SELECT val FROM resource_properties
-       WHERE restype = ? AND resname = ? AND name = ?
-END
-    $hpropq->execute($restype, $resname, $pmv->{Prop});
-
-    my $row= $hpropq->fetchrow_arrayref();
-    $hpropq->finish();
+sub _check {
+    my ($pmv, $propval) = @_;
 
-    return 1 unless $row; # No prop == no restriction.
+    return 1 unless defined $propval; # No prop == no restriction.
 
     # If the maximum minimum is >= to the resource's minimum then the
     # resource meets the requirement.
-    return versioncmp($pmv->{MinVal}, $row->[0]) >= 0;
+    return versioncmp($pmv->{Val}, $propval) >= 0;
 }
 
 1;