Fix bug 1263 02/9902/1
authorDavid Garcia <david.garcia@canonical.com>
Thu, 22 Oct 2020 08:50:56 +0000 (10:50 +0200)
committerDavid Garcia <david.garcia@canonical.com>
Thu, 22 Oct 2020 08:56:24 +0000 (10:56 +0200)
Update database endpoints when a new VCA_HOST_IP is provided

Before, we were not considering the that the VCA_HOST_IP could be
updated, in that case, the old ips stored in the database should be
replace by the new one. Of course, this won't do any model migration
from the previous VCA to the new one. It will only start pointing to
the new VCA provided.

Change-Id: I5e259f773f90e4f440d87c4b6342cd82425c0e0c
Signed-off-by: David Garcia <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/tests/unit/test_libjuju.py
n2vc/tests/unit/test_n2vc_juju_conn.py

index 8d1fdad..4702414 100644 (file)
@@ -81,9 +81,12 @@ class Libjuju:
         self.log = log or logging.getLogger("Libjuju")
         self.db = db
         db_endpoints = self._get_api_endpoints_db()
         self.log = log or logging.getLogger("Libjuju")
         self.db = db
         db_endpoints = self._get_api_endpoints_db()
-        self.endpoints = db_endpoints or [endpoint]
-        if db_endpoints is None:
+        self.endpoints = None
+        if (db_endpoints and endpoint not in db_endpoints) or not db_endpoints:
+            self.endpoints = [endpoint]
             self._update_api_endpoints_db(self.endpoints)
             self._update_api_endpoints_db(self.endpoints)
+        else:
+            self.endpoints = db_endpoints
         self.api_proxy = api_proxy
         self.username = username
         self.password = password
         self.api_proxy = api_proxy
         self.username = username
         self.password = password
@@ -104,7 +107,10 @@ class Libjuju:
         self.models = set()
         self.log.debug("Libjuju initialized!")
 
         self.models = set()
         self.log.debug("Libjuju initialized!")
 
-        self.health_check_task = self.loop.create_task(self.health_check())
+        self.health_check_task = self._create_health_check_task()
+
+    def _create_health_check_task(self):
+        return self.loop.create_task(self.health_check())
 
     async def get_controller(self, timeout: float = 5.0) -> Controller:
         """
 
     async def get_controller(self, timeout: float = 5.0) -> Controller:
         """
index a384e44..76bbebe 100644 (file)
@@ -73,6 +73,91 @@ class LibjujuTestCase(asynctest.TestCase):
         loop.run_until_complete(self.libjuju.disconnect())
 
 
         loop.run_until_complete(self.libjuju.disconnect())
 
 
+@asynctest.mock.patch("n2vc.libjuju.Libjuju._create_health_check_task")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju._update_api_endpoints_db")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju._get_api_endpoints_db")
+class LibjujuInitTestCase(asynctest.TestCase):
+    def setUp(self):
+        self.loop = asyncio.get_event_loop()
+        self.n2vc = FakeN2VC()
+        self.endpoint = "192.168.100.100:17070"
+        self.username = "admin"
+        self.password = "secret"
+        self.cacert = """
+    -----BEGIN CERTIFICATE-----
+    SOMECERT
+    -----END CERTIFICATE-----"""
+
+    def test_endpoint_not_in_db(
+        self,
+        mock__get_api_endpoints_db,
+        mock_update_endpoints,
+        mock_create_health_check_task,
+    ):
+        mock__get_api_endpoints_db.return_value = ["another_ip"]
+        Libjuju(
+            self.endpoint,
+            "192.168.0.155:17070",
+            self.username,
+            self.password,
+            self.cacert,
+            self.loop,
+            log=None,
+            db={"get_one": []},
+            n2vc=self.n2vc,
+            apt_mirror="192.168.0.100",
+            enable_os_upgrade=True,
+        )
+        mock_update_endpoints.assert_called_once_with([self.endpoint])
+        mock__get_api_endpoints_db.assert_called_once()
+
+    def test_endpoint_in_db(
+        self,
+        mock__get_api_endpoints_db,
+        mock_update_endpoints,
+        mock_create_health_check_task,
+    ):
+        mock__get_api_endpoints_db.return_value = [self.endpoint, "another_ip"]
+        Libjuju(
+            self.endpoint,
+            "192.168.0.155:17070",
+            self.username,
+            self.password,
+            self.cacert,
+            self.loop,
+            log=None,
+            db={"get_one": []},
+            n2vc=self.n2vc,
+            apt_mirror="192.168.0.100",
+            enable_os_upgrade=True,
+        )
+        mock_update_endpoints.assert_not_called()
+        mock__get_api_endpoints_db.assert_called_once()
+
+    def test_no_db_endpoints(
+        self,
+        mock__get_api_endpoints_db,
+        mock_update_endpoints,
+        mock_create_health_check_task,
+    ):
+        mock__get_api_endpoints_db.return_value = None
+        Libjuju(
+            self.endpoint,
+            "192.168.0.155:17070",
+            self.username,
+            self.password,
+            self.cacert,
+            self.loop,
+            log=None,
+            db={"get_one": []},
+            n2vc=self.n2vc,
+            apt_mirror="192.168.0.100",
+            enable_os_upgrade=True,
+        )
+        mock_update_endpoints.assert_called_once_with([self.endpoint])
+        mock__get_api_endpoints_db.assert_called_once()
+
+
 @asynctest.mock.patch("juju.controller.Controller.connect")
 @asynctest.mock.patch(
     "juju.controller.Controller.api_endpoints",
 @asynctest.mock.patch("juju.controller.Controller.connect")
 @asynctest.mock.patch(
     "juju.controller.Controller.api_endpoints",
index 4044637..663f2ba 100644 (file)
@@ -26,6 +26,7 @@ from n2vc.exceptions import (
 
 
 class N2VCJujuConnTestCase(asynctest.TestCase):
 
 
 class N2VCJujuConnTestCase(asynctest.TestCase):
+    @asynctest.mock.patch("n2vc.libjuju.Libjuju._create_health_check_task")
     @asynctest.mock.patch("juju.controller.Controller.update_endpoints")
     @asynctest.mock.patch("juju.client.connector.Connector.connect")
     @asynctest.mock.patch("juju.controller.Controller.connection")
     @asynctest.mock.patch("juju.controller.Controller.update_endpoints")
     @asynctest.mock.patch("juju.client.connector.Connector.connect")
     @asynctest.mock.patch("juju.controller.Controller.connection")
@@ -36,7 +37,9 @@ class N2VCJujuConnTestCase(asynctest.TestCase):
         mock_connection=None,
         mock_connect=None,
         mock_update_endpoints=None,
         mock_connection=None,
         mock_connect=None,
         mock_update_endpoints=None,
+        mock__create_health_check_task=None,
     ):
     ):
+        mock__get_api_endpoints_db.return_value = ["2.2.2.2:17070"]
         loop = asyncio.get_event_loop()
         db = {}
         vca_config = {
         loop = asyncio.get_event_loop()
         db = {}
         vca_config = {