def test_equality_not_equals_not_same_object(): """Check a vf and something different are not equals.""" svc_1 = Service(name="equal") svc_1.identifier = "1234" svc_2 = SdcResource() svc_2.name = "equal" assert svc_1 != svc_2
def test_onboard_service_several_resources(mock_create, mock_add_resource, mock_checkin, mock_submit, mock_start_certification, mock_certify, mock_approve, mock_distribute): getter_mock = mock.Mock(wraps=Service.status.fget) mock_status = Service.status.getter(getter_mock) with mock.patch.object(Service, 'status', mock_status): getter_mock.side_effect = [ const.DRAFT, const.DRAFT, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, None ] resource1 = SdcResource() resource2 = SdcResource() service = Service(resources=[resource1, resource2]) service._time_wait = 0 service.onboard() mock_create.assert_not_called() calls = [mock.call(resource1), mock.call(resource2)] mock_add_resource.assert_has_calls(calls, any_order=True) assert mock_add_resource.call_count == 2 mock_checkin.assert_called_once() mock_submit.assert_not_called() mock_start_certification.assert_not_called() mock_certify.assert_not_called() mock_approve.assert_not_called() mock_distribute.assert_not_called()
def test_really_submit_OK(mock_load, mock_action): mock_action.return_value = "yes" svc = Service() svc._really_submit() mock_load.assert_called_once() mock_action.assert_called_once_with('Certify', action_type='lifecycleState')
def test_link_service_subscription_to_cloud_region_and_tenant(): requests.get(f"{Customer.base_url}/reset") customer = Customer.create(global_customer_id="test_global_customer_id", subscriber_name="test_subscriber_name", subscriber_type="test_subscriber_type") service = Service("test_service") service.unique_uuid = str(uuid4()) customer.subscribe_service(service) service_subscription = customer.get_service_subscription_by_service_type( service.name) assert len(list(service_subscription.relationships)) == 0 with pytest.raises(AttributeError): service_subscription.cloud_region with pytest.raises(AttributeError): service_subscription.tenant cloud_region = CloudRegion.create("test_owner", "test_cloud_region", orchestration_disabled=True, in_maint=False) cloud_region.add_tenant(tenant_id="test_tenant_name", tenant_name="test_tenant_name", tenant_context="test_tenant_context") tenant = cloud_region.get_tenant(tenant_id="test_tenant_name") service_subscription.link_to_cloud_region_and_tenant( cloud_region=cloud_region, tenant=tenant) assert service_subscription.cloud_region assert service_subscription.tenant
def test_verify_action_to_sdc_bad_status(mock_created, mock_action, mock_load): mock_created.return_value = True svc = Service() svc._status = "no_yes" svc._verify_action_to_sdc("yes", "action", action_type='lifecycleState') mock_created.assert_called() mock_action.assert_not_called() mock_load.assert_not_called()
def test_customer_subscribe_service(mock_send_message, mock_send_message_json): customer = Customer(global_customer_id="test_customer_id", subscriber_name="test_subscriber_name", subscriber_type="test_subscriber_type") service = SdcService("test_service") service._unique_uuid = "1234" mock_send_message_json.side_effect = (ValueError, SERVICE_SUBSCRIPTION) customer.subscribe_service(service)
def test_distribute(mock_verify): svc = Service() svc.distribute() mock_verify.assert_called_once_with(const.CERTIFIED, const.DISTRIBUTE, 'distribution', headers=headers_sdc_creator( svc.headers))
def test_certify(mock_verify): svc = Service() svc.certify() mock_verify.assert_called_once_with(const.CHECKED_IN, const.CERTIFY, 'lifecycleState', headers=headers_sdc_creator( svc.headers))
def test_verify_action_to_sdc_OK(mock_created, mock_action, mock_load): mock_created.return_value = True mock_action.return_value = "good" svc = Service() svc._status = "yes" svc._verify_action_to_sdc("yes", "action", action_type='lifecycleState') mock_created.assert_called() mock_action.assert_called_once() mock_load.assert_called_once()
def test_init_no_name(): """Check init with no names.""" svc = Service() assert isinstance(svc, SdcResource) assert svc._identifier is None assert svc._version is None assert svc.name == "ONAP-test-Service" assert svc.headers["USER_ID"] == "cs0008" assert svc.distribution_status is None assert svc._distribution_id is None assert isinstance(svc._base_url(), str)
def test_load_metadata_bad_json(mock_send): mock_send.return_value = {'yolo': 'in the wood'} svc = Service() svc.identifier = "1" svc.load_metadata() assert svc._distribution_id is None mock_send.assert_called_once_with( 'GET', 'Get Metadata for ONAP-test-Service', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/rest/v1/catalog/services/1/distribution', headers=headers_sdc_creator(svc.headers))
def test_get_tosca_result(requests_mock): if path.exists('/tmp/tosca_files'): shutil.rmtree('/tmp/tosca_files') with open('tests/data/test.csar', mode='rb') as file: file_content = file.read() requests_mock.get( 'https://sdc.api.be.simpledemo.onap.org:30204/sdc/v1/catalog/services/12/toscaModel', content=file_content) svc = Service() svc.identifier = "12" svc.get_tosca()
def test_init_with_name(mock_exists): """Check init with no names.""" mock_exists.return_value = False svc = Service(name="YOLO") assert svc._identifier == None assert svc._version == None assert svc.name == "YOLO" assert svc.created() == False assert svc.headers["USER_ID"] == "cs0008" assert svc.distribution_status is None assert svc._distribution_id is None assert isinstance(svc._base_url(), str)
def test_subscribe_service(): requests.get(f"{Customer.base_url}/reset") customer = Customer.create(global_customer_id="test_global_customer_id", subscriber_name="test_subscriber_name", subscriber_type="test_subscriber_type") assert len(list(customer.service_subscriptions)) == 0 service = Service("test_service") service.unique_uuid = str(uuid4()) customer.subscribe_service(service) assert len(list(customer.service_subscriptions)) == 1 assert customer.get_service_subscription_by_service_type(service.name)
def test_vnf_vf_modules_one(): """Test parsing TOSCA file with one VNF which has associated one VFmodule""" service = Service(name="test") with open( Path( Path(__file__).resolve().parent, "data/service-Ubuntu16-template.yml"), "r") as ubuntu: service._tosca_template = yaml.safe_load(ubuntu.read()) assert len(service.vnfs) == 1 vnf = service.vnfs[0] assert vnf.name == "ubuntu16_VF 0" assert vnf.node_template_type == "org.openecomp.resource.vf.Ubuntu16Vf" assert vnf.vf_module assert vnf.vf_module.name == "ubuntu16_vf0..Ubuntu16Vf..base_ubuntu16..module-0"
def test_get_tosca_no_result(mock_send): if path.exists('/tmp/tosca_files'): shutil.rmtree('/tmp/tosca_files') mock_send.return_value = {} svc = Service() svc.identifier = "12" svc.get_tosca() headers = headers_sdc_creator(svc.headers) headers['Accept'] = 'application/octet-stream' mock_send.assert_called_once_with( 'GET', 'Download Tosca Model for ONAP-test-Service', 'https://sdc.api.be.simpledemo.onap.org:30204/sdc/v1/catalog/services/12/toscaModel', headers=headers) assert not path.exists('/tmp/tosca_files')
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_load_metadata_OK(mock_send): mock_send.return_value = { 'distributionStatusOfServiceList': [{ 'distributionID': "11" }, { 'distributionID': "12" }] } svc = Service() svc.identifier = "1" svc.load_metadata() assert svc._distribution_id == "11" mock_send.assert_called_once_with( 'GET', 'Get Metadata for ONAP-test-Service', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/rest/v1/catalog/services/1/distribution', headers=headers_sdc_creator(svc.headers))
def test_tosca_model(mock_send): service = Service(name="test") service.identifier = "toto" service.tosca_model mock_send.assert_called_once_with( "GET", "Download Tosca Model for test", "https://sdc.api.be.simpledemo.onap.org:30204/sdc/v1/catalog/services/toto/toscaModel", exception=mock.ANY, headers={ 'Content-Type': 'application/json', 'Accept': 'application/octet-stream', 'USER_ID': 'cs0008', 'Authorization': 'Basic YWFpOktwOGJKNFNYc3pNMFdYbGhhazNlSGxjc2UyZ0F3ODR2YW9HR21KdlV5MlU=', 'X-ECOMP-InstanceID': 'onapsdk' })
def test_tosca_template_no_tosca_model(mock_unzip): service = Service(name="test") getter_mock = mock.Mock(wraps=Service.tosca_model.fget) getter_mock.return_value = False mock_tosca_model = Service.tosca_model.getter(getter_mock) with mock.patch.object(Service, 'tosca_model', mock_tosca_model): service.tosca_template mock_unzip.assert_not_called()
def test_vnf_no_template(): getter_mock = mock.Mock(wraps=Service.tosca_template.fget) getter_mock.return_value = False mock_status = Service.tosca_template.getter(getter_mock) with mock.patch.object(Service, 'tosca_template', mock_status): with pytest.raises(AttributeError): service = Service(name="test") service.vnfs
def test_distributed_not_distributed(mock_send): mock_send.return_value = { 'distributionStatusList': [{ 'omfComponentID': "SO", 'status': "DOWNLOAD_OK" }, { 'omfComponentID': "aai", 'status': "DOWNLOAD_OK" }] } svc = Service() svc.distribution_id = "12" assert not svc.distributed mock_send.assert_called_once_with( 'GET', 'Check distribution for ONAP-test-Service', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/rest/v1/catalog/services/distribution/12', headers=headers_sdc_creator(svc.headers))
def test_init_with_sdc_values(mock_exists): """Check init with no names.""" sdc_values = { 'uuid': '12', 'version': '14', 'invariantUUID': '56', 'distributionStatus': 'yes', 'lifecycleState': 'state' } svc = Service(sdc_values=sdc_values) mock_exists.return_value = True assert svc._identifier == "12" assert svc._version == "14" assert svc.name == "ONAP-test-Service" assert svc.created() assert svc.headers["USER_ID"] == "cs0008" assert svc.distribution_status == "yes" assert svc._distribution_id is None assert isinstance(svc._base_url(), str)
def test_vnf_vf_modules_two(): """Test parsing TOSCA file with two VNF which has associated one VFmodule""" service = Service(name="test") with open( Path( Path(__file__).resolve().parent, "data/service-Foo-template.yml"), "r") as ubuntu: service._tosca_template = yaml.safe_load(ubuntu.read()) assert len(service.vnfs) == 2 vnf = service.vnfs[0] assert vnf.name == "vFWCL_vPKG-vf 0" assert vnf.node_template_type == "org.openecomp.resource.vf.VfwclVpkgVf" assert vnf.vf_module assert vnf.vf_module.name == "vfwcl_vpkgvf0..VfwclVpkgVf..base_vpkg..module-0" vnf = service.vnfs[1] assert vnf.name == "vFWCL_vFWSNK-vf 0" assert vnf.node_template_type == "org.openecomp.resource.vf.VfwclVfwsnkVf" assert vnf.vf_module assert vnf.vf_module.name == "vfwcl_vfwsnkvf0..VfwclVfwsnkVf..base_vfw..module-0"
def test_onboard_service_distribute(mock_create, mock_add_resource, mock_checkin, mock_certify, mock_distribute): getter_mock = mock.Mock(wraps=Service.status.fget) mock_status = Service.status.getter(getter_mock) with mock.patch.object(Service, 'status', mock_status): getter_mock.side_effect = [ const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, None ] service = Service() service._time_wait = 0 service.onboard() mock_create.assert_not_called() mock_add_resource.assert_not_called() mock_checkin.assert_not_called() mock_certify.assert_not_called() mock_distribute.assert_called_once()
def test_equality_not_equals(): """Check two vfs are not equals if name is not the same.""" svc_1 = Service(name="equal") svc_1.identifier = "1234" svc_2 = Service(name="not_equal") svc_2.identifier = "1234" assert svc_1 != svc_2
def test_equality_really_equals(): """Check two vfs are equals if name is the same.""" svc_1 = Service(name="equal") svc_1.identifier = "1234" svc_2 = Service(name="equal") svc_2.identifier = "1235" assert svc_1 == svc_2
def test_onboard_whole_service(mock_create, mock_add_resource, mock_checkin, mock_certify, mock_distribute): getter_mock = mock.Mock(wraps=Service.status.fget) mock_status = Service.status.getter(getter_mock) with mock.patch.object(Service, 'status', mock_status): getter_mock.side_effect = [ None, const.DRAFT, const.DRAFT, const.CHECKED_IN, const.CHECKED_IN, const.CHECKED_IN, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.CERTIFIED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, None ] resource = SdcResource() service = Service(resources=[resource]) service._time_wait = 0 service.onboard() mock_create.assert_called_once() mock_add_resource.assert_called_once_with(resource) mock_checkin.assert_called_once() mock_certify.assert_called_once() mock_distribute.assert_called_once()
def get_service_model_info(cls, service_name): """Retrieve Service Model info.""" service = Service(name=service_name) template_service = jinja_env().get_template( "service_instance_model_info.json.j2") # Get service instance model parsed = json.loads( template_service.render( model_invariant_id=service.unique_uuid, model_name_version_id=service.identifier, model_name=service.name, model_version=service.version, )) return json.dumps(parsed, indent=4)
def test_onboard_service_resources(mock_create, mock_add_resource, mock_checkin, mock_submit, mock_start_certification, mock_certify, mock_approve, mock_distribute): getter_mock = mock.Mock(wraps=Service.status.fget) mock_status = Service.status.getter(getter_mock) with mock.patch.object(Service, 'status', mock_status): getter_mock.side_effect = [ const.DRAFT, const.DRAFT, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, const.DISTRIBUTED, None ] resource = SdcResource() service = Service(resources=[resource]) service._time_wait = 0 service.onboard() mock_create.assert_not_called() mock_add_resource.assert_called_once_with(resource) mock_checkin.assert_called_once() mock_submit.assert_not_called() mock_start_certification.assert_not_called() mock_certify.assert_not_called() mock_approve.assert_not_called() mock_distribute.assert_not_called()