Skip to content

Commit

Permalink
Merged separate-test-servers branch
Browse files Browse the repository at this point in the history
  • Loading branch information
smclay committed Sep 23, 2020
2 parents 3be8318 + 39be688 commit 41a4e03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
10 changes: 6 additions & 4 deletions test/commands/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,9 @@ def test_ArchiveProxyMode_01(self):
"""
ports = range(8001, 8005)
cfg_file, cfg_props = self._genArchProxyCfg(ports)
self.prepExtSrv(port=8000, cfgFile=cfg_file, cfgProps=cfg_props)
self.prepCluster(ports, createDatabase = False)
sqlite_file = tmp_path('ngas.sqlite')
self.prepExtSrv(port=8000, cfgFile=cfg_file, cfgProps=cfg_props, sqlite_file=sqlite_file)
self.prepCluster(ports, createDatabase = False, sqlite_file=sqlite_file)
noOfNodes = len(ports)
nodeCount = 0
counts = {p: 0 for p in ports}
Expand Down Expand Up @@ -1228,8 +1229,9 @@ def test_ArchiveProxyMode_03(self):
"""
ports = range(8001, 8005)
cfg_file, cfg_props = self._genArchProxyCfg(ports)
_, dbObj = self.prepExtSrv(port=8000, cfgFile=cfg_file, cfgProps=cfg_props)
self.prepCluster(ports, createDatabase = False)
sqlite_file = tmp_path('ngas.sqlite')
_, dbObj = self.prepExtSrv(port=8000, cfgFile=cfg_file, cfgProps=cfg_props, sqlite_file=sqlite_file)
self.prepCluster(ports, createDatabase = False, sqlite_file=sqlite_file)
# Set all Disks in unit <Host>:8002 to completed.
dbObj.query2("UPDATE ngas_disks SET completed=1 WHERE host_id={0}", args=("%s:8002" % getHostName(),))
# Set <Host>:8004 to Offline.
Expand Down
24 changes: 14 additions & 10 deletions test/ngamsTestLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ def prepExtSrv(self,
srvModule = None,
force=False,
daemon = False,
cert_file=None):
cert_file=None,
sqlite_file=None):
"""
Prepare a standard server object, which runs as a separate process and
serves via the standard HTTP interface.
Expand Down Expand Up @@ -942,10 +943,16 @@ def prepExtSrv(self,

cfgObj = self.env_aware_cfg(cfgFile)

# Calculate root directory and clear if needed
root_dir = root_dir or tmp_path('NGAS')
if delDirs:
shutil.rmtree(root_dir, True)

# Change what needs to be changed, like the position of the Sqlite
# database file when necessary, the custom configuration items, and the
# port number
self.point_to_sqlite_database(cfgObj, not dbCfgName and clearDb)
sqlite_file = sqlite_file or os.path.join(root_dir, 'ngas.sqlite')
self.point_to_sqlite_database(cfgObj, sqlite_file, create=(not dbCfgName and clearDb))
if (cfgProps):
for cfgProp in cfgProps:
# TODO: Handle Cfg. Group ID.
Expand All @@ -956,10 +963,7 @@ def prepExtSrv(self,

# Now connect to the database and perform any cleanups before we start
# the server, like removing existing NGAS dirs and clearing tables
root_dir = root_dir or tmp_path('NGAS')
dbObj = ngamsDb.from_config(cfgObj, maxpool=1)
if delDirs:
shutil.rmtree(root_dir, True)
if (clearDb):
logger.debug("Clearing NGAS DB ...")
delNgasTbls(dbObj)
Expand Down Expand Up @@ -1422,7 +1426,7 @@ def start_srv_in_cluster(
return [srvId, port, cfg, db]

def prepCluster(self, server_list, cfg_file='src/ngamsCfg.xml', createDatabase=True,
cfg_props=(), cert_file=None):
cfg_props=(), cert_file=None, sqlite_file=None):
"""
Prepare a common, simulated cluster. This consists of 1 to N
servers running on the same node. It is ensured that each of
Expand Down Expand Up @@ -1457,7 +1461,8 @@ def prepCluster(self, server_list, cfg_file='src/ngamsCfg.xml', createDatabase=T

# Create the shared database first of all and generate a new config file
tmpCfg = self.env_aware_cfg(cfg_file)
self.point_to_sqlite_database(tmpCfg, createDatabase)
sqlite_file = sqlite_file or tmp_path('ngas.sqlite')
self.point_to_sqlite_database(tmpCfg, sqlite_file, create=createDatabase)
if createDatabase:
with contextlib.closing(ngamsDb.from_config(tmpCfg, maxpool=1)) as db:
delNgasTbls(db)
Expand All @@ -1480,14 +1485,13 @@ def prepCluster(self, server_list, cfg_file='src/ngamsCfg.xml', createDatabase=T

return d

def point_to_sqlite_database(self, cfgObj, create):
def point_to_sqlite_database(self, cfgObj, sqlite_file, create=True):
# Exceptional handling for SQLite.
if 'sqlite' in cfgObj.getDbInterface().lower():

# sqlite_file = os.path.join(tmp_root, 'ngas.sqlite')
sqlite_file = genTmpFilename(suffix='.sqlite')
if create:
rmFile(sqlite_file)
checkCreatePath(os.path.dirname(sqlite_file))
import sqlite3
fname = 'ngamsCreateTables-SQLite.sql'
script = utils.b2s(pkg_resources.resource_string('ngamsSql', fname)) # @UndefinedVariable
Expand Down
4 changes: 2 additions & 2 deletions test/test_config_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from ngamsLib import ngamsConfig, ngamsDb, ngamsXmlMgr
from .ngamsTestLib import delNgasTbls, ngamsTestSuite, \
save_to_tmp
save_to_tmp, tmp_path


dbIdAttr = 'Db-Test'
Expand Down Expand Up @@ -130,7 +130,7 @@ def loadCfg(self,
revAttr = "NgamsCfg.Header[1].Revision"
cfgObj.storeVal(revAttr, "TEST-REVISION", "ngamsCfg-Test")

self.point_to_sqlite_database(cfgObj, createDatabase)
self.point_to_sqlite_database(cfgObj, tmp_path('ngas.sqlite'), create=createDatabase)
dbObj = ngamsDb.from_config(cfgObj, maxpool=1)
if (delDbTbls): delNgasTbls(dbObj)
cfgObj.writeToDb(dbObj)
Expand Down
4 changes: 2 additions & 2 deletions test/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DbTests(ngamsTestLib.ngamsTestSuite):
def setUp(self):
super(DbTests, self).setUp()
cfg = self.env_aware_cfg()
self.point_to_sqlite_database(cfg, True)
self.point_to_sqlite_database(cfg, ngamsTestLib.tmp_path('ngas.sqlite'))
self.db = ngamsDb.from_config(cfg, maxpool=1)
ngamsTestLib.delNgasTbls(self.db)

Expand All @@ -47,4 +47,4 @@ def test_get_file_info_list_with_wildcards(self):
file_info.setFileId('file-id')
file_info.write('host-id', self.db, genSnapshot=0)
res = list(self.db.getFileInfoList('disk-id', fileId="*"))
self.assertEqual(1, len(res))
self.assertEqual(1, len(res))

0 comments on commit 41a4e03

Please sign in to comment.