5562: Use static method. Fixes "TypeError: _socket_open() takes exactly 5 arguments...
[arvados.git] / services / nodemanager / tests / testutil.py
index d51aab22e7fbf21ec4c58d087380054b811c6b37..82d6479e24ae53b33b676637ca772d867326b196 100644 (file)
@@ -2,6 +2,7 @@
 
 from __future__ import absolute_import, print_function
 
+import datetime
 import threading
 import time
 
@@ -12,14 +13,17 @@ from . import pykka_timeout
 
 no_sleep = mock.patch('time.sleep', lambda n: None)
 
-def arvados_node_mock(node_num=99, job_uuid=None, age=0, **kwargs):
+def arvados_node_mock(node_num=99, job_uuid=None, age=-1, **kwargs):
+    mod_time = datetime.datetime.utcnow() - datetime.timedelta(seconds=age)
+    mod_time_s = mod_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
     if job_uuid is True:
         job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
     crunch_worker_state = 'idle' if (job_uuid is None) else 'busy'
     node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
-            'created_at': '2014-01-01T01:02:03Z',
-            'modified_at': time.strftime('%Y-%m-%dT%H:%M:%SZ',
-                                         time.gmtime(time.time() - age)),
+            'created_at': '2014-01-01T01:02:03.04050607Z',
+            'modified_at': mod_time_s,
+            'first_ping_at': kwargs.pop('first_ping_at', mod_time_s),
+            'last_ping_at': mod_time_s,
             'slot_number': node_num,
             'hostname': 'compute{}'.format(node_num),
             'domain': 'zzzzz.arvadosapi.com',
@@ -30,13 +34,14 @@ def arvados_node_mock(node_num=99, job_uuid=None, age=0, **kwargs):
     node.update(kwargs)
     return node
 
-def cloud_object_mock(name_id):
+def cloud_object_mock(name_id, **extra):
     # A very generic mock, useful for stubbing libcloud objects we
     # only search for and pass around, like locations, subnets, etc.
     cloud_object = mock.NonCallableMagicMock(['id', 'name'],
                                              name='cloud_object')
-    cloud_object.id = str(name_id)
-    cloud_object.name = cloud_object.id.upper()
+    cloud_object.name = str(name_id)
+    cloud_object.id = 'id_' + cloud_object.name
+    cloud_object.extra = extra
     return cloud_object
 
 def cloud_node_mock(node_num=99, **extra):
@@ -51,6 +56,11 @@ def cloud_node_mock(node_num=99, **extra):
     node.extra = extra
     return node
 
+def cloud_node_fqdn(node):
+    # We intentionally put the FQDN somewhere goofy to make sure tested code is
+    # using this function for lookups.
+    return node.extra.get('testname', 'NoTestName')
+
 def ip_address_mock(last_octet):
     return '10.20.30.{}'.format(last_octet)