From 612926b0cf41d791459fb8788448ad6632a97dde Mon Sep 17 00:00:00 2001 From: Lukas Zapletal Date: Thu, 25 May 2017 11:11:19 +0200 Subject: [PATCH] Fixes #19666 - add DHCP record type field --- modules/dhcp_common/record.rb | 2 +- modules/dhcp_common/record/lease.rb | 3 ++- modules/dhcp_common/record/reservation.rb | 3 ++- test/dhcp/dhcp_api_test.rb | 32 +++++++++++++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/modules/dhcp_common/record.rb b/modules/dhcp_common/record.rb index 9d85867cf..a487bff4d 100644 --- a/modules/dhcp_common/record.rb +++ b/modules/dhcp_common/record.rb @@ -5,7 +5,7 @@ module Proxy::DHCP # represent a DHCP Record class Record - attr_reader :ip, :mac, :subnet, :options + attr_reader :ip, :mac, :subnet, :options, :type include Proxy::DHCP include Proxy::Log include Proxy::Validations diff --git a/modules/dhcp_common/record/lease.rb b/modules/dhcp_common/record/lease.rb index b36dd057c..065fd7cb1 100644 --- a/modules/dhcp_common/record/lease.rb +++ b/modules/dhcp_common/record/lease.rb @@ -5,6 +5,7 @@ class Lease < Record attr_reader :name, :starts, :ends, :state def initialize(name, ip_address, mac_address, subnet, starts, ends, state, options = {}) + @type = "lease" @name = name || "lease-#{mac_address.tr(':-','')}" @starts = starts @ends = ends @@ -21,7 +22,7 @@ def ==(other) end def to_json(*opts) - Hash[[:name, :ip, :mac, :subnet, :starts, :ends, :state].map{|s| [s, send(s)]}].merge(options).to_json(*opts) + Hash[[:name, :ip, :mac, :subnet, :starts, :ends, :state, :type].map{|s| [s, send(s)]}].merge(options).to_json(*opts) end end end diff --git a/modules/dhcp_common/record/reservation.rb b/modules/dhcp_common/record/reservation.rb index 67d519f82..2949308dc 100644 --- a/modules/dhcp_common/record/reservation.rb +++ b/modules/dhcp_common/record/reservation.rb @@ -5,6 +5,7 @@ class Reservation < Record attr_reader :name def initialize(name, ip_address, mac_address, subnet, options = {}) + @type = "reservation" @name = name super(ip_address, mac_address, subnet, options) end @@ -22,7 +23,7 @@ def ==(other) end def to_json(*opts) - Hash[[:name, :ip, :mac, :subnet].map{|s| [s, send(s)]}].merge(options).to_json(*opts) + Hash[[:name, :ip, :mac, :subnet, :type].map{|s| [s, send(s)]}].merge(options).to_json(*opts) end end end diff --git a/test/dhcp/dhcp_api_test.rb b/test/dhcp/dhcp_api_test.rb index 8f58a21e0..1149314e8 100644 --- a/test/dhcp/dhcp_api_test.rb +++ b/test/dhcp/dhcp_api_test.rb @@ -144,18 +144,39 @@ def test_get_record_for_nonexistent_network assert_equal 404, last_response.status end - def test_get_record_by_ip + def test_get_reservation_record_by_ip @server.expects(:find_records_by_ip).with("192.168.122.0", "192.168.122.1").returns([@reservations.first]) get "/192.168.122.0/ip/192.168.122.1" assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}" expected = [{ - "hostname" =>"test.example.com", - "ip" =>"192.168.122.1", - "mac" =>"00:11:bb:cc:dd:ee", + "type" => "reservation", + "hostname" => "test.example.com", + "ip" => "192.168.122.1", + "mac" => "00:11:bb:cc:dd:ee", "name" => 'test.example.com', - "subnet" =>"192.168.122.0/255.255.255.0" # NOTE: 'subnet' attribute isn't being used by foreman, which adds a 'network' attribute instead + "subnet" => "192.168.122.0/255.255.255.0" + }] + assert_equal expected, JSON.parse(last_response.body) + end + + def test_get_lease_record_by_ip + @server.expects(:find_records_by_ip).with("192.168.122.0", "192.168.122.1").returns([@leases.first]) + + get "/192.168.122.0/ip/192.168.122.1" + + assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}" + + expected = [{ + "ends" => nil, + "ip" => "192.168.122.2", + "mac" => "00:aa:bb:cc:dd:ee", + "name" => "lease-00aabbccddee", + "starts" => "2014-07-12 10:08:29 UTC", + "state" => "active", + "subnet" => "192.168.122.0/255.255.255.0", + "type" => "lease" }] assert_equal expected, JSON.parse(last_response.body) end @@ -179,6 +200,7 @@ def test_get_record_by_mac assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}" expected = { + "type" => "reservation", "hostname" =>"test.example.com", "ip" =>"192.168.122.1", "mac" =>"00:11:bb:cc:dd:ee",