Skip to content

test:Refactor DNS upstream resolver to use interface-based client and improve test coverage#1554

Open
adity1raut wants to merge 1 commit intokmesh-net:mainfrom
adity1raut:TestCoverage
Open

test:Refactor DNS upstream resolver to use interface-based client and improve test coverage#1554
adity1raut wants to merge 1 commit intokmesh-net:mainfrom
adity1raut:TestCoverage

Conversation

@adity1raut
Copy link
Contributor

What type of PR is this?

/kind enhancement
/kind security
/kind feature

What this PR does / why we need it:
This PR refactors the DNS upstream resolver to improve testability and code quality by introducing an interface-based DNS client. It removes direct dependency on *dns.Client, enabling clean mocking in unit tests without method overriding.

  • Introduced a DNSExchanger interface to abstract DNS client behavior.
  • Updated UpstreamResolver to depend on the interface instead of *dns.Client.
  • Added a fake DNS client for deterministic and isolated unit testing.
  • Fixed test failures and typecheck issues reported by golangci-lint.
  • Improved unit test coverage for upstream resolution logic, including:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


…t tests for resolver functionality

Signed-off-by: adity1raut <araut7798@gmail.com>
Copilot AI review requested due to automatic review settings December 23, 2025 06:06
@kmesh-bot kmesh-bot added kind/enhancement New feature or request kind/security labels Dec 23, 2025
@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign yaozengzeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link

Summary of Changes

Hello @adity1raut, 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 enhances the testability and robustness of the DNS upstream resolver. By introducing an interface for the DNS client, it removes direct dependencies on external network interactions, allowing for more isolated and reliable unit testing. This refactoring improves the overall code quality and ensures the DNS resolution logic behaves as expected under various conditions.

Highlights

  • Interface Introduction: A "dnsClient" interface has been introduced to abstract the "Exchange" method, decoupling the "upstreamResolver" from the concrete "*dns.Client" implementation.
  • Refactoring upstreamResolver: The "upstreamResolver" struct has been updated to depend on the new "dnsClient" interface, making it more flexible and testable.
  • Enhanced Testability: A "fakeDNSClient" implementation has been added, enabling deterministic and isolated unit testing of the DNS resolution logic without relying on actual network calls.
  • Improved Test Coverage: New unit tests have been added for various "Resolve" scenarios, including single upstream success, fallback to multiple upstreams, and handling cases where all upstreams fail.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.


Interface defined, Tests now run, clean and swift, Code gains strength anew.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 effectively refactors the upstreamResolver to use an interface for the DNS client, which is a great improvement for testability. The introduction of fakeDNSClient and the new unit tests significantly improve the test coverage for the upstream resolution logic.

My main feedback is regarding the error handling strategy in upstreamResolver.Resolve. When all upstream servers fail (e.g., due to network errors), the function currently swallows these errors and returns a synthetic ServerFailure response. This masks the true cause of the failure from callers. I've added a specific comment in upstream_test.go with a suggestion to propagate the error in such scenarios, which would make the resolver more robust and easier to debug. The new test TestResolve_AllUpstreamsFail should be adjusted to reflect this change.

Comment on lines +111 to +112
assert.NoError(t, err)
assert.Equal(t, dns.RcodeServerFailure, out.Rcode)

Choose a reason for hiding this comment

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

high

This test asserts that Resolve returns NoError when all upstreams fail. However, the Resolve function in upstream.go should not swallow errors. When all upstreams are unreachable (e.g., due to network issues), it should propagate an error instead of returning a synthetic RcodeServerFailure response with a nil error. Hiding the error makes it difficult for callers to understand the root cause of the failure.

Please modify upstreamResolver.Resolve to return an error when all upstream queries fail. Consequently, this test should be updated to assert that an error is returned and the response is nil.

Suggested change
assert.NoError(t, err)
assert.Equal(t, dns.RcodeServerFailure, out.Rcode)
assert.Error(t, err)
assert.Nil(t, out)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the DNS upstream resolver to improve testability by introducing an interface-based design pattern. It replaces the embedded *dns.Client with a dnsClient interface, enabling clean dependency injection and comprehensive unit testing without method overriding.

  • Introduced a dnsClient interface to abstract DNS exchange behavior
  • Refactored upstreamResolver to use the interface instead of embedding *dns.Client
  • Added comprehensive unit tests with a fake DNS client for deterministic testing scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
pkg/dns/upstream.go Introduces dnsClient interface and updates upstreamResolver to use it as a field instead of embedding *dns.Client
pkg/dns/upstream_test.go Adds new test file with fakeDNSClient implementation and three test cases covering success, fallback, and failure scenarios, plus a test for WithUpstreams method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

responses: map[string]*dns.Msg{
"1.1.1.1:53": resp,
},
errors: map[string]error{},
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The empty map initialization on this line is unnecessary. In Go, when you check a map with the comma-ok idiom (as done in the Exchange method), a nil map behaves the same as an empty map for lookups. You can safely remove this line to simplify the test setup.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.65%. Comparing base (b85e001) to head (2f06973).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Coverage Δ
pkg/dns/upstream.go 100.00% <100.00%> (+24.00%) ⬆️

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 232fb70...2f06973. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adity1raut
Copy link
Contributor Author

/cc @hzxuzhonghu , @LiZhenCheng9527 PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants