Fix cross-model relation condition
[osm/N2VC.git] / n2vc / tests / unit / test_n2vc_juju_conn.py
index 7606b4a..df7be50 100644 (file)
@@ -40,10 +40,7 @@ class N2VCJujuConnTestCase(asynctest.TestCase):
     @asynctest.mock.patch("n2vc.n2vc_juju_conn.get_connection")
     @asynctest.mock.patch("n2vc.vca.connection_data.base64_to_cacert")
     def setUp(
-        self,
-        mock_base64_to_cacert=None,
-        mock_get_connection=None,
-        mock_store=None,
+        self, mock_base64_to_cacert=None, mock_get_connection=None, mock_store=None
     ):
         self.loop = asyncio.get_event_loop()
         self.db = Mock()
@@ -156,10 +153,7 @@ class K8sProxyCharmsTest(N2VCJujuConnTestCase):
         "n2vc.n2vc_juju_conn.generate_random_alfanum_string",
         **{"return_value": "random"}
     )
-    def test_success(
-        self,
-        mock_generate_random_alfanum_string,
-    ):
+    def test_success(self, mock_generate_random_alfanum_string):
         self.n2vc.fs.file_exists = MagicMock(create_autospec=True)
         self.n2vc.fs.file_exists.return_value = True
         ee_id = self.loop.run_until_complete(
@@ -258,14 +252,16 @@ class AddRelationTest(N2VCJujuConnTestCase):
         self.n2vc.libjuju.get_controller = AsyncMock()
         self.n2vc.libjuju.consume = AsyncMock()
 
-    def test_standard_relation(self):
-        relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
-        relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint")
+    def test_standard_relation_same_model_and_controller(self):
+        relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint1")
+        relation_endpoint_2 = RelationEndpoint("model-1.app2.1", None, "endpoint2")
         self.loop.run_until_complete(
             self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
         )
         self.n2vc.libjuju.add_relation.assert_called_once_with(
-            model_name="model-1", endpoint_1="app1:endpoint", endpoint_2="app2:endpoint"
+            model_name="model-1",
+            endpoint_1="app1:endpoint1",
+            endpoint_2="app2:endpoint2",
         )
         self.n2vc.libjuju.offer.assert_not_called()
         self.n2vc.libjuju.consume.assert_not_called()
@@ -285,6 +281,26 @@ class AddRelationTest(N2VCJujuConnTestCase):
             "model-2", "app2:endpoint", "saas"
         )
 
+    def test_cmr_relation_different_controller(self):
+        self.n2vc._get_libjuju = AsyncMock(return_value=self.n2vc.libjuju)
+        relation_endpoint_1 = RelationEndpoint(
+            "model-1.app1.0", "vca-id-1", "endpoint1"
+        )
+        relation_endpoint_2 = RelationEndpoint(
+            "model-1.app2.1", "vca-id-2", "endpoint2"
+        )
+        offer = Offer("admin/model-1.app1")
+        self.n2vc.libjuju.offer.return_value = offer
+        self.n2vc.libjuju.consume.return_value = "saas"
+        self.loop.run_until_complete(
+            self.n2vc.add_relation(relation_endpoint_1, relation_endpoint_2)
+        )
+        self.n2vc.libjuju.offer.assert_called_once_with(relation_endpoint_1)
+        self.n2vc.libjuju.consume.assert_called_once()
+        self.n2vc.libjuju.add_relation.assert_called_once_with(
+            "model-1", "app2:endpoint2", "saas"
+        )
+
     def test_relation_exception(self):
         relation_endpoint_1 = RelationEndpoint("model-1.app1.0", None, "endpoint")
         relation_endpoint_2 = RelationEndpoint("model-2.app2.1", None, "endpoint")
@@ -469,7 +485,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_id": None,
                 "application": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = ""
         vdu_count = ""
@@ -505,9 +521,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
 
     def test_generate_application_name_vnf_charm(self):
         charm_level = "vnf-level"
-        vnfrs = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vca_records = [
             {
                 "target_element": "vnf/vnf1",
@@ -516,7 +530,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = "1"
         vdu_count = ""
@@ -639,9 +653,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         self,
     ):
         charm_level = "vdu-level"
-        vnfrs = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vca_records = [
             {
                 "target_element": "vnf/vnf1/mgmtVM",
@@ -654,7 +666,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = "2"
         vdu_count = "0"
@@ -671,9 +683,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
 
     def test_generate_application_name_vdu_charm_given_vdu_id_is_none(self):
         charm_level = "vdu-level"
-        vnfrs = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vca_records = [
             {
                 "target_element": "vnf/vnf1/mgmtvVM",
@@ -686,7 +696,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = "2"
         vdu_count = "0"
@@ -705,9 +715,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         self,
     ):
         charm_level = "vdu-level"
-        vnfrs = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vca_records = [
             {
                 "target_element": "vnf/vnf1/mgmtVM",
@@ -720,7 +728,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = "2"
         vdu_count = "0"
@@ -737,9 +745,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
 
     def test_generate_application_name_vdu_charm_vdu_id_in_vca_record_is_none(self):
         charm_level = "vdu-level"
-        vnfrs = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        vnfrs = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vca_records = [
             {
                 "target_element": "vnf/vnf1/mgmtVM",
@@ -752,7 +758,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vnf_count = "2"
         vdu_count = "0"
@@ -839,9 +845,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         expected_result = [
             {
@@ -855,16 +861,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "simple-ee-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
         self.assertEqual(vca_records, expected_result)
 
     def test_get_vca_records_vnf_charm_member_vnf_index_mismatch(self):
         charm_level = "vnf-level"
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
         db_nsr = {
             "_admin": {
                 "deployed": {
@@ -893,9 +897,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         expected_result = []
         vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
@@ -903,9 +907,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
 
     def test_get_vca_records_ns_charm(self):
         charm_level = "ns-level"
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
         db_nsr = {
             "_admin": {
                 "deployed": {
@@ -934,9 +936,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         expected_result = [
             {
@@ -950,16 +952,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "",
                 "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
         self.assertEqual(vca_records, expected_result)
 
     def test_get_vca_records_ns_charm_empty_charm_name(self):
         charm_level = "ns-level"
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
         db_nsr = {
             "_admin": {
                 "deployed": {
@@ -988,9 +988,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         expected_result = [
             {
@@ -1004,7 +1004,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                 "ee_descriptor_id": "",
                 "charm_name": "",
                 "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-            },
+            }
         ]
         vca_records = self.n2vc._get_vca_records(charm_level, db_nsr, db_vnfr)
         self.assertEqual(vca_records, expected_result)
@@ -1039,14 +1039,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vnf_count = "0"
         mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
         expected_result = "simple-ee-ab-z0-vnf111-xxx-y-vnf"
@@ -1095,14 +1093,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "ee_descriptor_id": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf111-xxx-yyy-zzz"}
         vnf_count = "0"
         mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
         expected_result = "app-vnf-eb3161eec0-z0-random"
@@ -1146,14 +1142,12 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
-        db_vnfr = {
-            "member-vnf-index-ref": "vnf222-xxx-yyy-zzz",
-        }
+        db_vnfr = {"member-vnf-index-ref": "vnf222-xxx-yyy-zzz"}
         vnf_count = "0"
         mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
         with patch.object(self.n2vc, "db", self.db), patch.object(
@@ -1246,16 +1240,13 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "application": "openldap-ee-z0-openldap-vdu",
                             "model": "82b11965-e580-47c0-9ee0-329f318a305b",
                             "config_sw_installed": True,
-                        },
+                        }
                     ]
                 }
             }
         }
         mock_vnf_count_and_record = MagicMock()
-        db_vnfr = {
-            "member-vnf-index-ref": "openldap",
-            "vdur": {},
-        }
+        db_vnfr = {"member-vnf-index-ref": "openldap", "vdur": {}}
         vnf_count = "0"
         mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
         expected_result = "openldap-ee-z0-openldap-ldap-vdu"
@@ -1304,9 +1295,9 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "ee_descriptor_id": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
                         },
-                    ],
-                },
-            },
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
         db_vnfr = {
@@ -1348,10 +1339,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "ee_descriptor_id": "",
                             "charm_name": "simple-ns-charm-abc-000-rrrr-nnnn-4444-hhh-3333-yyyy-333-hhh-ttt-444",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-                        },
-                    ],
-                },
-            },
+                        }
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
         db_vnfr = {}
@@ -1384,10 +1375,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "ee_descriptor_id": "",
                             "charm_name": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-                        },
-                    ],
-                },
-            },
+                        }
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
         db_vnfr = {}
@@ -1423,10 +1414,10 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                             "vdu_name": "",
                             "ee_descriptor_id": "",
                             "model": "dbfbd751-3de4-4e68-bd40-ec5ae0a53898",
-                        },
-                    ],
-                },
-            },
+                        }
+                    ]
+                }
+            }
         }
         mock_vnf_count_and_record = MagicMock()
         db_vnfr = {}