Skip to content

Commit

Permalink
Merge pull request #558 from manali-deshmukh/vcf-issue
Browse files Browse the repository at this point in the history
Fix for application multi startup issue and subscription file path
  • Loading branch information
Vijayakannan-M authored May 21, 2019
2 parents 2efffe6 + 4ee6304 commit 7a50395
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 13 deletions.
3 changes: 1 addition & 2 deletions oneview_redfish_toolkit/api/scmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def _scmb_base_dir():


def _scmb_oneview_dir(ov_ip):
ov_certs_dir = os.path.basename(_scmb_base_dir())
return os.path.join(ov_certs_dir, ov_ip)
return os.path.join(_scmb_base_dir(), ov_ip)


def _oneview_ca_path(ov_ip):
Expand Down
41 changes: 35 additions & 6 deletions oneview_redfish_toolkit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# under the License.

# Python libs
import appdirs
import argparse
import ipaddress
import logging
import os
import sys
import time

# 3rd party libs
Expand Down Expand Up @@ -111,6 +113,7 @@


PID_FILE_NAME = 'toolkit.pid'
CFG_DIR_NAME = 'oneview-redfish-toolkit'


def main(config_file_path, logging_config_file_path,
Expand Down Expand Up @@ -388,12 +391,38 @@ def oneview_redfish_exception(exception):
app.run(host=host, port=port, debug=is_debug_mode,
ssl_context=ssl_context)
else:
start_cherrypy(app,
host=host,
port=port,
ssl_cert_file=ssl_cert_file,
ssl_key_file=ssl_key_file,
is_dev_env=is_dev_env)
is_service_up = check_if_pid_exists()
if is_service_up:
print('Application is already running')
sys.exit(1)
else:
start_cherrypy(app,
host=host,
port=port,
ssl_cert_file=ssl_cert_file,
ssl_key_file=ssl_key_file,
is_dev_env=is_dev_env)


def check_if_pid_exists():
is_pid_alive = False
cfg_dir = appdirs.user_config_dir(CFG_DIR_NAME)
pid_file_path = os.path.join(cfg_dir, PID_FILE_NAME)
if not os.path.isfile(pid_file_path):
is_pid_alive = False
else:
pid_file = open(pid_file_path, 'r')
pid = pid_file.read()
# Check if process is already running or not
try:
# Sending signal 0 to a pid will raise an OSError exception
# if the pid is not running.
os.kill(int(pid), 0)
except OSError:
is_pid_alive = False
else:
is_pid_alive = True
return is_pid_alive


def start_cherrypy(app,
Expand Down
6 changes: 4 additions & 2 deletions oneview_redfish_toolkit/blueprints/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.


import inspect
import json
import logging
import os
Expand All @@ -41,7 +41,9 @@


def _all_subscription_file():
base_dir = os.path.basename(REDFISH_TOOLKIT_BASE_DIR)
base_dir = os.path.abspath(os.path.join(
os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
logging.info(base_dir)
return os.path.join(base_dir, ALL_SUBSCRIPTION_FILE)


Expand Down
15 changes: 15 additions & 0 deletions oneview_redfish_toolkit/tests/api/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ def test_serialize(self):
self.fail("Failed to serialize. Error: {}".format(e))

self.assertEqualMockup(self.event_mockup, result)

def test_build_event_from_task(self):

task = self.alert
task["resource"]["category"] = "task"
task["resourceUri"] = \
"/rest/server-hardware/30373737-3237-4D32-3230-313530314752"
task["resource"]["name"] = "0000A66101, bay 3"
event = Event(self.alert)

result = json.loads(event.serialize())

event_mockup = self.event_mockup
event_mockup["Events"][0]["EventType"] = "ResourceAdded"
self.assertEqualMockup(self.event_mockup, result)
49 changes: 46 additions & 3 deletions oneview_redfish_toolkit/tests/api/test_scmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from hpOneView.exceptions import HPOneViewException

# Own libs
from oneview_redfish_toolkit.api.errors import OneViewRedfishException
from oneview_redfish_toolkit.api import scmb
from oneview_redfish_toolkit.api.scmb import SCMB
from oneview_redfish_toolkit import client_session
Expand Down Expand Up @@ -67,11 +68,11 @@ def test_paths_generated_for_scmb_files(self, config_mock):
}
}
ov_ip = '1.1.1.1'
self.assertEqual('scmb/1.1.1.1/oneview_ca.pem',
self.assertEqual('/dir/scmb/1.1.1.1/oneview_ca.pem',
scmb._oneview_ca_path(ov_ip))
self.assertEqual('scmb/1.1.1.1/oneview_scmb.pem',
self.assertEqual('/dir/scmb/1.1.1.1/oneview_scmb.pem',
scmb._scmb_cert_path(ov_ip))
self.assertEqual('scmb/1.1.1.1/oneview_scmb.key',
self.assertEqual('/dir/scmb/1.1.1.1/oneview_scmb.key',
scmb._scmb_key_path(ov_ip))

@mock.patch.object(scmb, 'config')
Expand Down Expand Up @@ -214,7 +215,11 @@ def test_generate_new_cert_for_oneview(self,
@mock.patch.object(scmb, 'config')
@mock.patch.object(client_session, 'get_oneview_client')
@mock.patch.object(SCMB, '_get_ov_ca_cert_base64data')
@mock.patch.object(scmb, 'ResourceClient')
@mock.patch.object(SCMB, 'scmb_connect')
def test_get_oneview_cert_unexpected_error(self,
scmb_connect,
resource_client,
_get_ov_ca_cert_base64data,
get_oneview_client,
config_mock):
Expand Down Expand Up @@ -248,6 +253,44 @@ def test_get_oneview_cert_unexpected_error(self,
self.assertEqual(hp_ov_exception_msg, test_exception.msg)
self.assertEqual(e.oneview_response, test_exception.oneview_response)

scmb_connect.side_effect = e

scmb_thread._listen_scmb()

# test get certificate exception
_get_ov_ca_cert_base64data.return_value = None

with self.assertRaises(OneViewRedfishException) as redfish_exception:
scmb_thread.get_scmb_certs()

test_exception = redfish_exception.exception.msg
self.assertEqual(test_exception,
"Failed to fetch OneView CA Certificate")

e = HPOneViewException({
'errorCode': 'RABBITMQ_CLIENTCERT_CONFLICT',
'message': 'RABBITMQ_CLIENTCERT_CONFLICT',
})
oneview_client.certificate_rabbitmq.generate.side_effect = e
scmb_thread._generate_certificate_in_oneview(oneview_client)

resource_client.get.return_value = "Cert"

scmb_thread._get_ov_ca_cert(oneview_client)

# test certificate generation exception
e = HPOneViewException({
'errorCode': 'NOT_FOUND',
'message': 'NOT_FOUND',
})
oneview_client.certificate_rabbitmq.generate.side_effect = e
with self.assertRaises(HPOneViewException) as hp_exception:
scmb_thread._generate_certificate_in_oneview(oneview_client)

test_exception = hp_exception.exception.msg
self.assertEqual(test_exception,
"NOT_FOUND")

@mock.patch('pika.channel.Channel')
@mock.patch('pika.BlockingConnection')
@mock.patch('pika.ConnectionParameters')
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package-data =
oneview_redfish_toolkit = conf/*.conf
oneview_redfish_toolkit = registry/*.json
oneview_redfish_toolkit = schemas/*.json
oneview_redfish_toolkit = all_subscription.json


[build_sphinx]
Expand Down

0 comments on commit 7a50395

Please sign in to comment.