from copy import deepcopy
from http import HTTPStatus
import logging
-from os import mkdir
+from os import makedirs
+from os import path
import queue
from shutil import rmtree
import threading
from importlib_metadata import entry_points
from osm_common.dbbase import DbException
from osm_ng_ro.vim_admin import LockRenew
-from osm_ro_plugin import sdnconn, vimconn
+from osm_ro_plugin import sdnconn
+from osm_ro_plugin import vimconn
from osm_ro_plugin.sdn_dummy import SdnDummyConnector
from osm_ro_plugin.vim_dummy import VimDummyConnector
import yaml
self.task_lock.release()
return False
- def _process_vim_config(self, target_id, db_vim):
+ def _process_vim_config(self, target_id: str, db_vim: dict) -> None:
"""
Process vim config, creating vim configuration files as ca_cert
:param target_id: vim/sdn/wim + id
return
file_name = ""
+ work_dir = "/app/osm_ro/certs"
try:
if db_vim["config"].get("ca_cert_content"):
- file_name = "{}:{}".format(target_id, self.worker_index)
+ file_name = f"{work_dir}/{target_id}:{self.worker_index}"
- try:
- mkdir(file_name)
- except FileExistsError:
- self.logger.exception(
- "FileExistsError occured while processing vim_config."
- )
+ if not path.isdir(file_name):
+ makedirs(file_name)
file_name = file_name + "/ca_cert"
import logging
import unittest
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, Mock, mock_open, patch
from osm_common.dbmemory import DbMemory
from osm_ng_ro.ns_thread import (
ConfigValidate,
NsWorker,
+ NsWorkerException,
VimInteractionAffinityGroup,
VimInteractionMigration,
VimInteractionNet,
len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count
)
+ @patch("osm_ng_ro.ns_thread.makedirs", return_value="")
+ def test_create_file_cert(self, mock_makedirs):
+ vim_config = {"config": {"ca_cert_content": "test"}}
+ target_id = "1234"
+ db = Mock()
+
+ with patch("builtins.open", mock_open()) as mocked_file:
+ nsw = NsWorker(self.worker_index, self.config, self.plugins, db)
+ nsw._process_vim_config(target_id, vim_config)
+ mocked_file.assert_called_once_with(
+ f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w"
+ )
+ assert (
+ vim_config["config"]["ca_cert"]
+ == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert"
+ )
+
+ @patch("osm_ng_ro.ns_thread.makedirs")
+ @patch("osm_ng_ro.ns_thread.path")
+ def test_create_file_cert_exists(self, mock_path, mock_makedirs):
+ vim_config = {"config": {"ca_cert_content": "test"}}
+ target_id = "1234"
+ db = Mock()
+ mock_path.isdir.return_value = True
+
+ with patch("builtins.open", mock_open()) as mocked_file:
+ nsw = NsWorker(self.worker_index, self.config, self.plugins, db)
+ nsw._process_vim_config(target_id, vim_config)
+ mock_makedirs.assert_not_called()
+ mocked_file.assert_called_once_with(
+ f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w"
+ )
+ assert (
+ vim_config["config"]["ca_cert"]
+ == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert"
+ )
+
+ @patch("osm_ng_ro.ns_thread.path")
+ @patch("osm_ng_ro.ns_thread.makedirs", side_effect=Exception)
+ def test_create_file_cert_makedirs_except(self, mock_makedirs, mock_path):
+ vim_config = {"config": {"ca_cert_content": "test"}}
+ target_id = "1234"
+ db = Mock()
+ mock_path.isdir.return_value = False
+
+ with patch("builtins.open", mock_open()) as mocked_file:
+ nsw = NsWorker(self.worker_index, self.config, self.plugins, db)
+ with self.assertRaises(NsWorkerException):
+ nsw._process_vim_config(target_id, vim_config)
+ mocked_file.assert_not_called()
+ assert vim_config["config"]["ca_cert_content"] == "test"
+
+ @patch("osm_ng_ro.ns_thread.makedirs", return_value="")
+ def test_create_file_cert_open_excepts(self, mock_makedirs):
+ vim_config = {"config": {"ca_cert_content": "test"}}
+ target_id = "1234"
+ db = Mock()
+
+ with patch("builtins.open", mock_open()) as mocked_file:
+ mocked_file.side_effect = Exception
+ nsw = NsWorker(self.worker_index, self.config, self.plugins, db)
+ with self.assertRaises(NsWorkerException):
+ nsw._process_vim_config(target_id, vim_config)
+ mocked_file.assert_called_once_with(
+ f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w"
+ )
+ assert vim_config["config"]["ca_cert_content"] == "test"
+
class TestVimInteractionNet(unittest.TestCase):
def setUp(self):
--- /dev/null
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+---
+features:
+ - |
+ Feature 10946 - Add VIM certificates to OSM DB
+ RO and MON will get the VIM certificates from the MongoDB database,
+ instead of depending on the certificates which are manually created.