[OSSTEST,v4,1/3] host: introduce helpers to modify hostflags
diff mbox series

Message ID 20200311160021.6075-1-roger.pau@citrix.com
State New
Headers show
Series
  • [OSSTEST,v4,1/3] host: introduce helpers to modify hostflags
Related show

Commit Message

Roger Pau Monné March 11, 2020, 4 p.m. UTC
Add a generic function to perform database changes related to a host
and use it to implement set_property and and {set/remove}_flag.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
Changes since v3:
 - Introduce modify_flag instead of {set/remove}_flag.
 - Introduce a generic modify_host helper.
 - Split from patch 1.
---
 Osstest/HostDB/Executive.pm | 33 ++++++++++++++++++++++++++++-----
 Osstest/HostDB/Static.pm    |  7 +++++++
 Osstest/TestSupport.pm      | 21 ++++++++++++++++++++-
 3 files changed, 55 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/Osstest/HostDB/Executive.pm b/Osstest/HostDB/Executive.pm
index 7ffca6c4..a6dc4462 100644
--- a/Osstest/HostDB/Executive.pm
+++ b/Osstest/HostDB/Executive.pm
@@ -51,6 +51,16 @@  END
     }
 }
 
+sub modify_host ($$$) {
+    my ($hd, $ho, $query) = @_;
+    my $blessing = intended_blessing();
+
+    die "Attempting to modify host with intended blessing $blessing != real"
+        if $blessing ne "real";
+
+    db_retry($dbh_tests, [qw(resources)], $query);
+}
+
 sub set_property($$$$) {
     my ($hd, $ho, $prop, $val) = @_;
     my $rmq = $dbh_tests->prepare(<<END);
@@ -61,12 +71,8 @@  END
         INSERT INTO resource_properties (restype,resname,name,val)
                VALUES ('host', ?,?,?)
 END
-    my $blessing = intended_blessing();
-
-    die "Attempting to modify host props with intended blessing $blessing != real"
-        if $blessing ne "real";
 
-    db_retry($dbh_tests, [qw(resources)], sub {
+    modify_host($hd, $ho, sub {
         $rmq->execute($ho->{Name}, $prop);
         if (length $val) {
             $addq->execute($ho->{Name}, $prop, $val);
@@ -90,6 +96,23 @@  END
     return $flags;
 }
 
+sub modify_flag ($$$$) {
+    my ($hd, $ho, $flag, $set) = @_;
+    my $rmq = $dbh_tests->prepare(<<END);
+        DELETE FROM hostflags WHERE hostname=? AND hostflag=?
+END
+    my $addq = $dbh_tests->prepare(<<END);
+        INSERT INTO hostflags (hostname,hostflag) VALUES (?,?)
+END
+
+    modify_host($hd, $ho, sub {
+        $rmq->execute($ho->{Name}, $flag);
+        if ($set) {
+            $addq->execute($ho->{Name}, $flag);
+        }
+    });
+}
+
 sub get_arch_platforms ($$$) {
     my ($hd, $blessing, $arch, $suite) = @_;
 
diff --git a/Osstest/HostDB/Static.pm b/Osstest/HostDB/Static.pm
index 0c6be3ee..d0669fb2 100644
--- a/Osstest/HostDB/Static.pm
+++ b/Osstest/HostDB/Static.pm
@@ -72,6 +72,13 @@  sub get_flags ($$) { #method
     return $flags;
 }
 
+sub modify_flag ($$$$) {
+    my ($hd, $ho, $flag, $set) = @_;
+
+    die
+    "Cannot modify flags in standalone mode for $ho->{Name} $flag set: $set\n";
+}
+
 sub get_arch_platforms ($$$) {
     my ($hd, $blessing, $arch, $suite) = @_;
 
diff --git a/Osstest/TestSupport.pm b/Osstest/TestSupport.pm
index f49ed529..b80e89bc 100644
--- a/Osstest/TestSupport.pm
+++ b/Osstest/TestSupport.pm
@@ -85,8 +85,9 @@  BEGIN {
                       hostnamepath hostnamepath_list set_runtime_hostflag
                       power_state power_cycle power_reboot_attempts
                       serial_fetch_logs set_host_property
+                      set_host_flag remove_host_flag
                       propname_massage propname_check
-                      hostprop_putative_record
+                      hostprop_putative_record hostflag_putative_record
          
                       get_stashed open_unique_stashfile compress_stashed
                       dir_identify_vcs
@@ -1411,6 +1412,24 @@  sub hostprop_putative_record ($$$) {
     store_runvar("hostprop/$ho->{Ident}/$prop", $val);
 }
 
+sub set_host_flag ($$) {
+    my ($ho,$flag) = @_;
+
+    $mhostdb->modify_flag($ho, $flag, 1);
+}
+
+sub remove_host_flag ($$) {
+    my ($ho,$flag) = @_;
+
+    $mhostdb->modify_flag($ho, $flag, 0);
+}
+
+sub hostflag_putative_record ($$$) {
+    my ($ho, $prop, $set) = @_;
+
+    store_runvar("hostflag/$ho->{Ident}/$prop", !!$set);
+}
+
 sub get_target_property ($$;$);
 sub get_target_property ($$;$) {
     my ($ho, $prop, $defval) = @_;