Skip to content

Commit

Permalink
Port test_hosts to SynchronousTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Mar 28, 2020
1 parent 34fd0bd commit b27490c
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions src/twisted/names/test/test_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from __future__ import division, absolute_import

from twisted.trial.unittest import TestCase
from twisted.trial.unittest import SynchronousTestCase
from twisted.python.filepath import FilePath
from twisted.internet.defer import gatherResults

Expand All @@ -22,7 +22,7 @@ def path(self):



class SearchHostsFileTests(TestCase, GoodTempPathMixin):
class SearchHostsFileTests(SynchronousTestCase, GoodTempPathMixin):
"""
Tests for L{searchFileFor}, a helper which finds the first address for a
particular hostname in a I{hosts(5)}-style file.
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_searchFileForAliases(self):



class SearchHostsFileForAllTests(TestCase, GoodTempPathMixin):
class SearchHostsFileForAllTests(SynchronousTestCase, GoodTempPathMixin):
"""
Tests for L{searchFileForAll}, a helper which finds all addresses for a
particular hostname in a I{hosts(5)}-style file.
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_readError(self):



class HostsTests(TestCase, GoodTempPathMixin):
class HostsTests(SynchronousTestCase, GoodTempPathMixin):
"""
Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
"""
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_getHostByName(self):
]
ds = [self.resolver.getHostByName(n).addCallback(self.assertEqual, ip)
for n, ip in data]
return gatherResults(ds)
self.successResultOf(gatherResults(ds))


def test_lookupAddress(self):
Expand All @@ -171,16 +171,13 @@ def test_lookupAddress(self):
records from the hosts file.
"""
d = self.resolver.lookupAddress(b'multiple')
def resolved(results):
answers, authority, additional = results
self.assertEqual(
(RRHeader(b"multiple", A, IN, self.ttl,
Record_A("1.1.1.3", self.ttl)),
RRHeader(b"multiple", A, IN, self.ttl,
Record_A("1.1.1.4", self.ttl))),
answers)
d.addCallback(resolved)
return d
answers, authority, additional = self.successResultOf(d)
self.assertEqual(
(RRHeader(b"multiple", A, IN, self.ttl,
Record_A("1.1.1.3", self.ttl)),
RRHeader(b"multiple", A, IN, self.ttl,
Record_A("1.1.1.4", self.ttl))),
answers)


def test_lookupIPV6Address(self):
Expand All @@ -189,16 +186,13 @@ def test_lookupIPV6Address(self):
with AAAA records from the hosts file.
"""
d = self.resolver.lookupIPV6Address(b'ip6-multiple')
def resolved(results):
answers, authority, additional = results
self.assertEqual(
(RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
Record_AAAA("::3", self.ttl)),
RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
Record_AAAA("::4", self.ttl))),
answers)
d.addCallback(resolved)
return d
answers, authority, additional = self.successResultOf(d)
self.assertEqual(
(RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
Record_AAAA("::3", self.ttl)),
RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
Record_AAAA("::4", self.ttl))),
answers)


def test_lookupAllRecords(self):
Expand All @@ -207,26 +201,26 @@ def test_lookupAllRecords(self):
with A records from the hosts file.
"""
d = self.resolver.lookupAllRecords(b'mixed')
def resolved(results):
answers, authority, additional = results
self.assertEqual(
(RRHeader(b"mixed", A, IN, self.ttl,
Record_A("1.1.1.2", self.ttl)),),
answers)
d.addCallback(resolved)
return d
answers, authority, additional = self.successResultOf(d)
self.assertEqual(
(RRHeader(b"mixed", A, IN, self.ttl,
Record_A("1.1.1.2", self.ttl)),),
answers)


def test_notImplemented(self):
return self.assertFailure(self.resolver.lookupMailExchange(b'EXAMPLE'),
NotImplementedError)
"""
L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver}
methods it doesn't implement.
"""
self.failureResultOf(self.resolver.lookupMailExchange(b'EXAMPLE'),
NotImplementedError)


def test_query(self):
d = self.resolver.query(Query(b'EXAMPLE'))
d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(),
'1.1.1.1'))
return d
[answer], authority, additional = self.successResultOf(d)
self.assertEqual(answer.payload.dottedQuad(), '1.1.1.1')


def test_lookupAddressNotFound(self):
Expand All @@ -235,23 +229,23 @@ def test_lookupAddressNotFound(self):
L{dns.DomainError} if the name passed in has no addresses in the hosts
file.
"""
return self.assertFailure(self.resolver.lookupAddress(b'foueoa'),
DomainError)
self.failureResultOf(self.resolver.lookupAddress(b'foueoa'),
DomainError)


def test_lookupIPV6AddressNotFound(self):
"""
Like L{test_lookupAddressNotFound}, but for
L{hosts.Resolver.lookupIPV6Address}.
"""
return self.assertFailure(self.resolver.lookupIPV6Address(b'foueoa'),
DomainError)
self.failureResultOf(self.resolver.lookupIPV6Address(b'foueoa'),
DomainError)


def test_lookupAllRecordsNotFound(self):
"""
Like L{test_lookupAddressNotFound}, but for
L{hosts.Resolver.lookupAllRecords}.
"""
return self.assertFailure(self.resolver.lookupAllRecords(b'foueoa'),
DomainError)
self.failureResultOf(self.resolver.lookupAllRecords(b'foueoa'),
DomainError)

0 comments on commit b27490c

Please sign in to comment.