Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform test locally instead of using a live dns resolution #13

Merged
merged 1 commit into from
Oct 12, 2021
Merged

Perform test locally instead of using a live dns resolution #13

merged 1 commit into from
Oct 12, 2021

Conversation

thibmeu
Copy link
Contributor

@thibmeu thibmeu commented Oct 12, 2021

Fixes #11.

Tests were relying on an external domain to be configured properly.
Especially, it needed to have both an A and AAAA record configured. It
turns out this domain has changed and no longer has an AAAA record,
making the tests to fail.

This commit updates the tests to use a local DoH resolver, which can be
configured to return deterministic answers. This way, the resolution
does not depend on an external configuration.

@marten-seemann marten-seemann self-requested a review October 12, 2021 09:14
@marten-seemann marten-seemann changed the title Perform test locally instead of using a live dns resolution (#11) Perform test locally instead of using a live dns resolution Oct 12, 2021
Copy link
Contributor

@marten-seemann marten-seemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a mock resolver makes sense to me. It's always good to not depend on an active internet connection and a DNS configuration that we don't control here.

I found a few stylistic nits, see comments.

resolver.go Outdated
@@ -32,7 +32,7 @@ type txtEntry struct {
}

func NewResolver(url string) *Resolver {
if !strings.HasPrefix(url, "https://") {
if !(strings.HasPrefix(url, "https:") || strings.HasPrefix(url, "http:")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand RFC 8484 correctly, DoH is only defined for HTTPS, not for HTTP. Or am I mistaken here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I have changed the test to modify the url field directly.

resolver_test.go Outdated

func mockDoHResolver(msgs map[uint16]*dns.Msg) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't ignore error values. If this fails at some point in the future, this would be quite hard to debug.
Better: pass testing.T to mockDoHResolver, and check the returned error.

@thibmeu
Copy link
Contributor Author

thibmeu commented Oct 12, 2021

In the latest commit:

  • update mockDNSAnswer methods to be more readable.
  • revert the change on resolver.go allowing http://.
  • Update the test to modify the unexported field url, to provide a local DNS over HTTP (not HTTPS) server.
  • Add a check on errors from reading the request body to the mock server.

Copy link
Contributor

@marten-seemann marten-seemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just need to check for one more error return value, then this is ready for merging.

resolver_test.go Outdated
r.Unpack(body)
m := msgs[r.Question[0].Qtype]

b, _ := m.Pack()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another error return value here.

resolver_test.go Outdated
dns.TypeA: mockDNSAnswerA(dns.Fqdn(domain), net.IPv4(127, 0, 0, 1)),
dns.TypeAAAA: mockDNSAnswerAAAA(dns.Fqdn(domain), net.IPv6loopback),
})
defer func() { resolver.Close() }()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defer func() { resolver.Close() }()
defer resolver.Close()

resolver_test.go Outdated
resolver := mockDoHResolver(t, map[uint16]*dns.Msg{
dns.TypeTXT: mockDNSAnswerTXT(dns.Fqdn(domain), []string{"dnslink=/ipns/example.com"}),
})
defer func() { resolver.Close() }()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defer func() { resolver.Close() }()
defer resolver.Close()

Tests were relying on an external domain to be configured properly.
Especially, it needed to have both an A and AAAA record configured. It
turns out this domain has changed and no longer has an AAAA record,
making the tests to fail.

This commit updates the tests to use a local DoH resolver, which can be
configured to return deterministic answers. This way, the resolution
does not depend on an external configuration.
@marten-seemann marten-seemann merged commit 11f3c73 into libp2p:master Oct 12, 2021
@thibmeu thibmeu deleted the fix-tests-ipv6 branch October 12, 2021 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tests failing: no IPv6 address
2 participants