from juju.model import Model
from juju.machine import Machine
from juju.application import Application
-from juju.client._definitions import FullStatus, QueryApplicationOffersResults
+from juju.client._definitions import (
+ FullStatus,
+ QueryApplicationOffersResults,
+ Cloud,
+ CloudCredential,
+)
from n2vc.juju_watcher import JujuModelWatcher
from n2vc.provisioner import AsyncSSHProvisioner
from n2vc.n2vc_conn import N2VCConnector
return await controller.list_offers(model_name)
finally:
await self.disconnect_controller(controller)
+
+ async def add_k8s(self, name: str, auth_data: dict, storage_class: str):
+ """
+ Add a Kubernetes cloud to the controller
+
+ Similar to the `juju add-k8s` command in the CLI
+
+ :param: name: Name for the K8s cloud
+ :param: auth_data: Dictionary with needed credentials. Format:
+ {
+ "server": "192.168.0.21:16443",
+ "cacert": "-----BEGIN CERTIFI...",
+ "token": "clhkRExRem5Xd1dCdnFEVXdvRGt...",
+
+ }
+ :param: storage_class: Storage Class to use in the cloud
+ """
+
+ required_auth_data_keys = ["server", "cacert", "token"]
+ missing_keys = []
+ for k in required_auth_data_keys:
+ if k not in auth_data:
+ missing_keys.append(k)
+ if missing_keys:
+ raise Exception(
+ "missing keys in auth_data: {}".format(",".join(missing_keys))
+ )
+ if not storage_class:
+ raise Exception("storage_class must be a non-empty string")
+ if not name:
+ raise Exception("name must be a non-empty string")
+
+ endpoint = auth_data["server"]
+ cacert = auth_data["cacert"]
+ token = auth_data["token"]
+ region_name = "{}-region".format(name)
+
+ cloud = client.Cloud(
+ auth_types=["certificate"],
+ ca_certificates=[cacert],
+ endpoint=endpoint,
+ config={
+ "operator-storage": storage_class,
+ "workload-storage": storage_class,
+ },
+ regions=[client.CloudRegion(endpoint=endpoint, name=region_name)],
+ type_="kubernetes",
+ )
+
+ cred = client.CloudCredential(
+ auth_type="certificate",
+ attrs={"ClientCertificateData": cacert, "Token": token},
+ )
+ return await self.add_cloud(name, cloud, cred)
+
+ async def add_cloud(
+ self, name: str, cloud: Cloud, credential: CloudCredential = None
+ ) -> Cloud:
+ """
+ Add cloud to the controller
+
+ :param: name: Name of the cloud to be added
+ :param: cloud: Cloud object
+ :param: credential: CloudCredentials object for the cloud
+ """
+ controller = await self.get_controller()
+ try:
+ _ = await controller.add_cloud(name, cloud)
+ if credential:
+ await controller.add_credential(name, credential=credential, cloud=name)
+ # Need to return the object returned by the controller.add_cloud() function
+ # I'm returning the original value now until this bug is fixed:
+ # https://github.com/juju/python-libjuju/issues/443
+ return cloud
+ finally:
+ await self.disconnect_controller(controller)
+
+ async def remove_cloud(self, name: str):
+ """
+ Remove cloud
+
+ :param: name: Name of the cloud to be removed
+ """
+ controller = await self.get_controller()
+ try:
+ await controller.remove_cloud(name)
+ finally:
+ await self.disconnect_controller(controller)
):
self.libjuju.endpoints = []
mock__update_api_endpoints_db.side_effect = Exception()
+ controller = None
with self.assertRaises(JujuControllerFailedConnecting):
controller = self.loop.run_until_complete(self.libjuju.get_controller())
- self.assertIsNone(controller)
- mock_disconnect_controller.assert_called_once()
+ self.assertIsNone(controller)
+ mock_disconnect_controller.assert_called_once()
def test_same_endpoint_get_controller(
self, mock__update_api_endpoints_db, mock_api_endpoints, mock_connect
self.libjuju.add_model("existing_model", "cloud")
)
- mock_disconnect_controller.assert_called()
+ mock_disconnect_controller.assert_called()
# TODO Check two job executing at the same time and one returning without doing anything.
self.assertEqual(status, {"status"})
- def test_excpetion(
+ def test_exception(
self,
mock_get_status,
mock_disconnect_controller,
):
mock_get_model.return_value = juju.model.Model()
mock_get_status.side_effect = Exception()
-
+ status = None
with self.assertRaises(Exception):
status = self.loop.run_until_complete(
self.libjuju.get_model_status("model")
)
- mock_disconnect_controller.assert_called_once()
- mock_disconnect_model.assert_called_once()
+ mock_disconnect_controller.assert_called_once()
+ mock_disconnect_model.assert_called_once()
- self.assertIsNone(status)
+ self.assertIsNone(status)
@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
mock_get_model,
mock_get_controller,
):
+ machine = None
+ bool_res = None
mock_get_model.return_value = juju.model.Model()
with self.assertRaises(JujuMachineNotFound):
machine, bool_res = self.loop.run_until_complete(
self.libjuju.create_machine("model", "non_existing_machine")
)
- self.assertIsNone(machine)
- self.assertIsNone(bool_res)
+ self.assertIsNone(machine)
+ self.assertIsNone(bool_res)
- mock_disconnect_controller.assert_called()
- mock_disconnect_model.assert_called()
+ mock_disconnect_controller.assert_called()
+ mock_disconnect_model.assert_called()
def test_no_machine(
self,
mock_get_model.return_value = juju.model.Model()
mock_applications.return_value = {"existing_app"}
+ application = None
with self.assertRaises(JujuApplicationExists):
application = self.loop.run_until_complete(
self.libjuju.deploy_charm("existing_app", "path", "model", "machine",)
)
- self.assertIsNone(application)
+ self.assertIsNone(application)
- mock_disconnect_controller.assert_called()
- mock_disconnect_model.assert_called()
+ mock_disconnect_controller.assert_called()
+ mock_disconnect_model.assert_called()
def test_non_existing_machine(
self,
):
mock_get_model.return_value = juju.model.Model()
mock_machines.return_value = {"existing_machine": FakeMachine()}
+ application = None
with self.assertRaises(JujuMachineNotFound):
application = self.loop.run_until_complete(
self.libjuju.deploy_charm("app", "path", "model", "machine",)
)
- self.assertIsNone(application)
+ self.assertIsNone(application)
- mock_disconnect_controller.assert_called()
- mock_disconnect_model.assert_called()
+ mock_disconnect_controller.assert_called()
+ mock_disconnect_model.assert_called()
def test_2_units(
self,
):
mock__get_application.return_value = None
mock_get_model.return_value = juju.model.Model()
-
+ output = None
+ status = None
with self.assertRaises(JujuApplicationNotFound):
output, status = self.loop.run_until_complete(
self.libjuju.execute_action("app", "model", "action",)
)
- self.assertIsNone(output)
- self.assertIsNone(status)
+ self.assertIsNone(output)
+ self.assertIsNone(status)
- mock_disconnect_controller.assert_called()
- mock_disconnect_model.assert_called()
+ mock_disconnect_controller.assert_called()
+ mock_disconnect_model.assert_called()
def test_no_action(
self,
mock_get_model.return_value = juju.model.Model()
mock__get_application.return_value = FakeApplication()
+ output = None
+ status = None
with self.assertRaises(JujuActionNotFound):
output, status = self.loop.run_until_complete(
self.libjuju.execute_action("app", "model", "action",)
)
- self.assertIsNone(output)
- self.assertIsNone(status)
+ self.assertIsNone(output)
+ self.assertIsNone(status)
- mock_disconnect_controller.assert_called()
- mock_disconnect_model.assert_called()
+ mock_disconnect_controller.assert_called()
+ mock_disconnect_model.assert_called()
# TODO no leader unit found exception
mock_get_controller,
):
mock_get_application.side_effect = Exception()
-
+ actions = None
with self.assertRaises(Exception):
actions = self.loop.run_until_complete(
self.libjuju.get_actions("app", "model")
)
- self.assertIsNone(actions)
- mock_disconnect_controller.assert_called_once()
- mock_disconnect_model.assert_called_once()
+ self.assertIsNone(actions)
+ mock_disconnect_controller.assert_called_once()
+ mock_disconnect_model.assert_called_once()
def test_success(
self,
self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
)
- mock_disconnect_controller.assert_called_once()
- mock_disconnect_model.assert_called_once()
+ mock_disconnect_controller.assert_called_once()
+ mock_disconnect_model.assert_called_once()
def test_success(
self,
self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
)
- mock_add_relation.assert_called_with(
- "app1:relation1", "app2:relation2"
- )
+ mock_add_relation.assert_called_with("app1:relation1", "app2:relation2")
mock_disconnect_controller.assert_called_once()
mock_disconnect_model.assert_called_once()
self.libjuju.add_relation("model", "app1:relation1", "saas_name",)
)
- mock_add_relation.assert_called_with(
- "app1:relation1", "saas_name"
- )
+ mock_add_relation.assert_called_with("app1:relation1", "saas_name")
mock_disconnect_controller.assert_called_once()
mock_disconnect_model.assert_called_once()
self.loop.run_until_complete(
self.libjuju.configure_application("model", "app", {"config"},)
)
- mock_disconnect_controller.assert_called_once()
- mock_disconnect_model.assert_called_once()
+ mock_disconnect_controller.assert_called_once()
+ mock_disconnect_model.assert_called_once()
# TODO _get_api_endpoints_db test case
mock_get_controller.return_value = juju.controller.Controller()
mock_get_model.return_value = juju.model.Model()
- self.loop.run_until_complete(
- self.libjuju.consume("offer_url", "model_name")
- )
+ self.loop.run_until_complete(self.libjuju.consume("offer_url", "model_name"))
mock_consume.assert_called_once()
mock_disconnect_model.assert_called_once()
mock_disconnect_controller.assert_called_once()
):
mock_get_controller.return_value = juju.controller.Controller()
mock_get_model.return_value = juju.model.Model()
- mock_consume.side_effect = juju.errors.JujuAPIError({
- "error": "",
- "response": "",
- "request-id": "",
-
- })
+ mock_consume.side_effect = juju.errors.JujuAPIError(
+ {"error": "", "response": "", "request-id": ""}
+ )
with self.assertRaises(juju.errors.JujuAPIError):
self.loop.run_until_complete(
mock_consume.assert_called_once()
mock_disconnect_model.assert_called_once()
mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.add_cloud")
+class AddK8sTest(LibjujuTestCase):
+ def setUp(self):
+ super(AddK8sTest, self).setUp()
+ self.auth_data = {
+ "server": "https://192.168.0.21:16443",
+ "token": "1234",
+ "cacert": "cacert",
+ }
+
+ def test_add_k8s(self, mock_add_cloud):
+ self.loop.run_until_complete(
+ self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+ )
+ mock_add_cloud.assert_called_once()
+
+ def test_add_k8s_exception(self, mock_add_cloud):
+ mock_add_cloud.side_effect = Exception()
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+ )
+ mock_add_cloud.assert_called_once()
+
+ def test_add_k8s_missing_name(self, mock_add_cloud):
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_k8s("", self.auth_data, "storage_class")
+ )
+ mock_add_cloud.assert_not_called()
+
+ def test_add_k8s_missing_storage_name(self, mock_add_cloud):
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_k8s("cloud", self.auth_data, "")
+ )
+ mock_add_cloud.assert_not_called()
+
+ def test_add_k8s_missing_auth_data_keys(self, mock_add_cloud):
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_k8s("cloud", {}, "")
+ )
+ mock_add_cloud.assert_not_called()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.add_cloud")
+@asynctest.mock.patch("juju.controller.Controller.add_credential")
+class AddCloudTest(LibjujuTestCase):
+ def setUp(self):
+ super(AddCloudTest, self).setUp()
+ self.cloud = juju.client.client.Cloud()
+ self.credential = juju.client.client.CloudCredential()
+
+ def test_add_cloud_with_credential(
+ self,
+ mock_add_credential,
+ mock_add_cloud,
+ mock_disconnect_controller,
+ mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+
+ cloud = self.loop.run_until_complete(
+ self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+ )
+ self.assertEqual(cloud, self.cloud)
+ mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+ mock_add_credential.assert_called_once_with(
+ "cloud", credential=self.credential, cloud="cloud"
+ )
+ mock_disconnect_controller.assert_called_once()
+
+ def test_add_cloud_no_credential(
+ self,
+ mock_add_credential,
+ mock_add_cloud,
+ mock_disconnect_controller,
+ mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+
+ cloud = self.loop.run_until_complete(
+ self.libjuju.add_cloud("cloud", self.cloud)
+ )
+ self.assertEqual(cloud, self.cloud)
+ mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+ mock_add_credential.assert_not_called()
+ mock_disconnect_controller.assert_called_once()
+
+ def test_add_cloud_exception(
+ self,
+ mock_add_credential,
+ mock_add_cloud,
+ mock_disconnect_controller,
+ mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+ mock_add_cloud.side_effect = Exception()
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+ )
+
+ mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+ mock_add_credential.assert_not_called()
+ mock_disconnect_controller.assert_called_once()
+
+ def test_add_credential_exception(
+ self,
+ mock_add_credential,
+ mock_add_cloud,
+ mock_disconnect_controller,
+ mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+ mock_add_credential.side_effect = Exception()
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(
+ self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+ )
+
+ mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+ mock_add_credential.assert_called_once_with(
+ "cloud", credential=self.credential, cloud="cloud"
+ )
+ mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.remove_cloud")
+class RemoveCloudTest(LibjujuTestCase):
+ def setUp(self):
+ super(RemoveCloudTest, self).setUp()
+
+ def test_remove_cloud(
+ self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+
+ self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+ mock_remove_cloud.assert_called_once_with("cloud")
+ mock_disconnect_controller.assert_called_once()
+
+ def test_remove_cloud_exception(
+ self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+ ):
+ mock_get_controller.return_value = juju.controller.Controller()
+ mock_remove_cloud.side_effect = Exception()
+
+ with self.assertRaises(Exception):
+ self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+ mock_remove_cloud.assert_called_once_with("cloud")
+ mock_disconnect_controller.assert_called_once()