def test_create_service(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" expected_response = {"name": name, "display_name": display_name} expected_response = service_pb2.Service(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup Request parent = client.project_path("[PROJECT]") service = {} response = client.create_service(parent, service) assert expected_response == response assert len(channel.requests) == 1 expected_request = service_service_pb2.CreateServiceRequest( parent=parent, service=service) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_service_level_objective(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" goal = 317825.0 expected_response = { "name": name, "display_name": display_name, "goal": goal } expected_response = service_pb2.ServiceLevelObjective( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup Request service_level_objective = {} response = client.update_service_level_objective( service_level_objective) assert expected_response == response assert len(channel.requests) == 1 expected_request = service_service_pb2.UpdateServiceLevelObjectiveRequest( service_level_objective=service_level_objective) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_service_level_objectives(self): # Setup Expected Response next_page_token = "" service_level_objectives_element = {} service_level_objectives = [service_level_objectives_element] expected_response = { "next_page_token": next_page_token, "service_level_objectives": service_level_objectives, } expected_response = service_service_pb2.ListServiceLevelObjectivesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup Request parent = client.service_path("[PROJECT]", "[SERVICE]") paged_list_response = client.list_service_level_objectives(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.service_level_objectives[0] == resources[0] assert len(channel.requests) == 1 expected_request = service_service_pb2.ListServiceLevelObjectivesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_service_level_objective(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" goal = 317825.0 expected_response = { "name": name_2, "display_name": display_name, "goal": goal } expected_response = service_pb2.ServiceLevelObjective( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup Request name = client.service_level_objective_path( "[PROJECT]", "[SERVICE]", "[SERVICE_LEVEL_OBJECTIVE]") response = client.get_service_level_objective(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = service_service_pb2.GetServiceLevelObjectiveRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def cleanupSlos(project_name): """ Deletes every SLO associated with every service. """ client = monitoring_v3.ServiceMonitoringServiceClient() slos = True while slos: slos = [slo for service in client.list_services(project_name) for slo in client.list_service_level_objectives(service.name)] for slo in slos: try: client.delete_service_level_objective(slo.name) except: print('Could not delete SLO: ' + slo.name)
def cleanupServices(project_name): """ Deletes only custom services that are identified by a trailing 'srv' in the name. """ client = monitoring_v3.ServiceMonitoringServiceClient() # only delete custom services custom_services = True while custom_services: custom_services = [s for s in client.list_services(project_name) if s.name.endswith("srv")] for service in custom_services: try: client.delete_service(service.name) except: print('Could not delete service: ' + service.name)
def test_update_service_level_objective_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup request service_level_objective = {} with pytest.raises(CustomException): client.update_service_level_objective(service_level_objective)
def test_list_service_level_objectives_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup request parent = client.service_path("[PROJECT]", "[SERVICE]") paged_list_response = client.list_service_level_objectives(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_delete_service_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup request name = client.service_path("[PROJECT]", "[SERVICE]") with pytest.raises(CustomException): client.delete_service(name)
def test_create_service_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup request parent = client.project_path("[PROJECT]") service = {} with pytest.raises(CustomException): client.create_service(parent, service)
def test_delete_service(self): channel = ChannelStub() patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = monitoring_v3.ServiceMonitoringServiceClient() # Setup Request name = client.service_path("[PROJECT]", "[SERVICE]") client.delete_service(name) assert len(channel.requests) == 1 expected_request = service_service_pb2.DeleteServiceRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def waitForIstioServicesDetection(project_id, zone, should_timeout): """ Waits for Istio services to be detected in Cloud Monitoring as a prerequisite for Terraform monitoring provisioning Arguments: project_id - the Sandbox project id (cloud-ops-sandbox-###) zone - the zone of the Sandbox cluster should_timeout - whether to timeout after 1 minute or wait indefinitely for the service """ client = monitoring_v3.ServiceMonitoringServiceClient() # wait for each Istio service to be detected by Cloud Monitoring services = [ "cartservice", "productcatalogservice", "currencyservice", "recommendationservice", "adservice", "frontend", "checkoutservice", "paymentservice", "emailservice", "shippingservice" ] with concurrent.futures.ThreadPoolExecutor( max_workers=len(services)) as executor: for service in services: executor.submit(findService, client, service, project_id, zone, should_timeout)
def setUp(self): self.client = monitoring_v3.ServiceMonitoringServiceClient() self.project_id = getProjectId()