]> xenbits.xensource.com Git - people/liuw/osstest.git/commitdiff
mg-schema-test-database: Safety catch in JobDB database open
authorIan Jackson <ian.jackson@eu.citrix.com>
Mon, 7 Dec 2015 16:33:57 +0000 (16:33 +0000)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Tue, 8 Dec 2015 14:51:02 +0000 (14:51 +0000)
When we open the `osstest' database, see whether this is a parent DB
(main DB) from which a test DB has been spawned by this user.

If it has, bomb out, unless the user has specified a suitable regexp
matching the DB name in the env var
  OSSTEST_DB_USEREAL_IGNORETEST

This means that when a test database is in play, the user who created
it cannot accidentally operate on the real DB.

The safety catch does not affect Tcl programs, which get the DB config
directly, but in general that just means sg-execute-flight and
sg-run-job which already have a fair amount of safety catch because
they demand flight numbers.

mg-schema-test-database hits this feature over the head.  We assume
that the caller of mg-schema-test-database knows what they are doing;
particularly, that if they create nested test DBs (!), they do not
need the assitance of this feature to stop themselves operating
mg-schema-test-database incorrectly.  Anyone who creates nested test
DBs will hopefully recognise the potential for confusion!

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
---
v3: Fix unclarity in a comment.

Osstest/JobDB/Executive.pm
mg-schema-test-database

index 2572f5fc4ed82e9cdc7049279862f9ef9a783b89..69cb2763daa9b0b5c827f586870fa52dcf69e3ea 100644 (file)
@@ -51,8 +51,39 @@ sub current_flight ($) { #method
     return $ENV{'OSSTEST_FLIGHT'};
 }
 
+sub _check_testdbs ($) {
+    my ($dbh) = @_;
+
+    my $re = $ENV{OSSTEST_DB_USEREAL_IGNORETEST} // '';
+    return if $re eq '.*'; # needed by mg-schema-test-database during setup
+
+    # mg-schema-test-database creates a task
+    #   xdbref/DBNAME with username ${Username}@NODENAME
+    my $sth = $dbh->prepare(<<END);
+        SELECT refkey AS dbname,
+               username, comment
+            FROM tasks
+           WHERE type = 'xdbref'
+             AND live
+             AND username LIKE (? || '@%')
+END
+    $sth->execute($c{Username});
+    my $allok = 1;
+    while (my $row = $sth->fetchrow_hashref()) {
+       next if $row->{dbname} =~ m/^$re$/o;
+       $allok = 0;
+       print STDERR <<END;
+Test DB exists: $row->{dbname} ($row->{username}, "$row->{comment}")
+END
+    }
+    die "Test dbs exist.  Set OSSTEST_DB_USEREAL_IGNORETEST to regexp ?\n"
+       unless $allok;
+}
+
 sub open ($) { #method
-    return opendb('osstestdb');
+    my $dbh = opendb('osstestdb');
+    _check_testdbs($dbh);
+    return $dbh;
 }
 
 sub dbfl_check ($$) { #method
index b41a99bba2663c14fdadb25f58826e30906b5a78..90a361ba53b012ed005761c63f63c183832aeb36 100755 (executable)
@@ -47,6 +47,8 @@ cmd="$1"; shift
 
 localconfig=local-config.test-database
 
+export OSSTEST_DB_USEREAL_IGNORETEST='.*'
+
 maindbname=$(perl -we '
        use Osstest;
        use Osstest::Executive;