2013-01-02 00:00:30 -05:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2012-03-19 21:18:30 -04:00
|
|
|
|
import nilmdb
|
2012-12-31 15:52:28 -05:00
|
|
|
|
from nilmdb.utils.printf import *
|
2012-03-22 17:00:01 -04:00
|
|
|
|
import nilmdb.cmdline
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
|
|
|
|
from nose.tools import *
|
|
|
|
|
from nose.tools import assert_raises
|
2012-04-06 14:25:09 -04:00
|
|
|
|
import itertools
|
2012-04-09 15:35:14 -04:00
|
|
|
|
import datetime_tz
|
2012-03-19 21:18:30 -04:00
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import sys
|
|
|
|
|
import threading
|
|
|
|
|
import urllib2
|
|
|
|
|
from urllib2 import urlopen, HTTPError
|
|
|
|
|
import Queue
|
2013-01-03 17:00:23 -05:00
|
|
|
|
import StringIO
|
2012-03-19 21:18:30 -04:00
|
|
|
|
import shlex
|
|
|
|
|
|
2012-03-29 17:43:05 -04:00
|
|
|
|
from test_helpers import *
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-03-29 17:43:05 -04:00
|
|
|
|
testdb = "tests/cmdline-testdb"
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-05-25 12:44:24 -04:00
|
|
|
|
def server_start(max_results = None):
|
2012-04-04 14:54:24 -04:00
|
|
|
|
global test_server, test_db
|
|
|
|
|
# Start web app on a custom port
|
2012-05-25 12:44:24 -04:00
|
|
|
|
test_db = nilmdb.NilmDB(testdb, sync = False, max_results = max_results)
|
2012-04-04 14:54:24 -04:00
|
|
|
|
test_server = nilmdb.Server(test_db, host = "127.0.0.1",
|
|
|
|
|
port = 12380, stoppable = False,
|
|
|
|
|
fast_shutdown = True,
|
|
|
|
|
force_traceback = False)
|
|
|
|
|
test_server.start(blocking = False)
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-04-24 17:59:33 -04:00
|
|
|
|
def server_stop():
|
2012-04-04 14:54:24 -04:00
|
|
|
|
global test_server, test_db
|
|
|
|
|
# Close web app
|
|
|
|
|
test_server.stop()
|
|
|
|
|
test_db.close()
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-04-24 17:59:33 -04:00
|
|
|
|
def setup_module():
|
|
|
|
|
global test_server, test_db
|
|
|
|
|
# Clear out DB
|
|
|
|
|
recursive_unlink(testdb)
|
|
|
|
|
server_start()
|
|
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
|
server_stop()
|
|
|
|
|
|
2013-01-03 17:00:23 -05:00
|
|
|
|
# Add an encoding property to StringIO so Python will convert Unicode
|
|
|
|
|
# properly when writing or reading.
|
|
|
|
|
class UTF8StringIO(StringIO.StringIO):
|
|
|
|
|
encoding = 'utf-8'
|
|
|
|
|
|
2012-04-04 14:54:24 -04:00
|
|
|
|
class TestCmdline(object):
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
def run(self, arg_string, infile=None, outfile=None):
|
2012-03-20 10:21:55 -04:00
|
|
|
|
"""Run a cmdline client with the specified argument string,
|
|
|
|
|
passing the given input. Returns a tuple with the output and
|
|
|
|
|
exit code"""
|
2013-01-03 16:58:26 -05:00
|
|
|
|
# printf("TZ=UTC ./nilmtool.py %s\n", arg_string)
|
2012-03-19 21:18:30 -04:00
|
|
|
|
class stdio_wrapper:
|
|
|
|
|
def __init__(self, stdin, stdout, stderr):
|
|
|
|
|
self.io = (stdin, stdout, stderr)
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
self.saved = ( sys.stdin, sys.stdout, sys.stderr )
|
|
|
|
|
( sys.stdin, sys.stdout, sys.stderr ) = self.io
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
|
( sys.stdin, sys.stdout, sys.stderr ) = self.saved
|
2012-04-06 14:25:09 -04:00
|
|
|
|
# Empty input if none provided
|
|
|
|
|
if infile is None:
|
2013-01-03 17:00:23 -05:00
|
|
|
|
infile = UTF8StringIO("")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
# Capture stderr
|
2013-01-03 17:00:23 -05:00
|
|
|
|
errfile = UTF8StringIO()
|
2012-04-06 14:25:09 -04:00
|
|
|
|
if outfile is None:
|
|
|
|
|
# If no output file, capture stdout with stderr
|
|
|
|
|
outfile = errfile
|
2012-04-04 18:34:01 -04:00
|
|
|
|
with stdio_wrapper(infile, outfile, errfile) as s:
|
2012-03-19 21:18:30 -04:00
|
|
|
|
try:
|
2013-01-03 17:00:23 -05:00
|
|
|
|
# shlex doesn't support Unicode very well. Encode the
|
|
|
|
|
# string as UTF-8 explicitly before splitting.
|
|
|
|
|
args = shlex.split(arg_string.encode('utf-8'))
|
|
|
|
|
nilmdb.cmdline.Cmdline(args).run()
|
2012-03-19 21:18:30 -04:00
|
|
|
|
sys.exit(0)
|
|
|
|
|
except SystemExit as e:
|
|
|
|
|
exitcode = e.code
|
2012-04-06 14:25:09 -04:00
|
|
|
|
captured = outfile.getvalue()
|
|
|
|
|
self.captured = captured
|
2012-04-04 14:54:24 -04:00
|
|
|
|
self.exitcode = exitcode
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
def ok(self, arg_string, infile = None):
|
|
|
|
|
self.run(arg_string, infile)
|
2012-04-04 14:54:24 -04:00
|
|
|
|
if self.exitcode != 0:
|
|
|
|
|
self.dump()
|
|
|
|
|
eq_(self.exitcode, 0)
|
|
|
|
|
|
2012-05-30 14:24:36 -04:00
|
|
|
|
def fail(self, arg_string, infile = None, exitcode = None):
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.run(arg_string, infile)
|
2012-05-30 14:24:36 -04:00
|
|
|
|
if exitcode is not None and self.exitcode != exitcode:
|
|
|
|
|
self.dump()
|
|
|
|
|
eq_(self.exitcode, exitcode)
|
2012-04-04 14:54:24 -04:00
|
|
|
|
if self.exitcode == 0:
|
|
|
|
|
self.dump()
|
|
|
|
|
ne_(self.exitcode, 0)
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
def contain(self, checkstring):
|
|
|
|
|
in_(checkstring, self.captured)
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
def match(self, checkstring):
|
2012-04-06 14:25:09 -04:00
|
|
|
|
eq_(checkstring, self.captured)
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
2012-05-30 14:24:36 -04:00
|
|
|
|
def matchfile(self, file):
|
2012-06-25 14:52:50 -04:00
|
|
|
|
# Captured data should match file contents exactly
|
2012-05-30 14:24:36 -04:00
|
|
|
|
with open(file) as f:
|
2012-08-06 17:46:09 -04:00
|
|
|
|
contents = f.read()
|
|
|
|
|
if contents != self.captured:
|
|
|
|
|
#print contents[1:1000] + "\n"
|
|
|
|
|
#print self.captured[1:1000] + "\n"
|
2012-05-30 14:24:36 -04:00
|
|
|
|
raise AssertionError("captured data doesn't match " + file)
|
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def matchfilecount(self, file):
|
|
|
|
|
# Last line of captured data should match the number of
|
|
|
|
|
# non-commented lines in file
|
|
|
|
|
count = 0
|
|
|
|
|
with open(file) as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
if line[0] != '#':
|
|
|
|
|
count += 1
|
|
|
|
|
eq_(self.captured.splitlines()[-1], sprintf("%d", count))
|
|
|
|
|
|
2012-04-04 14:54:24 -04:00
|
|
|
|
def dump(self):
|
2012-04-06 14:25:09 -04:00
|
|
|
|
printf("-----dump start-----\n%s-----dump end-----\n", self.captured)
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_01_basic(self):
|
2012-04-06 14:25:09 -04:00
|
|
|
|
|
2012-04-04 14:54:24 -04:00
|
|
|
|
# help
|
|
|
|
|
self.ok("--help")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("usage:")
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
|
|
|
|
# fail for no args
|
|
|
|
|
self.fail("")
|
|
|
|
|
|
|
|
|
|
# fail for no such option
|
|
|
|
|
self.fail("--nosuchoption")
|
|
|
|
|
|
|
|
|
|
# fail for bad command
|
|
|
|
|
self.fail("badcommand")
|
|
|
|
|
|
|
|
|
|
# try some URL constructions
|
|
|
|
|
self.fail("--url http://nosuchurl/ info")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("Couldn't resolve host 'nosuchurl'")
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
|
|
|
|
self.fail("--url nosuchurl info")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("Couldn't resolve host 'nosuchurl'")
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
|
|
|
|
self.fail("-u nosuchurl/foo info")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("Couldn't resolve host 'nosuchurl'")
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
|
|
|
|
self.fail("-u localhost:0 info")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("couldn't connect to host")
|
2012-04-04 14:54:24 -04:00
|
|
|
|
|
|
|
|
|
self.ok("-u localhost:12380 info")
|
|
|
|
|
self.ok("info")
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-05-29 19:49:08 -04:00
|
|
|
|
# Duplicated arguments should fail, but this isn't implemented
|
|
|
|
|
# due to it being kind of a pain with argparse.
|
|
|
|
|
if 0:
|
|
|
|
|
self.fail("-u url1 -u url2 info")
|
|
|
|
|
self.contain("duplicated argument")
|
|
|
|
|
|
|
|
|
|
self.fail("list --detail --detail")
|
|
|
|
|
self.contain("duplicated argument")
|
|
|
|
|
|
|
|
|
|
self.fail("list --detail --path path1 --path path2")
|
|
|
|
|
self.contain("duplicated argument")
|
|
|
|
|
|
|
|
|
|
self.fail("extract --start 2000-01-01 --start 2001-01-02")
|
|
|
|
|
self.contain("duplicated argument")
|
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_02_info(self):
|
2012-04-04 14:54:24 -04:00
|
|
|
|
self.ok("info")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("Server URL: http://localhost:12380/")
|
|
|
|
|
self.contain("Server version: " + test_server.version)
|
2012-07-24 17:52:38 -04:00
|
|
|
|
self.contain("Server database path")
|
|
|
|
|
self.contain("Server database size")
|
2012-03-19 21:18:30 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_03_createlist(self):
|
2012-04-04 18:34:01 -04:00
|
|
|
|
# Basic stream tests, like those in test_client.
|
|
|
|
|
|
|
|
|
|
# No streams
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("")
|
|
|
|
|
|
|
|
|
|
# Bad paths
|
|
|
|
|
self.fail("create foo/bar/baz PrepData")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("paths must start with /")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
|
|
|
|
self.fail("create /foo PrepData")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("invalid path")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
|
|
|
|
# Bad layout type
|
|
|
|
|
self.fail("create /newton/prep NoSuchLayout")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.contain("no such layout")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
|
|
|
|
# Create a few streams
|
2012-12-04 22:15:00 -05:00
|
|
|
|
self.ok("create /newton/zzz/rawnotch RawNotchedData")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("create /newton/prep PrepData")
|
|
|
|
|
self.ok("create /newton/raw RawData")
|
|
|
|
|
|
2012-12-04 22:15:00 -05:00
|
|
|
|
# Should not be able to create a stream with another stream as
|
|
|
|
|
# its parent
|
|
|
|
|
self.fail("create /newton/prep/blah PrepData")
|
2012-12-30 15:36:57 -05:00
|
|
|
|
self.contain("path is subdir of existing node")
|
|
|
|
|
|
|
|
|
|
# Should not be able to create a stream at a location that
|
|
|
|
|
# has other nodes as children
|
|
|
|
|
self.fail("create /newton/zzz PrepData")
|
|
|
|
|
self.contain("subdirs of this path already exist")
|
2012-12-04 22:15:00 -05:00
|
|
|
|
|
|
|
|
|
# Verify we got those 3 streams and they're returned in
|
|
|
|
|
# alphabetical order.
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("/newton/prep PrepData\n"
|
|
|
|
|
"/newton/raw RawData\n"
|
|
|
|
|
"/newton/zzz/rawnotch RawNotchedData\n")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
# Match just one type or one path
|
|
|
|
|
self.ok("list --path /newton/raw")
|
|
|
|
|
self.match("/newton/raw RawData\n")
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("list --layout RawData")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.match("/newton/raw RawData\n")
|
|
|
|
|
|
|
|
|
|
# Wildcard matches
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("list --layout Raw*")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.match("/newton/raw RawData\n"
|
|
|
|
|
"/newton/zzz/rawnotch RawNotchedData\n")
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("list --path *zzz* --layout Raw*")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.match("/newton/zzz/rawnotch RawNotchedData\n")
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("list --path *zzz* --layout Prep*")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.match("")
|
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_04_metadata(self):
|
2012-04-04 18:34:01 -04:00
|
|
|
|
# Set / get metadata
|
|
|
|
|
self.fail("metadata")
|
|
|
|
|
self.fail("metadata --get")
|
|
|
|
|
|
|
|
|
|
self.ok("metadata /newton/prep")
|
|
|
|
|
self.match("")
|
|
|
|
|
|
|
|
|
|
self.ok("metadata /newton/raw --get")
|
|
|
|
|
self.match("")
|
|
|
|
|
|
|
|
|
|
self.ok("metadata /newton/prep --set "
|
|
|
|
|
"'description=The Data' "
|
|
|
|
|
"v_scale=1.234")
|
|
|
|
|
self.ok("metadata /newton/raw --update "
|
|
|
|
|
"'description=The Data'")
|
|
|
|
|
self.ok("metadata /newton/raw --update "
|
|
|
|
|
"v_scale=1.234")
|
|
|
|
|
|
2012-04-06 14:37:05 -04:00
|
|
|
|
# various parsing tests
|
|
|
|
|
self.ok("metadata /newton/raw --update foo=")
|
|
|
|
|
self.fail("metadata /newton/raw --update =bar")
|
|
|
|
|
self.fail("metadata /newton/raw --update foo==bar")
|
|
|
|
|
self.fail("metadata /newton/raw --update foo;bar")
|
|
|
|
|
|
|
|
|
|
# errors
|
|
|
|
|
self.fail("metadata /newton/nosuchstream foo=bar")
|
|
|
|
|
self.contain("unrecognized arguments")
|
|
|
|
|
self.fail("metadata /newton/nosuchstream")
|
|
|
|
|
self.contain("No stream at path")
|
|
|
|
|
self.fail("metadata /newton/nosuchstream --set foo=bar")
|
|
|
|
|
self.contain("No stream at path")
|
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("metadata /newton/prep")
|
|
|
|
|
self.match("description=The Data\nv_scale=1.234\n")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("metadata /newton/prep --get")
|
|
|
|
|
self.match("description=The Data\nv_scale=1.234\n")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("metadata /newton/prep --get descr")
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.match("descr=\n")
|
|
|
|
|
|
2012-04-04 18:34:01 -04:00
|
|
|
|
self.ok("metadata /newton/prep --get description")
|
|
|
|
|
self.match("description=The Data\n")
|
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("metadata /newton/prep --get description v_scale")
|
|
|
|
|
self.match("description=The Data\nv_scale=1.234\n")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("metadata /newton/prep --set "
|
|
|
|
|
"'description=The Data'")
|
|
|
|
|
|
|
|
|
|
self.ok("metadata /newton/prep --get")
|
|
|
|
|
self.match("description=The Data\n")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.fail("metadata /newton/nosuchpath")
|
|
|
|
|
self.contain("No stream at path /newton/nosuchpath")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_05_parsetime(self):
|
2012-08-09 14:03:18 -04:00
|
|
|
|
os.environ['TZ'] = "America/New_York"
|
2012-04-09 15:35:14 -04:00
|
|
|
|
cmd = nilmdb.cmdline.Cmdline(None)
|
|
|
|
|
test = datetime_tz.datetime_tz.now()
|
|
|
|
|
eq_(cmd.parse_time(str(test)), test)
|
|
|
|
|
test = datetime_tz.datetime_tz.smartparse("20120405 1400-0400")
|
|
|
|
|
eq_(cmd.parse_time("hi there 20120405 1400-0400 testing! 123"), test)
|
2012-05-04 18:36:27 -04:00
|
|
|
|
eq_(cmd.parse_time("20120405 1800 UTC"), test)
|
|
|
|
|
eq_(cmd.parse_time("20120405 1400-0400 UTC"), test)
|
2013-01-03 16:58:26 -05:00
|
|
|
|
for badtime in [ "20120405 1400-9999", "hello", "-", "", "14:00" ]:
|
|
|
|
|
with assert_raises(ValueError):
|
|
|
|
|
x = cmd.parse_time(badtime)
|
2012-04-09 17:31:39 -04:00
|
|
|
|
eq_(cmd.parse_time("snapshot-20120405-140000.raw.gz"), test)
|
|
|
|
|
eq_(cmd.parse_time("prep-20120405T1400"), test)
|
2012-04-09 15:35:14 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_06_insert(self):
|
2012-04-06 14:25:09 -04:00
|
|
|
|
self.ok("insert --help")
|
|
|
|
|
|
|
|
|
|
self.fail("insert /foo/bar baz qwer")
|
|
|
|
|
self.contain("Error getting stream info")
|
|
|
|
|
|
|
|
|
|
self.fail("insert /newton/prep baz qwer")
|
|
|
|
|
self.match("Error opening input file baz\n")
|
|
|
|
|
|
2012-04-09 17:31:39 -04:00
|
|
|
|
self.fail("insert /newton/prep")
|
|
|
|
|
self.contain("Error extracting time")
|
2012-04-04 18:34:01 -04:00
|
|
|
|
|
2012-05-04 18:36:27 -04:00
|
|
|
|
self.fail("insert --start 19801205 /newton/prep 1 2 3 4")
|
2012-04-09 17:31:39 -04:00
|
|
|
|
self.contain("--start can only be used with one input file")
|
|
|
|
|
|
2012-08-06 18:05:09 -04:00
|
|
|
|
self.fail("insert /newton/prep "
|
|
|
|
|
"tests/data/prep-20120323T1000")
|
|
|
|
|
|
2012-04-09 17:31:39 -04:00
|
|
|
|
# insert pre-timestamped data, from stdin
|
2012-05-04 18:36:27 -04:00
|
|
|
|
os.environ['TZ'] = "UTC"
|
2012-04-09 17:31:39 -04:00
|
|
|
|
with open("tests/data/prep-20120323T1004-timestamped") as input:
|
|
|
|
|
self.ok("insert --none /newton/prep", input)
|
|
|
|
|
|
|
|
|
|
# insert data with normal timestamper from filename
|
2012-05-04 18:36:27 -04:00
|
|
|
|
os.environ['TZ'] = "UTC"
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.ok("insert --rate 120 /newton/prep "
|
2012-04-09 17:31:39 -04:00
|
|
|
|
"tests/data/prep-20120323T1000 "
|
|
|
|
|
"tests/data/prep-20120323T1002")
|
|
|
|
|
|
|
|
|
|
# overlap
|
2012-05-04 18:36:27 -04:00
|
|
|
|
os.environ['TZ'] = "UTC"
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.fail("insert --rate 120 /newton/prep "
|
2012-04-09 17:31:39 -04:00
|
|
|
|
"tests/data/prep-20120323T1004")
|
|
|
|
|
self.contain("overlap")
|
|
|
|
|
|
2012-04-24 17:59:33 -04:00
|
|
|
|
# Just to help test more situations -- stop and restart
|
|
|
|
|
# the server now. This tests nilmdb's interval caching,
|
|
|
|
|
# at the very least.
|
|
|
|
|
server_stop()
|
|
|
|
|
server_start()
|
|
|
|
|
|
2012-05-07 18:32:02 -04:00
|
|
|
|
# still an overlap if we specify a different start
|
2012-05-04 18:36:27 -04:00
|
|
|
|
os.environ['TZ'] = "America/New_York"
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.fail("insert --rate 120 --start '03/23/2012 06:05:00' /newton/prep"
|
|
|
|
|
" tests/data/prep-20120323T1004")
|
2012-04-09 17:31:39 -04:00
|
|
|
|
self.contain("overlap")
|
|
|
|
|
|
|
|
|
|
# wrong format
|
2012-05-04 18:36:27 -04:00
|
|
|
|
os.environ['TZ'] = "UTC"
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.fail("insert --rate 120 /newton/raw "
|
2012-04-09 17:31:39 -04:00
|
|
|
|
"tests/data/prep-20120323T1004")
|
|
|
|
|
self.contain("Error parsing input data")
|
|
|
|
|
|
|
|
|
|
# empty data does nothing
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.ok("insert --rate 120 --start '03/23/2012 06:05:00' /newton/prep "
|
2012-04-09 17:31:39 -04:00
|
|
|
|
"/dev/null")
|
|
|
|
|
|
|
|
|
|
# bad start time
|
2012-08-06 17:46:09 -04:00
|
|
|
|
self.fail("insert --rate 120 --start 'whatever' /newton/prep /dev/null")
|
2012-05-04 18:36:27 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_07_detail(self):
|
2012-05-07 18:32:02 -04:00
|
|
|
|
# Just count the number of lines, it's probably fine
|
2012-05-04 18:36:27 -04:00
|
|
|
|
self.ok("list --detail")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 8)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 4)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 10:02'")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 3)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05'")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 2)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15'")
|
2012-12-11 23:31:55 -05:00
|
|
|
|
lines_(self.captured, 2)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
self.contain("10:05:15.000")
|
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15.50'")
|
2012-12-11 23:31:55 -05:00
|
|
|
|
lines_(self.captured, 2)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
self.contain("10:05:15.500")
|
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 19:05:15.50'")
|
2012-12-11 23:31:55 -05:00
|
|
|
|
lines_(self.captured, 2)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
self.contain("no intervals")
|
|
|
|
|
|
|
|
|
|
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15.50'"
|
|
|
|
|
+ " --end='23 Mar 2012 10:05:15.50'")
|
2012-12-11 23:31:55 -05:00
|
|
|
|
lines_(self.captured, 2)
|
2012-05-07 18:32:02 -04:00
|
|
|
|
self.contain("10:05:15.500")
|
|
|
|
|
|
2012-05-11 14:31:11 -04:00
|
|
|
|
self.ok("list --detail")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 8)
|
2012-05-11 14:31:11 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_08_extract(self):
|
2012-05-16 18:19:00 -04:00
|
|
|
|
# nonexistent stream
|
2012-05-11 14:31:11 -04:00
|
|
|
|
self.fail("extract /no/such/foo --start 2000-01-01 --end 2020-01-01")
|
2012-05-16 18:19:00 -04:00
|
|
|
|
self.contain("Error getting stream info")
|
2012-05-11 14:31:11 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
# empty ranges return an error
|
2012-05-30 14:24:36 -04:00
|
|
|
|
self.fail("extract -a /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 10:00:30' " +
|
|
|
|
|
"--end '23 Mar 2012 10:00:30'", exitcode = 2)
|
|
|
|
|
self.contain("no data")
|
2012-06-04 19:46:33 -04:00
|
|
|
|
self.fail("extract -a /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 10:00:30.000001' " +
|
|
|
|
|
"--end '23 Mar 2012 10:00:30.000001'", exitcode = 2)
|
|
|
|
|
self.contain("no data")
|
2012-06-25 14:52:50 -04:00
|
|
|
|
self.fail("extract -a /newton/prep " +
|
2012-08-09 14:04:47 -04:00
|
|
|
|
"--start '23 Mar 2022 10:00:30' " +
|
|
|
|
|
"--end '23 Mar 2022 10:00:30'", exitcode = 2)
|
2012-06-25 14:52:50 -04:00
|
|
|
|
self.contain("no data")
|
|
|
|
|
|
|
|
|
|
# but are ok if we're just counting results
|
|
|
|
|
self.ok("extract --count /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 10:00:30' " +
|
|
|
|
|
"--end '23 Mar 2012 10:00:30'")
|
|
|
|
|
self.match("0\n")
|
|
|
|
|
self.ok("extract -c /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 10:00:30.000001' " +
|
|
|
|
|
"--end '23 Mar 2012 10:00:30.000001'")
|
|
|
|
|
self.match("0\n")
|
2012-05-30 14:24:36 -04:00
|
|
|
|
|
|
|
|
|
# Check various dumps against stored copies of how they should appear
|
|
|
|
|
def test(file, start, end, extra=""):
|
|
|
|
|
self.ok("extract " + extra + " /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 " + start + "' " +
|
|
|
|
|
"--end '23 Mar 2012 " + end + "'")
|
|
|
|
|
self.matchfile("tests/data/extract-" + str(file))
|
2012-06-25 14:52:50 -04:00
|
|
|
|
self.ok("extract --count " + extra + " /newton/prep " +
|
|
|
|
|
"--start '23 Mar 2012 " + start + "' " +
|
|
|
|
|
"--end '23 Mar 2012 " + end + "'")
|
|
|
|
|
self.matchfilecount("tests/data/extract-" + str(file))
|
2012-05-30 14:24:36 -04:00
|
|
|
|
test(1, "10:00:30", "10:00:31", extra="-a")
|
|
|
|
|
test(1, "10:00:30.000000", "10:00:31", extra="-a")
|
|
|
|
|
test(2, "10:00:30.000001", "10:00:31")
|
|
|
|
|
test(2, "10:00:30.008333", "10:00:31")
|
|
|
|
|
test(3, "10:00:30.008333", "10:00:30.008334")
|
|
|
|
|
test(3, "10:00:30.008333", "10:00:30.016667")
|
|
|
|
|
test(4, "10:00:30.008333", "10:00:30.025")
|
|
|
|
|
test(5, "10:00:30", "10:00:31", extra="--annotate --bare")
|
|
|
|
|
test(6, "10:00:30", "10:00:31", extra="-b")
|
|
|
|
|
|
2012-06-04 19:46:33 -04:00
|
|
|
|
# all data put in by tests
|
2012-05-24 17:05:38 -04:00
|
|
|
|
self.ok("extract -a /newton/prep --start 2000-01-01 --end 2020-01-01")
|
2012-12-11 23:31:55 -05:00
|
|
|
|
lines_(self.captured, 43204)
|
2012-06-25 14:52:50 -04:00
|
|
|
|
self.ok("extract -c /newton/prep --start 2000-01-01 --end 2020-01-01")
|
|
|
|
|
self.match("43200\n")
|
2012-05-11 14:31:11 -04:00
|
|
|
|
|
2012-06-25 14:52:50 -04:00
|
|
|
|
def test_cmdline_09_truncated(self):
|
2012-05-25 12:44:24 -04:00
|
|
|
|
# Test truncated responses by overriding the nilmdb max_results
|
2012-05-11 14:31:11 -04:00
|
|
|
|
server_stop()
|
2012-05-25 12:44:24 -04:00
|
|
|
|
server_start(max_results = 2)
|
2012-05-11 14:31:11 -04:00
|
|
|
|
self.ok("list --detail")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 8)
|
2012-06-25 14:52:50 -04:00
|
|
|
|
server_stop()
|
|
|
|
|
server_start()
|
2012-12-04 22:15:00 -05:00
|
|
|
|
|
|
|
|
|
def test_cmdline_10_destroy(self):
|
|
|
|
|
# Delete records
|
|
|
|
|
self.ok("destroy --help")
|
|
|
|
|
|
|
|
|
|
self.fail("destroy")
|
|
|
|
|
self.contain("too few arguments")
|
|
|
|
|
|
|
|
|
|
self.fail("destroy /no/such/stream")
|
|
|
|
|
self.contain("No stream at path")
|
|
|
|
|
|
|
|
|
|
self.fail("destroy asdfasdf")
|
|
|
|
|
self.contain("No stream at path")
|
|
|
|
|
|
|
|
|
|
# From previous tests, we have:
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("/newton/prep PrepData\n"
|
|
|
|
|
"/newton/raw RawData\n"
|
|
|
|
|
"/newton/zzz/rawnotch RawNotchedData\n")
|
|
|
|
|
|
|
|
|
|
# Notice how they're not empty
|
|
|
|
|
self.ok("list --detail")
|
2012-12-12 19:25:27 -05:00
|
|
|
|
lines_(self.captured, 8)
|
2012-12-04 22:15:00 -05:00
|
|
|
|
|
|
|
|
|
# Delete some
|
|
|
|
|
self.ok("destroy /newton/prep")
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("/newton/raw RawData\n"
|
|
|
|
|
"/newton/zzz/rawnotch RawNotchedData\n")
|
|
|
|
|
|
|
|
|
|
self.ok("destroy /newton/zzz/rawnotch")
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("/newton/raw RawData\n")
|
|
|
|
|
|
|
|
|
|
self.ok("destroy /newton/raw")
|
|
|
|
|
self.ok("create /newton/raw RawData")
|
|
|
|
|
self.ok("destroy /newton/raw")
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.match("")
|
|
|
|
|
|
|
|
|
|
# Re-create a previously deleted location, and some new ones
|
|
|
|
|
rebuild = [ "/newton/prep", "/newton/zzz",
|
|
|
|
|
"/newton/raw", "/newton/asdf/qwer" ]
|
|
|
|
|
for path in rebuild:
|
|
|
|
|
# Create the path
|
|
|
|
|
self.ok("create " + path + " PrepData")
|
|
|
|
|
self.ok("list")
|
|
|
|
|
self.contain(path)
|
|
|
|
|
# Make sure it was created empty
|
|
|
|
|
self.ok("list --detail --path " + path)
|
|
|
|
|
self.contain("(no intervals)")
|
2013-01-02 00:00:30 -05:00
|
|
|
|
|
|
|
|
|
def test_cmdline_11_unicode(self):
|
|
|
|
|
# Unicode paths.
|
|
|
|
|
self.ok("destroy /newton/asdf/qwer")
|
|
|
|
|
self.ok("destroy /newton/prep")
|
|
|
|
|
self.ok("destroy /newton/raw")
|
|
|
|
|
self.ok("destroy /newton/zzz")
|
|
|
|
|
|
|
|
|
|
self.ok(u"create /düsseldorf/raw uint16_6")
|
|
|
|
|
self.ok("list --detail")
|
|
|
|
|
self.contain(u"/düsseldorf/raw uint16_6")
|
|
|
|
|
self.contain("(no intervals)")
|
|
|
|
|
|
|
|
|
|
# Unicode metadata
|
|
|
|
|
self.ok(u"metadata /düsseldorf/raw --set α=beta 'γ=δ'")
|
|
|
|
|
self.ok(u"metadata /düsseldorf/raw --update 'α=β ε τ α'")
|
|
|
|
|
self.ok(u"metadata /düsseldorf/raw")
|
|
|
|
|
self.match(u"α=β ε τ α\nγ=δ\n")
|