diff options
author | Serdar Sutay <serdar@opscode.com> | 2014-10-27 09:59:23 -0700 |
---|---|---|
committer | Serdar Sutay <serdar@opscode.com> | 2014-10-27 09:59:23 -0700 |
commit | 9040b8f2d9957d373f4f35e10f5a46f1e01a3d61 (patch) | |
tree | d868b2571b64515d998c2806267e092976d7683e | |
parent | 2fa5d97b59f4807a0f04366ad8e3a573685848ea (diff) | |
parent | 436f72844291980ee3a89313dfc62bd2499700f6 (diff) | |
download | ohai-9040b8f2d9957d373f4f35e10f5a46f1e01a3d61.tar.gz |
Merge pull request #437 from opscode/sersut/7.6.0-rel-merge
7.6.0 Release Merge
57 files changed, 1591 insertions, 609 deletions
diff --git a/.travis.yml b/.travis.yml index bf2a4b83..ac384a9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,8 @@ -# Temporary workaround for issue with rubygems 2.2.0 with bundler 1.5 on ruby -# 1.8.7. -# -# A fix has been merged to rubygems but not yet released. See: -# https://github.com/rubygems/rubygems/commit/f8e0f1d5f67cfc4e1966cc1e2db367aebf8a09e4 -# -# See also CHEF-4916 -# -# This workaround should be removed when that fix is released. before_install: - - gem update --system 2.1.11 + - bundle --version - gem --version - rvm: - - 1.8.7 - - 1.9.2 - 1.9.3 - + - 2.0.0 + - 2.1.2 script: bundle exec rake spec diff --git a/CHANGELOG.md b/CHANGELOG.md index a2132007..f2e5bf5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,43 @@ ## Release: 7.6.0 -* Add Powershell plugin +* [**sawanoboly**](https://github.com/sawanoboly) + Retrieve OpenStack-specific metadata. +* [**Olle Lundberg**](https://github.com/lndbrg) + Add Cloudstack support. +* [**Tim Smith**](https://github.com/tas50) + Remove newlines in CPU strings on Darwin. +* [**Nathan Huff**](https://github.com/nhuff) + Match zpool output for OmniOS 151006. +* [**Pavel Yudin**](https://github.com/Kasen) + Add Parallels Cloud Server (PCS) platform support. +* [**Christian Vozar**](https://github.com/christianvozar): + Add Go language plugin. +* [**Phil Dibowitz**](https://github.com/jaymzh): + regression: qualify device names from lsblk +* [**Chris Read**](https://github.com/cread): + Add support for ip version ss131122. +* [**carck**](https://github.com/carck): + Reduce GCE metadata timeout to 6 seconds. +* [**barnabear**](https://github.com/barnabear): + Add Pidora platform detection. +* [**Ben Carpenter**](https://github.com/bcarpenter): + Presume 'latest' API version on 404 from Eucalyptus metadata server. +* [**Nabeel Shahzad**](https://github.com/nshahzad): + Look for any number of spaces between the VxID and the value. +* [**Daniel Searles**](https://github.com/squaresurf): + Removed *.static.cloud-ips.com and fixed the DNS resolution on Rackspace hosts. + +## Release 7.4.0 + +* Added Powershell plugin. -## Release: 7.2.4 +## Release 7.2.4 * [**Phil Dibowitz**](https://github.com/jaymzh): linux::network should handle ECMP routes -## Release: 7.2.2 +## Release 7.2.2 * [**Phil Dibowitz**:](https://github.com/jaymzh) Use lsblk instead of blkid if available. @@ -22,8 +51,8 @@ Added platform_family support for ibm_powerkvm (OHAI-558) * [**Pierre Carrier**:](https://github.com/pcarrier) EC2 metadata errors are unhelpful (OHAI-566) -* [**Elan Ruusamae**:](https://github.com/glensc) - Support deep virtualization systems in node[:virtualization][:systems] (OHAI-182) +* [**Elan Ruusamäe**:](https://github.com/glensc) + Support deep virtualization systems in `node[:virtualization][:systems]` (OHAI-182) * [**Sean Walberg**:](https://github.com/swalberg) :Passwd plugin now ignores duplicate users. (OHAI-561) * [**Joe Richards**:](https://github.com/viyh) @@ -55,6 +84,7 @@ * root_group provider not implemented for Windows (OHAI-491) * `Ohai::Exceptions::AttributeNotFound` errors in Chef's ohai resource * Be reluctant to call something an LXC host (OHAI-573) +* Assume 'latest' metadata versions on 404 ## Release: 7.0.4 @@ -5,10 +5,11 @@ gemspec # https://github.com/sj26/rspec_junit_formatter/pull/14 # rspec_junit_formatter isn't compatible with RSpec3 yet, but is fixed in # master. Once it's released we should remove this. -gem "rspec_junit_formatter", :git => 'git://github.com/sj26/rspec_junit_formatter.git', +gem "rspec_junit_formatter", :git => 'https://github.com/sj26/rspec_junit_formatter.git', :ref => "147836c41fab23ff7b92806f34122c8e5f2ddcad" group :development do + gem "chef", github: "opscode/chef", branch: "master" gem "sigar", :platform => "ruby" gem 'plist' @@ -16,5 +17,3 @@ group :development do # gem 'pry-debugger' # gem 'pry-stack_explorer' end - - @@ -3,7 +3,7 @@ # DESCRIPTION: Ohai detects data about your operating system. It can be used -standalone, but it's primary purpose is to provide node data to Chef. +standalone, but its primary purpose is to provide node data to Chef. Ohai will print out a JSON data blob for all the known data about your system. When used with Chef, that data is reported back via node @@ -66,20 +66,19 @@ Source: * http://github.com/opscode/ohai/tree/master -Tickets/Issues: +Issues: -* http://tickets.opscode.com/ - -(Use the OHAI project) +* https://github.com/opscode/ohai/issues # LICENSE: Ohai - system information application -Author:: Adam Jacob (<adam@getchef.com>) -Copyright:: Copyright (c) 2008-2014 Chef Software, Inc. -License:: Apache License, Version 2.0 +* Author:: Adam Jacob (<adam@getchef.com>) +* Copyright:: Copyright (c) 2008-2014 Chef Software, Inc. +* License:: Apache License, Version 2.0 +```text Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -91,3 +90,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +``` @@ -2,15 +2,15 @@ require 'rubygems' require 'rubygems/package_task' require 'rubygems/specification' require 'date' +require 'ohai/version' gemspec = eval(IO.read("ohai.gemspec")) - Gem::PackageTask.new(gemspec).define desc "install the gem locally" task :install => [:package] do - sh %{gem install pkg/#{ohai}-#{OHAI_VERSION}} + sh %{gem install pkg/ohai-#{Ohai::VERSION}.gem} end begin diff --git a/lib/ohai/hints.rb b/lib/ohai/hints.rb index fc2af32d..c8b0bc1b 100644 --- a/lib/ohai/hints.rb +++ b/lib/ohai/hints.rb @@ -17,7 +17,7 @@ # limitations under the License. # -require 'ffi_yajl/json_gem' +require 'ffi_yajl' module Ohai module Hints diff --git a/lib/ohai/mixin/cloudstack_metadata.rb b/lib/ohai/mixin/cloudstack_metadata.rb new file mode 100644 index 00000000..8ecb9daf --- /dev/null +++ b/lib/ohai/mixin/cloudstack_metadata.rb @@ -0,0 +1,88 @@ +# +# Author:: Olle Lundberg (<geek@nerd.sh>) +# Copyright:: Copyright (c) 2014 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'ohai/mixin/ec2_metadata' +require 'ohai/hints' +require 'net/dhcp' +require 'socket' + + +module Ohai + module Mixin + module CloudstackMetadata + include Ohai::Mixin::Ec2Metadata + + def self.discover_dhcp_server + response = '' + if Ohai::Hints.hint?('cloudstack') + begin + request = DHCP::Discover.new + + listensock = UDPSocket.new + sendsock = UDPSocket.new + + listensock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) + sendsock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) + + sendsock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true) + sendaddr = "<broadcast>" + listenport = 68 + + listensock.bind('', listenport) + sendsock.connect(sendaddr, 67) + + sendsock.send(request.pack, 0) + + data = listensock.recvfrom_nonblock(1500) + rescue Exception => e + if (defined?(IO::WaitReadable) && e.instance_of?(IO::WaitReadable)) || + (e.instance_of?(Errno::EAGAIN) || e.instance_of?(Errno::EWOULDBLOCK)) # This OR branch can be removed when ruby > 1.8.7 + unless IO.select([listensock], nil, nil, 10) + # timeout reached + Ohai::Log.debug("Timeout reached awaiting response from DHCP server") + else + # try to read from the socket again + data = listensock.recvfrom_nonblock(1500) + end + else + Ohai::Log.debug("Exceptions encountered when trying to connect to dhcp server. #{e.message}") + end + ensure + sendsock.close + listensock.close + if data + response = [DHCP::Message.from_udp_payload(data[0]).siaddr].pack('N').unpack('C4').join('.') + end + end + end + response + end + + CLOUDSTACK_METADATA_ADDR = self.discover_dhcp_server unless defined?(CLOUDSTACK_METADATA_ADDR) + + def http_client + Net::HTTP.start(CLOUDSTACK_METADATA_ADDR).tap { |h| h.read_timeout = 600 } + end + + def best_api_version + 'latest' + end + + end + end +end + diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb index d01d5cc1..8d775719 100644 --- a/lib/ohai/mixin/ec2_metadata.rb +++ b/lib/ohai/mixin/ec2_metadata.rb @@ -76,7 +76,10 @@ module Ohai def best_api_version response = http_client.get("/") - unless response.code == '200' + if response.code == '404' + Ohai::Log.debug("Received HTTP 404 from metadata server while determining API version, assuming 'latest'") + return "latest" + elsif response.code != '200' raise "Unable to determine EC2 metadata version (returned #{response.code} response)" end # Note: Sorting the list of versions may have unintended consequences in diff --git a/lib/ohai/mixin/gce_metadata.rb b/lib/ohai/mixin/gce_metadata.rb index 04334880..a3d57ade 100644 --- a/lib/ohai/mixin/gce_metadata.rb +++ b/lib/ohai/mixin/gce_metadata.rb @@ -51,7 +51,7 @@ module Ohai end def http_client - Net::HTTP.start(GCE_METADATA_ADDR).tap {|h| h.read_timeout = 600} + Net::HTTP.start(GCE_METADATA_ADDR).tap {|h| h.read_timeout = 6} end def fetch_metadata(id='') diff --git a/lib/ohai/plugins/aix/kernel.rb b/lib/ohai/plugins/aix/kernel.rb index 91fb58cd..ed189057 100644 --- a/lib/ohai/plugins/aix/kernel.rb +++ b/lib/ohai/plugins/aix/kernel.rb @@ -26,6 +26,20 @@ Ohai.plugin(:Kernel) do kernel[:release] = shell_out("uname -r").stdout.split($/)[0] kernel[:version] = shell_out("uname -v").stdout.split($/)[0] kernel[:machine] = shell_out("uname -p").stdout.split($/)[0] - kernel[:modules] = Mash.new + + modules = Mash.new + so = shell_out("genkex -d") + # Text address Size Data address Size File + # + # f1000000c0338000 77000 f1000000c0390000 1ec8c /usr/lib/drivers/cluster + # 6390000 20000 63a0000 ba8 /usr/lib/drivers/if_en + # f1000000c0318000 20000 f1000000c0320000 17138 /usr/lib/drivers/random + so.stdout.lines do |line| + if line =~ /\s*([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([a-zA-Z0-9\/\._]+)/ + modules[$5] = { :text => { :address => $1, :size => $2 }, :data => { :address => $3, :size => $4 } } + end + end + + kernel[:modules] = modules end end diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index b6dbb22e..0a9b82e9 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -42,14 +42,16 @@ Ohai.plugin(:Network) do iface = Mash.new + network Mash.new unless network + network[:interfaces] = Mash.new unless network[:interfaces] + # :default_interface, :default_gateway - route -n get 0 - so = shell_out("route -n get 0") + so = shell_out("netstat -rn |grep default") so.stdout.lines.each do |line| - case line - when /gateway: (\S+)/ - network[:default_gateway] = $1 - when /interface: (\S+)/ - network[:default_interface] = $1 + items = line.split(' ') + if items[0] == "default" + network[:default_gateway] = items[1] + network[:default_interface] = items[5] end end @@ -90,10 +92,10 @@ Ohai.plugin(:Network) do if line =~ /broadcast\s(\S+)\s/ iface[interface][:addresses][tmp_addr][:broadcast] = $1 end - elsif line =~ /inet6 ([a-f0-9\:%]+)\/(\d+)/ + elsif line =~ /inet6 ([a-f0-9\:]+)%?([\d]*)\/?(\d*)/ # TODO do we have more properties on inet6 in aix? broadcast iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] - iface[interface][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2 } + iface[interface][:addresses][$1] = { "family" => "inet6", "zone_index" => $2, "prefixlen" => $3 } else # load all key-values, example "tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1" properties = line.split diff --git a/lib/ohai/plugins/aix/uptime.rb b/lib/ohai/plugins/aix/uptime.rb index 6c33cb7b..270a692f 100644 --- a/lib/ohai/plugins/aix/uptime.rb +++ b/lib/ohai/plugins/aix/uptime.rb @@ -27,10 +27,10 @@ Ohai.plugin(:Uptime) do so = shell_out('who -b') so.stdout.lines.each do |line| if line =~ /.* boot (.+)/ - uptime_seconds Time.now.to_i - DateTime.parse($1).strftime('%s').to_i + uptime_seconds Time.now.to_i - DateTime.parse($1 + " #{Time.now.zone}").strftime('%s').to_i uptime seconds_to_human(uptime_seconds) break end end end -end +end
\ No newline at end of file diff --git a/lib/ohai/plugins/aix/virtualization.rb b/lib/ohai/plugins/aix/virtualization.rb new file mode 100644 index 00000000..ad625d9b --- /dev/null +++ b/lib/ohai/plugins/aix/virtualization.rb @@ -0,0 +1,39 @@ +# +# Author:: Julian C. Dunn (<jdunn@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +Ohai.plugin(:Virtualization) do + provides "virtualization" + + collect_data(:aix) do + virtualization Mash.new + + so = shell_out("uname -L") + lpar_no = so.stdout.split($/)[0].split(/\s/)[0] + lpar_name = so.stdout.split($/)[0].split(/\s/)[1] + + unless lpar_no.to_i == -1 || (lpar_no.to_i == 1 && lpar_name == "NULL") + virtualization[:lpar_no] = lpar_no + virtualization[:lpar_name] = lpar_name + end + + so = shell_out("uname -W") + wpar_no = so.stdout.split($/)[0] + virtualization[:wpar_no] = wpar_no unless wpar_no.to_i == 0 + + end +end
\ No newline at end of file diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb index d1c28562..ef81d641 100644 --- a/lib/ohai/plugins/cloud.rb +++ b/lib/ohai/plugins/cloud.rb @@ -24,6 +24,7 @@ Ohai.plugin(:Cloud) do depends "linode" depends "openstack" depends "azure" + depends "cloudstack" # Make top-level cloud hashes # @@ -215,6 +216,31 @@ Ohai.plugin(:Cloud) do cloud[:provider] = "azure" end + # ---------------------------------------- + # cloudstack + # ---------------------------------------- + + # Is current cloud cloudstack-based? + # + # === Return + # true:: If cloudstack Hash is defined + # false:: Otherwise + def on_cloudstack? + cloudstack != nil + end + + # Fill cloud hash with cloudstack values + def get_cloudstack_values + cloud[:public_ips] << cloudstack['public_ipv4'] + cloud[:private_ips] << cloudstack['local_ipv4'] + cloud[:public_ipv4] = cloudstack['public_ipv4'] + cloud[:public_hostname] = cloudstack['public_hostname'] + cloud[:local_ipv4] = cloudstack['local_ipv4'] + cloud[:local_hostname] = cloudstack['local_hostname'] + cloud[:vm_id] = cloudstack['vm_id'] + cloud[:provider] = "cloudstack" + end + collect_data do # setup gce cloud if on_gce? @@ -256,5 +282,11 @@ Ohai.plugin(:Cloud) do create_objects get_azure_values end + + # setup cloudstack cloud + if on_cloudstack? + create_objects + get_cloudstack_values + end end end diff --git a/lib/ohai/plugins/cloudstack.rb b/lib/ohai/plugins/cloudstack.rb new file mode 100644 index 00000000..cecd36c8 --- /dev/null +++ b/lib/ohai/plugins/cloudstack.rb @@ -0,0 +1,43 @@ +# +# Author:: Olle Lundberg (<geek@nerd.sh>) +# Copyright:: Copyright (c) 2014 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'ohai/mixin/cloudstack_metadata' + +Ohai.plugin(:Cloudstack) do + provides "cloudstack" + + include Ohai::Mixin::CloudstackMetadata + + collect_data do + # Adds cloudstack Mash + if hint?('cloudstack') + Ohai::Log.debug("found 'cloudstack' hint. Will try to connect to the metadata server") + + if can_metadata_connect?(Ohai::Mixin::CloudstackMetadata::CLOUDSTACK_METADATA_ADDR, 80) + cloudstack Mash.new + Ohai::Log.debug("connecting to the 'cloudstack' metadata service") + fetch_metadata.each { |k, v| cloudstack[k] = v } + else + Ohai::Log.debug("unable to connect to the 'cloudstack' metadata service") + end + else + Ohai::Log.debug("unable to find 'cloudstack' hint. Won't connect to the metadata server.") + end + end +end + + diff --git a/lib/ohai/plugins/darwin/cpu.rb b/lib/ohai/plugins/darwin/cpu.rb index 1c0b6180..ec8f095b 100644 --- a/lib/ohai/plugins/darwin/cpu.rb +++ b/lib/ohai/plugins/darwin/cpu.rb @@ -29,9 +29,9 @@ Ohai.plugin(:CPU) do so = shell_out("sysctl -n hw.cpufrequency") cpu[:mhz] = so.stdout.to_i / 1000000 so = shell_out("sysctl -n machdep.cpu.vendor") - cpu[:vendor_id] = so.stdout + cpu[:vendor_id] = so.stdout.chomp so = shell_out("sysctl -n machdep.cpu.brand_string") - cpu[:model_name] = so.stdout + cpu[:model_name] = so.stdout.chomp so = shell_out("sysctl -n machdep.cpu.model") cpu[:model] = so.stdout.to_i so = shell_out("sysctl -n machdep.cpu.family") diff --git a/lib/ohai/plugins/freebsd/cpu.rb b/lib/ohai/plugins/freebsd/cpu.rb index 8355ec02..88baf2b5 100644 --- a/lib/ohai/plugins/freebsd/cpu.rb +++ b/lib/ohai/plugins/freebsd/cpu.rb @@ -55,6 +55,6 @@ Ohai.plugin(:CPU) do cpu cpuinfo so = shell_out("sysctl -n hw.ncpu") - cpu[:total] = so.stdout.split($/)[0] + cpu[:total] = so.stdout.split($/)[0].to_i end end diff --git a/lib/ohai/plugins/go.rb b/lib/ohai/plugins/go.rb new file mode 100644 index 00000000..44a97d6c --- /dev/null +++ b/lib/ohai/plugins/go.rb @@ -0,0 +1,30 @@ +# Author:: Christian Vozar (<christian@rogueethic.com>) +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Ohai.plugin(:Go) do + provides "languages/go" + depends "languages" + + collect_data do + output = nil + go = Mash.new + so = shell_out("go version") + if so.exitstatus == 0 + output = so.stdout.split + go[:version] = output[2].slice!(2..16) + languages[:go] = go if go[:version] + end + end +end diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb index 303880ed..447b255d 100644 --- a/lib/ohai/plugins/hostname.rb +++ b/lib/ohai/plugins/hostname.rb @@ -67,7 +67,7 @@ Ohai.plugin(:Hostname) do def collect_hostname # Hostname is everything before the first dot if machinename - machinename =~ /(.+?)\./ + machinename =~ /(\w+)\.?/ hostname $1 elsif fqdn fqdn =~ /(.+?)\./ diff --git a/lib/ohai/plugins/java.rb b/lib/ohai/plugins/java.rb index 9f71eb7f..3c83d7cc 100644 --- a/lib/ohai/plugins/java.rb +++ b/lib/ohai/plugins/java.rb @@ -28,9 +28,9 @@ Ohai.plugin(:Java) do case line when /java version \"([0-9\.\_]+)\"/ java[:version] = $1 - when /^(.+Runtime Environment.*) \((build )?(.+)\)$/ + when /^(.+Runtime Environment.*) \((build)\s*(.+)\)$/ java[:runtime] = { "name" => $1, "build" => $3 } - when /^(.+ (Client|Server) VM) \(build (.+)\)$/ + when /^(.+ (Client|Server) VM) \(build\s*(.+)\)$/ java[:hotspot] = { "name" => $1, "build" => $3 } end end diff --git a/lib/ohai/plugins/linux/filesystem.rb b/lib/ohai/plugins/linux/filesystem.rb index b464b4b4..d01d5294 100644 --- a/lib/ohai/plugins/linux/filesystem.rb +++ b/lib/ohai/plugins/linux/filesystem.rb @@ -32,6 +32,14 @@ Ohai.plugin(:Filesystem) do have_lsblk ? /^(\S+) (\S+)/ : /^(\S+): #{attr}="(\S+)"/ end + def find_device(name) + %w{/dev /dev/mapper}.each do |dir| + path = File.join(dir, name) + return path if File.exist?(path) + end + name + end + collect_data(:linux) do fs = Mash.new have_lsblk = File.executable?('/bin/lsblk') @@ -82,7 +90,7 @@ Ohai.plugin(:Filesystem) do end end - have_lsblk = File.exists?('/bin/lsblk') + have_lsblk = File.exist?('/bin/lsblk') # Gather more filesystem types via libuuid, even devices that's aren't mounted cmd = get_blk_cmd('TYPE', have_lsblk) @@ -91,8 +99,10 @@ Ohai.plugin(:Filesystem) do so.stdout.lines do |line| if line =~ regex filesystem = $1 + type = $2 + filesystem = find_device(filesystem) unless filesystem.start_with?('/') fs[filesystem] = Mash.new unless fs.has_key?(filesystem) - fs[filesystem][:fs_type] = $2 + fs[filesystem][:fs_type] = type end end @@ -103,8 +113,10 @@ Ohai.plugin(:Filesystem) do so.stdout.lines do |line| if line =~ regex filesystem = $1 + uuid = $2 + filesystem = find_device(filesystem) unless filesystem.start_with?('/') fs[filesystem] = Mash.new unless fs.has_key?(filesystem) - fs[filesystem][:uuid] = $2 + fs[filesystem][:uuid] = uuid end end @@ -115,13 +127,15 @@ Ohai.plugin(:Filesystem) do so.stdout.lines do |line| if line =~ regex filesystem = $1 + label = $2 + filesystem = find_device(filesystem) unless filesystem.start_with?('/') fs[filesystem] = Mash.new unless fs.has_key?(filesystem) - fs[filesystem][:label] = $2 + fs[filesystem][:label] = label end end # Grab any missing mount information from /proc/mounts - if File.exists?('/proc/mounts') + if File.exist?('/proc/mounts') mounts = '' # Due to https://tickets.opscode.com/browse/OHAI-196 # we have to non-block read dev files. Ew. diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 3d80a057..c6fa8859 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -1,5 +1,6 @@ # # Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Chris Read <chris.read@gmail.com> # Copyright:: Copyright (c) 2008 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -161,10 +162,16 @@ Ohai.plugin(:Network) do net_counters[tmp_int][:tx][:queuelen] = $1 end - if line =~ /vlan id (\d+)/ - tmp_id = $1 + if line =~ /vlan id (\d+)/ or line =~ /vlan protocol ([\w\.]+) id (\d+)/ + if $2 + tmp_prot = $1 + tmp_id = $2 + else + tmp_id = $1 + end iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan] iface[tmp_int][:vlan][:id] = tmp_id + iface[tmp_int][:vlan][:protocol] = tmp_prot if tmp_prot vlan_flags = line.scan(/(REORDER_HDR|GVRP|LOOSE_BINDING)/) if vlan_flags.length > 0 diff --git a/lib/ohai/plugins/linux/platform.rb b/lib/ohai/plugins/linux/platform.rb index 90347a45..ed5f3499 100644 --- a/lib/ohai/plugins/linux/platform.rb +++ b/lib/ohai/plugins/linux/platform.rb @@ -55,6 +55,10 @@ Ohai.plugin(:Platform) do end platform_version File.read("/etc/debian_version").chomp end + elsif File.exists?("/etc/parallels-release") + contents = File.read("/etc/parallels-release").chomp + platform get_redhatish_platform(contents) + platform_version contents.match(/(\d\.\d\.\d)/)[0] elsif File.exists?("/etc/redhat-release") contents = File.read("/etc/redhat-release").chomp platform get_redhatish_platform(contents) @@ -107,9 +111,9 @@ Ohai.plugin(:Platform) do case platform when /debian/, /ubuntu/, /linuxmint/, /raspbian/ platform_family "debian" - when /fedora/ + when /fedora/, /pidora/ platform_family "fedora" - when /oracle/, /centos/, /redhat/, /scientific/, /enterpriseenterprise/, /amazon/, /xenserver/, /cloudlinux/, /ibm_powerkvm/ # Note that 'enterpriseenterprise' is oracle's LSB "distributor ID" + when /oracle/, /centos/, /redhat/, /scientific/, /enterpriseenterprise/, /amazon/, /xenserver/, /cloudlinux/, /ibm_powerkvm/, /parallels/ # Note that 'enterpriseenterprise' is oracle's LSB "distributor ID" platform_family "rhel" when /suse/ platform_family "suse" diff --git a/lib/ohai/plugins/linux/virtualization.rb b/lib/ohai/plugins/linux/virtualization.rb index 49d01565..73ba253a 100644 --- a/lib/ohai/plugins/linux/virtualization.rb +++ b/lib/ohai/plugins/linux/virtualization.rb @@ -137,7 +137,7 @@ Ohai.plugin(:Virtualization) do # Detect Linux-VServer if File.exists?("/proc/self/status") proc_self_status = File.read("/proc/self/status") - vxid = proc_self_status.match(/^(s_context|VxID): (\d+)$/) + vxid = proc_self_status.match(/^(s_context|VxID):\s*(\d+)$/) if vxid and vxid[2] virtualization[:system] = "linux-vserver" if vxid[2] == "0" @@ -158,7 +158,7 @@ Ohai.plugin(:Virtualization) do # /proc/self/cgroup could have a name including alpha/digit/dashes # <index #>:<subsystem>:/lxc/<named container id> # - # /proc/self/cgroup could have a non-lxc cgroup name indicating other uses + # /proc/self/cgroup could have a non-lxc cgroup name indicating other uses # of cgroups. This is probably not LXC/Docker. # <index #>:<subsystem>:/Charlie # diff --git a/lib/ohai/plugins/openstack.rb b/lib/ohai/plugins/openstack.rb index eb357336..28ba3970 100644 --- a/lib/ohai/plugins/openstack.rb +++ b/lib/ohai/plugins/openstack.rb @@ -22,6 +22,26 @@ Ohai.plugin(:Openstack) do include Ohai::Mixin::Ec2Metadata + def collect_openstack_metadata(addr = Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, api_version = '2013-04-04') + path = "/openstack/#{api_version}/meta_data.json" + uri = "http://#{addr}#{path}" + begin + response = http_client.get_response(URI.parse(uri),nil,nil) + case response.code + when '200' + FFI_Yajl::Parser.parse(response.body) + when '404' + Ohai::Log.debug("Encountered 404 response retreiving OpenStack specific metadata path: #{path} ; continuing.") + nil + else + raise "Encountered error retrieving OpenStack specific metadata (#{path} returned #{response.code} response)" + end + rescue => e + Ohai::Log.debug("Encountered error retrieving OpenStack specific metadata (#{uri}), due to #{e.class}") + nil + end + end + collect_data do # Adds openstack Mash if hint?('openstack') || hint?('hp') @@ -36,6 +56,8 @@ Ohai.plugin(:Openstack) do openstack['provider'] = 'hp' else openstack['provider'] = 'openstack' + Ohai::Log.debug("connecting to the OpenStack specific metadata service") + openstack['metadata'] = collect_openstack_metadata end else diff --git a/lib/ohai/plugins/passwd.rb b/lib/ohai/plugins/passwd.rb index 1e10f708..675a70b8 100644 --- a/lib/ohai/plugins/passwd.rb +++ b/lib/ohai/plugins/passwd.rb @@ -32,7 +32,11 @@ Ohai.plugin(:Passwd) do end unless current_user - current_user fix_encoding(Etc.getlogin) + current_user fix_encoding(Etc.getpwuid(Process.euid).name) end end + + collect_data(:windows) do + # Etc returns nil on Windows + end end diff --git a/lib/ohai/plugins/python.rb b/lib/ohai/plugins/python.rb index 683042ba..c6c4dfc8 100644 --- a/lib/ohai/plugins/python.rb +++ b/lib/ohai/plugins/python.rb @@ -6,9 +6,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -26,7 +26,7 @@ Ohai.plugin(:Python) do python = Mash.new - so = shell_out("python -c \"import sys; print sys.version\"") + so = shell_out("python -c \"import sys; print (sys.version)\"") if so.exitstatus == 0 output = so.stdout.split diff --git a/lib/ohai/plugins/rackspace.rb b/lib/ohai/plugins/rackspace.rb index df30e348..8736b370 100644 --- a/lib/ohai/plugins/rackspace.rb +++ b/lib/ohai/plugins/rackspace.rb @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +require "resolv" + Ohai.plugin(:Rackspace) do provides "rackspace" @@ -129,7 +131,11 @@ Ohai.plugin(:Rackspace) do rackspace[:public_ipv4] = rackspace[:public_ip] get_global_ipv6_address(:public_ipv6, :eth0) unless rackspace[:public_ip].nil? - rackspace[:public_hostname] = "#{rackspace[:public_ip].gsub('.','-')}.static.cloud-ips.com" + rackspace[:public_hostname] = begin + Resolv.getname(rackspace[:public_ip]) + rescue Resolv::ResolvError, Resolv::ResolvTimeout + rackspace[:public_ip] + end end rackspace[:local_ipv4] = rackspace[:private_ip] get_global_ipv6_address(:local_ipv6, :eth1) diff --git a/lib/ohai/plugins/solaris2/zpools.rb b/lib/ohai/plugins/solaris2/zpools.rb index f0867cd5..b9b07287 100644 --- a/lib/ohai/plugins/solaris2/zpools.rb +++ b/lib/ohai/plugins/solaris2/zpools.rb @@ -26,7 +26,7 @@ Ohai.plugin(:Zpools) do so = shell_out("zpool list -H -o name,size,alloc,free,cap,dedup,health,version") so.stdout.lines do |line| case line - when /^([-_0-9A-Za-z]*)\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+(\d+%)\s+([.0-9]+x)\s+([-_0-9A-Za-z]+)\s+(\d+)$/ + when /^([-_0-9A-Za-z]*)\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+([.0-9]+[MGTPE])\s+(\d+%)\s+([.0-9]+x)\s+([-_0-9A-Za-z]+)\s+(\d+|-)$/ pools[$1] = Mash.new pools[$1][:pool_size] = $2 pools[$1][:pool_allocated] = $3 diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 69919da9..511fb9ea 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -29,8 +29,6 @@ require 'ohai/provides_map' require 'ohai/hints' require 'mixlib/shellout' -require 'ffi_yajl/json_gem' - module Ohai class System include Ohai::Mixin::ConstantHelper diff --git a/lib/ohai/version.rb b/lib/ohai/version.rb index 4c1ecc87..f1512f27 100644 --- a/lib/ohai/version.rb +++ b/lib/ohai/version.rb @@ -18,5 +18,5 @@ module Ohai OHAI_ROOT = File.expand_path(File.dirname(__FILE__)) - VERSION = '7.4.0' + VERSION = '7.6.0.rc.0' end diff --git a/ohai.gemspec b/ohai.gemspec index 39c60924..d2a462ff 100644 --- a/ohai.gemspec +++ b/ohai.gemspec @@ -8,29 +8,29 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.summary = "Ohai profiles your system and emits JSON" s.description = s.summary + s.license = "Apache-2.0" s.author = "Adam Jacob" s.email = "adam@opscode.com" - s.homepage = "http://wiki.opscode.com/display/chef/Ohai" + s.homepage = "https://docs.getchef.com/ohai.html" s.add_dependency "mime-types", "~> 1.16" s.add_dependency "systemu", "~> 2.6.4" - s.add_dependency "ffi-yajl", "~> 1.0" + s.add_dependency "ffi-yajl", "~> 1.1" s.add_dependency "mixlib-cli" s.add_dependency "mixlib-config", "~> 2.0" s.add_dependency "mixlib-log" - s.add_dependency "mixlib-shellout", "~> 1.2" + s.add_dependency "mixlib-shellout", ">= 2.0.0.rc.0", "< 3.0" + s.add_dependency "net-dhcp" s.add_dependency "ipaddress" s.add_dependency "wmi-lite", "~> 1.0" s.add_dependency "ffi", "~> 1.9" - # Rake 10.2 drops Ruby 1.8 support, so stick to 10.1.x until chef also drops - # 1.8. - s.add_development_dependency "rake", "~> 10.1.0" + s.add_dependency "rake", "~> 10.1" s.add_development_dependency "rspec-core", "~> 3.0" s.add_development_dependency "rspec-expectations", "~> 3.0" s.add_development_dependency "rspec-mocks", "~> 3.0" s.add_development_dependency "rspec-collection_matchers", "~> 1.0" - s.add_development_dependency "rspec_junit_formatter" +# s.add_development_dependency "rspec_junit_formatter" s.add_development_dependency "chef" s.bindir = "bin" s.executables = %w(ohai) diff --git a/platform_simulation_specs/plugins/erlang_spec.rb b/platform_simulation_specs/plugins/erlang_spec.rb index fcee1e64..36b709f4 100644 --- a/platform_simulation_specs/plugins/erlang_spec.rb +++ b/platform_simulation_specs/plugins/erlang_spec.rb @@ -17,7 +17,6 @@ # limitations under the License. # -require 'json' require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') require File.expand_path( File.join( File.dirname( __FILE__ ), '..', 'common', 'ohai_plugin_common.rb' )) diff --git a/platform_simulation_specs/plugins/go_spec.rb b/platform_simulation_specs/plugins/go_spec.rb new file mode 100644 index 00000000..239d0c4e --- /dev/null +++ b/platform_simulation_specs/plugins/go_spec.rb @@ -0,0 +1,30 @@ +# Author:: Christian Vozar (<christian@rogueethic.com>) +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')) +require File.expand_path( File.join( File.dirname( __FILE__ ), '..', 'common', 'ohai_plugin_common.rb' )) + +describe Ohai::System, "plugin go" do + test_plugin([ "languages", "go" ], [ "go" ]) do | p | + p.test([ "centos-6.4", "ubuntu-10.04", "ubuntu-12.04" ], [ "x86", "x64" ], [[]], + { "languages" => { "go" => nil }}) + p.test([ "ubuntu-13.04" ], [ "x64" ], [[]], + { "languages" => { "go" => nil }}) + p.test([ "centos-6.4", "ubuntu-10.04", "ubuntu-12.04" ], [ "x86", "x64" ], [[ "go" ]], + { "languages" => { "go" => { "version" => "0.10.2" }}}) + p.test([ "ubuntu-13.04" ], [ "x64" ], [[ "go" ]], + { "languages" => { "go" => { "version" => "0.10.2" }}}) + end +end diff --git a/spec/unit/mixin/cloudstack_metadata_spec.rb b/spec/unit/mixin/cloudstack_metadata_spec.rb new file mode 100644 index 00000000..24634179 --- /dev/null +++ b/spec/unit/mixin/cloudstack_metadata_spec.rb @@ -0,0 +1,33 @@ +# +# Author:: Olle Lundberg (<geek@nerd.sh>) +# Copyright:: Copyright (c) 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDIT"Net::HTTP Response"NS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') +require 'ohai/mixin/cloudstack_metadata' + +describe Ohai::Mixin::CloudstackMetadata do + let(:mixin) { + metadata_object = Object.new.extend(Ohai::Mixin::CloudstackMetadata) + metadata_object + } + + context "#best_api_version" do + it "returns the string latest" do + mixin.best_api_version.should == "latest" + end + end +end diff --git a/spec/unit/mixin/ec2_metadata_spec.rb b/spec/unit/mixin/ec2_metadata_spec.rb index 1f5a65a2..c9543fe3 100644 --- a/spec/unit/mixin/ec2_metadata_spec.rb +++ b/spec/unit/mixin/ec2_metadata_spec.rb @@ -53,11 +53,12 @@ describe Ohai::Mixin::Ec2Metadata do end end + # Presume 'latest' when we get a 404 for Eucalyptus back-compat context "when the response code is 404" do - let(:response) { double("Net::HTTP Response", :body => "1.0\n2011-05-01\n2012-01-12\nUnsupported", :code => "404") } + let(:response) { double("Net::HTTP Response", :code => "404") } - it "raises an error" do - lambda { mixin.best_api_version}.should raise_error + it "returns 'latest' as the version" do + mixin.best_api_version.should == 'latest' end end diff --git a/spec/unit/plugins/aix/kernel_spec.rb b/spec/unit/plugins/aix/kernel_spec.rb index f1018e5b..6319db9c 100644 --- a/spec/unit/plugins/aix/kernel_spec.rb +++ b/spec/unit/plugins/aix/kernel_spec.rb @@ -25,8 +25,7 @@ describe Ohai::System, "AIX kernel plugin" do @plugin.stub(:shell_out).with("uname -r").and_return(mock_shell_out(0, "1", nil)) @plugin.stub(:shell_out).with("uname -v").and_return(mock_shell_out(0, "6", nil)) @plugin.stub(:shell_out).with("uname -p").and_return(mock_shell_out(0, "powerpc", nil)) - @modules = Mash.new - @plugin.stub(:modules).and_return(@modules) + @plugin.stub(:shell_out).with("genkex -d").and_return(mock_shell_out(0, " Text address Size Data address Size File\nf1000000c0338000 77000 f1000000c0390000 1ec8c /usr/lib/drivers/cluster\n 6390000 20000 63a0000 ba8 /usr/lib/drivers/if_en", nil)) @plugin.run end @@ -47,6 +46,10 @@ describe Ohai::System, "AIX kernel plugin" do end it "detects the modules" do - @plugin[:kernel][:modules].should == @modules + @plugin[:kernel][:modules]["/usr/lib/drivers/cluster"]["text"].should == { "address" => "f1000000c0338000", "size" => "77000" } + @plugin[:kernel][:modules]["/usr/lib/drivers/cluster"]["data"].should == { "address" => "f1000000c0390000", "size" => "1ec8c" } + @plugin[:kernel][:modules]["/usr/lib/drivers/if_en"]["text"].should == { "address" => "6390000", "size" => "20000"} + @plugin[:kernel][:modules]["/usr/lib/drivers/if_en"]["data"].should == { "address" => "63a0000", "size" => "ba8"} + end end diff --git a/spec/unit/plugins/aix/network_spec.rb b/spec/unit/plugins/aix/network_spec.rb index 1aa00a9e..ab4a6ac5 100644 --- a/spec/unit/plugins/aix/network_spec.rb +++ b/spec/unit/plugins/aix/network_spec.rb @@ -20,17 +20,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb') describe Ohai::System, "AIX network plugin" do before(:each) do - @route_n_get_0 = <<-ROUTE_N_GET_0 - route to: default -destination: default - mask: default - gateway: 172.29.128.13 - interface: en0 -interf addr: 172.29.174.58 - flags: <UP,GATEWAY,DONE> - recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire - 0 0 0 0 0 0 0 -79 -ROUTE_N_GET_0 + @netstat_rn_grep_default = <<-NETSTAT_RN_GREP_DEFAULT +default 172.31.8.1 UG 2 121789 en0 - - +NETSTAT_RN_GREP_DEFAULT @lsdev_Cc_if = <<-LSDEV_CC_IF en0 Available Standard Ethernet Network Interface @@ -74,7 +66,7 @@ ARP_AN @plugin = get_plugin("aix/network") @plugin.stub(:collect_os).and_return(:aix) @plugin[:network] = Mash.new - @plugin.stub(:shell_out).with("route -n get 0").and_return(mock_shell_out(0, @route_n_get_0, nil)) + @plugin.stub(:shell_out).with("netstat -rn |grep default").and_return(mock_shell_out(0, @netstat_rn_grep_default, nil)) @plugin.stub(:shell_out).with("lsdev -Cc if").and_return(mock_shell_out(0, @lsdev_Cc_if, nil)) @plugin.stub(:shell_out).with("ifconfig en0").and_return(mock_shell_out(0, @ifconfig_en0, nil)) @plugin.stub(:shell_out).with("entstat -d en0 | grep \"Hardware Address\"").and_return(mock_shell_out(0, "Hardware Address: be:42:80:00:b0:05", nil)) @@ -101,13 +93,13 @@ ARP_AN end end - describe "route -n get 0" do + describe "netstat -rn |grep default" do before do @plugin.run end it "returns the default gateway of the system's network" do - @plugin[:network][:default_gateway].should == '172.29.128.13' + @plugin[:network][:default_gateway].should == '172.31.8.1' end it "returns the default interface of the system's network" do @@ -183,7 +175,7 @@ ARP_AN before do @plugin.stub(:shell_out).with("ifconfig en0").and_return(mock_shell_out(0, "inet6 ::1%1/0", nil)) @plugin.run - @inet_entry = @plugin['network']['interfaces']['en0'][:addresses]["::1%1"] + @inet_entry = @plugin['network']['interfaces']['en0'][:addresses]["::1"] end it "detects the prefixlen" do diff --git a/spec/unit/plugins/aix/uptime_spec.rb b/spec/unit/plugins/aix/uptime_spec.rb index 2f66b031..6d089c8f 100644 --- a/spec/unit/plugins/aix/uptime_spec.rb +++ b/spec/unit/plugins/aix/uptime_spec.rb @@ -22,18 +22,19 @@ describe Ohai::System, "Aix plugin uptime" do before(:each) do @plugin = get_plugin("aix/uptime") @plugin.stub(:collect_os).and_return(:aix) - Time.stub_chain(:now, :to_i).and_return(1374258600) - DateTime.stub_chain(:parse, :strftime, :to_i).and_return(1373392260) - @plugin.stub(:shell_out).with("who -b").and_return(mock_shell_out(0, " . system boot Jul 9 17:51", nil)) + Time.stub_chain(:now, :to_i).and_return(1412072511) + Time.stub_chain(:now, :zone).and_return("IST") + DateTime.stub_chain(:parse, :strftime, :to_i).and_return(1411561320) + @plugin.stub(:shell_out).with("who -b").and_return(mock_shell_out(0, " . system boot Sep 24 17:52", nil)) - @plugin.run + @plugin.run end it "should set uptime_seconds to uptime" do - @plugin[:uptime_seconds].should == 866340 + @plugin[:uptime_seconds].should == 511191 end it "should set uptime to a human readable date" do - @plugin[:uptime].should == "10 days 00 hours 39 minutes 00 seconds" + @plugin[:uptime].should == "5 days 21 hours 59 minutes 51 seconds" end end diff --git a/spec/unit/plugins/aix/virtualization_spec.rb b/spec/unit/plugins/aix/virtualization_spec.rb new file mode 100644 index 00000000..1198e838 --- /dev/null +++ b/spec/unit/plugins/aix/virtualization_spec.rb @@ -0,0 +1,53 @@ +# +# Author:: Julian C. Dunn (<jdunn@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'spec_helper' + +describe Ohai::System, "AIX virtualization plugin" do + + context "inside an LPAR" do + let(:plugin) do + p = get_plugin("aix/virtualization") + allow(p).to receive(:collect_os).and_return(:aix) + allow(p).to receive(:shell_out).with("uname -L").and_return(mock_shell_out(0, "29 l273pp027", nil)) + allow(p).to receive(:shell_out).with("uname -W").and_return(mock_shell_out(0, "0", nil)) + p.run + p + end + + it "uname -L detects the LPAR number and name" do + plugin[:virtualization][:lpar_no].should == "29" + plugin[:virtualization][:lpar_name].should == "l273pp027" + end + end + + context "inside a WPAR" do + let(:plugin) do + p = get_plugin("aix/virtualization") + allow(p).to receive(:collect_os).and_return(:aix) + allow(p).to receive(:shell_out).with("uname -L").and_return(mock_shell_out(0, "43 l33t", nil)) + allow(p).to receive(:shell_out).with("uname -W").and_return(mock_shell_out(0, "42", nil)) + p.run + p + end + + it "uname -W detects the WPAR number" do + plugin[:virtualization][:wpar_no].should == "42" + end + end + +end diff --git a/spec/unit/plugins/cloud_spec.rb b/spec/unit/plugins/cloud_spec.rb index 1c44c823..828ee169 100644 --- a/spec/unit/plugins/cloud_spec.rb +++ b/spec/unit/plugins/cloud_spec.rb @@ -29,6 +29,7 @@ describe Ohai::System, "plugin cloud" do @plugin[:eucalyptus] = nil @plugin[:linode] = nil @plugin[:azure] = nil + @plugin[:cloudstack] = nil @plugin.run @plugin[:cloud].should be_nil end @@ -205,4 +206,34 @@ describe Ohai::System, "plugin cloud" do end end + describe "with cloudstack mash" do + before do + @plugin[:cloudstack] = Mash.new() + end + + it "populates cloud public ip" do + @plugin[:cloudstack]['public_ipv4'] = "174.129.150.8" + @plugin.run + @plugin[:cloud][:public_ips][0].should == @plugin[:cloudstack]['public_ipv4'] + end + + it "populates cloud private ip" do + @plugin[:cloudstack]['local_ipv4'] = "10.252.42.149" + @plugin.run + @plugin[:cloud][:private_ips][0].should == @plugin[:cloudstack]['local_ipv4'] + end + + it "populates cloud provider" do + @plugin.run + @plugin[:cloud][:provider].should == "cloudstack" + end + + it "populates vm id" do + @plugin[:cloudstack]['vm_id'] = "8983fb85-fb7f-46d6-8af1-c1b6666fec39" + @plugin.run + @plugin[:cloud][:vm_id].should == @plugin[:cloudstack]['vm_id'] + end + end + + end diff --git a/spec/unit/plugins/cloudstack_spec.rb b/spec/unit/plugins/cloudstack_spec.rb new file mode 100644 index 00000000..209901c1 --- /dev/null +++ b/spec/unit/plugins/cloudstack_spec.rb @@ -0,0 +1,155 @@ +# +# Author:: Olle Lundberg (<geek@nerd.sh>) +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDIT"Net::HTTP Response"NS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' +require 'ohai/plugins/cloudstack' + +describe "Cloudstack Plugin" do + + let(:cloudstack_hint) { false } + + let(:ohai_system) { Ohai::System.new } + let(:ohai_data) { ohai_system.data } + + let(:cloudstack_plugin) do + plugin = get_plugin("cloudstack", ohai_system) + plugin.stub(:hint?).with("cloudstack").and_return(cloudstack_hint) + plugin + end + + before do + stub_const("Ohai::Mixin::CloudstackMetadata::CLOUDSTACK_METADATA_ADDR", '10.10.10.10') + end + + context "when there is no relevant hint" do + + it "does not set any cloudstack data" do + cloudstack_plugin.run + expect(ohai_data).to_not have_key("cloudstack") + end + + end + + context "when there is a `cloudstack` hint" do + let(:cloudstack_hint) { true } + + context "and the metadata service is not available" do + + before do + cloudstack_plugin.should_receive(:can_metadata_connect?). + with(Ohai::Mixin::CloudstackMetadata::CLOUDSTACK_METADATA_ADDR,80). + and_return(false) + end + + it "does not set any cloudstack data" do + cloudstack_plugin.run + expect(ohai_data).to_not have_key("cloudstack") + end + end + + context "and the metadata service is available" do + + let(:metadata_version) { "latest" } + + let(:metadata_root) do + <<EOM +availability-zone +cloud-identifier +instance-id +local-hostname +local-ipv4 +public-hostname +public-ipv4 +service-offering +vm-id +EOM + end + + let(:metadata_values) do + { + "local-ipv4" =>"10.235.34.23", + "local-hostname" =>"VM-8983fb85-fb7f-46d6-8af1-c1b6666fec39", + "public-hostname" =>"awesome-doge", + "availability-zone" =>"TCS7", + "service-offering" =>"2vCPU, 1GHz, 2GB RAM", + "public-ipv4" =>"10.235.34.23", + "vm-id"=>"8983fb85-fb7f-46d6-8af1-c1b6666fec39", + "cloud-identifier"=>"CloudStack-{e84ff39d-ef64-4812-a8a9-7932f7b67f17}", + "instance-id"=>"8983fb85-fb7f-46d6-8af1-c1b6666fec39" + } + end + + let(:http_client) { double("Net::HTTP", :read_timeout= => nil) } + + def expect_get(url, response_body) + http_client.should_receive(:get). + with(url). + and_return(double("HTTP Response", :code => "200", :body => response_body)) + end + + before do + cloudstack_plugin.should_receive(:can_metadata_connect?). + with(Ohai::Mixin::CloudstackMetadata::CLOUDSTACK_METADATA_ADDR,80). + and_return(true) + + Net::HTTP.stub(:start). + with(Ohai::Mixin::CloudstackMetadata::CLOUDSTACK_METADATA_ADDR). + and_return(http_client) + + cloudstack_plugin.stub(:best_api_version).and_return(metadata_version) + + expect_get("/#{metadata_version}/meta-data/", metadata_root) + + metadata_values.each do |md_id, md_value| + expect_get("/#{metadata_version}/meta-data/#{md_id}", md_value) + end + + cloudstack_plugin.run + end + + it "reads the local ipv4 from the metadata service" do + expect(ohai_data['cloudstack']['local_ipv4']).to eq("10.235.34.23") + end + it "reads the local hostname from the metadata service" do + expect(ohai_data['cloudstack']['local_hostname']).to eq("VM-8983fb85-fb7f-46d6-8af1-c1b6666fec39") + end + it "reads the public hostname from the metadata service" do + expect(ohai_data['cloudstack']['public_hostname']).to eq("awesome-doge") + end + it "reads the availability zone from the metadata service" do + expect(ohai_data['cloudstack']['availability_zone']).to eq("TCS7") + end + it "reads the service offering from the metadata service" do + expect(ohai_data['cloudstack']['service_offering']).to eq("2vCPU, 1GHz, 2GB RAM") + end + it "reads the public ipv4 from the metadata service" do + expect(ohai_data['cloudstack']['public_ipv4']).to eq("10.235.34.23") + end + it "reads the vm id from the metadata service" do + expect(ohai_data['cloudstack']['vm_id']).to eq("8983fb85-fb7f-46d6-8af1-c1b6666fec39") + end + it "reads the cloud identifier from the metadata service" do + expect(ohai_data['cloudstack']['cloud_identifier']).to eq("CloudStack-{e84ff39d-ef64-4812-a8a9-7932f7b67f17}") + end + it "reads the instance id from the metadata service" do + expect(ohai_data['cloudstack']['instance_id']).to eq("8983fb85-fb7f-46d6-8af1-c1b6666fec39") + end + end + end +end + diff --git a/spec/unit/plugins/darwin/cpu_spec.rb b/spec/unit/plugins/darwin/cpu_spec.rb index 36a084dc..9ad385b4 100644 --- a/spec/unit/plugins/darwin/cpu_spec.rb +++ b/spec/unit/plugins/darwin/cpu_spec.rb @@ -26,8 +26,8 @@ describe Ohai::System, "Darwin cpu plugin" do @plugin.stub(:shell_out).with("sysctl -n hw.physicalcpu").and_return(mock_shell_out(0, "4", "")) @plugin.stub(:shell_out).with("sysctl -n hw.logicalcpu").and_return(mock_shell_out(0, "8", "")) @plugin.stub(:shell_out).with("sysctl -n hw.cpufrequency").and_return(mock_shell_out(0, "2300000000", "")) - @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.vendor").and_return(mock_shell_out(0, "GenuineIntel", "")) - @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.brand_string").and_return(mock_shell_out(0, "Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz", "")) + @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.vendor").and_return(mock_shell_out(0, "GenuineIntel\n", "")) + @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.brand_string").and_return(mock_shell_out(0, "Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz\n", "")) @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.model").and_return(mock_shell_out(0, "58", "")) @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.family").and_return(mock_shell_out(0, "6", "")) @plugin.stub(:shell_out).with("sysctl -n machdep.cpu.stepping").and_return(mock_shell_out(0, "9", "")) diff --git a/spec/unit/plugins/erlang_spec.rb b/spec/unit/plugins/erlang_spec.rb index 97c5cb14..c017e1ce 100644 --- a/spec/unit/plugins/erlang_spec.rb +++ b/spec/unit/plugins/erlang_spec.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,6 @@ # limitations under the License. # -require 'json' require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') describe Ohai::System, "plugin erlang" do @@ -28,7 +27,7 @@ describe Ohai::System, "plugin erlang" do @stderr = "Erlang (ASYNC_THREADS,SMP,HIPE) (BEAM) emulator version 5.6.2\n" @plugin.stub(:shell_out).with("erl +V").and_return(mock_shell_out(0, "", @stderr)) end - + it "should get the erlang version from erl +V" do @plugin.should_receive(:shell_out).with("erl +V").and_return(mock_shell_out(0, "", @stderr)) @plugin.run @@ -38,17 +37,17 @@ describe Ohai::System, "plugin erlang" do @plugin.run @plugin.languages[:erlang][:version].should eql("5.6.2") end - + it "should set languages[:erlang][:options]" do @plugin.run @plugin.languages[:erlang][:options].should eql(["ASYNC_THREADS", "SMP", "HIPE"]) end - + it "should set languages[:erlang][:emulator]" do @plugin.run @plugin.languages[:erlang][:emulator].should eql("BEAM") end - + it "should not set the languages[:erlang] tree up if erlang command fails" do @status = 1 @stdin = "" diff --git a/spec/unit/plugins/freebsd/cpu_spec.rb b/spec/unit/plugins/freebsd/cpu_spec.rb index 100c95f3..c988d897 100644 --- a/spec/unit/plugins/freebsd/cpu_spec.rb +++ b/spec/unit/plugins/freebsd/cpu_spec.rb @@ -62,7 +62,7 @@ describe Ohai::System, "FreeBSD cpu plugin" do it "detects all CPU total" do @plugin.run - @plugin[:cpu][:total].should == "2" + @plugin[:cpu][:total].should == 2 end end diff --git a/spec/unit/plugins/go_spec.rb b/spec/unit/plugins/go_spec.rb new file mode 100644 index 00000000..c6658d8a --- /dev/null +++ b/spec/unit/plugins/go_spec.rb @@ -0,0 +1,44 @@ +# Author:: Christian Vozar (<christian@rogueethic.com>) +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')) + +describe Ohai::System, "plugin go" do + + before(:each) do + @plugin = get_plugin("go") + @plugin[:languages] = Mash.new + @stdout = "go version go1.1.2 darwin/amd64\n" + @plugin.stub(:shell_out).with("go version").and_return(mock_shell_out(0, @stdout, "")) + end + + it "should get the go version" do + @plugin.should_receive(:shell_out).with("go version").and_return(mock_shell_out(0, @stdout, "")) + @plugin.run + end + + it "should set languages[:go][:version]" do + @plugin.run + @plugin.languages[:go][:version].should eql("1.1.2") + end + + it "should not set the languages[:go] tree up if go command fails" do + @stdout = "go version go1.1.2 darwin/amd64\n" + @plugin.stub(:shell_out).with("go version").and_return(mock_shell_out(1, @stdout, "")) + @plugin.run + @plugin.languages.should_not have_key(:go) + end + +end diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb index cd778aee..09c9a33e 100644 --- a/spec/unit/plugins/hostname_spec.rb +++ b/spec/unit/plugins/hostname_spec.rb @@ -72,6 +72,18 @@ describe Ohai::System, "hostname plugin" do end end + context "when a system has a bare hostname without a FQDN" do + before(:each) do + @plugin.stub(:collect_os).and_return(:default) + @plugin.stub(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie", "")) + end + + it "should correctly set the [short] hostname" do + @plugin.run + @plugin[:hostname].should == "katie" + end + end + context "hostname --fqdn when it returns empty string" do before(:each) do @plugin.stub(:collect_os).and_return(:linux) diff --git a/spec/unit/plugins/linux/filesystem_spec.rb b/spec/unit/plugins/linux/filesystem_spec.rb index 255b2e66..ffcc5393 100644 --- a/spec/unit/plugins/linux/filesystem_spec.rb +++ b/spec/unit/plugins/linux/filesystem_spec.rb @@ -26,7 +26,7 @@ describe Ohai::System, "Linux filesystem plugin" do @plugin.stub(:shell_out).with("df -P").and_return(mock_shell_out(0, "", "")) @plugin.stub(:shell_out).with("df -i").and_return(mock_shell_out(0, "", "")) @plugin.stub(:shell_out).with("mount").and_return(mock_shell_out(0, "", "")) - File.stub(:exists?).with("/bin/lsblk").and_return(false) + File.stub(:exist?).with("/bin/lsblk").and_return(false) @plugin.stub(:shell_out).with("blkid -s TYPE").and_return(mock_shell_out(0, "", "")) @plugin.stub(:shell_out).with("blkid -s UUID").and_return(mock_shell_out(0, "", "")) @plugin.stub(:shell_out).with("blkid -s LABEL").and_return(mock_shell_out(0, "", "")) @@ -38,7 +38,22 @@ describe Ohai::System, "Linux filesystem plugin" do @plugin.stub(:shell_out).with("lsblk -r -n -o NAME,LABEL"). and_return(mock_shell_out(0, "", "")) - File.stub(:exists?).with("/proc/mounts").and_return(false) + File.stub(:exist?).with("/proc/mounts").and_return(false) + + %w{sdb1 sdb2 sda1 sda2 md0 md1 md2}.each do |name| + File.stub(:exist?).with("/dev/#{name}").and_return(true) + end + %w{ + sys.vg-root.lv + sys.vg-swap.lv + sys.vg-tmp.lv + sys.vg-usr.lv + sys.vg-var.lv + sys.vg-home.lv + }.each do |name| + File.stub(:exist?).with("/dev/#{name}").and_return(false) + File.stub(:exist?).with("/dev/mapper/#{name}").and_return(true) + end end describe "when gathering filesystem usage data from df" do @@ -191,20 +206,20 @@ BLKID_TYPE describe "when gathering filesystem type data from lsblk" do before(:each) do - File.stub(:exists?).with("/bin/lsblk").and_return(true) + File.stub(:exist?).with("/bin/lsblk").and_return(true) @stdout = <<-BLKID_TYPE -/dev/sdb1 linux_raid_member -/dev/sdb2 linux_raid_member -/dev/sda1 linux_raid_member -/dev/sda2 linux_raid_member -/dev/md0 ext3 -/dev/md1 LVM2_member -/dev/mapper/sys.vg-root.lv ext4 -/dev/mapper/sys.vg-swap.lv swap -/dev/mapper/sys.vg-tmp.lv ext4 -/dev/mapper/sys.vg-usr.lv ext4 -/dev/mapper/sys.vg-var.lv ext4 -/dev/mapper/sys.vg-home.lv xfs +sdb1 linux_raid_member +sdb2 linux_raid_member +sda1 linux_raid_member +sda2 linux_raid_member +md0 ext3 +md1 LVM2_member +sys.vg-root.lv ext4 +sys.vg-swap.lv swap +sys.vg-tmp.lv ext4 +sys.vg-usr.lv ext4 +sys.vg-var.lv ext4 +sys.vg-home.lv xfs BLKID_TYPE @plugin.stub(:shell_out).with("lsblk -r -n -o NAME,FSTYPE"). and_return(mock_shell_out(0, @stdout, "")) @@ -254,20 +269,20 @@ BLKID_UUID describe "when gathering filesystem uuid data from lsblk" do before(:each) do - File.stub(:exists?).with("/bin/lsblk").and_return(true) + File.stub(:exist?).with("/bin/lsblk").and_return(true) @stdout = <<-BLKID_UUID -/dev/sdb1 bd1197e0-6997-1f3a-e27e-7801388308b5 -/dev/sdb2 e36d933e-e5b9-cfe5-6845-1f84d0f7fbfa -/dev/sda1 bd1197e0-6997-1f3a-e27e-7801388308b5 -/dev/sda2 e36d933e-e5b9-cfe5-6845-1f84d0f7fbfa -/dev/md0 37b8de8e-0fe3-4b5a-b9b4-dde33e19bb32 -/dev/md1 YsIe0R-fj1y-LXTd-imla-opKo-OuIe-TBoxSK -/dev/mapper/sys.vg-root.lv 7742d14b-80a3-4e97-9a32-478be9ea9aea -/dev/mapper/sys.vg-swap.lv 9bc2e515-8ddc-41c3-9f63-4eaebde9ce96 -/dev/mapper/sys.vg-tmp.lv 74cf7eb9-428f-479e-9a4a-9943401e81e5 -/dev/mapper/sys.vg-usr.lv 26ec33c5-d00b-4f88-a550-492def013bbc -/dev/mapper/sys.vg-var.lv 6b559c35-7847-4ae2-b512-c99012d3f5b3 -/dev/mapper/sys.vg-home.lv d6efda02-1b73-453c-8c74-7d8dee78fa5e +sdb1 bd1197e0-6997-1f3a-e27e-7801388308b5 +sdb2 e36d933e-e5b9-cfe5-6845-1f84d0f7fbfa +sda1 bd1197e0-6997-1f3a-e27e-7801388308b5 +sda2 e36d933e-e5b9-cfe5-6845-1f84d0f7fbfa +md0 37b8de8e-0fe3-4b5a-b9b4-dde33e19bb32 +md1 YsIe0R-fj1y-LXTd-imla-opKo-OuIe-TBoxSK +sys.vg-root.lv 7742d14b-80a3-4e97-9a32-478be9ea9aea +sys.vg-swap.lv 9bc2e515-8ddc-41c3-9f63-4eaebde9ce96 +sys.vg-tmp.lv 74cf7eb9-428f-479e-9a4a-9943401e81e5 +sys.vg-usr.lv 26ec33c5-d00b-4f88-a550-492def013bbc +sys.vg-var.lv 6b559c35-7847-4ae2-b512-c99012d3f5b3 +sys.vg-home.lv d6efda02-1b73-453c-8c74-7d8dee78fa5e BLKID_UUID @plugin.stub(:shell_out).with("lsblk -r -n -o NAME,UUID"). and_return(mock_shell_out(0, @stdout, "")) @@ -316,18 +331,18 @@ BLKID_LABEL describe "when gathering filesystem label data from lsblk" do before(:each) do - File.stub(:exists?).with("/bin/lsblk").and_return(true) + File.stub(:exist?).with("/bin/lsblk").and_return(true) @stdout = <<-BLKID_LABEL -/dev/sda1 fuego:0 -/dev/sda2 fuego:1 -/dev/sdb1 fuego:0 -/dev/sdb2 fuego:1 -/dev/md0 /boot -/dev/mapper/sys.vg-root.lv / -/dev/mapper/sys.vg-tmp.lv /tmp -/dev/mapper/sys.vg-usr.lv /usr -/dev/mapper/sys.vg-var.lv /var -/dev/mapper/sys.vg-home.lv /home +sda1 fuego:0 +sda2 fuego:1 +sdb1 fuego:0 +sdb2 fuego:1 +md0 /boot +sys.vg-root.lv / +sys.vg-tmp.lv /tmp +sys.vg-usr.lv /usr +sys.vg-var.lv /var +sys.vg-home.lv /home BLKID_LABEL @plugin.stub(:shell_out).with("lsblk -r -n -o NAME,LABEL"). and_return(mock_shell_out(0, @stdout, "")) @@ -348,7 +363,7 @@ BLKID_LABEL describe "when gathering data from /proc/mounts" do before(:each) do - File.stub(:exists?).with("/proc/mounts").and_return(true) + File.stub(:exist?).with("/proc/mounts").and_return(true) @double_file = double("/proc/mounts") @mounts = <<-MOUNTS rootfs / rootfs rw 0 0 diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index ae1dd348..d100f8a1 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -1,5 +1,6 @@ # # Author:: Caleb Tennis <caleb.tennis@gmail.com> +# Author:: Chris Read <chris.read@gmail.com> # Copyright:: Copyright (c) 2011 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -25,38 +26,25 @@ rescue LoadError => e raise e end -def do_stubs - @plugin.stub(:collect_os).and_return(:linux) - @plugin.stub(:shell_out).with("ip addr").and_return(mock_shell_out(0, @linux_ip_addr, "")) - @plugin.stub(:shell_out).with("ip -d -s link").and_return(mock_shell_out(0, @linux_ip_link_s_d, "")) - @plugin.stub(:shell_out).with("ip -f inet neigh show").and_return(mock_shell_out(0, @linux_ip_neighbor_show, "")) - @plugin.stub(:shell_out).with("ip -f inet6 neigh show").and_return(mock_shell_out(0, @linux_ip_inet6_neighbor_show, "")) - @plugin.stub(:shell_out).with("ip -o -f inet route show").and_return(mock_shell_out(0, @linux_ip_route, "")) - @plugin.stub(:shell_out).with("ip -o -f inet6 route show").and_return(mock_shell_out(0, @linux_ip_route_inet6, "")) - @plugin.stub(:shell_out).with("route -n").and_return(mock_shell_out(0, @linux_route_n, "")) - @plugin.stub(:shell_out).with("ifconfig -a").and_return(mock_shell_out(0, @linux_ifconfig, "")) - @plugin.stub(:shell_out).with("arp -an").and_return(mock_shell_out(0, @linux_arp_an, "")) -end - describe Ohai::System, "Linux Network Plugin" do + let(:plugin) { get_plugin("linux/network") } - before do - @linux_ifconfig = <<-ENDIFCONFIG -eth0 Link encap:Ethernet HWaddr 12:31:3D:02:BE:A2 + let(:linux_ifconfig) { +'eth0 Link encap:Ethernet HWaddr 12:31:3D:02:BE:A2 inet addr:10.116.201.76 Bcast:10.116.201.255 Mask:255.255.255.0 inet6 addr: fe80::1031:3dff:fe02:bea2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2659966 errors:0 dropped:0 overruns:0 frame:0 TX packets:1919690 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:1000 + collisions:0 txqueuelen:1000 RX bytes:1392844460 (1.2 GiB) TX bytes:691785313 (659.7 MiB) - Interrupt:16 + Interrupt:16 -eth0:5 Link encap:Ethernet HWaddr 00:0c:29:41:71:45 +eth0:5 Link encap:Ethernet HWaddr 00:0c:29:41:71:45 inet addr:192.168.5.1 Bcast:192.168.5.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 -eth0.11 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee +eth0.11 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee inet addr:192.168.0.16 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link inet6 addr: 1111:2222:3333:4444::2/64 Scope:Global @@ -64,78 +52,107 @@ eth0.11 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1208795008 errors:0 dropped:0 overruns:0 frame:0 TX packets:3269635153 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:0 + collisions:0 txqueuelen:0 RX bytes:1751940374 (1.6 GiB) TX bytes:2195567597 (2.0 GiB) -eth0.151 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee +eth0.151 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee inet addr:10.151.0.16 Bcast:10.151.0.255 Mask:255.255.255.0 inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:206553677 errors:0 dropped:0 overruns:0 frame:0 TX packets:163901336 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:0 + collisions:0 txqueuelen:0 RX bytes:3190792261 (2.9 GiB) TX bytes:755086548 (720.1 MiB) -eth0.152 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee +eth0.152 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee inet addr:10.152.1.16 Bcast:10.152.3.255 Mask:255.255.252.0 inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14016741 errors:0 dropped:0 overruns:0 frame:0 TX packets:55232 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:0 + collisions:0 txqueuelen:0 RX bytes:664957462 (634.1 MiB) TX bytes:4876434 (4.6 MiB) -eth0.153 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee +eth0.153 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee inet addr:10.153.1.16 Bcast:10.153.3.255 Mask:255.255.252.0 inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2022667595 errors:0 dropped:0 overruns:0 frame:0 TX packets:1798627472 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:0 + collisions:0 txqueuelen:0 RX bytes:4047036732 (3.7 GiB) TX bytes:3451231474 (3.2 GiB) -foo:veth0@eth0 Link encap:Ethernet HWaddr ca:b3:73:8b:0c:e4 +foo:veth0@eth0 Link encap:Ethernet HWaddr ca:b3:73:8b:0c:e4 BROADCAST MULTICAST MTU:1500 Metric:1 -tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 +tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:172.16.19.39 P-t-P:172.16.19.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1 RX packets:57200 errors:0 dropped:0 overruns:0 frame:0 TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:100 + collisions:0 txqueuelen:100 RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB) -venet0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 +venet0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1 RX packets:57200 errors:0 dropped:0 overruns:0 frame:0 TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:100 + collisions:0 txqueuelen:100 RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB) -venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 +venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1 RX packets:57200 errors:0 dropped:0 overruns:0 frame:0 TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:100 + collisions:0 txqueuelen:100 RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB) -lo Link encap:Local Loopback +lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:524 errors:0 dropped:0 overruns:0 frame:0 TX packets:524 errors:0 dropped:0 overruns:0 carrier:0 - collisions:0 txqueuelen:0 + collisions:0 txqueuelen:0 RX bytes:35224 (34.3 KiB) TX bytes:35224 (34.3 KiB) -ENDIFCONFIG +' # Note that ifconfig shows foo:veth0@eth0 but fails to show any address information. # This was not a mistake collecting the output and Apparently ifconfig is broken in this regard. + } + + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel +192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 +192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 +172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 +192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 +10.5.4.0/24 \\ nexthop via 10.5.4.1 dev eth0 weight 1\\ nexthop via 10.5.4.2 dev eth0 weight 1 +default via 10.116.201.1 dev eth0 +' + } + + let(:linux_route_n) { +'Kernel IP routing table +Destination Gateway Genmask Flags Metric Ref Use Iface +10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 +169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 +0.0.0.0 10.116.201.1 0.0.0.0 UG 0 0 0 eth0 +' + } + + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 +fe80::/64 dev eth0.11 proto kernel metric 256 +1111:2222:3333:4444::/64 dev eth0.11 metric 1024 expires 86023sec +default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 +' + } - @linux_ip_addr = <<-IP_ADDR -1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN + let(:linux_ip_addr) { +'1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo - inet6 ::1/128 scope host + inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff @@ -143,35 +160,35 @@ ENDIFCONFIG inet 10.116.201.75/32 scope global eth0 inet 10.116.201.74/24 scope global secondary eth0 inet 192.168.5.1/24 brd 192.168.5.255 scope global eth0:5 - inet6 fe80::1031:3dff:fe02:bea2/64 scope link + inet6 fe80::1031:3dff:fe02:bea2/64 scope link valid_lft forever preferred_lft forever - inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic + inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic valid_lft 6128sec preferred_lft 2526sec -3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP +3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff inet 192.168.0.16/24 brd 192.168.0.255 scope global eth0.11 - inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link inet6 1111:2222:3333:4444::2/64 scope global valid_lft forever preferred_lft forever inet6 1111:2222:3333:4444::3/64 scope global valid_lft forever preferred_lft forever -4: eth0.151@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP +4: eth0.151@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff inet 10.151.0.16/24 brd 10.151.0.255 scope global eth0.151 inet 10.151.1.16/24 scope global eth0.151 - inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link valid_lft forever preferred_lft forever -5: eth0.152@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP +5: eth0.152@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff inet 10.152.1.16/22 brd 10.152.3.255 scope global eth0.152 - inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link valid_lft forever preferred_lft forever -6: eth0.153@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP +6: eth0.153@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff inet 10.153.1.16/22 brd 10.153.3.255 scope global eth0.153 - inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link valid_lft forever preferred_lft forever -7: foo:veth0@eth0@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN +7: foo:veth0@eth0@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN link/ether ca:b3:73:8b:0c:e4 brd ff:ff:ff:ff:ff:ff inet 192.168.212.2/24 scope global foo:veth0@eth0 8: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 @@ -181,733 +198,724 @@ ENDIFCONFIG link/void inet 127.0.0.2/32 scope host venet0 inet 172.16.19.48/32 scope global venet0:0 -IP_ADDR +' + } - @linux_ip_link_s_d = <<-IP_LINK_S -1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN + let(:linux_ip_link_s_d) { +'1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 - RX: bytes packets errors dropped overrun mcast - 35224 524 0 0 0 0 - TX: bytes packets errors dropped carrier collsns - 35224 524 0 0 0 0 + RX: bytes packets errors dropped overrun mcast + 35224 524 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 35224 524 0 0 0 0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff - RX: bytes packets errors dropped overrun mcast - 1392844460 2659966 0 0 0 0 - TX: bytes packets errors dropped carrier collsns - 691785313 1919690 0 0 0 0 -3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 +3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:0c:29:41:71:45 brd ff:ff:ff:ff:ff:ff - vlan id 11 <REORDER_HDR> - RX: bytes packets errors dropped overrun mcast - 0 0 0 0 0 0 - TX: bytes packets errors dropped carrier collsns - 0 0 0 0 0 0 + vlan id 11 <REORDER_HDR> + RX: bytes packets errors dropped overrun mcast + 0 0 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 0 0 0 0 0 0 4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100 - link/none - RX: bytes packets errors dropped overrun mcast - 1392844460 2659966 0 0 0 0 - TX: bytes packets errors dropped carrier collsns - 691785313 1919690 0 0 0 0 + link/none + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 5: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/void - RX: bytes packets errors dropped overrun mcast - 1392844460 2659966 0 0 0 0 - TX: bytes packets errors dropped carrier collsns - 691785313 1919690 0 0 0 0 -IP_LINK_S - - @linux_route_n = <<-ROUTE_N -Kernel IP routing table -Destination Gateway Genmask Flags Metric Ref Use Iface -10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 -169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 -0.0.0.0 10.116.201.1 0.0.0.0 UG 0 0 0 eth0 -ROUTE_N - - @linux_arp_an = <<-ARP_AN -? (10.116.201.1) at fe:ff:ff:ff:ff:ff [ether] on eth0 -ARP_AN - - @linux_ip_neighbor_show = <<-NEIGHBOR_SHOW -10.116.201.1 dev eth0 lladdr fe:ff:ff:ff:ff:ff REACHABLE -NEIGHBOR_SHOW - - @linux_ip_inet6_neighbor_show = <<-NEIGHBOR_SHOW -1111:2222:3333:4444::1 dev eth0.11 lladdr 00:1c:0e:12:34:56 router REACHABLE + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 +' + } + + let(:linux_arp_an) { +'? (10.116.201.1) at fe:ff:ff:ff:ff:ff [ether] on eth0 +' + } + + let(:linux_ip_neighbor_show) { +'10.116.201.1 dev eth0 lladdr fe:ff:ff:ff:ff:ff REACHABLE +' + } + + let(:linux_ip_inet6_neighbor_show) { +'1111:2222:3333:4444::1 dev eth0.11 lladdr 00:1c:0e:12:34:56 router REACHABLE fe80::21c:eff:fe12:3456 dev eth0.11 lladdr 00:1c:0e:30:28:00 router REACHABLE fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE -NEIGHBOR_SHOW - - @linux_ip_route = <<-IP_ROUTE_SCOPE -10.116.201.0/24 dev eth0 proto kernel -192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 -192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 -172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 -192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 -10.5.4.0/24 \\ nexthop via 10.5.4.1 dev eth0 weight 1\\ nexthop via 10.5.4.2 dev eth0 weight 1 -default via 10.116.201.1 dev eth0 -IP_ROUTE_SCOPE - - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 -fe80::/64 dev eth0.11 proto kernel metric 256 -1111:2222:3333:4444::/64 dev eth0.11 metric 1024 expires 86023sec -default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 -IP_ROUTE_SCOPE - - @plugin = get_plugin("linux/network") - @plugin.stub(:shell_out).with("ifconfig -a").and_return([0, @linux_ifconfig, ""]) - @plugin.stub(:shell_out).with("arp -an").and_return([0, @linux_arp_an, ""]) +' + } + + before(:each) do + allow(plugin).to receive(:collect_os).and_return(:linux) + + allow(plugin).to receive(:shell_out).with("ip addr").and_return(mock_shell_out(0, linux_ip_addr, "")) + allow(plugin).to receive(:shell_out).with("ip -d -s link").and_return(mock_shell_out(0, linux_ip_link_s_d, "")) + allow(plugin).to receive(:shell_out).with("ip -f inet neigh show").and_return(mock_shell_out(0, linux_ip_neighbor_show, "")) + allow(plugin).to receive(:shell_out).with("ip -f inet6 neigh show").and_return(mock_shell_out(0, linux_ip_inet6_neighbor_show, "")) + allow(plugin).to receive(:shell_out).with("ip -o -f inet route show").and_return(mock_shell_out(0, linux_ip_route, "")) + allow(plugin).to receive(:shell_out).with("ip -o -f inet6 route show").and_return(mock_shell_out(0, linux_ip_route_inet6, "")) + + allow(plugin).to receive(:shell_out).with("route -n").and_return(mock_shell_out(0, linux_route_n, "")) + allow(plugin).to receive(:shell_out).with("ifconfig -a").and_return(mock_shell_out(0, linux_ifconfig, "")) + allow(plugin).to receive(:shell_out).with("arp -an").and_return(mock_shell_out(0, linux_arp_an, "")) end ["ifconfig","iproute2"].each do |network_method| describe "gathering IP layer address info via #{network_method}" do - before do - File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) - do_stubs + before(:each) do + allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "detects the interfaces" do - @plugin.run - @plugin['network']['interfaces'].keys.sort.should == ["eth0", "eth0.11", "eth0.151", "eth0.152", "eth0.153", "eth0:5", "foo:veth0@eth0", "lo", "tun0", "venet0", "venet0:0"] + expect(plugin['network']['interfaces'].keys.sort).to eq(["eth0", "eth0.11", "eth0.151", "eth0.152", "eth0.153", "eth0:5", "foo:veth0@eth0", "lo", "tun0", "venet0", "venet0:0"]) end it "detects the ipv4 addresses of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('10.116.201.76') - @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['netmask'].should == '255.255.255.0' - @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['broadcast'].should == '10.116.201.255' - @plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['family'].should == 'inet' + expect(plugin['network']['interfaces']['eth0']['addresses'].keys).to include('10.116.201.76') + expect(plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['netmask']).to eq('255.255.255.0') + expect(plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['broadcast']).to eq('10.116.201.255') + expect(plugin['network']['interfaces']['eth0']['addresses']['10.116.201.76']['family']).to eq('inet') end it "detects the ipv4 addresses of an ethernet subinterface" do - @plugin.run - @plugin['network']['interfaces']['eth0.11']['addresses'].keys.should include('192.168.0.16') - @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['netmask'].should == '255.255.255.0' - @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['broadcast'].should == '192.168.0.255' - @plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['family'].should == 'inet' + expect(plugin['network']['interfaces']['eth0.11']['addresses'].keys).to include('192.168.0.16') + expect(plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['netmask']).to eq('255.255.255.0') + expect(plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['broadcast']).to eq('192.168.0.255') + expect(plugin['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['family']).to eq('inet') end it "detects the ipv6 addresses of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('fe80::1031:3dff:fe02:bea2') - @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['scope'].should == 'Link' - @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['prefixlen'].should == '64' - @plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['family'].should == 'inet6' + expect(plugin['network']['interfaces']['eth0']['addresses'].keys).to include('fe80::1031:3dff:fe02:bea2') + expect(plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['scope']).to eq('Link') + expect(plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['prefixlen']).to eq('64') + expect(plugin['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['family']).to eq('inet6') end it "detects the ipv6 addresses of an ethernet subinterface" do - @plugin.run %w[ 1111:2222:3333:4444::2 1111:2222:3333:4444::3 ].each do |addr| - @plugin['network']['interfaces']['eth0.11']['addresses'].keys.should include(addr) - @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['scope'].should == 'Global' - @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['prefixlen'].should == '64' - @plugin['network']['interfaces']['eth0.11']['addresses'][addr]['family'].should == 'inet6' + expect(plugin['network']['interfaces']['eth0.11']['addresses'].keys).to include(addr) + expect(plugin['network']['interfaces']['eth0.11']['addresses'][addr]['scope']).to eq('Global') + expect(plugin['network']['interfaces']['eth0.11']['addresses'][addr]['prefixlen']).to eq('64') + expect(plugin['network']['interfaces']['eth0.11']['addresses'][addr]['family']).to eq('inet6') end end it "detects the mac addresses of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['addresses'].keys.should include('12:31:3D:02:BE:A2') - @plugin['network']['interfaces']['eth0']['addresses']['12:31:3D:02:BE:A2']['family'].should == 'lladdr' + expect(plugin['network']['interfaces']['eth0']['addresses'].keys).to include('12:31:3D:02:BE:A2') + expect(plugin['network']['interfaces']['eth0']['addresses']['12:31:3D:02:BE:A2']['family']).to eq('lladdr') end it "detects the encapsulation type of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['encapsulation'].should == 'Ethernet' + expect(plugin['network']['interfaces']['eth0']['encapsulation']).to eq('Ethernet') end it "detects the flags of the ethernet interface" do - @plugin.run if network_method == "ifconfig" - @plugin['network']['interfaces']['eth0']['flags'].sort.should == ['BROADCAST','MULTICAST','RUNNING','UP'] + expect(plugin['network']['interfaces']['eth0']['flags'].sort).to eq(['BROADCAST','MULTICAST','RUNNING','UP']) else - @plugin['network']['interfaces']['eth0']['flags'].sort.should == ['BROADCAST','LOWER_UP','MULTICAST','UP'] + expect(plugin['network']['interfaces']['eth0']['flags'].sort).to eq(['BROADCAST','LOWER_UP','MULTICAST','UP']) end end it "detects the number of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['number'].should == "0" + expect(plugin['network']['interfaces']['eth0']['number']).to eq("0") end it "detects the mtu of the ethernet interface" do - @plugin.run - @plugin['network']['interfaces']['eth0']['mtu'].should == "1500" + expect(plugin['network']['interfaces']['eth0']['mtu']).to eq("1500") end - + it "detects the ipv4 addresses of the loopback interface" do - @plugin.run - @plugin['network']['interfaces']['lo']['addresses'].keys.should include('127.0.0.1') - @plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['netmask'].should == '255.0.0.0' - @plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['family'].should == 'inet' + expect(plugin['network']['interfaces']['lo']['addresses'].keys).to include('127.0.0.1') + expect(plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['netmask']).to eq('255.0.0.0') + expect(plugin['network']['interfaces']['lo']['addresses']['127.0.0.1']['family']).to eq('inet') end it "detects the ipv6 addresses of the loopback interface" do - @plugin.run - @plugin['network']['interfaces']['lo']['addresses'].keys.should include('::1') - @plugin['network']['interfaces']['lo']['addresses']['::1']['scope'].should == 'Node' - @plugin['network']['interfaces']['lo']['addresses']['::1']['prefixlen'].should == '128' - @plugin['network']['interfaces']['lo']['addresses']['::1']['family'].should == 'inet6' + expect(plugin['network']['interfaces']['lo']['addresses'].keys).to include('::1') + expect(plugin['network']['interfaces']['lo']['addresses']['::1']['scope']).to eq('Node') + expect(plugin['network']['interfaces']['lo']['addresses']['::1']['prefixlen']).to eq('128') + expect(plugin['network']['interfaces']['lo']['addresses']['::1']['family']).to eq('inet6') end it "detects the encapsulation type of the loopback interface" do - @plugin.run - @plugin['network']['interfaces']['lo']['encapsulation'].should == 'Loopback' + expect(plugin['network']['interfaces']['lo']['encapsulation']).to eq('Loopback') end it "detects the flags of the ethernet interface" do - @plugin.run if network_method == "ifconfig" - @plugin['network']['interfaces']['lo']['flags'].sort.should == ['LOOPBACK','RUNNING','UP'] + expect(plugin['network']['interfaces']['lo']['flags'].sort).to eq(['LOOPBACK','RUNNING','UP']) else - @plugin['network']['interfaces']['lo']['flags'].sort.should == ['LOOPBACK','LOWER_UP','UP'] + expect(plugin['network']['interfaces']['lo']['flags'].sort).to eq(['LOOPBACK','LOWER_UP','UP']) end end it "detects the mtu of the loopback interface" do - @plugin.run - @plugin['network']['interfaces']['lo']['mtu'].should == "16436" + expect(plugin['network']['interfaces']['lo']['mtu']).to eq("16436") end it "detects the arp entries" do - @plugin.run - @plugin['network']['interfaces']['eth0']['arp']['10.116.201.1'].should == 'fe:ff:ff:ff:ff:ff' + expect(plugin['network']['interfaces']['eth0']['arp']['10.116.201.1']).to eq('fe:ff:ff:ff:ff:ff') end end - + describe "gathering interface counters via #{network_method}" do - before do - File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) - do_stubs - @plugin.run + before(:each) do + allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + plugin.run end it "detects the ethernet counters" do - @plugin['counters']['network']['interfaces']['eth0']['tx']['bytes'].should == "691785313" - @plugin['counters']['network']['interfaces']['eth0']['tx']['packets'].should == "1919690" - @plugin['counters']['network']['interfaces']['eth0']['tx']['collisions'].should == "0" - @plugin['counters']['network']['interfaces']['eth0']['tx']['queuelen'].should == "1000" - @plugin['counters']['network']['interfaces']['eth0']['tx']['errors'].should == "0" - @plugin['counters']['network']['interfaces']['eth0']['tx']['carrier'].should == "0" - @plugin['counters']['network']['interfaces']['eth0']['tx']['drop'].should == "0" + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['bytes']).to eq("691785313") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['packets']).to eq("1919690") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['collisions']).to eq("0") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['queuelen']).to eq("1000") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['errors']).to eq("0") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['carrier']).to eq("0") + expect(plugin['counters']['network']['interfaces']['eth0']['tx']['drop']).to eq("0") - @plugin['counters']['network']['interfaces']['eth0']['rx']['bytes'].should == "1392844460" - @plugin['counters']['network']['interfaces']['eth0']['rx']['packets'].should == "2659966" - @plugin['counters']['network']['interfaces']['eth0']['rx']['errors'].should == "0" - @plugin['counters']['network']['interfaces']['eth0']['rx']['overrun'].should == "0" - @plugin['counters']['network']['interfaces']['eth0']['rx']['drop'].should == "0" + expect(plugin['counters']['network']['interfaces']['eth0']['rx']['bytes']).to eq("1392844460") + expect(plugin['counters']['network']['interfaces']['eth0']['rx']['packets']).to eq("2659966") + expect(plugin['counters']['network']['interfaces']['eth0']['rx']['errors']).to eq("0") + expect(plugin['counters']['network']['interfaces']['eth0']['rx']['overrun']).to eq("0") + expect(plugin['counters']['network']['interfaces']['eth0']['rx']['drop']).to eq("0") end it "detects the loopback counters" do - @plugin['counters']['network']['interfaces']['lo']['tx']['bytes'].should == "35224" - @plugin['counters']['network']['interfaces']['lo']['tx']['packets'].should == "524" - @plugin['counters']['network']['interfaces']['lo']['tx']['collisions'].should == "0" - @plugin['counters']['network']['interfaces']['lo']['tx']['errors'].should == "0" - @plugin['counters']['network']['interfaces']['lo']['tx']['carrier'].should == "0" - @plugin['counters']['network']['interfaces']['lo']['tx']['drop'].should == "0" - - @plugin['counters']['network']['interfaces']['lo']['rx']['bytes'].should == "35224" - @plugin['counters']['network']['interfaces']['lo']['rx']['packets'].should == "524" - @plugin['counters']['network']['interfaces']['lo']['rx']['errors'].should == "0" - @plugin['counters']['network']['interfaces']['lo']['rx']['overrun'].should == "0" - @plugin['counters']['network']['interfaces']['lo']['rx']['drop'].should == "0" + expect(plugin['counters']['network']['interfaces']['lo']['tx']['bytes']).to eq("35224") + expect(plugin['counters']['network']['interfaces']['lo']['tx']['packets']).to eq("524") + expect(plugin['counters']['network']['interfaces']['lo']['tx']['collisions']).to eq("0") + expect(plugin['counters']['network']['interfaces']['lo']['tx']['errors']).to eq("0") + expect(plugin['counters']['network']['interfaces']['lo']['tx']['carrier']).to eq("0") + expect(plugin['counters']['network']['interfaces']['lo']['tx']['drop']).to eq("0") + + expect(plugin['counters']['network']['interfaces']['lo']['rx']['bytes']).to eq("35224") + expect(plugin['counters']['network']['interfaces']['lo']['rx']['packets']).to eq("524") + expect(plugin['counters']['network']['interfaces']['lo']['rx']['errors']).to eq("0") + expect(plugin['counters']['network']['interfaces']['lo']['rx']['overrun']).to eq("0") + expect(plugin['counters']['network']['interfaces']['lo']['rx']['drop']).to eq("0") end end describe "setting the node's default IP address attribute with #{network_method}" do - before do - File.stub(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) - do_stubs + before(:each) do + allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + plugin.run end describe "without a subinterface" do - before do - @plugin.run - end - it "finds the default interface by asking which iface has the default route" do - @plugin['network']['default_interface'].should == 'eth0' + expect(plugin['network']['default_interface']).to eq('eth0') end - + it "finds the default gateway by asking which iface has the default route" do - @plugin['network']['default_gateway'].should == '10.116.201.1' + expect(plugin['network']['default_gateway']).to eq('10.116.201.1') end end - + describe "with a link level default route" do - before do - @linux_ip_route = <<-IP_ROUTE -10.116.201.0/24 dev eth0 proto kernel + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel default dev eth0 scope link -IP_ROUTE - @linux_route_n = <<-ROUTE_N -Kernel IP routing table +' + } + + let(:linux_route_n) { +'Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth0 -ROUTE_N - do_stubs +' + } - @plugin.run + before(:each) do + plugin.run end it "finds the default interface by asking which iface has the default route" do - @plugin['network']['default_interface'].should == 'eth0' + expect(plugin['network']['default_interface']).to eq('eth0') end - + it "finds the default interface by asking which iface has the default route" do - @plugin['network']['default_gateway'].should == '0.0.0.0' + expect(plugin['network']['default_gateway']).to eq('0.0.0.0') end end describe "with a subinterface" do - before do - @linux_ip_route = <<-IP_ROUTE -192.168.0.0/24 dev eth0.11 proto kernel src 192.168.0.2 + let(:linux_ip_route) { +'192.168.0.0/24 dev eth0.11 proto kernel src 192.168.0.2 default via 192.168.0.15 dev eth0.11 -IP_ROUTE - @linux_route_n = <<-ROUTE_N -Kernel IP routing table +' + } + + let(:linux_route_n) { +'Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0.11 0.0.0.0 192.168.0.15 0.0.0.0 UG 0 0 0 eth0.11 -ROUTE_N - - do_stubs +' + } - @plugin.run + before(:each) do + plugin.run end - + it "finds the default interface by asking which iface has the default route" do - @plugin['network']["default_interface"].should == 'eth0.11' + expect(plugin['network']["default_interface"]).to eq('eth0.11') end - + it "finds the default interface by asking which iface has the default route" do - @plugin['network']["default_gateway"].should == '192.168.0.15' + expect(plugin['network']["default_gateway"]).to eq('192.168.0.15') end end end end describe "for newer network features using iproute2 only" do - before do - File.stub(:exist?).with("/sbin/ip").and_return(true) # iproute2 only - do_stubs + before(:each) do + allow(File).to receive(:exist?).with("/sbin/ip").and_return(true) # iproute2 only + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "finds the default inet6 interface if there's a inet6 default route" do - @plugin.run - @plugin['network']['default_inet6_interface'].should == 'eth0.11' + expect(plugin['network']['default_inet6_interface']).to eq('eth0.11') end it "finds the default inet6 gateway if there's a inet6 default route" do - @plugin.run - @plugin['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::1' + expect(plugin['network']['default_inet6_gateway']).to eq('1111:2222:3333:4444::1') end it "finds inet6 neighbours" do - @plugin.run - @plugin['network']['interfaces']['eth0.11']['neighbour_inet6']['1111:2222:3333:4444::1'].should == '00:1c:0e:12:34:56' + expect(plugin['network']['interfaces']['eth0.11']['neighbour_inet6']['1111:2222:3333:4444::1']).to eq('00:1c:0e:12:34:56') end it "detects the ipv4 addresses of an ethernet interface with a crazy name" do - @plugin.run - @plugin['network']['interfaces']['foo:veth0@eth0']['addresses'].keys.should include('192.168.212.2') - @plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['netmask'].should == '255.255.255.0' - @plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['family'].should == 'inet' + expect(plugin['network']['interfaces']['foo:veth0@eth0']['addresses'].keys).to include('192.168.212.2') + expect(plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['netmask']).to eq('255.255.255.0') + expect(plugin['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['family']).to eq('inet') end it "generates a fake interface for ip aliases for backward compatibility" do - @plugin.run - @plugin['network']['interfaces']['eth0:5']['addresses'].keys.should include('192.168.5.1') - @plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['netmask'].should == '255.255.255.0' - @plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['family'].should == 'inet' + expect(plugin['network']['interfaces']['eth0:5']['addresses'].keys).to include('192.168.5.1') + expect(plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['netmask']).to eq('255.255.255.0') + expect(plugin['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['family']).to eq('inet') end it "adds the vlan information of an interface" do - @plugin.run - @plugin['network']['interfaces']['eth0.11']['vlan']['id'].should == '11' - @plugin['network']['interfaces']['eth0.11']['vlan']['flags'].should == [ 'REORDER_HDR' ] + expect(plugin['network']['interfaces']['eth0.11']['vlan']['id']).to eq('11') + expect(plugin['network']['interfaces']['eth0.11']['vlan']['flags']).to eq([ 'REORDER_HDR' ]) end it "adds the state of an interface" do - @plugin.run - @plugin['network']['interfaces']['eth0.11']['state'].should == 'up' + expect(plugin['network']['interfaces']['eth0.11']['state']).to eq('up') end describe "when dealing with routes" do it "adds routes" do - @plugin.run - @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "10.116.201.0/24", :proto => "kernel", :family =>"inet" ) - @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "10.5.4.0/24", :family =>"inet", :via => "10.5.4.1") - @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "10.5.4.0/24", :family =>"inet", :via => "10.5.4.2") - @plugin['network']['interfaces']['foo:veth0@eth0']['routes'].should include Mash.new( :destination => "192.168.212.0/24", :proto => "kernel", :src => "192.168.212.2", :family =>"inet" ) - @plugin['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "fe80::/64", :metric => "256", :proto => "kernel", :family => "inet6" ) - @plugin['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "1111:2222:3333:4444::/64", :metric => "1024", :family => "inet6" ) - @plugin['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "default", :via => "1111:2222:3333:4444::1", :metric => "1024", :family => "inet6") + plugin.run + expect(plugin['network']['interfaces']['eth0']['routes']).to include Mash.new( :destination => "10.116.201.0/24", :proto => "kernel", :family =>"inet" ) + expect(plugin['network']['interfaces']['eth0']['routes']).to include Mash.new( :destination => "10.5.4.0/24", :family =>"inet", :via => "10.5.4.1") + expect(plugin['network']['interfaces']['eth0']['routes']).to include Mash.new( :destination => "10.5.4.0/24", :family =>"inet", :via => "10.5.4.2") + expect(plugin['network']['interfaces']['foo:veth0@eth0']['routes']).to include Mash.new( :destination => "192.168.212.0/24", :proto => "kernel", :src => "192.168.212.2", :family =>"inet" ) + expect(plugin['network']['interfaces']['eth0']['routes']).to include Mash.new( :destination => "fe80::/64", :metric => "256", :proto => "kernel", :family => "inet6" ) + expect(plugin['network']['interfaces']['eth0.11']['routes']).to include Mash.new( :destination => "1111:2222:3333:4444::/64", :metric => "1024", :family => "inet6" ) + expect(plugin['network']['interfaces']['eth0.11']['routes']).to include Mash.new( :destination => "default", :via => "1111:2222:3333:4444::1", :metric => "1024", :family => "inet6") end describe "when there isn't a source field in route entries " do + before(:each) do + plugin.run + end + it "doesn't set ipaddress" do - @plugin.run - @plugin['ipaddress'].should be nil + expect(plugin['ipaddress']).to be nil end it "doesn't set macaddress" do - @plugin.run - @plugin['macaddress'].should be nil + expect(plugin['macaddress']).to be nil end it "doesn't set ip6address" do - @plugin.run - @plugin['ip6address'].should be nil + expect(plugin['ip6address']).to be nil end end describe "when there's a source field in the default route entry" do - before do - @linux_ip_route = <<-IP_ROUTE_SCOPE -10.116.201.0/24 dev eth0 proto kernel + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 default via 10.116.201.1 dev eth0 src 10.116.201.76 -IP_ROUTE_SCOPE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3 -IP_ROUTE_SCOPE +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "sets ipaddress" do - @plugin.run - @plugin['ipaddress'].should == "10.116.201.76" + expect(plugin['ipaddress']).to eq("10.116.201.76") end it "sets ip6address" do - @plugin.run - @plugin['ip6address'].should == "1111:2222:3333:4444::3" + expect(plugin['ip6address']).to eq("1111:2222:3333:4444::3") end end describe "when there're several default routes" do - before do - @linux_ip_route = <<-IP_ROUTE_SCOPE -10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 default via 10.116.201.1 dev eth0 metric 10 default via 10.116.201.254 dev eth0 metric 9 -IP_ROUTE_SCOPE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3 default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023 -IP_ROUTE_SCOPE +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "sets default ipv4 interface and gateway" do - @plugin.run - @plugin['network']['default_interface'].should == 'eth0' - @plugin['network']['default_gateway'].should == '10.116.201.254' + expect(plugin['network']['default_interface']).to eq('eth0') + expect(plugin['network']['default_gateway']).to eq('10.116.201.254') end it "sets default ipv6 interface and gateway" do - @plugin.run - @plugin['network']['default_inet6_interface'].should == 'eth0.11' - @plugin['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::ffff' + expect(plugin['network']['default_inet6_interface']).to eq('eth0.11') + expect(plugin['network']['default_inet6_gateway']).to eq('1111:2222:3333:4444::ffff') end end describe "when there're a mixed setup of routes that could be used to set ipaddress" do - before do - @linux_ip_route = <<-IP_ROUTE_SCOPE -10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 default via 10.116.201.1 dev eth0 metric 10 default via 10.116.201.254 dev eth0 metric 9 src 10.116.201.74 -IP_ROUTE_SCOPE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3 default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023 src 1111:2222:3333:4444::2 -IP_ROUTE_SCOPE +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "sets ipaddress" do - @plugin.run - @plugin["ipaddress"].should == "10.116.201.74" + expect(plugin["ipaddress"]).to eq("10.116.201.74") end it "sets ip6address" do - @plugin.run - @plugin["ip6address"].should == "1111:2222:3333:4444::2" + expect(plugin["ip6address"]).to eq("1111:2222:3333:4444::2") end end describe "when there's a source field in a local route entry " do - before do - @linux_ip_route = <<-IP_ROUTE_SCOPE -10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 default via 10.116.201.1 dev eth0 -IP_ROUTE_SCOPE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3 default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 -IP_ROUTE_SCOPE - - do_stubs - end +' + } it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + plugin.run + expect(plugin['network']).not_to be_nil end it "sets ipaddress" do - @plugin.run - @plugin['ipaddress'].should == "10.116.201.76" + plugin.run + expect(plugin['ipaddress']).to eq("10.116.201.76") end describe "when about to set macaddress" do it "sets macaddress" do - @plugin.run - @plugin['macaddress'].should == "12:31:3D:02:BE:A2" + plugin.run + expect(plugin['macaddress']).to eq("12:31:3D:02:BE:A2") end describe "when then interface has the NOARP flag" do - before do - @linux_ip_route = <<-IP_ROUTE -10.118.19.1 dev tun0 proto kernel src 10.118.19.39 + let(:linux_ip_route) { +'10.118.19.1 dev tun0 proto kernel src 10.118.19.39 default via 172.16.19.1 dev tun0 -IP_ROUTE - - do_stubs - end +' + } it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + plugin.run + expect(plugin['network']).not_to be_nil end it "doesn't set macaddress" do - @plugin.run - @plugin['macaddress'].should be_nil + plugin.run + expect(plugin['macaddress']).to be_nil end end end it "sets ip6address" do - @plugin.run - @plugin['ip6address'].should == "1111:2222:3333:4444::3" + plugin.run + expect(plugin['ip6address']).to eq("1111:2222:3333:4444::3") end end describe "with a link level default route" do - before do - @linux_ip_route = <<-IP_ROUTE -default dev venet0 scope link -IP_ROUTE + let(:linux_ip_route) { +'default dev venet0 scope link +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "doesn't set ipaddress" do - @plugin.run - @plugin['ipaddress'].should be_nil + expect(plugin['ipaddress']).to be_nil end end describe "when not having a global scope ipv6 address" do - before do - @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 default via fe80::21c:eff:fe12:3456 dev eth0.153 src fe80::2e0:81ff:fe2b:48e7 metric 1024 -IP_ROUTE_SCOPE - - do_stubs +' + } + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "doesn't set ip6address" do - @plugin.run - @plugin['ip6address'].should be_nil + expect(plugin['ip6address']).to be_nil end end describe "with no default route" do - before do - @linux_ip_route = <<-IP_ROUTE -10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + let(:linux_ip_route) { +'10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1 192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2 172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 -IP_ROUTE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3 -IP_ROUTE +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "doesn't set ipaddress" do - @plugin.run - @plugin['ipaddress'].should be_nil + expect(plugin['ipaddress']).to be_nil end it "doesn't set ip6address" do - @plugin.run - @plugin['ip6address'].should be_nil + expect(plugin['ip6address']).to be_nil end end describe "with irrelevant routes (container setups)" do - before do - @linux_ip_route = <<-IP_ROUTE -10.116.201.0/26 dev eth0 proto kernel src 10.116.201.39 + let(:linux_ip_route) { +'10.116.201.0/26 dev eth0 proto kernel src 10.116.201.39 10.116.201.0/26 dev if4 proto kernel src 10.116.201.45 10.118.19.0/26 dev eth0 proto kernel src 10.118.19.39 10.118.19.0/26 dev if5 proto kernel src 10.118.19.45 default via 10.116.201.1 dev eth0 src 10.116.201.99 -IP_ROUTE +' + } - @linux_ip_route_inet6 = <<-IP_ROUTE -fe80::/64 dev eth0 proto kernel metric 256 + let(:linux_ip_route_inet6) { +'fe80::/64 dev eth0 proto kernel metric 256 fe80::/64 dev eth0.11 proto kernel metric 256 1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::FFFF:2 default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 -IP_ROUTE +' + } - do_stubs + before(:each) do + plugin.run end it "completes the run" do - Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/) - @plugin.run - @plugin['network'].should_not be_nil + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil end it "doesn't add bogus routes" do - @plugin.run - @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.116.201.0/26", :proto => "kernel", :family => "inet", :via => "10.116.201.39" ) - @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.118.19.0/26", :proto => "kernel", :family => "inet", :via => "10.118.19.39" ) - @plugin['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "1111:2222:3333:4444::/64", :family => "inet6", :metric => "1024" ) + expect(plugin['network']['interfaces']['eth0']['routes']).not_to include Mash.new( :destination => "10.116.201.0/26", :proto => "kernel", :family => "inet", :via => "10.116.201.39" ) + expect(plugin['network']['interfaces']['eth0']['routes']).not_to include Mash.new( :destination => "10.118.19.0/26", :proto => "kernel", :family => "inet", :via => "10.118.19.39" ) + expect(plugin['network']['interfaces']['eth0']['routes']).not_to include Mash.new( :destination => "1111:2222:3333:4444::/64", :family => "inet6", :metric => "1024" ) end it "doesn't set ipaddress" do - @plugin.run - @plugin['ipaddress'].should be_nil + expect(plugin['ipaddress']).to be_nil end it "doesn't set ip6address" do - @plugin.run - @plugin['ip6address'].should be_nil + expect(plugin['ip6address']).to be_nil end end # This should never happen in the real world. describe "when encountering a surprise interface" do - before do - @linux_ip_route = <<-IP_ROUTE -192.168.122.0/24 dev virbr0 proto kernel src 192.168.122.1 -IP_ROUTE - do_stubs - end - + let(:linux_ip_route) { +'192.168.122.0/24 dev virbr0 proto kernel src 192.168.122.1 +' + } + it "logs a message and skips previously unseen interfaces in 'ip route show'" do - Ohai::Log.should_receive(:debug).with("Skipping previously unseen interface from 'ip route show': virbr0").once - Ohai::Log.stub(:debug) # Catches the 'Loading plugin network' type messages - @plugin.run + expect(Ohai::Log).to receive(:debug).with("Skipping previously unseen interface from 'ip route show': virbr0").once + allow(Ohai::Log).to receive(:debug) # Catches the 'Loading plugin network' type messages + plugin.run + end + end + + describe "when running with ip version ss131122" do + let(:linux_ip_link_s_d) { +'1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN mode DEFAULT group default + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 + RX: bytes packets errors dropped overrun mcast + 35224 524 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 35224 524 0 0 0 0 +2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 + link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff promiscuity 0 + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 +3: eth0.11@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default + link/ether 00:0c:29:41:71:45 brd ff:ff:ff:ff:ff:ff promiscuity 0 + vlan protocol 802.1Q id 11 <REORDER_HDR> + RX: bytes packets errors dropped overrun mcast + 0 0 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 0 0 0 0 0 0 +4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 100 + link/none promiscuity 0 + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 +5: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 + link/void promiscuity 0 + RX: bytes packets errors dropped overrun mcast + 1392844460 2659966 0 0 0 0 + TX: bytes packets errors dropped carrier collsns + 691785313 1919690 0 0 0 0 +' + } + + it "adds the vlan information of an interface" do + plugin.run + expect(plugin['network']['interfaces']['eth0.11']['vlan']['id']).to eq('11') + expect(plugin['network']['interfaces']['eth0.11']['vlan']['protocol']).to eq('802.1Q') + expect(plugin['network']['interfaces']['eth0.11']['vlan']['flags']).to eq([ 'REORDER_HDR' ]) end end end end - end diff --git a/spec/unit/plugins/linux/platform_spec.rb b/spec/unit/plugins/linux/platform_spec.rb index defe8836..a148ecf9 100644 --- a/spec/unit/plugins/linux/platform_spec.rb +++ b/spec/unit/plugins/linux/platform_spec.rb @@ -35,6 +35,7 @@ describe Ohai::System, "Linux plugin platform" do File.stub(:exists?).with("/etc/slackware-version").and_return(false) File.stub(:exists?).with("/etc/enterprise-release").and_return(false) File.stub(:exists?).with("/etc/oracle-release").and_return(false) + File.stub(:exists?).with("/etc/parallels-release").and_return(false) File.stub(:exists?).with("/usr/bin/raspi-config").and_return(false) end @@ -295,6 +296,41 @@ describe Ohai::System, "Linux plugin platform" do @plugin[:platform].should == "fedora" @plugin[:platform_version].to_i.should == 13 end + + end + end + + describe "on pcs linux" do + describe "with lsb_result" do + it "should read the platform as parallels and version as 6.0.5" do + @plugin[:lsb][:id] = "CloudLinuxServer" + @plugin[:lsb][:release] = "6.5" + File.stub(:exists?).with("/etc/redhat-release").and_return(true) + File.stub(:read).with("/etc/redhat-release").and_return("CloudLinux Server release 6.5 (Pavel Popovich)") + File.should_receive(:exists?).with("/etc/parallels-release").and_return(true) + File.should_receive(:read).with("/etc/parallels-release").and_return("Parallels Cloud Server 6.0.5 (20007)") + @plugin.run + @plugin[:platform].should == "parallels" + @plugin[:platform_version].should == "6.0.5" + @plugin[:platform_family].should == "rhel" + end + end + + describe "without lsb_results" do + before(:each) do + @plugin.lsb = nil + end + + it "should read the platform as parallels and version as 6.0.5" do + File.stub(:exists?).with("/etc/redhat-release").and_return(true) + File.stub(:read).with("/etc/redhat-release").and_return("CloudLinux Server release 6.5 (Pavel Popovich)") + File.should_receive(:exists?).with("/etc/parallels-release").and_return(true) + File.should_receive(:read).with("/etc/parallels-release").and_return("Parallels Cloud Server 6.0.5 (20007)") + @plugin.run + @plugin[:platform].should == "parallels" + @plugin[:platform_version].should == "6.0.5" + @plugin[:platform_family].should == "rhel" + end end end diff --git a/spec/unit/plugins/linux/virtualization_spec.rb b/spec/unit/plugins/linux/virtualization_spec.rb index 890c425d..9fb3744f 100644 --- a/spec/unit/plugins/linux/virtualization_spec.rb +++ b/spec/unit/plugins/linux/virtualization_spec.rb @@ -230,6 +230,24 @@ VBOX @plugin[:virtualization][:systems]['linux-vserver'].should == "host" end + it "should set Linux-VServer host if /proc/self/status contains multiple space VxID: 0" do + File.should_receive(:exists?).with("/proc/self/status").and_return(true) + File.stub(:read).with("/proc/self/status").and_return("VxID: 0") + @plugin.run + @plugin[:virtualization][:system].should == "linux-vserver" + @plugin[:virtualization][:role].should == "host" + @plugin[:virtualization][:systems]['linux-vserver'].should == "host" + end + + it "should set Linux-VServer host if /proc/self/status contains tabbed VxID:\t0" do + File.should_receive(:exists?).with("/proc/self/status").and_return(true) + File.stub(:read).with("/proc/self/status").and_return("VxID:\t0") + @plugin.run + @plugin[:virtualization][:system].should == "linux-vserver" + @plugin[:virtualization][:role].should == "host" + @plugin[:virtualization][:systems]['linux-vserver'].should == "host" + end + it "should set Linux-VServer guest if /proc/self/status contains s_context > 0" do File.should_receive(:exists?).with("/proc/self/status").and_return(true) File.stub(:read).with("/proc/self/status").and_return("s_context: 2") @@ -373,7 +391,7 @@ CGROUP File.should_receive(:exists?).with("/proc/self/cgroup").and_return(true) File.stub(:read).with("/proc/self/cgroup").and_return(self_cgroup) end - + it "sets lxc host if lxc-version exists" do @plugin.stub(:lxc_version_exists?).and_return("/usr/bin/lxc-version") @plugin.run diff --git a/spec/unit/plugins/openstack_spec.rb b/spec/unit/plugins/openstack_spec.rb index d6bd08ce..fa8c6fdd 100644 --- a/spec/unit/plugins/openstack_spec.rb +++ b/spec/unit/plugins/openstack_spec.rb @@ -118,6 +118,26 @@ EOM } end + let(:openstack_metadata_version) { "2013-04-04" } + let(:openstack_metadata_endpoint) { "http://169.254.169.254/openstack/" } + + let(:openstack_metadata_values) do + '{ + "availability_zone" : "nova", + "hostname" : "ohai.novalocal", + "launch_index" : 0, + "meta" : { + "priority" : "low", + "role" : "ohaiserver" + }, + "name" : "ohai_spec", + "public_keys" : { + "mykey" : "SSH KEY DATA" + }, + "uuid" : "00000000-0000-0000-0000-100000000000" + }' + end + let(:http_client) { double("Net::HTTP", :read_timeout= => nil) } def expect_get(url, response_body) @@ -126,6 +146,12 @@ EOM and_return(double("HTTP Response", :code => "200", :body => response_body)) end + def expect_get_response(url, response_body) + http_client.should_receive(:get_response). + with(url,nil,nil). + and_return(double("HTTP Response", :code => "200", :body => response_body)) + end + before do openstack_plugin.should_receive(:can_metadata_connect?). with(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR,80). @@ -143,6 +169,11 @@ EOM expect_get("/#{metadata_version}/meta-data/#{md_id}", md_value) end + expect_get_response( + URI.parse("#{openstack_metadata_endpoint}#{openstack_metadata_version}/meta_data.json"), + openstack_metadata_values + ) + openstack_plugin.run end @@ -206,6 +237,30 @@ EOM it "reads the provider from the metadata service" do expect(ohai_data['openstack']['provider']).to eq("openstack") end + + context 'Retreive openStack specific metadata' do + it "reads the availability_zone from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['availability_zone']).to eq('nova') + end + it "reads the hostname from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['hostname']).to eq('ohai.novalocal') + end + it "reads the launch_index from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['launch_index']).to eq(0) + end + it "reads the meta from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['meta']).to eq({ "priority" => "low", "role" => "ohaiserver"}) + end + it "reads the name from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['name']).to eq('ohai_spec') + end + it "reads the public_keys from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['public_keys']).to eq({"mykey" => "SSH KEY DATA"}) + end + it "reads the uuid from the openstack metadata service" do + expect(ohai_data['openstack']['metadata']['uuid']).to eq('00000000-0000-0000-0000-100000000000') + end + end end diff --git a/spec/unit/plugins/passwd_spec.rb b/spec/unit/plugins/passwd_spec.rb index 33b57005..0fef63de 100644 --- a/spec/unit/plugins/passwd_spec.rb +++ b/spec/unit/plugins/passwd_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -describe Ohai::System, "plugin etc" do +describe Ohai::System, "plugin etc", :unix_only do before(:each) do @plugin = get_plugin("passwd") end @@ -24,7 +24,8 @@ describe Ohai::System, "plugin etc" do end it "should set the current user" do - Etc.should_receive(:getlogin).and_return('chef') + Process.should_receive(:euid).and_return('31337') + Etc.should_receive(:getpwuid).and_return(PasswdEntry.new('chef', 31337, 31337, '/home/chef', '/bin/ksh', 'Julia Child')) @plugin.run @plugin[:current_user].should == 'chef' end diff --git a/spec/unit/plugins/php_spec.rb b/spec/unit/plugins/php_spec.rb index 0046a665..cced3234 100644 --- a/spec/unit/plugins/php_spec.rb +++ b/spec/unit/plugins/php_spec.rb @@ -18,7 +18,6 @@ # limitations under the License. # -require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')) describe Ohai::System, "plugin php" do diff --git a/spec/unit/plugins/python_spec.rb b/spec/unit/plugins/python_spec.rb index 85892fc2..2d4d90a1 100644 --- a/spec/unit/plugins/python_spec.rb +++ b/spec/unit/plugins/python_spec.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,33 +17,35 @@ # limitations under the License. # - require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') describe Ohai::System, "plugin python" do + let(:stdout) { "2.5.2 (r252:60911, Jan 4 2009, 17:40:26)\n[GCC 4.3.2]\n" } + + let(:retval) { 0 } - before(:each) do - @plugin = get_plugin("python") - @plugin[:languages] = Mash.new - @stdout = "2.5.2 (r252:60911, Jan 4 2009, 17:40:26)\n[GCC 4.3.2]\n" - @plugin.stub(:shell_out).with("python -c \"import sys; print sys.version\"").and_return(mock_shell_out(0, @stdout, "")) + let(:plugin) do + plugin = get_plugin("python") + plugin[:languages] = Mash.new + expect(plugin).to receive(:shell_out).with("python -c \"import sys; print (sys.version)\"").and_return(mock_shell_out(retval, stdout, "")) + plugin end - + it "should get the python version from printing sys.version and sys.platform" do - @plugin.should_receive(:shell_out).with("python -c \"import sys; print sys.version\"").and_return(mock_shell_out(0, @stdout, "")) - @plugin.run + plugin.run end it "should set languages[:python][:version]" do - @plugin.run - @plugin.languages[:python][:version].should eql("2.5.2") - end - - it "should not set the languages[:python] tree up if python command fails" do - @stdout = "2.5.2 (r252:60911, Jan 4 2009, 17:40:26)\n[GCC 4.3.2]\n" - @plugin.stub(:shell_out).with("python -c \"import sys; print sys.version\"").and_return(mock_shell_out(1, @stdout, "")) - @plugin.run - @plugin.languages.should_not have_key(:python) + plugin.run + expect(plugin.languages[:python][:version]).to eql("2.5.2") end + context "when the python command fails" do + let(:retval) { 1 } + + it "should not set the languages[:python] tree up" do + plugin.run + expect(plugin.languages).not_to have_key(:python) + end + end end diff --git a/spec/unit/plugins/rackspace_spec.rb b/spec/unit/plugins/rackspace_spec.rb index 72dc6e28..4e60d111 100644 --- a/spec/unit/plugins/rackspace_spec.rb +++ b/spec/unit/plugins/rackspace_spec.rb @@ -19,6 +19,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') describe Ohai::System, "plugin rackspace" do before(:each) do + Resolv.stub(:getname).and_return("1.2.3.4") @plugin = get_plugin("rackspace") @plugin[:hostname] = "katie" @plugin[:network] = {:interfaces => {:eth0 => {"addresses"=> { @@ -95,6 +96,20 @@ describe Ohai::System, "plugin rackspace" do @plugin[:rackspace][:public_hostname].should_not be_nil end + it "should resolve hostname if reverse dns is set" do + Resolv.stub(:getname).and_return("1234.resolved.com") + @plugin.run + @plugin[:rackspace][:public_hostname].should == "1234.resolved.com" + end + + [Resolv::ResolvError, Resolv::ResolvTimeout].each do |exception| + it "should return ip address when reverse dns returns exception: #{exception}" do + Resolv.stub(:getname).and_raise(exception) + @plugin.run + @plugin[:rackspace][:public_hostname].should == "1.2.3.4" + end + end + it "should have correct values for all attributes" do @plugin.run @plugin[:rackspace][:public_ip].should == "1.2.3.4" @@ -103,7 +118,7 @@ describe Ohai::System, "plugin rackspace" do @plugin[:rackspace][:local_ipv4].should == "5.6.7.8" @plugin[:rackspace][:public_ipv6].should == "2a00:1a48:7805:111:e875:efaf:ff08:75" @plugin[:rackspace][:local_hostname].should == 'katie' - @plugin[:rackspace][:public_hostname].should == "1-2-3-4.static.cloud-ips.com" + @plugin[:rackspace][:public_hostname].should == "1.2.3.4" end it "should capture region information" do @@ -124,10 +139,13 @@ OUT it_should_behave_like "rackspace" before(:each) do + Resolv.stub(:getname).and_raise(Resolv::ResolvError) File.stub(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true) File.stub(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('') File.stub(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) File.stub(:read).with('C:\chef\ohai\hints/rackspace.json').and_return('') + File.stub(:exist?).with('/etc/resolv.conf').and_return(true) + File.stub(:read).with('/etc/resolv.conf').and_return('') end describe 'with no public interfaces (empty eth0)' do diff --git a/spec/unit/plugins/solaris2/zpools_spec.rb b/spec/unit/plugins/solaris2/zpools_spec.rb new file mode 100644 index 00000000..7dd8b8f7 --- /dev/null +++ b/spec/unit/plugins/solaris2/zpools_spec.rb @@ -0,0 +1,153 @@ +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb') + +describe Ohai::System, "Solaris 2.x zpool plugin" do + before(:each) do + @plugin = get_plugin("solaris2/zpools") + @plugin.stub(:collect_os).and_return(:solaris2) + + @zpool_status_rpool = <<-EOSR +pool: rpool +state: ONLINE +scan: resilvered 65.6G in 0h8m with 0 errors on Fri Jun 6 14:22:40 2014 +config: + + NAME STATE READ WRITE CKSUM + rpool ONLINE 0 0 0 + mirror-0 ONLINE 0 0 0 + c3t5d0s0 ONLINE 0 0 0 + c3t4d0s0 ONLINE 0 0 0 + +errors: No known data errors +EOSR + + @zpool_status_tank = <<-EOST +pool: tank +state: ONLINE +scan: scrub repaired 0 in 0h0m with 0 errors on Fri Jun 6 14:43:40 2014 +config: + + NAME STATE READ WRITE CKSUM + tank ONLINE 0 0 0 + raidz2-0 ONLINE 0 0 0 + c1t50014EE209D1DBA9d0 ONLINE 0 0 0 + c1t50014EE20A0ECED2d0 ONLINE 0 0 0 + c1t50014EE20A106BFFd0 ONLINE 0 0 0 + c1t50014EE20A1423E8d0 ONLINE 0 0 0 + c1t50014EE20A145447d0 ONLINE 0 0 0 + c1t50014EE20A29EE56d0 ONLINE 0 0 0 + raidz2-1 ONLINE 0 0 0 + c1t50014EE20A2B984Cd0 ONLINE 0 0 0 + c1t50014EE20A2BBC78d0 ONLINE 0 0 0 + c1t50014EE20A2BDCA9d0 ONLINE 0 0 0 + c1t50014EE25F697DC4d0 ONLINE 0 0 0 + c1t50014EE25F698BECd0 ONLINE 0 0 0 + c1t50014EE25F6998DAd0 ONLINE 0 0 0 +EOST + @zpool_out = <<-EOZO +rpool 109G 66.2G 42.8G 60% 1.00x ONLINE 34 +tank 130T 4.91M 130T 0% 1.00x ONLINE 34 +EOZO + @plugin.stub(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0,@zpool_out,"")) + @plugin.stub(:shell_out).with("su adm -c \"zpool status rpool\"").and_return(mock_shell_out(0,@zpool_status_rpool,"")) + @plugin.stub(:shell_out).with("su adm -c \"zpool status tank\"").and_return(mock_shell_out(0,@zpool_status_tank,"")) + end + + describe "On Solaris2 Common" do + it "Should have entries for both zpools" do + @plugin.run + expect(@plugin[:zpools][:rpool]).to be + expect(@plugin[:zpools][:tank]).to be + end + + it "Should have the correct pool size" do + @plugin.run + expect(@plugin[:zpools][:rpool][:pool_size]).to match("109G") + expect(@plugin[:zpools][:tank][:pool_size]).to match("130T") + end + + it "Should have the correct pool allocated size" do + @plugin.run + expect(@plugin[:zpools][:rpool][:pool_allocated]).to match("66.2G") + expect(@plugin[:zpools][:tank][:pool_allocated]).to match("4.91M") + end + + it "Should have the correct pool free size" do + @plugin.run + expect(@plugin[:zpools][:rpool][:pool_free]).to match("42.8G") + expect(@plugin[:zpools][:tank][:pool_free]).to match("130T") + end + + it "Should have the correct capacity_used" do + @plugin.run + expect(@plugin[:zpools][:rpool][:capacity_used]).to match("60%") + expect(@plugin[:zpools][:tank][:capacity_used]).to match("0%") + end + + it "Should have the correct dedup_factor" do + @plugin.run + expect(@plugin[:zpools][:rpool][:dedup_factor]).to match("1.00x") + expect(@plugin[:zpools][:tank][:dedup_factor]).to match("1.00x") + end + + it "Should have the correct health" do + @plugin.run + expect(@plugin[:zpools][:rpool][:health]).to match("ONLINE") + expect(@plugin[:zpools][:tank][:health]).to match("ONLINE") + end + + it "Should have the correct number of devices" do + @plugin.run + expect(@plugin[:zpools][:rpool][:devices].keys.size).to match(2) + expect(@plugin[:zpools][:tank][:devices].keys.size).to match(12) + end + end + + describe "On OmniOS_151006" do + before(:each) do + @zpool_out = <<-EOZO +rpool 109G 66.2G 42.8G 60% 1.00x ONLINE - +tank 130T 4.91M 130T 0% 1.00x ONLINE - +EOZO + @plugin.stub(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0,@zpool_out,"")) + end + + it "Won't have a version number" do + @plugin.run + expect(@plugin[:zpools][:rpool][:zpool_version]).to match("-") + expect(@plugin[:zpools][:tank][:zpool_version]).to match("-") + end + + end + + describe "On Solaris_11.1" do + before(:each) do + @zpool_out = <<-EOZO +rpool 109G 66.2G 42.8G 60% 1.00x ONLINE 34 +tank 130T 4.91M 130T 0% 1.00x ONLINE 34 +EOZO + @plugin.stub(:shell_out).with("zpool list -H -o name,size,alloc,free,cap,dedup,health,version").and_return(mock_shell_out(0,@zpool_out,"")) + end + + it "Should have a version number" do + @plugin.run + expect(@plugin[:zpools][:rpool][:zpool_version]).to match("34") + expect(@plugin[:zpools][:tank][:zpool_version]).to match("34") + end + + end +end |