2727from unittest import TestCase , SkipTest
2828from shutil import copyfile
2929from sys import exit , stderr
30+
3031try :
3132 from urllib .request import urlretrieve
3233except ImportError :
3738from neo4j import GraphDatabase
3839from neo4j .exceptions import AuthError
3940
40- from test .env import NEO4J_SERVER_PACKAGE , NEO4J_USER , NEO4J_PASSWORD
41- from test .integration .tools import ServerVersion
42-
43-
44- def copy_dist (source , target ):
45- if isfile (target ) and "SNAPSHOT" not in basename (source ):
46- return target
47- try :
48- makedirs (dirname (target ))
49- except OSError :
50- pass
51- if source .startswith ("http:" ):
52- stderr .write ("Downloading package from {}\n " .format (source ))
53- urlretrieve (source , target )
54- return target
55- else :
56- return copyfile (source , target )
41+ from test .env import NEO4J_USER , NEO4J_PASSWORD , NEO4J_SERVER_URI
5742
5843
5944def is_listening (address ):
@@ -67,65 +52,10 @@ def is_listening(address):
6752 return True
6853
6954
70- class GraphDatabaseServer (object ):
71-
72- bolt_port = 7687
73- bolt_address = ("localhost" , bolt_port )
74-
75- bolt_uri = "bolt://%s:%d" % bolt_address
76- bolt_routing_uri = "bolt+routing://%s:%d" % bolt_address
77-
78- user = NEO4J_USER or "test"
79- password = NEO4J_PASSWORD or "test"
80- auth_token = (user , password )
81-
82- controller = None
83- dist_path = path_join (dirname (__file__ ), "dist" )
84- run_path = path_join (dirname (__file__ ), "run" )
85-
86- server_package = NEO4J_SERVER_PACKAGE
87- local_server_package = path_join (dist_path , basename (server_package )) if server_package else None
88-
89- @classmethod
90- def server_version_info (cls ):
91- with GraphDatabase .driver (cls .bolt_uri , auth = cls .auth_token ) as driver :
92- with driver .session () as session :
93- full_version = session .run ("RETURN 1" ).summary ().server .version
94- return ServerVersion .from_str (full_version )
95-
96- @classmethod
97- def at_least_version (cls , major , minor ):
98- return cls .server_version_info ().at_least_version (major , minor )
99-
100- @classmethod
101- def delete_known_hosts_file (cls ):
102- known_hosts = path_join (expanduser ("~" ), ".neo4j" , "known_hosts" )
103- if isfile (known_hosts ):
104- remove (known_hosts )
105-
106- @classmethod
107- def _start_server (cls , package ):
108- try :
109- makedirs (cls .run_path )
110- except OSError :
111- pass
112- if platform .system () == "Windows" :
113- controller_class = WindowsController
114- else :
115- controller_class = UnixController
116- home = realpath (controller_class .extract (package , cls .run_path ))
117- cls .controller = controller_class (home , 1 )
118- if NEO4J_USER is None :
119- cls .controller .create_user (cls .user , cls .password )
120- cls .controller .set_user_role (cls .user , "admin" )
121- cls .controller .start ()
122-
123- @classmethod
124- def _stop_server (cls ):
125- if cls .controller is not None :
126- cls .controller .stop ()
127- if NEO4J_USER is None :
128- pass # TODO: delete user
55+ class RemoteGraphDatabaseServer (object ):
56+ server_uri = NEO4J_SERVER_URI or "bolt://localhost:7687"
57+ auth_token = (NEO4J_USER or "neo4j" , NEO4J_PASSWORD )
58+ encrypted = NEO4J_SERVER_URI is not None
12959
13060 def __enter__ (self ):
13161 self .start ()
@@ -136,22 +66,15 @@ def __exit__(self, exc_type, exc_value, traceback):
13666
13767 @classmethod
13868 def start (cls ):
139- if is_listening (cls .bolt_address ):
140- stderr .write ("Using existing server listening on port {}\n " .format (cls .bolt_port ))
141- with GraphDatabase .driver (cls .bolt_uri , auth = cls .auth_token ) as driver :
142- try :
143- with driver .session ():
144- pass
145- except AuthError as error :
146- stderr .write ("{}\n " .format (error ))
147- exit (1 )
148- return
149- if cls .server_package is None :
150- raise RuntimeError ("No Neo4j server available for %s" % cls .__name__ )
151- stderr .write ("Using server from package {}\n " .format (cls .server_package ))
152- package = copy_dist (cls .server_package , cls .local_server_package )
153- cls ._start_server (package )
69+ with GraphDatabase .driver (cls .server_uri , auth = cls .auth_token , encrypted = cls .encrypted ) as driver :
70+ try :
71+ with driver .session ():
72+ print ("Using existing remote server {}\n " .format (cls .server_uri ))
73+ return
74+ except AuthError as error :
75+ raise RuntimeError ("Failed to authenticate (%s)" % error )
76+ raise SkipTest ("No remote Neo4j server available for %s" % cls .__name__ )
15477
15578 @classmethod
15679 def stop (cls ):
157- cls . _stop_server ()
80+ pass
0 commit comments