def test_version_no_load_created(mock_load): """Test versions when created.""" vf = Vf() vf.identifier = "1234" vf._version = "64" assert vf.version == "64" mock_load.assert_not_called()
def test_vf_category(mock_resource_category, mock_created): mock_created.return_value = False vf = Vf(name="test") _ = vf.category mock_resource_category.assert_called_once_with(name="Generic", subcategory="Abstract") mock_resource_category.reset_mock() vf = Vf(name="test", category="Allotted Resource", subcategory="Allotted Resource") _ = vf.category mock_resource_category.assert_called_once_with( name="Allotted Resource", subcategory="Allotted Resource") mock_resource_category.reset_mock() vf = Vf(name="test", category="test", subcategory="test") _ = vf.category mock_resource_category.assert_called_once_with(name="test", subcategory="test") mock_resource_category.reset_mock() mock_created.return_value = True _ = vf.category mock_resource_category.assert_called_once_with(name="test", subcategory="test")
def test__get_item_details_created(mock_send): vf = Vf() vf.identifier = "1234" mock_send.return_value = {'return': 'value'} assert vf._get_item_details() == {'return': 'value'} mock_send.assert_called_once_with( 'GET', 'get item', "{}/items/1234/versions".format(vf._base_url()))
def test_service_upload_tca_artifact(): """Integration tests for Service.""" response = requests.post("{}/reset".format(SDC.base_front_url)) response.raise_for_status() vendor = Vendor(name="test") vendor.onboard() vsp = Vsp(name="test", package=open( "{}/ubuntu16.zip".format( os.path.dirname(os.path.abspath(__file__))), 'rb')) vsp.vendor = vendor vsp.onboard() vf = Vf(name='test', vsp=vsp) vf.onboard() svc = Service(name='test') svc.create() svc.add_resource(vf) assert svc.status == const.DRAFT payload_file = open( "{}/tca_clampnode.yaml".format( os.path.dirname(os.path.abspath(__file__))), 'rb') data = payload_file.read() svc.add_artifact_to_vf(vnf_name="test", artifact_type="DCAE_INVENTORY_BLUEPRINT", artifact_name="tca_clampnode.yaml", artifact=data) payload_file.close()
def test_submit_not_Commited(mock_send, mock_load, mock_exists, status): """Do nothing if not created.""" mock_exists.return_value = False vf = Vf() vf._status = status vf.submit() mock_send.assert_not_called()
def test_add_resource_not_draft(mock_send, mock_exists): mock_exists.return_value = False vf = Vf() resource = SdcResource() with pytest.raises(StatusError): vf.add_resource(resource) mock_send.assert_not_called()
def test_service_unknown(): """Integration tests for Service.""" response = requests.post("{}/reset".format(SDC.base_front_url)) response.raise_for_status() vendor = Vendor(name="test") vendor.onboard() vsp = Vsp(name="test", package=open( "{}/ubuntu16.zip".format( os.path.dirname(os.path.abspath(__file__))), 'rb')) vsp.vendor = vendor vsp.onboard() vf = Vf(name='test', vsp=vsp) vf.onboard() svc = Service(name='test') assert svc.identifier is None assert svc.status is None svc.create() assert svc.identifier is not None assert svc.status == const.DRAFT svc.add_resource(vf) svc.checkin() assert svc.status == const.CHECKED_IN svc.certify() assert svc.status == const.CERTIFIED svc.distribute() assert svc.status == const.DISTRIBUTED assert svc.distributed
def test_service_properties(): """Integration test to check properties assignment for Service.""" response = requests.post("{}/reset".format(SDC.base_front_url)) response.raise_for_status() vendor = Vendor(name="test") vendor.onboard() vsp = Vsp(name="test", package=open( "{}/ubuntu16.zip".format( os.path.dirname(os.path.abspath(__file__))), 'rb')) vsp.vendor = vendor vsp.onboard() vf = Vf(name='test', vsp=vsp) vf.onboard() properties = [ Property(name="test1", property_type="string", value="123"), Property(name="test2", property_type="integer") ] svc = Service(name='test', resources=[vf], properties=properties, inputs=[properties[1]]) svc.onboard() service_properties = list(svc.properties) service_inputs = list(svc.inputs) assert len(service_properties) == 2 assert len(service_inputs) == 1
def test_equality_not_equals_not_same_object(): """Check a vf and something different are not equals.""" vf_1 = Vf(name="equal") vf_1.identifier = "1234" vf_2 = SdcResource() vf_2.name = "equal" assert vf_1 != vf_2
def test_create_no_vsp(mock_send, mock_exists): """Do nothing if no vsp.""" vf = Vf() mock_exists.return_value = False with pytest.raises(ParameterError) as err: vf.create() assert err.type == ParameterError assert str(err.value) == "At least vsp or vendor needs to be given"
def test_vf_properties(mock_send_json): vf = Vf(name="test") vf.unique_identifier = "toto" mock_send_json.return_value = {} assert len(list(vf.properties)) == 0 mock_send_json.return_value = PROPERTIES properties_list = list(vf.properties) assert len(properties_list) == 4 prop1, prop2, prop3, prop4 = properties_list mock_send_json.return_value = INPUTS assert prop1.sdc_resource == vf assert prop1.unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079.llllll" assert prop1.name == "llllll" assert prop1.property_type == "integer" assert prop1.parent_unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079" assert prop1.value == '{"get_input":["lililili","INDEX","llllll"]}' assert prop1.description is None assert prop1.get_input_values prop1_input = prop1.input assert prop1_input.unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079.lililili" assert prop1_input.input_type == "list" assert prop1_input.name == "lililili" assert prop1_input.default_value is None assert prop2.sdc_resource == vf assert prop2.unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079.test" assert prop2.name == "test" assert prop2.property_type == "string" assert prop2.parent_unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079" assert prop2.value is None assert prop2.description is None assert prop2.get_input_values == [] assert prop2.input is None assert prop3.sdc_resource == vf assert prop3.unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079.yyy" assert prop3.name == "yyy" assert prop3.property_type == "string" assert prop3.parent_unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079" assert prop3.value == "lalala" assert prop3.description is None assert prop3.get_input_values is None assert prop3.input is None assert prop4.sdc_resource == vf assert prop4.unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079.test2" assert prop4.name == "test2" assert prop4.property_type == "boolean" assert prop4.parent_unique_id == "4a84415b-4580-4a78-aa33-501f0cd3d079" assert prop4.value == '{"get_input":"test2"}' assert prop4.description == "test2" assert prop4.get_input_values with pytest.raises(AttributeError): prop4.input
def test_create_already_exists(mock_category, mock_send, mock_exists): """Do nothing if already created in SDC.""" vf = Vf(vendor=MagicMock()) vsp = Vsp() vsp._identifier = "1232" vf.vsp = vsp mock_exists.return_value = True vf.create() mock_send.assert_not_called()
def test_init_no_name(): """Check init with no names.""" vf = Vf() assert isinstance(vf, SdcResource) assert vf._identifier is None assert vf._version is None assert vf.name == "ONAP-test-VF" assert vf.headers["USER_ID"] == "cs0008" assert vf.vsp is None assert isinstance(vf._base_url(), str)
def test_add_properties(mock_send_message_json): vf = Vf(name="test") vf._identifier = "toto" vf._unique_identifier = "toto" vf._status = const.CERTIFIED with pytest.raises(StatusError): vf.add_property(Property(name="test", property_type="string")) vf._status = const.DRAFT vf.add_property(Property(name="test", property_type="string")) mock_send_message_json.assert_called_once()
def test_init_with_name(mock_exists): """Check init with no names.""" mock_exists.return_value = False vf = Vf(name="YOLO") assert vf._identifier == None assert vf._version == None assert vf.name == "YOLO" assert vf.created() == False assert vf.headers["USER_ID"] == "cs0008" assert vf.vsp == None assert isinstance(vf._base_url(), str)
def test_onboard_vf_load(mock_create, mock_submit, mock_load): getter_mock = mock.Mock(wraps=Vf.status.fget) mock_status = Vf.status.getter(getter_mock) with mock.patch.object(Vf, 'status', mock_status): getter_mock.side_effect = [ const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.APPROVED, const.APPROVED, const.APPROVED ] vf = Vf() vf._time_wait = 0 vf.onboard() mock_create.assert_not_called() mock_submit.assert_not_called() mock_load.assert_called_once()
def test_vf_vendor_property(mock_resource_inputs_url, mock_send_message_json, mock_created): mock_created.return_value = False vf = Vf() assert vf.vendor is None vsp_mock = MagicMock() vsp_mock.vendor = MagicMock() vf.vsp = vsp_mock assert vf.vendor == vsp_mock.vendor vf._vendor = None mock_created.return_value = True mock_send_message_json.return_value = {"vendorName": "123"} assert vf.vendor.name == "123"
def assign_properties(self, service: Service) -> None: """Assign components properties. For each component set properties and it's value if are declared in YAML template. Args: service (Service): Service object """ if "networks" in self.yaml_template[self.service_name]: for net in self.yaml_template[self.service_name]["networks"]: if "properties" in net: vl: Vl = Vl(name=net['vl_name']) vl_component: Component = service.get_component(vl) self.assign_properties_to_component( vl_component, net["properties"]) if "vnfs" in self.yaml_template[self.service_name]: for vnf in self.yaml_template[self.service_name]["vnfs"]: if "properties" in vnf: vf: Vf = Vf(name=vnf["vnf_name"]) vf_component: Component = service.get_component(vf) self.assign_properties_to_component( vf_component, vnf["properties"]) if "pnfs" in self.yaml_template[self.service_name]: for pnf in self.yaml_template[self.service_name]["pnfs"]: if "properties" in pnf: pnf_obj: Pnf = Pnf(name=pnf["pnf_name"]) pnf_component: Component = service.get_component(pnf_obj) self.assign_properties_to_component( pnf_component, pnf["properties"])
def execute(self): """Onboard service. Use settings values: - VL_NAME, - VF_NAME, - PNF_NAME, - SERVICE_NAME, - SERVICE_INSTANTIATION_TYPE. """ super().execute() service: Service = Service( name=settings.SERVICE_NAME, instantiation_type=settings.SERVICE_INSTANTIATION_TYPE) if not service.created(): service.create() if settings.VL_NAME != "": vl: Vl = Vl(name=settings.VL_NAME) service.add_resource(vl) if settings.VF_NAME != "": vf: Vf = Vf(name=settings.VF_NAME) service.add_resource(vf) if settings.PNF_NAME != "": pnf: Pnf = Pnf(name=settings.PNF_NAME) service.add_resource(pnf) # If the service is already distributed, do not try to checkin/onboard (replay of tests) # checkin is done if needed # If service is replayed, no need to try to re-onboard the model if not service.distributed: time.sleep(10) service.checkin() service.onboard()
def execute(self): """Onboard service.""" super().execute() # retrieve the Vf vf = None for sdc_vf in Vf.get_all(): if sdc_vf.name == settings.VF_NAME: vf = sdc_vf self._logger.debug("Vf retrieved %s", vf) service: Service = Service(name=self.service_name, resources=[vf]) service.create() self._logger.info(" Service %s created", service) if not service.distributed: service.add_resource(vf) # we add the artifact to the first VNF self._logger.info("Try to add blueprint to %s", vf.name) payload_file = open( settings.CONFIGURATION_PATH + 'tca-microservice.yaml', 'rb') data = payload_file.read() self._logger.info("DCAE INVENTORY BLUEPRINT file retrieved") service.add_artifact_to_vf( vnf_name=vf.name, artifact_type="DCAE_INVENTORY_BLUEPRINT", artifact_name="tca-microservice.yaml", artifact=data) payload_file.close() service.checkin() service.onboard() self._logger.info("DCAE INVENTORY BLUEPRINT ADDED")
def test_exists_exists(mock_get_all): """Return True if vf exists in SDC.""" vf_1 = Vf(name="one") vf_1.identifier = "1234" vf_1.unique_uuid = "5689" vf_1.unique_identifier = "71011" vf_1.status = const.DRAFT vf_1.version = "1.1" mock_get_all.return_value = [vf_1] vf = Vf(name="one") assert vf.exists() assert vf.identifier == "1234" assert vf.unique_uuid == "5689" assert vf.unique_identifier == "71011" assert vf.status == const.DRAFT assert vf.version == "1.1"
def test_service_onboard_unknown(): """Integration tests for Service.""" response = requests.post("{}/reset".format(SDC.base_front_url)) response.raise_for_status() vendor = Vendor(name="test") vendor.onboard() vsp = Vsp(name="test", package=open( "{}/ubuntu16.zip".format( os.path.dirname(os.path.abspath(__file__))), 'rb')) vsp.vendor = vendor vsp.onboard() vf = Vf(name='test', vsp=vsp) vf.onboard() svc = Service(name='test', resources=[vf]) svc.onboard() assert svc.distributed
def test_get_all_no_vf(mock_send): """Returns empty array if no vfs.""" mock_send.return_value = {} assert Vf.get_all() == [] mock_send.assert_called_once_with( "GET", 'get Vfs', 'https://sdc.api.be.simpledemo.onap.org:30204/sdc/v1/catalog/resources?resourceType=VF' )
def test_vf_onboard_unknown(): """Integration tests for Vf.""" response = requests.post("{}/reset".format(Vendor.base_front_url)) response.raise_for_status() vendor = Vendor(name="test") vendor.onboard() vsp = Vsp(name="test", package=open( "{}/ubuntu16.zip".format( os.path.dirname(os.path.abspath(__file__))), 'rb')) vsp.vendor = vendor vsp.onboard() vf = Vf(name='test') vf.vsp = vsp vf.onboard() assert vsp.status == const.CERTIFIED assert vf.version == "1.0"
def test__deep_load_response_OK_under_cert(mock_send, mock_created): mock_created.return_value = True vf = Vf() vf.identifier = "5689" vf._version = "4567" vf._status = const.UNDER_CERTIFICATION mock_send.return_value = { 'resources': [{ 'uuid': '5689', 'name': 'test', 'uniqueId': '71011', 'categories': [{ 'name': 'test', 'subcategories': [{ 'name': 'test_subcategory' }] }] }] } vf.deep_load() assert vf.unique_identifier == "71011" assert vf._category_name == "test" assert vf._subcategory_name == "test_subcategory" mock_send.assert_called_once_with( 'GET', 'Deep Load Vf', "{}/sdc1/feProxy/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration" .format(vf.base_front_url), headers=headers_sdc_tester(vf.headers))
def test_onboard_vf_submit(mock_certify, mock_add_resource, mock_create, mock_submit, mock_load): getter_mock = mock.Mock(wraps=Vf.status.fget) mock_status = Vf.status.getter(getter_mock) with mock.patch.object(Vf, 'status', mock_status): getter_mock.side_effect = [ const.DRAFT, const.DRAFT, const.CHECKED_IN, const.CHECKED_IN, const.CHECKED_IN, const.APPROVED, const.APPROVED, const.APPROVED, const.APPROVED ] vf = Vf() vf._time_wait = 0 vf.onboard() mock_create.assert_not_called() mock_add_resource.assert_not_called() mock_submit.assert_called_once() mock_load.assert_not_called() mock_certify.assert_called_once()
def test_add_artifact_to_vf(mock_send_message, mock_load): """Test VF add artifact""" vf = Vf(name="test") vf.status = const.DRAFT mycbapath = Path( Path(__file__).resolve().parent, "data/vLB_CBA_Python.zip") result = vf.add_deployment_artifact( artifact_label="cba", artifact_type="CONTROLLER_BLUEPRINT_ARCHIVE", artifact_name="vLB_CBA_Python.zip", artifact=mycbapath) mock_send_message.assert_called() method, description, url = mock_send_message.call_args[0] assert method == "POST" assert description == "Add deployment artifact for test sdc resource" assert url == ( "https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/rest/v1/catalog/resources/" f"{vf.unique_identifier}/artifacts")
def test_exists_not_exists(mock_get_all): """Return False if vf doesn't exist in SDC.""" vf_1 = Vf(name="one") vf_1.identifier = "1234" mock_get_all.return_value = [vf_1] vf = Vf(name="two") assert not vf.exists()
def test_equality_really_equals(): """Check two vfs are equals if name is the same.""" vf_1 = Vf(name="equal") vf_1.identifier = "1234" vf_2 = Vf(name="equal") vf_2.identifier = "1235" assert vf_1 == vf_2
def test_equality_not_equals(): """Check two vfs are not equals if name is not the same.""" vf_1 = Vf(name="equal") vf_1.identifier = "1234" vf_2 = Vf(name="not_equal") vf_2.identifier = "1234" assert vf_1 != vf_2