-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add unit tests for DNS dispatcher logic #4
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
Conversation
Introduces comprehensive unit tests for the `DNSDispatcher` to verify core functionality like blocking, caching, and upstream resolution. * Tests successful lookups. * Verifies NXDOMAIN responses for blocked domains. * Checks cache hit and miss behavior. The `github.com/stretchr/testify` dependency was added to enable mocking DNS responses and provide robust assertions.
Summary of ChangesHello @rm-hull, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the test coverage for the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces valuable unit tests for the DNSDispatcher, covering core functionality like allowed/blocked domains and caching. The tests are well-structured, but I've identified a critical issue in TestDNSDispatcher_HandleDNSRequest_MultipleQuestions. The test's assertions are incorrect for the current implementation and, more importantly, they highlight a significant flaw in how the dispatcher handles requests with multiple questions. I've provided a fix for the test and strongly recommend addressing the underlying bug in the production code. I've also included a couple of medium-severity suggestions to improve test maintainability and isolation.
| var blockList = NewBlockList([]string{"ads.0xbt.net"}, 0.0001) | ||
| var upstream = "8.8.8.8:53" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func TestDNSDispatcher_HandleDNSRequest_Allowed(t *testing.T) { | ||
|
|
||
| dispatcher := NewDNSDispatcher(upstream, blockList, 100) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.SetQuestion("google.com.", dns.TypeA) | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Call the method under test | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
|
|
||
| // Assert that the response writer was called with a non-nil message | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeSuccess, writer.WrittenMsg.Rcode) | ||
| } | ||
|
|
||
| func TestDNSDispatcher_HandleDNSRequest_Blocked(t *testing.T) { | ||
|
|
||
| dispatcher := NewDNSDispatcher(upstream, blockList, 100) | ||
|
|
||
| req := new(dns.Msg) | ||
| req.SetQuestion("ads.0xbt.net.", dns.TypeA) | ||
|
|
||
| writer := new(MockResponseWriter) | ||
| writer.On("WriteMsg", mock.Anything).Return(nil) | ||
|
|
||
| // Call the method under test | ||
| dispatcher.HandleDNSRequest(writer, req) | ||
|
|
||
| // Assert that the response has an NXDOMAIN Rcode | ||
| assert.NotNil(t, writer.WrittenMsg) | ||
| assert.Equal(t, dns.RcodeNameError, writer.WrittenMsg.Rcode) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests TestDNSDispatcher_HandleDNSRequest_Allowed and TestDNSDispatcher_HandleDNSRequest_Blocked share a lot of boilerplate code. To improve maintainability and reduce duplication, consider refactoring them into a single table-driven test. This would make it easier to add more test cases in the future.
The `NewDNSDispatcher` function is updated to return an error, propagating registration failures for Prometheus collectors. A new helper function `shouldRegister` handles collector registration, ignoring `AlreadyRegisteredError` but returning other errors. This change ensures that dispatcher creation fails if essential metrics cannot be properly initialized in `internal/dns.go`. Corresponding error handling is added in `internal/app.go` and tests in `internal/dns_test.go` are updated.
This file standardizes formatting rules across the repository.
Key settings include:
* Enforcing spaces for most files (4 spaces for Python,
2 for YAML/JSON).
* Standardizing `lf` line endings and `utf-8` charset.
* Setting specific line length limits for Python code (120)
and docstrings (72).
* Requiring tabs for `Makefile` and **.go** files.
Set up continuous integration using GitHub Actions. The workflow handles: * Building, testing (Go 1.25), linting, and coverage reporting. * Building and pushing the Docker image to GHCR on `main` or tags. Also configure Dependabot to manage dependencies for Go modules, GitHub Actions, and Docker base images.
Align expectations in the multiple questions DNS test with the current implementation bug. If a DNS request contains multiple questions and one query is blocked, the system currently returns `NXDOMAIN` instead of handling the valid queries. This test is updated to reflect that temporary faulty behavior. The true fix is tracked elsewhere.
Pull Request Test Coverage Report for Build 19086843140Details
💛 - Coveralls |
Introduces comprehensive unit tests for the
DNSDispatcherto verify core functionality like blocking, caching, and upstream resolution.The
github.com/stretchr/testifydependency was added to enable mocking DNS responses and provide robust assertions.