diff options
author | Tom Doherty <tom.doherty@fixnetix.com> | 2017-09-08 18:31:19 +0100 |
---|---|---|
committer | Tom Doherty <tom.doherty@fixnetix.com> | 2017-09-08 18:31:19 +0100 |
commit | 4aaee228cec3df85018094350d95a90f62e774e4 (patch) | |
tree | 458629752a7f73fe145328d8f97574e0461c1eae | |
parent | d78b719df2a6ef127f7becb9b7b7be25a4260560 (diff) | |
download | chef-4aaee228cec3df85018094350d95a90f62e774e4.tar.gz |
Refactor and add an additional unit test
Signed-off-by: Tom Doherty <tom.doherty@fixnetix.com>
-rw-r--r-- | lib/chef/provider/route.rb | 39 | ||||
-rw-r--r-- | spec/unit/provider/route_spec.rb | 12 |
2 files changed, 35 insertions, 16 deletions
diff --git a/lib/chef/provider/route.rb b/lib/chef/provider/route.rb index 222d35181e..2439f45eda 100644 --- a/lib/chef/provider/route.rb +++ b/lib/chef/provider/route.rb @@ -174,15 +174,7 @@ class Chef conf[dev] = "" if conf[dev].nil? case @action when :add - if resource.target == "default" - network_file_name = "/etc/sysconfig/network" - network_file = Chef::Util::FileEdit.new network_file_name - network_file.search_file_replace_line /^GATEWAY=/, "GATEWAY=#{resource.gateway}" - network_file.insert_line_if_no_match /^GATEWAY=/, "GATEWAY=#{resource.gateway}" - network_file.write_file - else - conf[dev] << config_file_contents(:add, target: resource.target, netmask: resource.netmask, gateway: resource.gateway) if resource.action == [:add] - end + conf[dev] << config_file_contents(:add, target: resource.target, netmask: resource.netmask, gateway: resource.gateway) if resource.action == [:add] when :delete # need to do this for the case when the last route on an int # is removed @@ -190,12 +182,29 @@ class Chef end end conf.each do |k, v| - network_file_name = "/etc/sysconfig/network-scripts/route-#{k}" - converge_by("write route route.#{k}\n#{conf[k]} to #{network_file_name}") do - network_file = ::File.new(network_file_name, "w") - network_file.puts(conf[k]) - Chef::Log.debug("#{new_resource} writing route.#{k}\n#{conf[k]}") - network_file.close + if new_resource.target == "default" + network_file_name = "/etc/sysconfig/network" + converge_by("write route default route to #{network_file_name}") do + Chef::Log.debug("#{new_resource} writing default route #{new_resource.gateway} to #{network_file_name}") + if ::File.exists?(network_file_name) + network_file = ::Chef::Util::FileEdit.new(network_file_name) + network_file.search_file_replace_line /^GATEWAY=/, "GATEWAY=#{new_resource.gateway}" + network_file.insert_line_if_no_match /^GATEWAY=/, "GATEWAY=#{new_resource.gateway}" + network_file.write_file + else + network_file = ::File.new(network_file_name, "w") + network_file.puts("GATEWAY=#{new_resource.gateway}") + network_file.close + end + end + else + network_file_name = "/etc/sysconfig/network-scripts/route-#{k}" + converge_by("write route route.#{k}\n#{conf[k]} to #{network_file_name}") do + network_file = ::File.new(network_file_name, "w") + network_file.puts(conf[k]) + Chef::Log.debug("#{new_resource} writing route.#{k}\n#{conf[k]}") + network_file.close + end end end end diff --git a/spec/unit/provider/route_spec.rb b/spec/unit/provider/route_spec.rb index a9c7d91457..5e655bda0c 100644 --- a/spec/unit/provider/route_spec.rb +++ b/spec/unit/provider/route_spec.rb @@ -226,10 +226,20 @@ describe Chef::Provider::Route do route_file = StringIO.new expect(File).to receive(:new).with("/etc/sysconfig/network-scripts/route-eth0", "w").and_return(route_file) - # Chef::Log.should_receive(:debug).with("route[10.0.0.10] writing route.eth0\n10.0.0.10 via 10.0.0.9\n") @run_context.resource_collection << @new_resource @provider.generate_config end + + end + %w{ centos redhat fedora }.each do |platform| + it "should write a default route file on #{platform} platform" do + @node.automatic_attrs[:platform] = platform + + route_file = StringIO.new + expect(File).to receive(:new).with("/etc/sysconfig/network", "w").and_return(route_file) + @run_context.resource_collection << @default_resource + @default_provider.generate_config + end end it "should put all routes for a device in a route config file" do |