Beispiel #1
0
    def test_TFCloudHttpRequest_with_additional_metrics(
        self, mock_get_env_name, mock_consent_status):

        google_api_client.TFCloudHttpRequest.set_telemetry_dict(
            {"TEST_KEY1": "TEST_VALUE1"})
        mock_consent_status.return_value = True
        mock_get_env_name.return_value = "TEST_ENV"
        http_request = google_api_client.TFCloudHttpRequest(
            googleapiclient_http.HttpMockSequence([({"status": "200"}, "{}")]),
            object(),
            "fake_uri",
        )
        self.assertIsInstance(http_request, googleapiclient_http.HttpRequest)
        self.assertIn("user-agent", http_request.headers)

        header_comment = "TEST_KEY1:TEST_VALUE1;client_environment:TEST_ENV;"
        full_header = f"tf-cloud/{version.__version__} ({header_comment})"

        self.assertDictEqual({"user-agent": full_header}, http_request.headers)

        # Verify when telemetry dict is refreshed it is used in new http request
        google_api_client.TFCloudHttpRequest.set_telemetry_dict(
            {"TEST_KEY2": "TEST_VALUE2"})
        mock_consent_status.return_value = True
        mock_get_env_name.return_value = "TEST_ENV"
        http_request = google_api_client.TFCloudHttpRequest(
            googleapiclient_http.HttpMockSequence([({"status": "200"}, "{}")]),
            object(),
            "fake_uri",
        )

        header_comment = "TEST_KEY2:TEST_VALUE2;client_environment:TEST_ENV;"
        full_header = f"tf-cloud/{version.__version__} ({header_comment})"

        self.assertDictEqual({"user-agent": full_header}, http_request.headers)
Beispiel #2
0
 def test_cloud_tuner_request_header(self):
     http_request = optimizer_client.CloudTunerHttpRequest(
         googleapiclient_http.HttpMockSequence([({
             'status': '200'
         }, '{}')]), object(), 'fake_uri')
     self.assertIsInstance(http_request, googleapiclient_http.HttpRequest)
     self.assertEqual({'user-agent': 'cloud-tuner/' + version.__version__},
                      http_request.headers)
 def test_cloud_tuner_request_header(self):
     http_request = google_api_client.TFCloudHttpRequest(
         googleapiclient_http.HttpMockSequence([({
             "status": "200"
         }, "{}")]),
         object(),
         "fake_uri",
     )
     self.assertIsInstance(http_request, googleapiclient_http.HttpRequest)
     self.assertEqual({"user-agent": "tf-cloud/" + version.__version__},
                      http_request.headers)
Beispiel #4
0
    def test_TFCloudHttpRequest_with_rejected_consent(
        self, mock_consent_status):

        mock_consent_status.return_value = False
        http_request = google_api_client.TFCloudHttpRequest(
            googleapiclient_http.HttpMockSequence([({"status": "200"}, "{}")]),
            object(),
            "fake_uri",
        )
        self.assertIsInstance(http_request, googleapiclient_http.HttpRequest)
        self.assertIn("user-agent", http_request.headers)
        self.assertDictEqual(
            {"user-agent": f"tf-cloud/{version.__version__} ()"},
            http_request.headers)
Beispiel #5
0
    def test_TFCloudHttpRequest_with_consent(
        self, mock_get_env_name, mock_consent_status):

        mock_consent_status.return_value = True
        mock_get_env_name.return_value = "TEST_ENV"
        google_api_client.TFCloudHttpRequest.set_telemetry_dict({})
        http_request = google_api_client.TFCloudHttpRequest(
            googleapiclient_http.HttpMockSequence([({"status": "200"}, "{}")]),
            object(),
            "fake_uri",
        )
        self.assertIsInstance(http_request, googleapiclient_http.HttpRequest)
        self.assertIn("user-agent", http_request.headers)

        header_comment = "client_environment:TEST_ENV;"
        full_header = f"tf-cloud/{version.__version__} ({header_comment})"

        self.assertDictEqual({"user-agent": full_header}, http_request.headers)
def mock_http_response_sequence(responses):
    """Set the mock response to an http request."""
    http_mock = http.HttpMockSequence(responses)
    _base_repository.LOCAL_THREAD.http = http_mock