Changeset: 899c45d630bb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=899c45d630bb
Modified Files:
        tools/merovingian/client/Tests/monetdb_snapshot.py
        tools/merovingian/client/Tests/monetdbd.py
Branch: mtest
Log Message:

converted tools/merovingian/client/Tests


diffs (truncated from 342 to 300 lines):

diff --git a/tools/merovingian/client/Tests/monetdb_snapshot.py 
b/tools/merovingian/client/Tests/monetdb_snapshot.py
--- a/tools/merovingian/client/Tests/monetdb_snapshot.py
+++ b/tools/merovingian/client/Tests/monetdb_snapshot.py
@@ -8,90 +8,102 @@ import subprocess
 import sys
 import time
 
-from monetdbd import MonetDBD
+from monetdbd import MonetDBD, Runner
 
 # MonetDBD.VERBOSE = True
 
 gdk_farmdir = os.environ.get('TSTTRGDIR') or '/tmp/'
 farmdir = os.path.join(gdk_farmdir, 'monetdbd-test')
 
-def header(*args, **opts):
-    if 'file' in opts:
-        del opts['file']
-    print(*['--', *args], flush=True, **opts)
-    print(file=sys.stderr, flush=True, *['-  ', *args], **opts)
+with Runner(False) as run:
+
+    def header(text):
+        run.print()
+        run.print('-- ', text)
 
-header('CREATE FARM')
+    def note(*args, **kwargs):
+        run.print('NOTE ', sep='', end='')
+        run.print(*args, **kwargs)
+
+    header('CREATE FARM')
+    with MonetDBD(run, farmdir, set_snapdir=False) as m:
 
-# test that .snapshotdir is not set by default
-with MonetDBD(farmdir, set_snapdir=False) as m:
+        header('CHECK SNAPDIR NOT SET')
+        output = m.run_monetdbd('get', 'snapshotdir', output=True)
+        assert '<unknown>' in output
+        note("""'<unknown>' in output""")
 
-    header('CHECK SNAPDIR NOT SET')
-    output = m.run_monetdbd('get', 'snapshotdir', output=True)
-    assert('<unknown>' in output)
+        header('TRY TO CREATE')
+        try:
+            m.run_monetdb('snapshot', 'create', 'foo1')
+            note("should have failed")
+            assert False
+        except subprocess.CalledProcessError as e:
+            note("failed as expected")
 
-    header('CREATE')
-    try:
+        header('SET SNAPDIR')
+        m.run_monetdbd('set', f'snapshotdir={m.snapdir}')
+
+        header('TRY TO CREATE AGAIN')
         m.run_monetdb('snapshot', 'create', 'foo1')
-    except subprocess.CalledProcessError as e:
-        pass
-
-    header('SET SNAPDIR')
-    m.run_monetdbd('set', f'snapshotdir={m.qsnapdir}')
-
-    header('CREATE')
-    m.run_monetdb('snapshot', 'create', 'foo1')
 
-    header('LIST')
-    out = m.run_monetdb('snapshot', 'list', output=True)
-    lines = out.rstrip().split('\n')
-    assert(len(lines) == 2 and lines[1].startswith('foo1@1 '))
+        header('LIST')
+        out = m.run_monetdb('snapshot', 'list', output=True)
+        lines = out.rstrip().split('\n')
+        assert len(lines) == 2 and lines[1].startswith('foo1@1 ')
+        note("""len(lines) == 2 and lines[1].startswith('foo1@1 ')""")
 
-    header('RESTORE')
-    m.run_monetdb('snapshot', 'restore', 'foo1@1', 'foo_restored', output=True)
-    out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='foo_restored')
-    assert(out.strip() == 'foo1')
+        header('RESTORE')
+        m.run_monetdb('snapshot', 'restore', 'foo1@1', 'foo_restored', 
output=True)
+        out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='foo_restored')
+        assert out.strip() == 'foo1'
+        note("""output == 'foo1'""")
 
-    header('DESTROY')
-    m.run_monetdb('snapshot', 'destroy', '-f', 'foo1@1', output=True)
-    out = m.run_monetdb('snapshot', 'list', output=True)
-    lines = out.rstrip().split('\n')
-    assert(len(lines) == 1)
-    m.run_monetdb('destroy', '-f', 'foo_restored')
+        header('DESTROY')
+        m.run_monetdb('snapshot', 'destroy', '-f', 'foo1@1', output=True)
+        out = m.run_monetdb('snapshot', 'list', output=True)
+        lines = out.rstrip().split('\n')
+        assert len(lines) == 1
+        note("""len(lines) == 1""")
+        m.run_monetdb('destroy', '-f', 'foo_restored')
 
-    header('CHECK')
-    m.run_monetdb('status', output=True)
-    lines = out.rstrip().split('\n')
-    assert(len(lines) == 1)
+        header('CHECK')
+        m.run_monetdb('status', output=True)
+        lines = out.rstrip().split('\n')
+        assert len(lines) == 1
+        note("""len(lines) == 1""")
 
-    header('SNAPSHOT MULTI')
-    m.run_monetdb('snapshot', 'create', 'foo*')
-    out = m.run_monetdb('snapshot', 'list', output=True)
-    lines = out.rstrip().split('\n')
-    first_words = [ line.split(' ')[0] for line in lines ]
-    print(first_words)
-    assert first_words == ['name', 'foo1@1', 'foo2@1']
-    #
-    time.sleep(1.5) # ensure different snapshot names
-    m.run_monetdb('snapshot', 'create', '*')
-    out = m.run_monetdb('snapshot', 'list', output=True)
-    lines = out.rstrip().split('\n')
-    first_words = [ line.strip().split(' ')[0] for line in lines ]
-    print(first_words)
-    assert first_words == ['name', 'bar@1', 'foo1@1', '@2', 'foo2@1', '@2']
+        header('SNAPSHOT MULTI')
+        m.run_monetdb('snapshot', 'create', 'foo*')
+        out = m.run_monetdb('snapshot', 'list', output=True)
+        lines = out.rstrip().split('\n')
+        first_words = [ line.split(' ')[0] for line in lines ]
+        run.print(first_words)
+        assert first_words == ['name', 'foo1@1', 'foo2@1']
+        #
+        time.sleep(1.5) # ensure different snapshot names
+        m.run_monetdb('snapshot', 'create', '*')
+        out = m.run_monetdb('snapshot', 'list', output=True)
+        lines = out.rstrip().split('\n')
+        first_words = [ line.strip().split(' ')[0] for line in lines ]
+        run.print(first_words)
+        assert first_words == ['name', 'bar@1', 'foo1@1', '@2', 'foo2@1', '@2']
 
-    header('RESTORE OVER EXISTING')
-    m.run_monetdb('snapshot', 'restore', '-f', 'foo2@1', 'bar', output=True)
-    out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='bar')
-    assert out.strip() == 'foo2'
+        header('RESTORE OVER EXISTING')
+        m.run_monetdb('snapshot', 'restore', '-f', 'foo2@1', 'bar', 
output=True)
+        out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='bar')
+        assert out.strip() == 'foo2'
+        note("""output == 'foo2'""")
 
-    header('CUSTOM FILENAME')
-    custom_name = os.path.join(m.snapdir, 'snap.tar')
-    qcustom_name = pipes.quote(custom_name)
-    m.run_monetdb('snapshot', 'create', '-t', qcustom_name, 'foo1')
-    assert os.path.exists(custom_name)
-    m.run_monetdb('snapshot', 'restore', qcustom_name, 'foo99', output=True)
-    out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='foo99')
-    assert out.strip() == 'foo1'
+        header('CUSTOM FILENAME')
+        custom_name = os.path.join(m.snapdir, 'snap.tar')
+        qcustom_name = pipes.quote(custom_name)
+        m.run_monetdb('snapshot', 'create', '-t', qcustom_name, 'foo1')
+        assert os.path.exists(custom_name)
+        note("""os.path.exists(custom_name)""")
+        m.run_monetdb('snapshot', 'restore', qcustom_name, 'foo99', 
output=True)
+        out = m.run_mclient('-s', 'select * from t', '-fcsv', output=True, 
db='foo99')
+        assert out.strip() == 'foo1'
+        note("""output == 'foo1'""")
 
-    header('DONE')
+        header('DONE')
diff --git a/tools/merovingian/client/Tests/monetdbd.py 
b/tools/merovingian/client/Tests/monetdbd.py
--- a/tools/merovingian/client/Tests/monetdbd.py
+++ b/tools/merovingian/client/Tests/monetdbd.py
@@ -2,10 +2,12 @@
 
 import locale
 import os
-import pipes
+import shlex
 import shutil
 import socket
 import subprocess
+import sys
+import tempfile
 import time
 
 def pickport():
@@ -16,24 +18,81 @@ def pickport():
         s.close()
         return port
 
+class Runner:
+    def __init__(self, verbose=True):
+        self.buffering = not verbose
+        if self.buffering:
+            self.output = tempfile.TemporaryFile('w+')
+        else:
+            self.output = sys.stdout
+
+    def print(self, *args, **kwargs):
+        assert 'file' not in kwargs
+        print(*args, **kwargs, file=self.output, flush=True)
+
+    def quote(self, text):
+        def is_valid(attempt):
+            try:
+                decoded = shlex.split(attempt)[0]
+                return decoded == text
+            except ValueError:
+                return False
+
+        attempt = text
+        if is_valid(attempt):
+            return attempt
+
+        attempt = "'" + text + "'"
+        if is_valid(attempt):
+            return attempt
+
+        if '$' not in text:
+            attempt = '"' + text + '"'
+            if is_valid(attempt):
+                return attempt
+
+        attempt = shlex.quote(text)
+        assert is_valid(attempt)
+        return attempt
+
+    def run_command(self, cmd, output=False, timeout=10):
+        self.print(f"RUN  {' '.join(self.quote(a) for a in cmd)}")
+        if output:
+            out = subprocess.check_output(cmd, timeout=timeout, 
stderr=self.output)
+            self.output.flush()
+            out = str(out, locale.getlocale()[1])
+            return out
+        else:
+            subprocess.check_call(cmd, timeout=timeout, stdout=self.output, 
stderr=self.output)
+            self.output.flush()
+            return None
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        if exc_value and self.buffering:
+            self.output.seek(0)
+            sys.stderr.write(self.output.read())
+
+        return False # do not suppress the exception
+
 class MonetDBD:
     VERBOSE = False
 
-    def __init__(self, farmdir, keep=False, set_snapdir=True):
+    def __init__(self, runner, farmdir, keep=False, set_snapdir=True):
+        self.runner = runner
         self.farmdir = farmdir
         self.keep = keep
         # It's generally a bad idea to put the snapshot dir inside the database
         # farm but this test depends on it, see .prepare_dir
         self.snapdir = os.path.join(farmdir, 'MTESTSNAPS')
-        self.qfarmdir = pipes.quote(farmdir)
-        self.qsnapdir = pipes.quote(self.snapdir)
         self.port = pickport()
         self.proc = None # filled in by .start_monetdb() below
 
         self.prepare_dir(set_snapdir)
         self.start_monetdbd()
         # the above generated all sorts of output, give reader a (line) break
-        print()
 
     def prepare_dir(self, set_snapdir):
         try:
@@ -45,10 +104,9 @@ class MonetDBD:
         os.mkdir(self.snapdir)
 
         self.run_monetdbd('create')
-        self.run_monetdbd('set', 'listenaddr=0.0.0.0')
         self.run_monetdbd('set', f'port={self.port}')
         if set_snapdir:
-            self.run_monetdbd('set', f'snapshotdir={self.qsnapdir}')
+            self.run_monetdbd('set', f'snapshotdir={self.snapdir}')
 
     def remove_dir(self):
         if not os.listdir(self.farmdir) or os.path.isdir(self.snapdir):
@@ -57,33 +115,25 @@ class MonetDBD:
         else:
             raise Exception(f"Directory {self.farmdir} is nonempty but does 
not contain our marker file") from None
 
-    def run_command(self, cmd, output=False, timeout=10):
-        if self.VERBOSE:
-            print(f"- running {' '.join(pipes.quote(a) for a in cmd)}")
-        if output:
-            out = subprocess.check_output(cmd, timeout=timeout)
-            out = str(out, locale.getlocale()[1])
-            return out
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to