CORD-866 run_tests script, import models into locals

Change-Id: I1917b77adb48acdd922cd236bba261976c879c8e
diff --git a/xos/xos_client/tests/run_tests.sh b/xos/xos_client/tests/run_tests.sh
new file mode 100755
index 0000000..0d4c4f4
--- /dev/null
+++ b/xos/xos_client/tests/run_tests.sh
@@ -0,0 +1,10 @@
+#! /bin/bash
+
+# Run the tests from the head-node against an xos-client VM
+
+PW=`cat /opt/cord/build/platform-install/credentials/xosadmin@opencord.org`
+
+docker run -it --entrypoint python xosproject/xos-client /tmp/xos_client/tests/orm_user_crud.py -u xosadmin@opencord.org -p $PW -qq
+docker run -it --entrypoint python xosproject/xos-client /tmp/xos_client/tests/orm_listall.py -u xosadmin@opencord.org -p $PW -qq
+docker run -it --entrypoint python xosproject/xos-client /tmp/xos_client/tests/vtr_crud.py -u xosadmin@opencord.org -p $PW -qq
+docker run -it --entrypoint python xosproject/xos-client /tmp/xos_client/tests/vsg_introspect.py -u xosadmin@opencord.org -p $PW -qq
diff --git a/xos/xos_client/tests/vsg_introspect.py b/xos/xos_client/tests/vsg_introspect.py
new file mode 100644
index 0000000..e3c7329
--- /dev/null
+++ b/xos/xos_client/tests/vsg_introspect.py
@@ -0,0 +1,19 @@
+import sys
+sys.path.append("..")
+
+from xosapi import xos_grpc_client
+
+def test_callback():
+    print "TEST: vsg_introspect"
+
+    c = xos_grpc_client.coreclient
+
+    for vsg in c.xos_orm.VSGTenant.objects.all():
+        print "  vsg", vsg.id
+        for field_name in ["wan_container_ip", "wan_container_mac", "wan_container_netbits", "wan_container_gateway_ip", "wan_container_gateway_mac", "wan_vm_ip", "wan_vm_mac"]:
+            print "    %s: %s" % (field_name, getattr(vsg, field_name))
+
+    print "    okay"
+
+xos_grpc_client.start_api_parseargs(test_callback)
+
diff --git a/xos/xos_client/xosapi/xos_grpc_client.py b/xos/xos_client/xosapi/xos_grpc_client.py
index f06aad5..8213662 100644
--- a/xos/xos_client/xosapi/xos_grpc_client.py
+++ b/xos/xos_client/xosapi/xos_grpc_client.py
@@ -170,6 +170,20 @@
 
     return args
 
+def setup_logging(args):
+    import logging
+    import structlog
+
+    verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
+    logging.basicConfig()
+    logger = logging.getLogger()
+    logger.setLevel(logging.DEBUG - 10*verbosity_adjust)
+
+    def logger_factory():
+        return logger
+
+    structlog.configure(logger_factory=logger_factory)
+
 def coreclient_reconnect(client, reconnect_callback, *args, **kwargs):
     global coreapi
 
@@ -194,8 +208,14 @@
     reactor.run()
 
 def start_api_parseargs(reconnect_callback):
+    """ This function is an entrypoint for tests and other simple programs to
+        setup the API and get a callback when the API is ready.
+    """
+
     args = parse_args()
 
+    setup_logging(args)
+
     if args.username:
         start_api(reconnect_callback, endpoint=args.grpc_secure_endpoint, username=args.username, password=args.password)
     else:
diff --git a/xos/xos_client/xossh b/xos/xos_client/xossh
index 6c186e7..7e3fe58 100755
--- a/xos/xos_client/xossh
+++ b/xos/xos_client/xossh
@@ -11,7 +11,7 @@
 import rlcompleter
 
 from twisted.internet import reactor
-from xosapi.xos_grpc_client import InsecureClient, SecureClient
+from xosapi.xos_grpc_client import InsecureClient, SecureClient, setup_logging
 
 current_client = None
 
@@ -105,7 +105,7 @@
 
 def examples():
     print 'coreapi.Slice.objects.all() # list all slices'
-    print 's = coreapi.Slice.objects.new() # create a new slice'
+    print 's = Slice.objects.new() # create a new slice'
     print 's.name = "mysite_foo" # set a slice name'
     print 's.site_id = coreapi.Site.objects.all()[0].id # grab the first site'
     print 's.save() # save the slice'
@@ -130,7 +130,7 @@
 
         print "XOS Core server at %s" % client.endpoint
 
-        print 'Type "coreapi.listObjects()" for a list of all objects'
+        print 'Type "listObjects()" for a list of all objects'
         print 'Type "login("username", "password")" to switch to a secure shell'
         print 'Type "examples()" for some examples'
 
@@ -149,6 +149,11 @@
     reactor.callLater(0, functools.partial(do_xossh_prompt, client))
 
 def do_xossh_prompt(client):
+    for k in client.xos_orm.all_model_names:
+        locals()[k] = getattr(client.xos_orm, k)
+
+    locals()["listObjects"] = client.xos_orm.listObjects
+
     prompt = "xossh "
     try:
         while True:
@@ -205,6 +210,9 @@
 def main():
     global args
     args = parse_args()
+
+    setup_logging(args)
+
     login(username=args.username, password=args.password)
     reactor.run()