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)
+ else:
+ self.endpoints = db_endpoints
self.api_proxy = api_proxy
self.username = username
self.password = password
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:
"""
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",
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")
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 = {