ohai 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +98 -0
  3. data/Rakefile +61 -0
  4. data/bin/ohai +24 -0
  5. data/lib/ohai.rb +27 -0
  6. data/lib/ohai/application.rb +113 -0
  7. data/lib/ohai/config.rb +29 -0
  8. data/lib/ohai/exception.rb +23 -0
  9. data/lib/ohai/log.rb +26 -0
  10. data/lib/ohai/mixin/command.rb +208 -0
  11. data/lib/ohai/mixin/from_file.rb +36 -0
  12. data/lib/ohai/mixin/string.rb +29 -0
  13. data/lib/ohai/plugins/command.rb +21 -0
  14. data/lib/ohai/plugins/darwin/filesystem.rb +57 -0
  15. data/lib/ohai/plugins/darwin/hostname.rb +22 -0
  16. data/lib/ohai/plugins/darwin/kernel.rb +37 -0
  17. data/lib/ohai/plugins/darwin/network.rb +187 -0
  18. data/lib/ohai/plugins/darwin/platform.rb +36 -0
  19. data/lib/ohai/plugins/darwin/ps.rb +23 -0
  20. data/lib/ohai/plugins/darwin/ssh_host_key.rb +25 -0
  21. data/lib/ohai/plugins/darwin/system_profiler.rb +33 -0
  22. data/lib/ohai/plugins/darwin/uptime.rb +32 -0
  23. data/lib/ohai/plugins/dmi.rb +59 -0
  24. data/lib/ohai/plugins/ec2.rb +100 -0
  25. data/lib/ohai/plugins/erlang.rb +40 -0
  26. data/lib/ohai/plugins/freebsd/cpu.rb +52 -0
  27. data/lib/ohai/plugins/freebsd/filesystem.rb +57 -0
  28. data/lib/ohai/plugins/freebsd/hostname.rb +22 -0
  29. data/lib/ohai/plugins/freebsd/kernel.rb +37 -0
  30. data/lib/ohai/plugins/freebsd/memory.rb +50 -0
  31. data/lib/ohai/plugins/freebsd/network.rb +112 -0
  32. data/lib/ohai/plugins/freebsd/platform.rb +23 -0
  33. data/lib/ohai/plugins/freebsd/ps.rb +24 -0
  34. data/lib/ohai/plugins/freebsd/ssh_host_key.rb +26 -0
  35. data/lib/ohai/plugins/freebsd/uptime.rb +32 -0
  36. data/lib/ohai/plugins/freebsd/virtualization.rb +66 -0
  37. data/lib/ohai/plugins/hostname.rb +27 -0
  38. data/lib/ohai/plugins/java.rb +36 -0
  39. data/lib/ohai/plugins/kernel.rb +33 -0
  40. data/lib/ohai/plugins/keys.rb +22 -0
  41. data/lib/ohai/plugins/languages.rb +21 -0
  42. data/lib/ohai/plugins/linux/block_device.rb +38 -0
  43. data/lib/ohai/plugins/linux/cpu.rb +60 -0
  44. data/lib/ohai/plugins/linux/filesystem.rb +57 -0
  45. data/lib/ohai/plugins/linux/hostname.rb +26 -0
  46. data/lib/ohai/plugins/linux/kernel.rb +33 -0
  47. data/lib/ohai/plugins/linux/lsb.rb +38 -0
  48. data/lib/ohai/plugins/linux/memory.rb +83 -0
  49. data/lib/ohai/plugins/linux/network.rb +112 -0
  50. data/lib/ohai/plugins/linux/platform.rb +43 -0
  51. data/lib/ohai/plugins/linux/ps.rb +23 -0
  52. data/lib/ohai/plugins/linux/ssh_host_key.rb +26 -0
  53. data/lib/ohai/plugins/linux/uptime.rb +28 -0
  54. data/lib/ohai/plugins/linux/virtualization.rb +77 -0
  55. data/lib/ohai/plugins/netbsd/cpu.rb +48 -0
  56. data/lib/ohai/plugins/netbsd/filesystem.rb +57 -0
  57. data/lib/ohai/plugins/netbsd/hostname.rb +22 -0
  58. data/lib/ohai/plugins/netbsd/kernel.rb +35 -0
  59. data/lib/ohai/plugins/netbsd/memory.rb +98 -0
  60. data/lib/ohai/plugins/netbsd/network.rb +111 -0
  61. data/lib/ohai/plugins/netbsd/platform.rb +22 -0
  62. data/lib/ohai/plugins/netbsd/ps.rb +24 -0
  63. data/lib/ohai/plugins/netbsd/ssh_host_key.rb +26 -0
  64. data/lib/ohai/plugins/netbsd/uptime.rb +31 -0
  65. data/lib/ohai/plugins/netbsd/virtualization.rb +65 -0
  66. data/lib/ohai/plugins/network.rb +52 -0
  67. data/lib/ohai/plugins/ohai_time.rb +21 -0
  68. data/lib/ohai/plugins/openbsd/cpu.rb +38 -0
  69. data/lib/ohai/plugins/openbsd/filesystem.rb +57 -0
  70. data/lib/ohai/plugins/openbsd/hostname.rb +22 -0
  71. data/lib/ohai/plugins/openbsd/kernel.rb +35 -0
  72. data/lib/ohai/plugins/openbsd/memory.rb +98 -0
  73. data/lib/ohai/plugins/openbsd/network.rb +111 -0
  74. data/lib/ohai/plugins/openbsd/platform.rb +23 -0
  75. data/lib/ohai/plugins/openbsd/ps.rb +24 -0
  76. data/lib/ohai/plugins/openbsd/ssh_host_key.rb +26 -0
  77. data/lib/ohai/plugins/openbsd/uptime.rb +32 -0
  78. data/lib/ohai/plugins/openbsd/virtualization.rb +66 -0
  79. data/lib/ohai/plugins/os.rb +48 -0
  80. data/lib/ohai/plugins/perl.rb +37 -0
  81. data/lib/ohai/plugins/platform.rb +25 -0
  82. data/lib/ohai/plugins/python.rb +37 -0
  83. data/lib/ohai/plugins/ruby.rb +39 -0
  84. data/lib/ohai/plugins/solaris2/cpu.rb +33 -0
  85. data/lib/ohai/plugins/solaris2/hostname.rb +25 -0
  86. data/lib/ohai/plugins/solaris2/kernel.rb +40 -0
  87. data/lib/ohai/plugins/solaris2/network.rb +141 -0
  88. data/lib/ohai/plugins/solaris2/platform.rb +33 -0
  89. data/lib/ohai/plugins/solaris2/ps.rb +23 -0
  90. data/lib/ohai/plugins/solaris2/ssh_host_key.rb +26 -0
  91. data/lib/ohai/plugins/uptime.rb +42 -0
  92. data/lib/ohai/plugins/virtualization.rb +86 -0
  93. data/lib/ohai/plugins/windows/filesystem.rb +44 -0
  94. data/lib/ohai/plugins/windows/hostname.rb +25 -0
  95. data/lib/ohai/plugins/windows/kernel.rb +75 -0
  96. data/lib/ohai/plugins/windows/network.rb +114 -0
  97. data/lib/ohai/plugins/windows/platform.rb +26 -0
  98. data/lib/ohai/system.rb +241 -0
  99. data/spec/ohai/mixin/command_spec.rb +37 -0
  100. data/spec/ohai/mixin/from_file_spec.rb +53 -0
  101. data/spec/ohai/plugins/darwin/hostname_spec.rb +34 -0
  102. data/spec/ohai/plugins/darwin/kernel_spec.rb +46 -0
  103. data/spec/ohai/plugins/darwin/platform_spec.rb +67 -0
  104. data/spec/ohai/plugins/dmi_spec.rb +73 -0
  105. data/spec/ohai/plugins/ec2_spec.rb +81 -0
  106. data/spec/ohai/plugins/erlang_spec.rb +63 -0
  107. data/spec/ohai/plugins/freebsd/hostname_spec.rb +34 -0
  108. data/spec/ohai/plugins/freebsd/kernel_spec.rb +37 -0
  109. data/spec/ohai/plugins/freebsd/platform_spec.rb +40 -0
  110. data/spec/ohai/plugins/hostname_spec.rb +39 -0
  111. data/spec/ohai/plugins/java_spec.rb +70 -0
  112. data/spec/ohai/plugins/kernel_spec.rb +43 -0
  113. data/spec/ohai/plugins/linux/cpu_spec.rb +128 -0
  114. data/spec/ohai/plugins/linux/hostname_spec.rb +52 -0
  115. data/spec/ohai/plugins/linux/kernel_spec.rb +31 -0
  116. data/spec/ohai/plugins/linux/lsb_spec.rb +60 -0
  117. data/spec/ohai/plugins/linux/platform_spec.rb +81 -0
  118. data/spec/ohai/plugins/linux/uptime_spec.rb +61 -0
  119. data/spec/ohai/plugins/linux/virtualization_spec.rb +131 -0
  120. data/spec/ohai/plugins/netbsd/hostname_spec.rb +34 -0
  121. data/spec/ohai/plugins/netbsd/kernel_spec.rb +36 -0
  122. data/spec/ohai/plugins/netbsd/platform_spec.rb +40 -0
  123. data/spec/ohai/plugins/ohai_time_spec.rb +46 -0
  124. data/spec/ohai/plugins/openbsd/hostname_spec.rb +34 -0
  125. data/spec/ohai/plugins/openbsd/kernel_spec.rb +37 -0
  126. data/spec/ohai/plugins/openbsd/platform_spec.rb +40 -0
  127. data/spec/ohai/plugins/os_spec.rb +69 -0
  128. data/spec/ohai/plugins/perl_spec.rb +89 -0
  129. data/spec/ohai/plugins/platform_spec.rb +56 -0
  130. data/spec/ohai/plugins/python_spec.rb +53 -0
  131. data/spec/ohai/plugins/ruby_spec.rb +51 -0
  132. data/spec/ohai/plugins/solaris2/hostname_spec.rb +43 -0
  133. data/spec/ohai/plugins/solaris2/kernel_spec.rb +164 -0
  134. data/spec/ohai/system_spec.rb +130 -0
  135. data/spec/ohai_spec.rb +27 -0
  136. data/spec/rcov.opts +2 -0
  137. data/spec/spec.opts +2 -0
  138. data/spec/spec_helper.rb +68 -0
  139. metadata +240 -0
@@ -0,0 +1,114 @@
1
+ #
2
+ # Author:: James Gartrell (<jgartrel@gmail.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'ruby-wmi'
20
+
21
+ def encaps_lookup(encap)
22
+ return "Ethernet" if encap.eql?("Ethernet 802.3")
23
+ encap
24
+ end
25
+
26
+ def derive_bcast(ipaddr, ipmask, zero_bcast = false)
27
+ begin
28
+ ipaddr_int = ipaddr.split(".").collect{ |x| x.to_i}.pack("C4").unpack("N").first
29
+ ipmask_int = ipmask.split(".").collect{ |x| x.to_i}.pack("C4").unpack("N").first
30
+ if zero_bcast
31
+ bcast_int = ipaddr_int & ipmask_int
32
+ else
33
+ bcast_int = ipaddr_int | 2 ** 32 - ipmask_int - 1
34
+ end
35
+ bcast = [bcast_int].pack("N").unpack("C4").join(".")
36
+ return bcast
37
+ rescue
38
+ return nil
39
+ end
40
+ end
41
+
42
+ iface = Mash.new
43
+ iface_config = Mash.new
44
+ iface_instance = Mash.new
45
+
46
+ adapters = WMI::Win32_NetworkAdapterConfiguration.find(:all)
47
+ adapters.each do |adapter|
48
+ i = adapter.Index
49
+ iface_config[i] = Mash.new
50
+ adapter.properties_.each do |p|
51
+ iface_config[i][p.name.wmi_underscore.to_sym] = adapter[p.name]
52
+ end
53
+ end
54
+
55
+ adapters = WMI::Win32_NetworkAdapter.find(:all)
56
+ adapters.each do |adapter|
57
+ i = adapter.Index
58
+ iface_instance[i] = Mash.new
59
+ adapter.properties_.each do |p|
60
+ iface_instance[i][p.name.wmi_underscore.to_sym] = adapter[p.name]
61
+ end
62
+ end
63
+
64
+ iface_instance.keys.each do |i|
65
+ if iface_config[i][:ip_enabled] and iface_instance[i][:net_connection_id] and iface_instance[i][:interface_index]
66
+ cint = sprintf("0x%X", iface_instance[i][:interface_index])
67
+ iface[cint] = Mash.new
68
+ iface[cint][:configuration] = iface_config[i]
69
+ iface[cint][:instance] = iface_instance[i]
70
+
71
+ iface[cint][:counters] = Mash.new
72
+ iface[cint][:addresses] = Mash.new
73
+ iface[cint][:configuration][:ip_address].each_index do |i|
74
+ begin
75
+ if iface[cint][:configuration][:ip_address][i] =~ /./
76
+ iface[cint][:addresses][iface[cint][:configuration][:ip_address][i]] = {
77
+ "family" => "inet",
78
+ "netmask" => iface[cint][:configuration][:ip_subnet][i],
79
+ "broadcast" => derive_bcast( iface[cint][:configuration][:ip_address][i],
80
+ iface[cint][:configuration][:ip_subnet][i],
81
+ iface[cint][:configuration][:ip_use_zero_broadcast]
82
+ )
83
+ }
84
+ end
85
+ rescue
86
+ end
87
+ end
88
+ iface[cint][:configuration][:mac_address].each do |mac_addr|
89
+ iface[cint][:addresses][mac_addr] = {
90
+ "family" => "lladdr"
91
+ }
92
+ end
93
+ iface[cint][:mtu] = iface[cint][:configuration][:mtu]
94
+ iface[cint][:type] = iface[cint][:instance][:adapter_type]
95
+ iface[cint][:arp] = {}
96
+ iface[cint][:encapsulation] = encaps_lookup(iface[cint][:instance][:adapter_type])
97
+ end
98
+ end
99
+
100
+ cint=nil
101
+ from("arp /a").split("\n").each do |line|
102
+ if line == ""
103
+ cint = nil
104
+ end
105
+ if line =~ /^Interface:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+[-]+\s+(0x\d+)/
106
+ cint = $2
107
+ end
108
+ next unless iface[cint]
109
+ if line =~ /^\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-fA-F0-9\:-]+)/
110
+ iface[cint][:arp][$1] = $2.gsub("-",":").downcase
111
+ end
112
+ end
113
+
114
+ network["interfaces"] = iface
@@ -0,0 +1,26 @@
1
+ #
2
+ # Author:: James Gartrell (<jgartrel@gmail.com>)
3
+ # Copyright:: Copyright (c) 2009 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ # After long discussion in IRC the "powers that be" have come to a concensus
20
+ # that there is no other Windows platforms exist that were not based on the
21
+ # Windows_NT kernel, so we herby decree that "windows" will refer to all
22
+ # platforms built upon the Windows_NT kernel and have access to win32 or win64
23
+ # subsystems.
24
+ platform os
25
+ platform_version kernel['release']
26
+
@@ -0,0 +1,241 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'rubygems'
20
+ require 'extlib'
21
+ require 'ohai/log'
22
+ require 'ohai/mixin/from_file'
23
+ require 'ohai/mixin/command'
24
+ require 'ohai/mixin/string'
25
+
26
+ begin
27
+ require 'yajl/json_gem'
28
+ rescue LoadError
29
+ begin
30
+ require 'json'
31
+ rescue LoadError
32
+ begin
33
+ require 'json/pure'
34
+ rescue LoadError
35
+ STDERR.puts "No valid JSON library detected, please install one of 'yajl-ruby', 'json' or 'json_pure'."
36
+ exit -2
37
+ end
38
+ end
39
+ end
40
+
41
+ module Ohai
42
+ class System
43
+ attr_accessor :data, :seen_plugins
44
+
45
+ include Ohai::Mixin::FromFile
46
+ include Ohai::Mixin::Command
47
+
48
+ def initialize
49
+ @data = Mash.new
50
+ @seen_plugins = Hash.new
51
+ @providers = Mash.new
52
+ @plugin_path = ""
53
+ Ohai::Log.level(Ohai::Config.log_level)
54
+ end
55
+
56
+ def [](key)
57
+ @data[key]
58
+ end
59
+
60
+ def []=(key, value)
61
+ @data[key] = value
62
+ end
63
+
64
+ def each(&block)
65
+ @data.each do |key, value|
66
+ block.call(key, value)
67
+ end
68
+ end
69
+
70
+ def attribute?(name)
71
+ @data.has_key?(name)
72
+ end
73
+
74
+ def set(name, *value)
75
+ set_attribute(name, *value)
76
+ end
77
+
78
+ def from(cmd)
79
+ status, stdout, stderr = run_command(:command => cmd)
80
+ return "" if stdout.nil?
81
+ stdout.chomp!.strip
82
+ end
83
+
84
+ def provides(*paths)
85
+ paths.each do |path|
86
+ parts = path.split('/')
87
+ h = @providers
88
+ unless parts.length == 0
89
+ parts.shift if parts[0].length == 0
90
+ parts.each do |part|
91
+ h[part] ||= Mash.new
92
+ h = h[part]
93
+ end
94
+ end
95
+ h[:_providers] ||= []
96
+ h[:_providers] << @plugin_path
97
+ end
98
+ end
99
+
100
+ # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
101
+ def from_with_regex(cmd, *regex_list)
102
+ regex_list.flatten.each do |regex|
103
+ status, stdout, stderr = run_command(:command => cmd)
104
+ return "" if stdout.nil?
105
+ stdout.chomp!.strip
106
+ md = stdout.match(regex)
107
+ return md[1]
108
+ end
109
+ end
110
+
111
+ def set_attribute(name, *value)
112
+ @data[name] = *value
113
+ @data[name]
114
+ end
115
+
116
+ def get_attribute(name)
117
+ @data[name]
118
+ end
119
+
120
+ def all_plugins
121
+ require_plugin('os')
122
+
123
+ Ohai::Config[:plugin_path].each do |path|
124
+ [
125
+ Dir[File.join(path, '*')],
126
+ Dir[File.join(path, @data[:os], '**', '*')]
127
+ ].flatten.each do |file|
128
+ file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
129
+ md = file_regex.match(file)
130
+ if md
131
+ plugin_name = md[1].gsub(File::SEPARATOR, "::")
132
+ require_plugin(plugin_name) unless @seen_plugins.has_key?(plugin_name)
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def collect_providers(providers)
139
+ refreshments = []
140
+ if providers.is_a?(Mash)
141
+ providers.keys.each do |provider|
142
+ if provider.eql?("_providers")
143
+ refreshments << providers[provider]
144
+ else
145
+ refreshments << collect_providers(providers[provider])
146
+ end
147
+ end
148
+ else
149
+ refreshments << providers
150
+ end
151
+ refreshments.flatten.uniq
152
+ end
153
+
154
+ def refresh_plugins(path = '/')
155
+ parts = path.split('/')
156
+ if parts.length == 0
157
+ h = @providers
158
+ else
159
+ parts.shift if parts[0].length == 0
160
+ h = @providers
161
+ parts.each do |part|
162
+ break unless h.has_key?(part)
163
+ h = h[part]
164
+ end
165
+ end
166
+
167
+ refreshments = collect_providers(h)
168
+ Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
169
+
170
+ refreshments.each do |r|
171
+ @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
172
+ end
173
+ refreshments.each do |r|
174
+ require_plugin(r) unless @seen_plugins.has_key?(r)
175
+ end
176
+ end
177
+
178
+ def require_plugin(plugin_name, force=false)
179
+ unless force
180
+ return true if @seen_plugins[plugin_name]
181
+ end
182
+
183
+ @plugin_path = plugin_name
184
+
185
+ filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
186
+
187
+ Ohai::Config[:plugin_path].each do |path|
188
+ check_path = File.expand_path(File.join(path, filename))
189
+ begin
190
+ @seen_plugins[plugin_name] = true
191
+ Ohai::Log.debug("Loading plugin #{plugin_name}")
192
+ from_file(check_path)
193
+ return true
194
+ rescue IOError => e
195
+ Ohai::Log.debug("No #{plugin_name} at #{check_path}")
196
+ rescue Exception,Errno::ENOENT => e
197
+ Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect}")
198
+ end
199
+ end
200
+ end
201
+
202
+ # Sneaky! Lets us stub out require_plugin when testing plugins, but still
203
+ # call the real require_plugin to kick the whole thing off.
204
+ alias :_require_plugin :require_plugin
205
+
206
+ # Serialize this object as a hash
207
+ def to_json(*a)
208
+ output = @data.clone
209
+ output["json_class"] = self.class.name
210
+ output.to_json(*a)
211
+ end
212
+
213
+ # Pretty Print this object as JSON
214
+ def json_pretty_print
215
+ JSON.pretty_generate(@data)
216
+ end
217
+
218
+ def attributes_print(a)
219
+ JSON.pretty_generate(@data[a])
220
+ end
221
+ # Create an Ohai::System from JSON
222
+ def self.json_create(o)
223
+ ohai = new
224
+ o.each do |key, value|
225
+ ohai.data[key] = value unless key == "json_class"
226
+ end
227
+ ohai
228
+ end
229
+
230
+ def method_missing(name, *args)
231
+ return get_attribute(name) if args.length == 0
232
+
233
+ set_attribute(name, *args)
234
+ end
235
+
236
+ private
237
+ def load_plugin_file
238
+
239
+ end
240
+ end
241
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Author:: Diego Algorta (diego@oboxodo.com)
3
+ # Copyright:: Copyright (c) 2009 Diego Algorta
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')
20
+
21
+ describe Ohai::Mixin::Command, "popen4" do
22
+
23
+ it "should default all commands to be run in the POSIX standard C locale" do
24
+ Ohai::Mixin::Command.popen4("echo $LC_ALL") do |pid, stdin, stdout, stderr|
25
+ stdin.close
26
+ stdout.read.strip.should == "C"
27
+ end
28
+ end
29
+
30
+ it "should respect locale when specified explicitly" do
31
+ Ohai::Mixin::Command.popen4("echo $LC_ALL", :environment => {"LC_ALL" => "es"}) do |pid, stdin, stdout, stderr|
32
+ stdin.close
33
+ stdout.read.strip.should == "es"
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')
20
+
21
+ describe Ohai::System, "from_file" do
22
+ before(:each) do
23
+ @ohai = Ohai::System.new
24
+ File.stub!(:exists?).and_return(true)
25
+ File.stub!(:readable?).and_return(true)
26
+ IO.stub!(:read).and_return("king 'herod'")
27
+ end
28
+
29
+ it "should check to see that the file exists" do
30
+ File.should_receive(:exists?).and_return(true)
31
+ @ohai.from_file("/tmp/foo")
32
+ end
33
+
34
+ it "should check to see that the file is readable" do
35
+ File.should_receive(:readable?).and_return(true)
36
+ @ohai.from_file("/tmp/foo")
37
+ end
38
+
39
+ it "should actually read the file" do
40
+ IO.should_receive(:read).and_return("king 'herod'")
41
+ @ohai.from_file("/tmp/foo")
42
+ end
43
+
44
+ it "should call instance_eval with the contents of the file, file name, and line 1" do
45
+ @ohai.should_receive(:instance_eval).with("king 'herod'", "/tmp/foo", 1)
46
+ @ohai.from_file("/tmp/foo")
47
+ end
48
+
49
+ it "should raise an IOError if it cannot read the file" do
50
+ File.stub!(:exists?).and_return(false)
51
+ lambda { @ohai.from_file("/tmp/foo") }.should raise_error(IOError)
52
+ end
53
+ end