• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The gRPC Authors
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"""Tests for an actual dns resolution."""
15
16import logging
17import unittest
18
19import grpc
20
21from tests.unit import test_common
22from tests.unit.framework.common import test_constants
23
24_SERVICE_NAME = "test"
25_METHOD = "METHOD"
26_REQUEST = b"\x00\x00\x00"
27_RESPONSE = _REQUEST
28
29
30_METHOD_HANDLERS = {
31    _METHOD: grpc.unary_unary_rpc_method_handler(
32        lambda request, unused_context: request,
33    )
34}
35
36
37class DNSResolverTest(unittest.TestCase):
38    def setUp(self):
39        self._server = test_common.test_server()
40        self._server.add_registered_method_handlers(
41            _SERVICE_NAME, _METHOD_HANDLERS
42        )
43        self._port = self._server.add_insecure_port("[::]:0")
44        self._server.start()
45
46    def tearDown(self):
47        self._server.stop(None)
48
49    def test_connect_loopback(self):
50        # NOTE(https://github.com/grpc/grpc/issues/18422)
51        # In short, Gevent + C-Ares = Segfault. The C-Ares driver is not
52        # supported by custom io manager like "gevent"
53        # NOTE(b/201064791): use loopback46.unittest.grpc.io since
54        # it returns the expected responses even when DNS64 dns servers
55        # are used on the test worker (and for purposes of this
56        # test the use of loopback4 vs loopback46 makes no difference).
57        with grpc.insecure_channel(
58            "loopback46.unittest.grpc.io:%d" % self._port
59        ) as channel:
60            self.assertEqual(
61                channel.unary_unary(
62                    grpc._common.fully_qualified_method(_SERVICE_NAME, _METHOD),
63                    _registered_method=True,
64                )(
65                    _REQUEST,
66                    timeout=10,
67                ),
68                _RESPONSE,
69            )
70
71
72if __name__ == "__main__":
73    logging.basicConfig()
74    unittest.main(verbosity=2)
75