From 4809ea985869b39636dfdaf4e556caecc5cabd54 Mon Sep 17 00:00:00 2001 From: Pedro Escaleira Date: Mon, 21 Mar 2022 17:54:45 +0000 Subject: [PATCH] Bug 1938 fixed: : added a random suffix to the end of the Juju app name, in order to allow multiple Juju charms per VDU I followed the second option of the bug's description. Now, Juju applications have a random suffix with size=5 (the random suffix size used by K8s), in order to avoid collisions between applications' names; Also fixed unit-test on test_n2vc_juju_conn.py Change-Id: I53195113de8a3c4f04792742001e3d2a356b4dfd Signed-off-by: Pedro Escaleira --- n2vc/n2vc_juju_conn.py | 10 ++++++++-- n2vc/tests/unit/test_n2vc_juju_conn.py | 10 ++++++++-- n2vc/utils.py | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index c01c436..3f6a501 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -38,6 +38,7 @@ from n2vc.n2vc_conn import N2VCConnector from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml from n2vc.libjuju import Libjuju from n2vc.store import MotorStore +from n2vc.utils import generate_random_alfanum_string from n2vc.vca.connection import get_connection from retrying_async import retry @@ -1167,7 +1168,7 @@ class N2VCJujuConnector(N2VCConnector): """ Build application name from namespace :param namespace: - :return: app-vnf--vdu--cnt- + :return: app-vnf--vdu--cnt-- """ # TODO: Enforce the Juju 50-character application limit @@ -1194,7 +1195,12 @@ class N2VCJujuConnector(N2VCConnector): else: vdu_count = "-cnt-" + vdu_count - application_name = "app-{}{}{}".format(vnf_id, vdu_id, vdu_count) + # Generate a random suffix with 5 characters (the default size used by K8s) + random_suffix = generate_random_alfanum_string(size=5) + + application_name = "app-{}{}{}-{}".format( + vnf_id, vdu_id, vdu_count, random_suffix + ) return N2VCJujuConnector._format_app_name(application_name) diff --git a/n2vc/tests/unit/test_n2vc_juju_conn.py b/n2vc/tests/unit/test_n2vc_juju_conn.py index d89de3f..9365106 100644 --- a/n2vc/tests/unit/test_n2vc_juju_conn.py +++ b/n2vc/tests/unit/test_n2vc_juju_conn.py @@ -16,6 +16,7 @@ import asyncio import logging from unittest.mock import Mock +from unittest.mock import patch import asynctest @@ -143,8 +144,13 @@ class K8sProxyCharmsTest(N2VCJujuConnTestCase): self.n2vc.libjuju.deploy_charm = AsyncMock() self.n2vc.libjuju.model_exists.return_value = False + @patch( + "n2vc.n2vc_juju_conn.generate_random_alfanum_string", + **{"return_value": "random"} + ) def test_success( self, + mock_generate_random_alfanum_string, mock_path, mock_file_exists, ): @@ -162,7 +168,7 @@ class K8sProxyCharmsTest(N2VCJujuConnTestCase): self.n2vc.libjuju.add_model.assert_called_once() self.n2vc.libjuju.deploy_charm.assert_called_once_with( model_name="ns-id-k8s", - application_name="app-vnf-vnf-id-vdu-vdu", + application_name="app-vnf-vnf-id-vdu-vdu-random", path="/path/path/", machine_id=None, db_dict={}, @@ -170,7 +176,7 @@ class K8sProxyCharmsTest(N2VCJujuConnTestCase): total_timeout=None, config=None, ) - self.assertEqual(ee_id, "ns-id-k8s.app-vnf-vnf-id-vdu-vdu.k8s") + self.assertEqual(ee_id, "ns-id-k8s.app-vnf-vnf-id-vdu-vdu-random.k8s") def test_no_artifact_path( self, diff --git a/n2vc/utils.py b/n2vc/utils.py index 0dbd71e..5ac0e2c 100644 --- a/n2vc/utils.py +++ b/n2vc/utils.py @@ -16,6 +16,8 @@ import base64 import re import binascii import yaml +import string +import secrets from enum import Enum from juju.machine import Machine from juju.application import Application @@ -147,3 +149,15 @@ def obj_to_dict(obj: object) -> dict: yaml_text = obj_to_yaml(obj) # parse to dict return yaml.load(yaml_text, Loader=yaml.Loader) + + +def generate_random_alfanum_string(size: int) -> str: + """ + Generate random alfa-numeric string with a size given by argument + :param size: + :return: random generated string + """ + + return "".join( + secrets.choice(string.ascii_letters + string.digits) for i in range(size) + ) -- 2.25.1