From b23bb69042e3645cc5b665d82f71ae676916d544 Mon Sep 17 00:00:00 2001 From: Jahir <124062934+juddin927@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:47:19 +0100 Subject: [PATCH] Update lease_spec.rb --- dhcp-service/spec/lease_spec.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/dhcp-service/spec/lease_spec.rb b/dhcp-service/spec/lease_spec.rb index 4572242..af54131 100644 --- a/dhcp-service/spec/lease_spec.rb +++ b/dhcp-service/spec/lease_spec.rb @@ -11,25 +11,27 @@ def wait_for_leases(expected_count, retries = 5, delay = 2) return if db_client[:lease4].count == expected_count sleep delay end - #raise "Expected #{expected_count} leases, got #{db_client[:lease4].count}" + raise "Expected #{expected_count} leases, got #{db_client[:lease4].count}" end def wait_for_packet_capture(process, packets, delay = 5) - sleep delay - sleep(packets * 0.1) + sleep delay # Ensure packet capture process has started + sleep(packets * 0.1) # Additional wait time based on number of packets + Process.wait(process) # Ensure the packet capture process has finished end before do db_client[:lease4].truncate db_client.disconnect - Process.fork { `tshark -iany -f 'ip src 172.1.0.10 and udp port 67' -w ./dhcp_offer_packet.pcap -q -a packets:10` } - sleep 10 # Adding a delay to ensure the process is ready end after { db_client.disconnect } context "when ordinary dhcp clients send DHCP requests" do it "provides 10 leases to 10 clients, leases persist in the DB and provides DHCP options from global options" do + pid = Process.fork { exec "tshark -iany -f 'ip src 172.1.0.10 and udp port 67' -w ./dhcp_offer_packet.pcap -q -a packets:10" } + sleep 5 # Ensure the process is ready + `perfdhcp -r 2 \ -n 10 \ -R 10 \ @@ -37,9 +39,9 @@ def wait_for_packet_capture(process, packets, delay = 5) -4 \ -W 20000000 \ 172.1.0.10` + wait_for_leases(10) - wait_for_packet_capture() - expect(db_client[:lease4].count).to eq(10) + wait_for_packet_capture(pid, 10) expect(dhcp_offer_packet_content).to include(File.read("./spec/fixtures/expected_lease_options_ordinary.txt")) expect(dhcp_offer_packet_content).not_to include("Option: (234) Private") end @@ -47,7 +49,10 @@ def wait_for_packet_capture(process, packets, delay = 5) context "when Windows 10 devices with client class value of 'W10TEST' send DHCP requests" do it "provides a lease with DHCP options from global options but overrides dns-name option from client class options" do - `perfdhcp -r 2 \ + pid = Process.fork { exec "tshark -iany -f 'ip src 172.1.0.10 and udp port 67' -w ./dhcp_offer_packet.pcap -q -a packets:3" } + sleep 5 # Ensure the process is ready + + `perfdhcp -r 2 \ -n 3 \ -R 1 \ -b mac=00:0c:01:02:03:04 \ @@ -56,15 +61,19 @@ def wait_for_packet_capture(process, packets, delay = 5) -o 77,57313054455354 \ -W 20000000 \ 172.1.0.10` + wait_for_leases(3) - wait_for_packet_capture() + wait_for_packet_capture(pid, 3) expect(dhcp_offer_packet_content).to include(File.read("./spec/fixtures/expected_lease_options_client_class.txt")) end end context "when Windows 10 devices with delivery optimisation enabled send DHCP requests" do it "provides a lease with DHCP options from global options as well as an additional option: private option 234" do - `perfdhcp -r 2 \ + pid = Process.fork { exec "tshark -iany -f 'ip src 172.1.0.10 and udp port 67' -w ./dhcp_offer_packet.pcap -q -a packets:3" } + sleep 5 # Ensure the process is ready + + `perfdhcp -r 2 \ -n 3 \ -R 1 \ -b mac=00:0c:01:02:03:04 \ @@ -73,8 +82,9 @@ def wait_for_packet_capture(process, packets, delay = 5) -o 55,00EA \ -W 20000000 \ 172.1.0.10` + wait_for_leases(3) - wait_for_packet_capture() + wait_for_packet_capture(pid, 3) expect(dhcp_offer_packet_content).to include(File.read("./spec/fixtures/expected_lease_options_delivery_optimised.txt")) end end