-
Notifications
You must be signed in to change notification settings - Fork 46
fix: fall back to lease file when DHCPLeases API is empty #190
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
Open
ajmeese7
wants to merge
8
commits into
fog:master
Choose a base branch
from
ajmeese7:fix/leasefile-fallback-for-external-dhcp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e247897
fix: fall back to lease file when DHCPLeases API is empty
ajmeese7 6d761a9
refactor: harden leasefile fallback and add tests
ajmeese7 3781a55
chore: reorder code
ajmeese7 66bcf68
fix: emit leasefile fallback warning only once per MAC
ajmeese7 6d22927
fix: switch instance var to class var
ajmeese7 974f70e
fix: retain Tempfile reference to prevent GC race
ajmeese7 30fb398
fix: guard leasefile fallback to local connections and satisfy rubocop
ajmeese7 5c8dfe6
Merge branch 'master' into fix/leasefile-fallback-for-external-dhcp
ajmeese7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| require 'test_helper' | ||
| require 'tmpdir' | ||
|
|
||
| class LeasefileFallbackTest < Minitest::Test | ||
| def setup | ||
| @compute = Fog::Compute[:libvirt] | ||
| @server = @compute.servers.new(:name => "test") | ||
| @mac = "52:54:00:01:02:03" | ||
| @net = stub(:name => "default") | ||
| @tmpdir = Dir.mktmpdir("fog-leasefile-test") | ||
| @original_dir = Fog::Libvirt::Compute::Server::DNSMASQ_LEASE_DIR | ||
| Fog::Libvirt::Compute::Server.send(:remove_const, :DNSMASQ_LEASE_DIR) | ||
| Fog::Libvirt::Compute::Server.const_set(:DNSMASQ_LEASE_DIR, @tmpdir) | ||
| end | ||
|
|
||
| def teardown | ||
| FileUtils.remove_entry(@tmpdir) | ||
| Fog::Libvirt::Compute::Server.send(:remove_const, :DNSMASQ_LEASE_DIR) | ||
| Fog::Libvirt::Compute::Server.const_set(:DNSMASQ_LEASE_DIR, @original_dir) | ||
| end | ||
|
|
||
| def test_returns_ip_for_matching_mac | ||
| write_lease_file("default", "1000 52:54:00:01:02:03 192.168.122.10 host1 *\n") | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_equal "192.168.122.10", result | ||
| end | ||
|
|
||
| def test_returns_ip_with_highest_expiry | ||
| content = <<~LEASES | ||
| 1000 52:54:00:01:02:03 192.168.122.10 host1 * | ||
| 2000 52:54:00:01:02:03 192.168.122.20 host1 * | ||
| 1500 52:54:00:01:02:03 192.168.122.15 host1 * | ||
| LEASES | ||
| write_lease_file("default", content) | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_equal "192.168.122.20", result | ||
| end | ||
|
|
||
| def test_mac_matching_is_case_insensitive | ||
| write_lease_file("default", "1000 52:54:00:01:02:03 192.168.122.10 host1 *\n") | ||
| result = @server.send(:ip_address_from_leasefile, @net, "52:54:00:01:02:03".upcase) | ||
| assert_equal "192.168.122.10", result | ||
| end | ||
|
|
||
| def test_returns_nil_when_lease_file_missing | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_nil result | ||
| end | ||
|
|
||
| def test_skips_malformed_lines | ||
| content = <<~LEASES | ||
| short line | ||
| 1000 52:54:00:01:02:03 192.168.122.10 host1 * | ||
| incomplete | ||
| LEASES | ||
| write_lease_file("default", content) | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_equal "192.168.122.10", result | ||
| end | ||
|
|
||
| def test_returns_nil_when_no_mac_matches | ||
| write_lease_file("default", "1000 52:54:00:ff:ff:ff 192.168.122.99 other *\n") | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_nil result | ||
| end | ||
|
|
||
| def test_returns_nil_on_permission_error | ||
| skip("Cannot test permission denial as root") if Process.uid.zero? | ||
| write_lease_file("default", "1000 52:54:00:01:02:03 192.168.122.10 host1 *\n") | ||
| File.chmod(0o000, File.join(@tmpdir, "default.leases")) | ||
| result = @server.send(:ip_address_from_leasefile, @net, @mac) | ||
| assert_nil result | ||
| end | ||
|
|
||
| def test_returns_nil_when_net_name_is_nil | ||
| net_nil_name = stub(:name => nil) | ||
| result = @server.send(:ip_address_from_leasefile, net_nil_name, @mac) | ||
| assert_nil result | ||
| end | ||
|
|
||
| def test_addresses_uses_leasefile_for_local_connection | ||
| write_lease_file("default", "1000 52:54:00:01:02:03 192.168.122.10 host1 *\n") | ||
| stub_addresses_lookup(:remote => false) | ||
| result = @server.send(:addresses) | ||
| assert_equal "192.168.122.10", result[:public].first | ||
| end | ||
|
|
||
| def test_addresses_skips_leasefile_for_remote_connection | ||
| write_lease_file("default", "1000 52:54:00:01:02:03 192.168.122.10 host1 *\n") | ||
| stub_addresses_lookup(:remote => true) | ||
| result = @server.send(:addresses) | ||
| assert_nil result[:public].first | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Wire up the minimum collaborators so #addresses reaches the lease-file | ||
| # fallback: a NIC with no libvirt DHCP lease, and a connection whose URI is | ||
| # local or remote depending on :remote. | ||
| def stub_addresses_lookup(remote:) | ||
| nic = stub(:mac => @mac, :network => "default") | ||
| @server.stubs(:nics).returns([nic]) | ||
| net = stub(:name => "default", :dhcp_leases => []) | ||
| networks = stub | ||
| networks.stubs(:all).returns([net]) | ||
| @server.service.stubs(:networks).returns(networks) | ||
| @server.service.stubs(:uri).returns(stub(:remote? => remote)) | ||
| end | ||
|
|
||
| def write_lease_file(net_name, content) | ||
| File.write(File.join(@tmpdir, "#{net_name}.leases"), content) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.