Feature 10239: Distributed VCA

- Add vca_id in all calls that invoke libjuju. This is for being able to
talk to the default VCA or the VCA associated to the VIM
- Add store.py: Abstraction to talk to the database.
  - DBMongoStore: Use the db from common to talk to the database
  - MotorStore: Use motor, an asynchronous mongodb client to talk to the
database
- Add vca/connection.py: Represents the data needed to connect the VCA
- Add EnvironConfig in config.py: Class to get the environment config,
and avoid LCM from passing that

Change-Id: I28625e0c56ce408114022c83d4b7cacbb649434c
Signed-off-by: David Garcia <david.garcia@canonical.com>
diff --git a/n2vc/tests/unit/test_connection.py b/n2vc/tests/unit/test_connection.py
new file mode 100644
index 0000000..c7f0bb4
--- /dev/null
+++ b/n2vc/tests/unit/test_connection.py
@@ -0,0 +1,68 @@
+# Copyright 2020 Canonical Ltd.
+#
+# 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.
+
+import asyncio
+from unittest import TestCase
+from unittest.mock import Mock, patch
+
+
+from n2vc.tests.unit.utils import AsyncMock
+from n2vc.vca import connection
+
+
+class TestConnection(TestCase):
+    def setUp(self):
+        self.loop = asyncio.get_event_loop()
+        self.store = AsyncMock()
+
+    def test_load_from_store(self):
+        self.loop.run_until_complete(connection.get_connection(self.store, "vim_id"))
+
+        self.store.get_vca_connection_data.assert_called_once()
+
+    def test_cloud_properties(self):
+        conn = self.loop.run_until_complete(
+            connection.get_connection(self.store, "vim_id")
+        )
+        conn._data = Mock()
+        conn._data.lxd_cloud = "name"
+        conn._data.k8s_cloud = "name"
+        conn._data.lxd_credentials = "credential"
+        conn._data.k8s_credentials = "credential"
+
+        self.assertEqual(conn.lxd_cloud.name, "name")
+        self.assertEqual(conn.lxd_cloud.credential_name, "credential")
+        self.assertEqual(conn.k8s_cloud.name, "name")
+        self.assertEqual(conn.k8s_cloud.credential_name, "credential")
+
+    @patch("n2vc.vca.connection.EnvironConfig")
+    @patch("n2vc.vca.connection_data.base64_to_cacert")
+    def test_load_from_env(self, mock_base64_to_cacert, mock_env):
+        mock_base64_to_cacert.return_value = "cacert"
+        mock_env.return_value = {
+            "endpoints": "1.2.3.4:17070",
+            "user": "user",
+            "secret": "secret",
+            "cacert": "cacert",
+            "pubkey": "pubkey",
+            "cloud": "cloud",
+            "credentials": "credentials",
+            "k8s_cloud": "k8s_cloud",
+            "k8s_credentials": "k8s_credentials",
+            "model_config": {},
+            "api-proxy": "api_proxy",
+        }
+        self.store.get_vca_endpoints.return_value = ["1.2.3.5:17070"]
+        self.loop.run_until_complete(connection.get_connection(self.store))
+        self.store.get_vca_connection_data.assert_not_called()