# DEBUG WITH PDB
-import os
import pdb
import asyncio
import logging.handlers
import getopt
import sys
+from random import SystemRandom
from osm_lcm import ns, vim_sdn, netslice
from osm_lcm.ng_ro import NgRoException, NgRoClient
from osm_lcm.data_utils.filesystem.filesystem import Filesystem
from osm_lcm.data_utils.lcm_config import LcmCfg
from osm_lcm.lcm_hc import get_health_check_file
-from os import path
-from random import choice as random_choice
+from os import path, getenv
from n2vc import version as n2vc_version
import traceback
-if os.getenv("OSMLCM_PDB_DEBUG", None) is not None:
+if getenv("OSMLCM_PDB_DEBUG", None) is not None:
pdb.set_trace()
will provide a random one
:return: Obtained ID
"""
- # Try getting docker id. If fails, get pid
- try:
- with open("/proc/self/cgroup", "r") as f:
- text_id_ = f.readline()
- _, _, text_id = text_id_.rpartition("/")
- text_id = text_id.replace("\n", "")[:12]
- if text_id:
- return text_id
- except Exception:
- pass
- # Return a random id
- return "".join(random_choice("0123456789abcdef") for _ in range(12))
+
+ def get_docker_id():
+ try:
+ with open("/proc/self/cgroup", "r") as f:
+ text_id_ = f.readline()
+ _, _, text_id = text_id_.rpartition("/")
+ return text_id.replace("\n", "")[:12]
+ except Exception:
+ return None
+
+ def generate_random_id():
+ return "".join(SystemRandom().choice("0123456789abcdef") for _ in range(12))
+
+ # Try getting docker id. If it fails, generate a random id
+ docker_id = get_docker_id()
+ return docker_id if docker_id else generate_random_id()
def usage():
from osm_lcm.lcm_hc import health_check
health_check(config_file, Lcm.ping_interval_pace)
- # elif o == "--log-socket-port":
- # log_socket_port = a
- # elif o == "--log-socket-host":
- # log_socket_host = a
- # elif o == "--log-file":
- # log_file = a
else:
- assert False, "Unhandled option"
+ print(f"Unhandled option: {o}")
+ exit(1)
if config_file:
if not path.isfile(config_file):
charm = tmpfile
hexdigest = self.hexdigest
mock_file_hash = MagicMock()
- mock_hashlib.md5.return_value = mock_file_hash
+ mock_hashlib.sha256.return_value = mock_file_hash
mock_file_hash.hexdigest.return_value = hexdigest
result = LcmBase.calculate_charm_hash(charm)
self.assertEqual(result, hexdigest)
self.assertEqual(mocking_open.call_count, 1)
self.assertEqual(mock_file_hash.update.call_count, 1)
self.assertEqual(mock_file_hash.hexdigest.call_count, 1)
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
@patch("builtins.open", new_callable=mock_open(read_data="charm content"))
@patch("osm_lcm.lcm_utils.hashlib")
charm = tmpfile
hexdigest = self.hexdigest
mock_file_hash = MagicMock()
- mock_hashlib.md5.return_value = mock_file_hash
+ mock_hashlib.sha256.return_value = mock_file_hash
mock_file_hash.hexdigest.return_value = hexdigest
mocking_open.side_effect = IOError
with self.assertRaises(IOError):
self.assertEqual(mocking_open.call_count, 1)
mock_file_hash.update.assert_not_called()
mock_file_hash.hexdigest.assert_not_called()
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
@patch("builtins.open", new_callable=mock_open(read_data="charm content"))
@patch("osm_lcm.lcm_utils.hashlib")
hexdigest = self.hexdigest
mock_file_hash = MagicMock()
mock_file_hash.update.side_effect = Exception
- mock_hashlib.md5.return_value = mock_file_hash
+ mock_hashlib.sha256.return_value = mock_file_hash
mock_file_hash.hexdigest.return_value = hexdigest
with self.assertRaises(Exception):
LcmBase.calculate_charm_hash(charm)
self.assertEqual(mocking_open.call_count, 1)
self.assertEqual(mock_file_hash.update.call_count, 1)
mock_file_hash.hexdigest.assert_not_called()
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
@patch("builtins.open", new_callable=mock_open(read_data="charm content"))
@patch("osm_lcm.lcm_utils.hashlib")
"""Filehash hexdigest raises exception."""
charm = tmpfile
mock_file_hash = MagicMock()
- mock_hashlib.md5.return_value = mock_file_hash
+ mock_hashlib.sha256.return_value = mock_file_hash
mock_file_hash.hexdigest.side_effect = Exception
with self.assertRaises(Exception):
LcmBase.calculate_charm_hash(charm)
self.assertEqual(mocking_open.call_count, 1)
self.assertEqual(mock_file_hash.update.call_count, 1)
mock_file_hash.hexdigest.assert_called_once()
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
mock_file_hash.update.assert_called_once()
@patch("builtins.open", new_callable=mock_open(read_data="charm content"))
@patch("osm_lcm.lcm_utils.hashlib")
- def test_calculate_charm_filehash_hashlib_md5_raises(
+ def test_calculate_charm_filehash_hashlib_sha256_raises(
self, mock_hashlib, mocking_open
):
- """Filehash hashlib md5 raises exception."""
+ """Filehash hashlib sha256 raises exception."""
charm = tmpfile
- mock_hashlib.md5.side_effect = Exception
+ mock_hashlib.sha256.side_effect = Exception
with self.assertRaises(Exception):
LcmBase.calculate_charm_hash(charm)
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
mocking_open.assert_not_called()
@patch("builtins.open", new_callable=mock_open(read_data="charm content"))
"""Calculate charm hash, charm file does not exist."""
file = None
mock_file_hash = MagicMock()
- mock_hashlib.md5.return_value = mock_file_hash
+ mock_hashlib.sha256.return_value = mock_file_hash
mocking_open.side_effect = FileNotFoundError
with self.assertRaises(FileNotFoundError):
LcmBase.calculate_charm_hash(file)
self.assertEqual(mocking_open.call_count, 1)
mock_file_hash.update.assert_not_called()
mock_file_hash.hexdigest.assert_not_called()
- self.assertEqual(mock_hashlib.md5.call_count, 1)
+ self.assertEqual(mock_hashlib.sha256.call_count, 1)
@patch("osm_lcm.lcm_utils.LcmBase.calculate_charm_hash")
def test_compare_charm_hash_charm_changed(self, mock_calculate_charm_hash):