From f22d4c9d24939afb8f29323adffe3eb570f14804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=3D=3FUTF-8=3Fq=3FIgor=3D20Gali=3DC4=3D87=3F=3D?= Date: Wed, 2 Nov 2016 14:54:28 -0400 Subject: [PATCH] fix "absent" options analogous to redhat, we check if options are absent, before appending them to the file. This fixes #160 Signed-off-by: Allain Legacy --- .../lib/puppet/provider/network_route/redhat.rb | 10 ++--- .../lib/puppet/provider/network_route/routes.rb | 3 +- .../unit/provider/network_route/routes_spec.rb | 48 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb b/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb index 9841c8e..7123d44 100644 --- a/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb +++ b/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb @@ -84,15 +84,11 @@ Puppet::Type.type(:network_route).provide(:redhat) do raise Puppet::Error, "#{provider.name} does not have a #{prop}." if provider.send(prop).nil? end if provider.network == "default" - contents << "#{provider.network} via #{provider.gateway} dev #{provider.interface}\n" + contents << "#{provider.network} via #{provider.gateway} dev #{provider.interface}" else - contents << "#{provider.network}/#{provider.netmask} via #{provider.gateway} dev #{provider.interface}\n" + contents << "#{provider.network}/#{provider.netmask} via #{provider.gateway} dev #{provider.interface}" end - contents << if provider.options == :absent - "\n" - else - " #{provider.options}\n" - end + contents << (provider.options == :absent ? "\n" : " #{provider.options}\n") end contents.join end diff --git a/packstack/puppet/modules/network/lib/puppet/provider/network_route/routes.rb b/packstack/puppet/modules/network/lib/puppet/provider/network_route/routes.rb index 2dd579f..ca7066d 100644 --- a/packstack/puppet/modules/network/lib/puppet/provider/network_route/routes.rb +++ b/packstack/puppet/modules/network/lib/puppet/provider/network_route/routes.rb @@ -93,7 +93,8 @@ Puppet::Type.type(:network_route).provide(:routes) do raise Puppet::Error, "#{provider.name} is missing the required parameter 'gateway'." if provider.gateway.nil? raise Puppet::Error, "#{provider.name} is missing the required parameter 'interface'." if provider.interface.nil? - contents << "#{provider.network} #{provider.netmask} #{provider.gateway} #{provider.interface} #{provider.options}\n" + contents << "#{provider.network} #{provider.netmask} #{provider.gateway} #{provider.interface}" + contents << (provider.options == :absent ? "\n" : " #{provider.options}\n") end contents.join diff --git a/packstack/puppet/modules/network/spec/unit/provider/network_route/routes_spec.rb b/packstack/puppet/modules/network/spec/unit/provider/network_route/routes_spec.rb index 2e55eba..9376739 100644 --- a/packstack/puppet/modules/network/spec/unit/provider/network_route/routes_spec.rb +++ b/packstack/puppet/modules/network/spec/unit/provider/network_route/routes_spec.rb @@ -93,4 +93,52 @@ describe Puppet::Type.type(:network_route).provider(:routes) do end end end + describe 'when formatting simple files' do + let(:route1_provider) do + stub('route1_provider', + name: '172.17.67.0', + network: '172.17.67.0', + netmask: '255.255.255.0', + gateway: '172.18.6.2', + interface: 'vlan200', + options: :absent, + ) + end + + let(:route2_provider) do + stub('lo_provider', + name: '172.28.45.0', + network: '172.28.45.0', + netmask: '255.255.255.0', + gateway: '172.18.6.2', + interface: 'eth0', + options: :absent, + ) + end + + let(:content) { described_class.format_file('', [route1_provider, route2_provider]) } + + describe 'writing the route line' do + it 'should write only fields' do + expect(content.scan(/^172.17.67.0 .*$/).length).to eq(1) + expect(content.scan(/^172.17.67.0 .*$/).first.split(/\s/, 5).length).to eq(4) + end + + it 'should have the correct fields appended' do + expect(content.scan(/^172.17.67.0 .*$/).first).to include('172.17.67.0 255.255.255.0 172.18.6.2 vlan200') + end + + it 'should fail if the netmask property is not defined' do + route2_provider.unstub(:netmask) + route2_provider.stubs(:netmask).returns nil + expect { content }.to raise_exception + end + + it 'should fail if the gateway property is not defined' do + route2_provider.unstub(:gateway) + route2_provider.stubs(:gateway).returns nil + expect { content }.to raise_exception + end + end + end end -- 1.8.3.1