|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for tensorboard.uploader.server_info.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import errno |
| 22 | +import os |
| 23 | +import socket |
| 24 | +from wsgiref import simple_server |
| 25 | + |
| 26 | +from concurrent import futures |
| 27 | +from werkzeug import wrappers |
| 28 | + |
| 29 | +from tensorboard import test as tb_test |
| 30 | +from tensorboard import version |
| 31 | +from tensorboard.uploader import server_info |
| 32 | +from tensorboard.uploader.proto import server_info_pb2 |
| 33 | + |
| 34 | + |
| 35 | +class FetchServerInfoTest(tb_test.TestCase): |
| 36 | + """Tests for `fetch_server_info`.""" |
| 37 | + |
| 38 | + def _start_server(self, app): |
| 39 | + """Starts a server and returns its origin ("http://localhost:PORT").""" |
| 40 | + (_, localhost) = _localhost() |
| 41 | + server_class = _make_ipv6_compatible_wsgi_server() |
| 42 | + server = simple_server.make_server(localhost, 0, app, server_class) |
| 43 | + executor = futures.ThreadPoolExecutor() |
| 44 | + future = executor.submit(server.serve_forever, poll_interval=0.01) |
| 45 | + |
| 46 | + def cleanup(): |
| 47 | + server.shutdown() # stop handling requests |
| 48 | + server.server_close() # release port |
| 49 | + future.result(timeout=3) # wait for server termination |
| 50 | + |
| 51 | + self.addCleanup(cleanup) |
| 52 | + return "http://localhost:%d" % server.server_port |
| 53 | + |
| 54 | + def test_fetches_response(self): |
| 55 | + expected_result = server_info_pb2.ServerInfoResponse() |
| 56 | + expected_result.compatibility.verdict = server_info_pb2.VERDICT_OK |
| 57 | + expected_result.compatibility.details = "all clear" |
| 58 | + expected_result.api_server.endpoint = "api.example.com:443" |
| 59 | + expected_result.url_format.template = "http://localhost:8080/{{eid}}" |
| 60 | + expected_result.url_format.id_placeholder = "{{eid}}" |
| 61 | + |
| 62 | + @wrappers.BaseRequest.application |
| 63 | + def app(request): |
| 64 | + self.assertEqual(request.method, "POST") |
| 65 | + self.assertEqual(request.path, "/api/uploader") |
| 66 | + body = request.get_data() |
| 67 | + request_pb = server_info_pb2.ServerInfoRequest.FromString(body) |
| 68 | + self.assertEqual(request_pb.version, version.VERSION) |
| 69 | + return wrappers.BaseResponse(expected_result.SerializeToString()) |
| 70 | + |
| 71 | + origin = self._start_server(app) |
| 72 | + result = server_info.fetch_server_info(origin) |
| 73 | + self.assertEqual(result, expected_result) |
| 74 | + |
| 75 | + def test_econnrefused(self): |
| 76 | + (family, localhost) = _localhost() |
| 77 | + s = socket.socket(family) |
| 78 | + s.bind((localhost, 0)) |
| 79 | + self.addCleanup(s.close) |
| 80 | + port = s.getsockname()[1] |
| 81 | + with self.assertRaises(server_info.CommunicationError) as cm: |
| 82 | + server_info.fetch_server_info("http://localhost:%d" % port) |
| 83 | + msg = str(cm.exception) |
| 84 | + self.assertIn("Failed to connect to backend", msg) |
| 85 | + if os.name != "nt": |
| 86 | + self.assertIn(os.strerror(errno.ECONNREFUSED), msg) |
| 87 | + |
| 88 | + def test_non_ok_response(self): |
| 89 | + @wrappers.BaseRequest.application |
| 90 | + def app(request): |
| 91 | + del request # unused |
| 92 | + return wrappers.BaseResponse(b"very sad", status="502 Bad Gateway") |
| 93 | + |
| 94 | + origin = self._start_server(app) |
| 95 | + with self.assertRaises(server_info.CommunicationError) as cm: |
| 96 | + server_info.fetch_server_info(origin) |
| 97 | + msg = str(cm.exception) |
| 98 | + self.assertIn("Non-OK status from backend (502 Bad Gateway)", msg) |
| 99 | + self.assertIn("very sad", msg) |
| 100 | + |
| 101 | + def test_corrupt_response(self): |
| 102 | + @wrappers.BaseRequest.application |
| 103 | + def app(request): |
| 104 | + del request # unused |
| 105 | + return wrappers.BaseResponse(b"an unlikely proto") |
| 106 | + |
| 107 | + origin = self._start_server(app) |
| 108 | + with self.assertRaises(server_info.CommunicationError) as cm: |
| 109 | + server_info.fetch_server_info(origin) |
| 110 | + msg = str(cm.exception) |
| 111 | + self.assertIn("Corrupt response from backend", msg) |
| 112 | + self.assertIn("an unlikely proto", msg) |
| 113 | + |
| 114 | + |
| 115 | +class CreateServerInfoTest(tb_test.TestCase): |
| 116 | + """Tests for `create_server_info`.""" |
| 117 | + |
| 118 | + def test(self): |
| 119 | + frontend = "http://localhost:8080" |
| 120 | + backend = "localhost:10000" |
| 121 | + result = server_info.create_server_info(frontend, backend) |
| 122 | + |
| 123 | + expected_compatibility = server_info_pb2.Compatibility() |
| 124 | + expected_compatibility.verdict = server_info_pb2.VERDICT_OK |
| 125 | + expected_compatibility.details = "" |
| 126 | + self.assertEqual(result.compatibility, expected_compatibility) |
| 127 | + |
| 128 | + expected_api_server = server_info_pb2.ApiServer() |
| 129 | + expected_api_server.endpoint = backend |
| 130 | + self.assertEqual(result.api_server, expected_api_server) |
| 131 | + |
| 132 | + url_format = result.url_format |
| 133 | + actual_url = url_format.template.replace(url_format.id_placeholder, "123") |
| 134 | + expected_url = "http://localhost:8080/experiment/123/" |
| 135 | + self.assertEqual(actual_url, expected_url) |
| 136 | + |
| 137 | + |
| 138 | +def _localhost(): |
| 139 | + """Gets family and nodename for a loopback address.""" |
| 140 | + s = socket |
| 141 | + infos = s.getaddrinfo(None, 0, s.AF_UNSPEC, s.SOCK_STREAM, 0, s.AI_ADDRCONFIG) |
| 142 | + (family, _, _, _, address) = infos[0] |
| 143 | + nodename = address[0] |
| 144 | + return (family, nodename) |
| 145 | + |
| 146 | + |
| 147 | +def _make_ipv6_compatible_wsgi_server(): |
| 148 | + """Creates a `WSGIServer` subclass that works on IPv6-only machines.""" |
| 149 | + address_family = _localhost()[0] |
| 150 | + attrs = {"address_family": address_family} |
| 151 | + bases = (simple_server.WSGIServer, object) # `object` needed for py2 |
| 152 | + return type("_Ipv6CompatibleWsgiServer", bases, attrs) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + tb_test.main() |
0 commit comments