def test_logging(self): """Minimal logging end-to-end test. Logs are written, and then same logs should be read back. """ # Write some logs, waiting for them to arrive: service = random_string() run_python(self.python_binary, "write_logs.py", [service])
def test_environmentFallback(self): """ Imagine an environment 'parent:child'. Service A that is in environment 'parent:child' can communicate with a service B that is in enviornment 'parent'. Furthermore, the session preserves the original environment, so if A calls B and B wants to call C within the same session, if C is in 'parent:child' it will be found by B. """ # 1. Create a session in environment parent:child context_id = run_python(self.python_binary, "create_trace.py", output=True, additional_env={"MDK_ENVIRONMENT": "parent:child"}) # 2. Register B in parent, and C in parent:child serviceB, addressB = self.registerInAnEnvironment("parent") serviceC, addressC = self.registerInAnEnvironment("parent:child") # 3. Services in parent:child can resolve B, even though it's in # parent, both with and without a parent:child session: self.assertResolvable(serviceB, "parent:child", addressB) self.assertResolvable(serviceB, "parent:child", addressB, context_id) # 4. Services in parent can resolve C if it's joined a parent:child # session, but not with its normal sessions: self.assertResolvable(serviceC, "parent", addressC, context_id) assertNotResolvable(self, serviceC, {"MDK_ENVIRONMENT": "parent"})
def create_context(): """Create a new MDK context.""" return run_python( sys.executable, "create-context.py", output=True, additional_env={"MDK_DISCOVERY_SOURCE": "static:nodes={}"})
def test_environmentFallback(self): """ Imagine an environment 'parent:child'. Service A that is in environment 'parent:child' can communicate with a service B that is in enviornment 'parent'. Furthermore, the session preserves the original environment, so if A calls B and B wants to call C within the same session, if C is in 'parent:child' it will be found by B. """ # 1. Create a session in environment parent:child context_id = run_python( self.python_binary, "create_trace.py", output=True, additional_env={"MDK_ENVIRONMENT": "parent:child"}) # 2. Register B in parent, and C in parent:child serviceB, addressB = self.registerInAnEnvironment("parent") serviceC, addressC = self.registerInAnEnvironment("parent:child") # 3. Services in parent:child can resolve B, even though it's in # parent, both with and without a parent:child session: self.assertResolvable(serviceB, "parent:child", addressB) self.assertResolvable(serviceB, "parent:child", addressB, context_id) # 4. Services in parent can resolve C if it's joined a parent:child # session, but not with its normal sessions: self.assertResolvable(serviceC, "parent", addressC, context_id) assertNotResolvable(self, serviceC, {"MDK_ENVIRONMENT": "parent"})
def assertNotResolvable(test, service, additional_env={}): """Assert the service isn't resolvable.""" env = os.environ.copy() env.update(additional_env) resolved_address = run_python(sys.executable, "resolve.py", [service], output=True, additional_env=env) test.assertEqual(b"not found", resolved_address)
def test_tracing(self): """Minimal tracing end-to-end test. One process can start a session context and a second one can join it, and they both get logged together. """ context_id = run_python(self.python_binary, "create_trace.py", output=True) expected_messages = dumps([("process1", "hello"), ("process2", "world")]) p = Popen([sys.executable, os.path.join(CODE_PATH, "read_logs.py"), context_id, expected_messages]) print("context_id", context_id) # Ensure the subscription is in place before we send messages: time.sleep(8) context_id = run_python(self.python_binary, "start_trace.py", [context_id], output=True) run_python(self.python_binary, "continue_trace.py", [context_id]) assert p.wait() == 0
def try_python(self, python_exe): r = run_python(python_exe, 'import sys;print("%s@@@%s" % (sys.executable, sys.version))', suppress_errors = True) if not r[0]: EXE, VER = r[1].split ('@@@') EXE = os.path.realpath(EXE) print '%r -> %r' % (python_exe, EXE) return EXE, VER.rstrip() return None, None
def test_discovery(self): """Minimal discovery end-to-end test.""" # 1. Services registered by one process can be looked up by another. p, service, _ = assertRegisteryDiscoverable( self, lambda service: run_python( self.python_binary, "resolve.py", [service], output=True)) # 2. If the service is unregistered via MDK stop() then it is no longer resolvable. p.terminate() time.sleep(3) assertNotResolvable(self, service)
def assertResolvable(self, service, environment, expected_address, context=None): """ Assert a service can be resolved in the given environment. """ params = [service] if context is not None: params.append(context) address = run_python(self.python_binary, "resolve.py", params, output=True, additional_env={"MDK_ENVIRONMENT": environment}) self.assertEqual(address.decode("utf-8"), expected_address)
def test_discovery(self): """Minimal discovery end-to-end test.""" # 1. Services registered by one process can be looked up by another. p, service, _ = assertRegisteryDiscoverable( self, lambda service: run_python(self.python_binary, "resolve.py", [service], output=True)) # 2. If the service is unregistered via MDK stop() then it is no longer resolvable. p.terminate() time.sleep(3) assertNotResolvable(self, service)
def registerInAnEnvironment(self, environment): """ Register a service in the specified environment. """ env = {"MDK_ENVIRONMENT": environment} _, service, address = assertRegisteryDiscoverable( self, lambda service: run_python(self.python_binary, "resolve.py", [service], output=True, additional_env=env), additional_env=env) return service, address
def test_defaultEnvironmentIsolation(self): """ A service registered in default environment can't be resolved from another environment. """ # 1. Register in default environment _, service, _ = assertRegisteryDiscoverable( self, lambda service: run_python(self.python_binary, "resolve.py", [service], output=True, additional_env= {"MDK_ENVIRONMENT": "sandbox"})) # 2. Assert it can't be found in a different environment: assertNotResolvable(self, service, {"MDK_ENVIRONMENT": "anotherenv"})
def test_defaultEnvironmentIsolation(self): """ A service registered in default environment can't be resolved from another environment. """ # 1. Register in default environment _, service, _ = assertRegisteryDiscoverable( self, lambda service: run_python(self.python_binary, "resolve.py", [service], output=True, additional_env={ "MDK_ENVIRONMENT": "sandbox"})) # 2. Assert it can't be found in a different environment: assertNotResolvable(self, service, {"MDK_ENVIRONMENT": "anotherenv"})
def quick_test(self): if self.path is not None: r = run_python(self.get('python path'), ''' import %s as package version = getattr(package, "__version__", "N/A") if version=="N/A": try: from %s.version import version except: pass print(version) ''' % (self.packagename, self.packagename), self.version) return not r[0] else: print '%s quick test skipped' % (self.title)
def test_tracing(self): """Minimal tracing end-to-end test. One process can start a session context and a second one can join it, and they both get logged together. """ context_id = run_python(self.python_binary, "create_trace.py", output=True) expected_messages = dumps([("process1", "hello"), ("process2", "world")]) p = Popen([ sys.executable, os.path.join(CODE_PATH, "read_logs.py"), context_id, expected_messages ]) print("context_id", context_id) # Ensure the subscription is in place before we send messages: time.sleep(8) context_id = run_python(self.python_binary, "start_trace.py", [context_id], output=True) run_python(self.python_binary, "continue_trace.py", [context_id]) assert p.wait() == 0
def get_tiff_version(self, dll): if dll is None: return r = run_python (self.get('python path'),\ ''' from ctypes import windll, c_char_p lib = windll.libtiff3 lib.TIFFGetVersion.restype = c_char_p v = windll.libtiff3.TIFFGetVersion() print v ''', env = dict (PATH=os.path.dirname(dll)) ) if not r[0]: v = r[1].split('\n')[0] if v.startswith ('LIBTIFF, Version'): return v.split(' ')[-1]
def run_python(self, *args, **kws): if 'cwd' not in kws: kws['cwd'] = self.model.working_dir if 'env' not in kws: kws['env'] = self.environ return run_python(self.get('python path'), *args, **kws)
def quick_test(self): if self.path is not None: r = run_python(self.path, 'print ("Hello!")', 'Hello!') return not r[0] else: print '%s quick test skipped' % (self.title)