Skip to content

Commit

Permalink
Added partner sites unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
smclay committed Sep 22, 2020
1 parent 9512149 commit 3be8318
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/ngamsServer/ngamsServer/ngamsFileUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ def lookup_partner_site_file_status(ngas_server,
# exception.
if request_properties.hasHttpPar("partner_site_redirect"):
error_message = genLog("NGAMS_ER_UNAVAIL_FILE", [file_reference])
logger.debug(error_message)
raise Exception(error_message)

# Check partner sites is enabled are available from the configuration
if not ngas_server.is_partner_sites_proxy_mode()\
or not ngas_server.get_partner_sites_address_list():
error_message = genLog("NGAMS_ER_UNAVAIL_FILE", [file_reference])
logger.debug(error_message)
raise Exception(error_message)

# Lets query the partner sites for the availability of the requested file
Expand Down Expand Up @@ -160,10 +158,9 @@ def lookup_partner_site_file_status(ngas_server,
port = partner_port
break

if status_info is None:
if status_info is None or status_info.getStatus() == "FAILURE":
# Failed to find file on a partner site
error_message = genLog("NGAMS_ER_UNAVAIL_FILE", [file_reference])
logger.debug(error_message)
raise Exception(error_message)

return host, port, status_info, disk_info, file_info
Expand Down
3 changes: 2 additions & 1 deletion test/ngamsTestLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,8 @@ def point_to_sqlite_database(self, cfgObj, create):
# Exceptional handling for SQLite.
if 'sqlite' in cfgObj.getDbInterface().lower():

sqlite_file = os.path.join(tmp_root, 'ngas.sqlite')
# sqlite_file = os.path.join(tmp_root, 'ngas.sqlite')
sqlite_file = genTmpFilename(suffix='.sqlite')
if create:
rmFile(sqlite_file)
import sqlite3
Expand Down
124 changes: 124 additions & 0 deletions test/test_partner_sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#
# ALMA - Atacama Large Millimiter Array
# (c) European Southern Observatory, 2002
# Copyright by ESO (in the framework of the ALMA collaboration),
# All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

"""
This module contains the Test Suite for the partner sites feature.
"""

import contextlib
import functools
import os

from ngamsLib import ngamsHttpUtils, ngamsStatus
from ngamsLib.ngamsCore import getHostName
from .ngamsTestLib import ngamsTestSuite, tmp_path

class NgasPartnerSiteTest(ngamsTestSuite):
"""
Synopsis:
Test the partner site feature.
Description:
NGAS offers a partner site feature for proxy requests to remote NGAS hosts
Missing Test Cases:
"""

def _prepare_partner_site_cluster(self, *original_server_list, **kwargs):
server_list = []
for server_info in original_server_list:
port, property_list = server_info[0], list(server_info[1])
server_list.append((port, property_list))

return self.prepCluster(server_list)

def test_archive_status_retrieve_sequence(self):
host_name = getHostName()
sample_file_name = "SmallFile.fits"
sample_file_path = os.path.join("src", sample_file_name)
# sample_file_size = os.path.getsize(sample_file_path)
sample_mime_type = "application/octet-stream"

# We create two cluster each container two NGAS nodes
# We configure the first cluster to use the second cluster as a partner site
config_list_1 = [("NgamsCfg.Server[1].RootDirectory", "/tmp/ngas1"),
("NgamsCfg.PartnerSites[1].ProxyMode", "1"),
("NgamsCfg.PartnerSites[1].PartnerSite[1].Address", "localhost:9011")]
self._prepare_partner_site_cluster((9001, config_list_1))

config_list_2 = [("NgamsCfg.Server[1].RootDirectory", "/tmp/ngas2")]
self._prepare_partner_site_cluster((9011, config_list_2))

# We archive a test sample file on the partner site cluster
self.archive(9011, sample_file_path, mimeType=sample_mime_type)

# We check the status of a file ID found on the partner site cluster
command_status = functools.partial(ngamsHttpUtils.httpGet, "localhost", 9001, "STATUS", timeout=5)
argument_list = {"file_id": sample_file_name}
with contextlib.closing(command_status(pars=argument_list)) as response:
self.assertEqual(response.status, 200)
# Parse the response XML status information
response_data = response.read()
status_info = ngamsStatus.ngamsStatus().unpackXmlDoc(response_data, 1)
disk_info = status_info.getDiskStatusList()[0]
file_info = disk_info.getFileObjList()[0]
self.assertEqual(disk_info.getHostId(), "{0}:9011".format(host_name))
self.assertEqual(file_info.getFileId(), sample_file_name)
self.assertEqual(file_info.getFileVersion(), 1)
self.assertEqual(file_info.getFormat(), sample_mime_type)

# Retrieve a file found on the partner site cluster
retrieve_file_path = tmp_path(sample_file_name)
self.retrieve(9001, sample_file_name, targetFile=retrieve_file_path)
# retrieve_file_size = os.path.getsize(retrieve_file_path)
# self.assertEqual(sample_file_size, retrieve_file_size)

def test_status_retrieve_sequence(self):
sample_file_name = "SmallFile.fits"
sample_file_path = os.path.join("src", sample_file_name)
bad_file_name = "dummy.fits"

# We create two cluster each container two NGAS nodes
# We configure the first cluster to use the second cluster as a partner site
config_list_1 = [("NgamsCfg.Server[1].RootDirectory", "/tmp/ngas1"),
("NgamsCfg.PartnerSites[1].ProxyMode", "1"),
("NgamsCfg.PartnerSites[1].PartnerSite[1].Address", "localhost:9011")]
self._prepare_partner_site_cluster((9001, config_list_1))

config_list_2 = [("NgamsCfg.Server[1].RootDirectory", "/tmp/ngas2")]
self._prepare_partner_site_cluster((9011, config_list_2))

# We check the status of a file ID found on the partner site cluster
command_status = functools.partial(ngamsHttpUtils.httpGet, "localhost", 9001, "STATUS", timeout=1000)
argument_list = {"file_id": bad_file_name}
with contextlib.closing(command_status(pars=argument_list)) as response:
self.assertEqual(response.status, 400)
# Parse the response XML status information
response_data = response.read()
status_info = ngamsStatus.ngamsStatus().unpackXmlDoc(response_data, 1)
self.assertEqual(status_info.getStatus(), "FAILURE")
self.assertEqual(status_info.getMessage(),
"NGAMS_ER_UNAVAIL_FILE:4019:ERROR: File with ID: dummy.fits appears not to be available.")

# Retrieve a file found on the partner site cluster
retrieve_file_path = tmp_path(bad_file_name)
self.retrieve(9001, bad_file_name, targetFile=retrieve_file_path, expectedStatus='FAILURE')

0 comments on commit 3be8318

Please sign in to comment.