MCPcopy Create free account
hub / github.com/Kaggle/docker-python / _test_client

Method _test_client

tests/test_datasets.py:44–103  ·  view source on GitHub ↗
(self, client_func, expected_path, expected_body, is_tpu=True, success=True, iap_token=False)

Source from the content-addressed store, hash-verified

42 SERVER_ADDRESS = urlparse(os.getenv(_KAGGLE_URL_BASE_ENV_VAR_NAME, default="http://127.0.0.1:0"))
43
44 def _test_client(self, client_func, expected_path, expected_body, is_tpu=True, success=True, iap_token=False):
45 _request = {}
46
47 class GetGcsPathHandler(GcsDatasetsHTTPHandler):
48
49 def set_request(self):
50 _request['path'] = self.path
51 content_len = int(self.headers.get('Content-Length'))
52 _request['body'] = json.loads(self.rfile.read(content_len))
53 _request['headers'] = self.headers
54
55 def get_response(self):
56 if success:
57 gcs_path = _TPU_GCS_BUCKET if is_tpu else _AUTOML_GCS_BUCKET
58 return {'result': {
59 'destinationBucket': gcs_path,
60 'destinationPath': None}, 'wasSuccessful': "true"}
61 else:
62 return {'wasSuccessful': "false"}
63
64 env = EnvironmentVarGuard()
65 env.set(_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, _TEST_JWT)
66 if is_tpu:
67 env.set(_KAGGLE_TPU_NAME_ENV_VAR_NAME, 'FAKE_TPU')
68 if iap_token:
69 env.set(_KAGGLE_IAP_TOKEN_ENV_VAR_NAME, _TEST_IAP)
70 with env:
71 with HTTPServer((self.SERVER_ADDRESS.hostname, self.SERVER_ADDRESS.port), GetGcsPathHandler) as httpd:
72 threading.Thread(target=httpd.serve_forever).start()
73
74 try:
75 # Make sure this server is pointed to by clients (the port may have been dynamically chosen):
76 env.set(_KAGGLE_URL_BASE_ENV_VAR_NAME, "http://"+httpd.server_address[0]+":"+str(httpd.server_address[1]))
77 client_func()
78 finally:
79 httpd.shutdown()
80
81 path, headers, body = _request['path'], _request['headers'], _request['body']
82 self.assertEqual(
83 path,
84 expected_path,
85 msg="Fake server did not receive the right request from the KaggleDatasets client.")
86 self.assertEqual(
87 body,
88 expected_body,
89 msg="Fake server did not receive the right body from the KaggleDatasets client.")
90 self.assertIn('Content-Type', headers.keys(),
91 msg="Fake server did not receive a Content-Type header from the KaggleDatasets client.")
92 self.assertEqual('application/json', headers.get('Content-Type'),
93 msg="Fake server did not receive an application/json content type header from the KaggleDatasets client.")
94 self.assertIn('X-Kaggle-Authorization', headers.keys(),
95 msg="Fake server did not receive an X-Kaggle-Authorization header from the KaggleDatasets client.")
96 if iap_token:
97 self.assertEqual(f'Bearer {_TEST_IAP}', headers.get('Authorization'),
98 msg="Fake server did not receive an Authorization header from the KaggleDatasets client.")
99 else:
100 self.assertNotIn('Authorization', headers.keys(),
101 msg="Fake server received an Authorization header from the KaggleDatasets client. It shouldn't.")

Calls

no outgoing calls

Tested by

no test coverage detected