summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2020-01-28 14:14:25 -0800
committerBryan McLellan <btm@loftninjas.org>2020-04-04 01:23:30 -0400
commitbd407d7d55bc1c1dc7780585d970e384b5e64ba5 (patch)
tree4f1ffef1ea11e4471f3073cf83db2bd9b45b0f7e
parent77bbb339501191224801c384541ceda8796a67d4 (diff)
downloadchef-bd407d7d55bc1c1dc7780585d970e384b5e64ba5.tar.gz
debian 10 ifconfig fix
debian 10 version of net-tools switches its --version output to stdout from stderr. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/chef/provider/ifconfig.rb17
-rw-r--r--spec/unit/provider/ifconfig_spec.rb57
2 files changed, 56 insertions, 18 deletions
diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb
index 1575127c25..9ba1d5ed46 100644
--- a/lib/chef/provider/ifconfig.rb
+++ b/lib/chef/provider/ifconfig.rb
@@ -39,6 +39,10 @@ class Chef
attr_accessor :config_template
attr_accessor :config_path
+ # @api private
+ # @return [String] the major.minor of the net-tools version as a string
+ attr_accessor :ifconfig_version
+
def initialize(new_resource, run_context)
super(new_resource, run_context)
@config_template = nil
@@ -54,15 +58,20 @@ class Chef
@ifconfig_version = nil
@net_tools_version = shell_out("ifconfig", "--version")
+ @net_tools_version.stdout.each_line do |line|
+ if line =~ /^net-tools (\d+\.\d+)/
+ @ifconfig_version = line.match(/^net-tools (\d+\.\d+)/)[1]
+ end
+ end
@net_tools_version.stderr.each_line do |line|
- if line =~ /^net-tools (\d+.\d+)/
- @ifconfig_version = line.match(/^net-tools (\d+.\d+)/)[1]
+ if line =~ /^net-tools (\d+\.\d+)/
+ @ifconfig_version = line.match(/^net-tools (\d+\.\d+)/)[1]
end
end
if @ifconfig_version.nil?
raise "net-tools not found - this is required for ifconfig"
- elsif @ifconfig_version.to_f < 2.0
+ elsif @ifconfig_version.to_i < 2
# Example output for 1.60 is as follows: (sanitized but format intact)
# eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
# inet addr:192.168.1.1 Bcast:192.168.0.1 Mask:255.255.248.0
@@ -98,7 +107,7 @@ class Chef
current_resource.mtu(@interface["mtu"])
current_resource.metric(@interface["metric"])
end
- elsif @ifconfig_version.to_f >= 2.0
+ elsif @ifconfig_version.to_i >= 2
# Example output for 2.10-alpha is as follows: (sanitized but format intact)
# eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
# inet 192.168.1.1 netmask 255.255.240.0 broadcast 192.168.0.1
diff --git a/spec/unit/provider/ifconfig_spec.rb b/spec/unit/provider/ifconfig_spec.rb
index 38561d6e49..56d2f250a6 100644
--- a/spec/unit/provider/ifconfig_spec.rb
+++ b/spec/unit/provider/ifconfig_spec.rb
@@ -1,6 +1,6 @@
#
# Author:: Prajakta Purohit (prajakta@chef.io)
-# Copyright:: Copyright 2008-2018, Chef Software Inc.
+# Copyright:: Copyright 2008-2020, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,19 +46,48 @@ describe Chef::Provider::Ifconfig do
ifconfig 1.42 (2001-04-13)
EOS
- before do
- ifconfig = double(stdout: "", exitstatus: 1)
- allow(@provider).to receive(:shell_out_compacted).and_return(ifconfig)
- ifconfig_version = double(stdout: "", stderr: net_tools_version, exitstatus: 4)
- allow(@provider).to receive(:shell_out_compacted).with("ifconfig", "--version").and_return(ifconfig_version)
- @provider.load_current_resource
- end
- it "should track state of ifconfig failure" do
- expect(@provider.instance_variable_get("@status").exitstatus).not_to eq(0)
- end
- it "should thrown an exception when ifconfig fails" do
- @provider.define_resource_requirements
- expect { @provider.process_resource_requirements }.to raise_error Chef::Exceptions::Ifconfig
+ let(:net_tools_version2) { StringIO.new <<~EOS }
+ net-tools 2.10-alpha
+ EOS
+
+ context "when ifconfig returns its version on stdout" do
+ before do
+ ifconfig = double(stdout: "", exitstatus: 1)
+ allow(@provider).to receive(:shell_out_compacted).and_return(ifconfig)
+ ifconfig_version = double(stdout: net_tools_version2, stderr: "", exitstatus: 4)
+ allow(@provider).to receive(:shell_out_compacted).with("ifconfig", "--version").and_return(ifconfig_version)
+ @provider.load_current_resource
+ end
+ it "should track state of ifconfig failure" do
+ expect(@provider.instance_variable_get("@status").exitstatus).not_to eq(0)
+ end
+ it "should thrown an exception when ifconfig fails" do
+ @provider.define_resource_requirements
+ expect { @provider.process_resource_requirements }.to raise_error Chef::Exceptions::Ifconfig
+ end
+ it "should grab the correct major.minor version of net-tools" do
+ expect(@provider.ifconfig_version).to eql("2.10")
+ end
+ end
+
+ context "when ifconfig returns its version on stderr" do
+ before do
+ ifconfig = double(stdout: "", exitstatus: 1)
+ allow(@provider).to receive(:shell_out_compacted).and_return(ifconfig)
+ ifconfig_version = double(stdout: "", stderr: net_tools_version, exitstatus: 4)
+ allow(@provider).to receive(:shell_out_compacted).with("ifconfig", "--version").and_return(ifconfig_version)
+ @provider.load_current_resource
+ end
+ it "should track state of ifconfig failure" do
+ expect(@provider.instance_variable_get("@status").exitstatus).not_to eq(0)
+ end
+ it "should thrown an exception when ifconfig fails" do
+ @provider.define_resource_requirements
+ expect { @provider.process_resource_requirements }.to raise_error Chef::Exceptions::Ifconfig
+ end
+ it "should grab the correct major.minor version of net-tools" do
+ expect(@provider.ifconfig_version).to eql("1.60")
+ end
end
end
describe Chef::Provider::Ifconfig, "action_add" do