facter 1.6.18 → 1.7.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of facter might be problematic. Click here for more details.

Files changed (126) hide show
  1. data/Gemfile +23 -8
  2. data/bin/facter +4 -1
  3. data/ext/build_defaults.yaml +1 -1
  4. data/ext/debian/changelog.erb +4 -22
  5. data/ext/debian/control +2 -2
  6. data/ext/project_data.yaml +1 -1
  7. data/ext/redhat/facter.spec.erb +10 -9
  8. data/install.rb +27 -53
  9. data/lib/facter.rb +23 -5
  10. data/lib/facter/application.rb +21 -0
  11. data/lib/facter/blockdevices.rb +105 -0
  12. data/lib/facter/domain.rb +19 -7
  13. data/lib/facter/filesystems.rb +38 -0
  14. data/lib/facter/hardwaremodel.rb +3 -2
  15. data/lib/facter/ipaddress6.rb +1 -2
  16. data/lib/facter/kernelrelease.rb +10 -2
  17. data/lib/facter/ldom.rb +47 -0
  18. data/lib/facter/macaddress.rb +1 -1
  19. data/lib/facter/manufacturer.rb +7 -1
  20. data/lib/facter/memory.rb +58 -160
  21. data/lib/facter/operatingsystem.rb +17 -2
  22. data/lib/facter/operatingsystemmajrelease.rb +33 -0
  23. data/lib/facter/operatingsystemrelease.rb +88 -37
  24. data/lib/facter/osfamily.rb +6 -2
  25. data/lib/facter/processor.rb +2 -2
  26. data/lib/facter/ps.rb +5 -0
  27. data/lib/facter/ssh.rb +50 -12
  28. data/lib/facter/util/cfpropertylist.rb +6 -0
  29. data/lib/facter/util/cfpropertylist/LICENSE +19 -0
  30. data/lib/facter/util/cfpropertylist/README +44 -0
  31. data/lib/facter/util/cfpropertylist/Rakefile +44 -0
  32. data/lib/facter/util/cfpropertylist/THANKS +7 -0
  33. data/lib/facter/util/cfpropertylist/lib/cfpropertylist.rb +6 -0
  34. data/lib/facter/util/cfpropertylist/lib/rbBinaryCFPropertyList.rb +562 -0
  35. data/lib/facter/util/cfpropertylist/lib/rbCFPlistError.rb +26 -0
  36. data/lib/facter/util/cfpropertylist/lib/rbCFPropertyList.rb +402 -0
  37. data/lib/facter/util/cfpropertylist/lib/rbCFTypes.rb +244 -0
  38. data/lib/facter/util/cfpropertylist/lib/rbLibXMLParser.rb +135 -0
  39. data/lib/facter/util/cfpropertylist/lib/rbNokogiriParser.rb +140 -0
  40. data/lib/facter/util/cfpropertylist/lib/rbREXMLParser.rb +136 -0
  41. data/lib/facter/util/collection.rb +36 -14
  42. data/lib/facter/util/composite_loader.rb +12 -0
  43. data/lib/facter/util/config.rb +36 -0
  44. data/lib/facter/util/confine.rb +1 -6
  45. data/lib/facter/util/directory_loader.rb +83 -0
  46. data/lib/facter/util/fact.rb +49 -42
  47. data/lib/facter/util/file_read.rb +32 -0
  48. data/lib/facter/util/ip.rb +2 -9
  49. data/lib/facter/util/loader.rb +16 -2
  50. data/lib/facter/util/macosx.rb +15 -2
  51. data/lib/facter/util/memory.rb +154 -27
  52. data/lib/facter/util/nothing_loader.rb +15 -0
  53. data/lib/facter/util/parser.rb +141 -0
  54. data/lib/facter/util/processor.rb +27 -35
  55. data/lib/facter/util/resolution.rb +97 -26
  56. data/lib/facter/util/solaris_zones.rb +153 -0
  57. data/lib/facter/util/virtual.rb +32 -3
  58. data/lib/facter/version.rb +72 -2
  59. data/lib/facter/virtual.rb +56 -3
  60. data/lib/facter/zfs_version.rb +10 -0
  61. data/lib/facter/zonename.rb +6 -0
  62. data/lib/facter/zones.rb +17 -0
  63. data/lib/facter/zpool_version.rb +10 -0
  64. data/spec/fixtures/ifconfig/ifconfig_net_tools_1.60.txt +19 -0
  65. data/spec/fixtures/ifconfig/ifconfig_ubuntu_1204.txt +16 -0
  66. data/spec/fixtures/ldom/ldom_v1 +6 -0
  67. data/spec/fixtures/unit/filesystems/linux +28 -0
  68. data/spec/fixtures/unit/util/manufacturer/intel_linux_dmidecode +549 -0
  69. data/spec/fixtures/unit/virtual/sysfs_dmi_entries_raw.txt +0 -0
  70. data/spec/fixtures/unit/zfs_version/freebsd_8.2 +14 -0
  71. data/spec/fixtures/unit/zfs_version/freebsd_9.0 +13 -0
  72. data/spec/fixtures/unit/zfs_version/linux-fuse_0.6.9 +14 -0
  73. data/spec/fixtures/unit/zfs_version/solaris_10 +10 -0
  74. data/spec/fixtures/unit/zfs_version/solaris_11 +12 -0
  75. data/spec/fixtures/unit/zpool_version/freebsd_8.2 +26 -0
  76. data/spec/fixtures/unit/zpool_version/freebsd_9.0 +38 -0
  77. data/spec/fixtures/unit/zpool_version/linux-fuse_0.6.9 +35 -0
  78. data/spec/fixtures/unit/zpool_version/solaris_10 +31 -0
  79. data/spec/fixtures/unit/zpool_version/solaris_11 +43 -0
  80. data/spec/integration/facter_spec.rb +12 -0
  81. data/spec/spec_helper.rb +9 -0
  82. data/spec/unit/architecture_spec.rb +1 -1
  83. data/spec/unit/blockdevices_spec.rb +109 -0
  84. data/spec/unit/domain_spec.rb +189 -81
  85. data/spec/unit/ec2_spec.rb +15 -8
  86. data/spec/unit/filesystems_spec.rb +50 -0
  87. data/spec/unit/hardwaremodel_spec.rb +8 -1
  88. data/spec/unit/id_spec.rb +6 -5
  89. data/spec/unit/ipaddress6_spec.rb +14 -2
  90. data/spec/unit/ipaddress_spec.rb +1 -1
  91. data/spec/unit/kernel_spec.rb +24 -0
  92. data/spec/unit/kernelmajversion_spec.rb +17 -0
  93. data/spec/unit/kernelrelease_spec.rb +53 -0
  94. data/spec/unit/kernelversion_spec.rb +32 -0
  95. data/spec/unit/ldom_spec.rb +74 -0
  96. data/spec/unit/macaddress_spec.rb +3 -1
  97. data/spec/unit/manufacturer_spec.rb +115 -0
  98. data/spec/unit/memory_spec.rb +442 -75
  99. data/spec/unit/operatingsystem_spec.rb +16 -2
  100. data/spec/unit/operatingsystemmajrelease_spec.rb +16 -0
  101. data/spec/unit/operatingsystemrelease_spec.rb +110 -1
  102. data/spec/unit/processor_spec.rb +22 -7
  103. data/spec/unit/ps_spec.rb +42 -0
  104. data/spec/unit/ssh_spec.rb +76 -0
  105. data/spec/unit/util/collection_spec.rb +94 -118
  106. data/spec/unit/util/config_spec.rb +36 -5
  107. data/spec/unit/util/confine_spec.rb +31 -43
  108. data/spec/unit/util/directory_loader_spec.rb +87 -0
  109. data/spec/unit/util/fact_spec.rb +37 -25
  110. data/spec/unit/util/file_read_spec.rb +29 -0
  111. data/spec/unit/util/ip_spec.rb +4 -2
  112. data/spec/unit/util/loader_spec.rb +102 -45
  113. data/spec/unit/util/macosx_spec.rb +40 -9
  114. data/spec/unit/util/manufacturer_spec.rb +12 -1
  115. data/spec/unit/util/parser_spec.rb +135 -0
  116. data/spec/unit/util/resolution_spec.rb +136 -4
  117. data/spec/unit/util/solaris_zones_spec.rb +127 -0
  118. data/spec/unit/util/virtual_spec.rb +54 -0
  119. data/spec/unit/version_spec.rb +42 -0
  120. data/spec/unit/virtual_spec.rb +102 -27
  121. data/spec/unit/zfs_version_spec.rb +76 -0
  122. data/spec/unit/zonename_spec.rb +14 -0
  123. data/spec/unit/zones_spec.rb +55 -0
  124. data/spec/unit/zpool_version_spec.rb +76 -0
  125. metadata +113 -11
  126. data/lib/facter/arp.rb +0 -28
@@ -4,15 +4,46 @@ require 'spec_helper'
4
4
  require 'facter/util/macosx'
5
5
 
6
6
  describe Facter::Util::Macosx do
7
+ let(:badplist) do
8
+ '<?xml version="1.0" encoding="UTF-8"?>
9
+ <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
10
+ <plist version="1.0">
11
+ <dict>
12
+ <key>test</key>
13
+ <string>file</string>
14
+ </dict>
15
+ </plist>'
16
+ end
17
+
18
+ let(:goodplist) do
19
+ '<?xml version="1.0" encoding="UTF-8"?>
20
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
21
+ <plist version="1.0">
22
+ <dict>
23
+ <key>test</key>
24
+ <string>file</string>
25
+ </dict>
26
+ </plist>'
27
+ end
28
+
7
29
  it "should be able to retrieve profiler data as xml for a given data field" do
8
30
  Facter::Util::Resolution.expects(:exec).with("/usr/sbin/system_profiler -xml foo").returns "yay"
9
31
  Facter::Util::Macosx.profiler_xml("foo").should == "yay"
10
32
  end
11
33
 
12
- it "should use PList to convert xml to data structures" do
13
- Plist.expects(:parse_xml).with("foo").returns "bar"
34
+ it 'should correct a bad XML doctype string' do
35
+ Facter.expects(:debug).with('Had to fix plist with incorrect DOCTYPE declaration')
36
+ Facter::Util::Macosx.intern_xml(badplist)
37
+ end
38
+
39
+ it 'should return a hash given XML data' do
40
+ test_hash = { 'test' => 'file' }
41
+ Facter::Util::Macosx.intern_xml(goodplist).should == test_hash
42
+ end
14
43
 
15
- Facter::Util::Macosx.intern_xml("foo").should == "bar"
44
+ it 'should fail when trying to read invalid XML' do
45
+ expect { Facter::Util::Macosx.intern_xml('<bad}|%-->xml<--->') }.to \
46
+ raise_error(RuntimeError, /A plist file could not be properly read by Facter::Util::CFPropertyList/)
16
47
  end
17
48
 
18
49
  describe "when collecting profiler data" do
@@ -43,19 +74,19 @@ describe Facter::Util::Macosx do
43
74
  Facter::Util::Macosx.expects(:profiler_data).with("SPSoftwareDataType").returns "eh"
44
75
  Facter::Util::Macosx.os_overview.should == "eh"
45
76
  end
46
-
77
+
47
78
  describe "when working out software version" do
48
-
79
+
49
80
  before do
50
81
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productName").returns "Mac OS X"
51
82
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -buildVersion").returns "9J62"
52
83
  end
53
-
84
+
54
85
  it "should have called sw_vers three times when determining software version" do
55
86
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7"
56
87
  Facter::Util::Macosx.sw_vers
57
88
  end
58
-
89
+
59
90
  it "should return a hash with the correct keys when determining software version" do
60
91
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "10.5.7"
61
92
  Facter::Util::Macosx.sw_vers.keys.sort.should == ["macosx_productName",
@@ -64,14 +95,14 @@ describe Facter::Util::Macosx do
64
95
  "macosx_productversion_major",
65
96
  "macosx_productVersion"].sort
66
97
  end
67
-
98
+
68
99
  it "should split a product version of 'x.y.z' into separate hash entries correctly" do
69
100
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "1.2.3"
70
101
  sw_vers = Facter::Util::Macosx.sw_vers
71
102
  sw_vers["macosx_productversion_major"].should == "1.2"
72
103
  sw_vers["macosx_productversion_minor"].should == "3"
73
104
  end
74
-
105
+
75
106
  it "should treat a product version of 'x.y' as 'x.y.0" do
76
107
  Facter::Util::Resolution.expects(:exec).with("/usr/bin/sw_vers -productVersion").returns "2.3"
77
108
  Facter::Util::Macosx.sw_vers["macosx_productversion_minor"].should == "0"
@@ -34,8 +34,9 @@ describe Facter::Manufacturer do
34
34
  it "should not set manufacturer if prtdiag output is nil" do
35
35
  # Stub kernel so we don't have windows fall through to its own mechanism
36
36
  Facter.fact(:kernel).stubs(:value).returns("SunOS")
37
+ Facter.fact(:hardwareisa).stubs(:value).returns("sparc")
37
38
 
38
- Facter::Util::Resolution.stubs(:exec).returns(nil)
39
+ Facter::Util::Resolution.stubs(:exec).with(regexp_matches(/prtdiag/)).returns(nil)
39
40
  Facter::Manufacturer.prtdiag_sparc_find_system_info()
40
41
  Facter.value(:manufacturer).should_not == "Sun Microsystems"
41
42
  end
@@ -122,6 +123,16 @@ Handle 0x001F
122
123
  Facter.value(:ramlocation).should == "System Board Or Motherboard"
123
124
  end
124
125
 
126
+ it "should return an appropriate uuid on linux" do
127
+ Facter.fact(:kernel).stubs(:value).returns("Linux")
128
+ dmidecode = my_fixture_read("intel_linux_dmidecode")
129
+ Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode)
130
+
131
+ query = { '[Ss]ystem [Ii]nformation' => [ { 'UUID:' => 'uuid' } ] }
132
+ Facter::Manufacturer.dmi_find_system_info(query)
133
+ Facter.value(:uuid).should == "60A98BB3-95B6-E111-AF74-4C72B9247D28"
134
+ end
135
+
125
136
  def find_product_name(os)
126
137
  output_file = case os
127
138
  when "FreeBSD" then my_fixture("freebsd_dmidecode")
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'spec_helper'
4
+
5
+ require 'facter/util/parser'
6
+ require 'tempfile'
7
+ require 'tmpdir.rb'
8
+
9
+ describe Facter::Util::Parser do
10
+ include PuppetlabsSpec::Files
11
+
12
+ describe "extension_matches? function" do
13
+ it "should match extensions when subclass uses match_extension" do
14
+ Facter::Util::Parser.extension_matches?("myfile.foobar", "foobar").should == true
15
+ end
16
+
17
+ it "should match extensions when subclass uses match_extension with an array" do
18
+ Facter::Util::Parser.extension_matches?("myfile.ext1", ["ext1","ext2","ext3"]).should == true
19
+ Facter::Util::Parser.extension_matches?("myfile.ext2", ["ext1","ext2","ext3"]).should == true
20
+ Facter::Util::Parser.extension_matches?("myfile.ext3", ["ext1","ext2","ext3"]).should == true
21
+ end
22
+
23
+ it "should match extension ignoring case on file" do
24
+ Facter::Util::Parser.extension_matches?("myfile.EXT1", "ext1").should == true
25
+ Facter::Util::Parser.extension_matches?("myfile.ExT1", "ext1").should == true
26
+ Facter::Util::Parser.extension_matches?("myfile.exT1", "ext1").should == true
27
+ end
28
+
29
+ it "should match extension ignoring case for match_extension" do
30
+ Facter::Util::Parser.extension_matches?("myfile.EXT1", "EXT1").should == true
31
+ Facter::Util::Parser.extension_matches?("myfile.ExT1", "EXT1").should == true
32
+ Facter::Util::Parser.extension_matches?("myfile.exT1", "EXT1").should == true
33
+ end
34
+ end
35
+
36
+ let(:data) do {"one" => "two", "three" => "four"} end
37
+
38
+ describe "yaml" do
39
+ let(:data_in_yaml) do YAML.dump(data) end
40
+ let(:data_file) do "/tmp/foo.yaml" end
41
+
42
+ it "should return a hash of whatever is stored on disk" do
43
+ File.stubs(:read).with(data_file).returns(data_in_yaml)
44
+ described_class.parser_for(data_file).results.should == data
45
+ end
46
+
47
+ it "should handle exceptions and warn" do
48
+ # YAML data with an error
49
+ File.stubs(:read).with(data_file).returns(data_in_yaml + "}")
50
+ Facter.expects(:warn).at_least_once
51
+ lambda { Facter::Util::Parser.parser_for(data_file).results }.should_not raise_error
52
+ end
53
+ end
54
+
55
+ describe "json" do
56
+ let(:data_in_json) do JSON.dump(data) end
57
+ let(:data_file) do "/tmp/foo.json" end
58
+
59
+ it "should return a hash of whatever is stored on disk" do
60
+ pending("this test requires the json library") unless Facter.json?
61
+ File.stubs(:read).with(data_file).returns(data_in_json)
62
+ Facter::Util::Parser.parser_for(data_file).results.should == data
63
+ end
64
+ end
65
+
66
+ describe "txt" do
67
+ let(:data_file) do "/tmp/foo.txt" end
68
+
69
+ shared_examples_for "txt parser" do
70
+ it "should return a hash of whatever is stored on disk" do
71
+ File.stubs(:read).with(data_file).returns(data_in_txt)
72
+ Facter::Util::Parser.parser_for(data_file).results.should == data
73
+ end
74
+ end
75
+
76
+ context "well formed data" do
77
+ let(:data_in_txt) do "one=two\nthree=four\n" end
78
+ it_behaves_like "txt parser"
79
+ end
80
+
81
+ context "extra equal sign" do
82
+ let(:data_in_txt) do "one=two\nthree=four=five\n" end
83
+ let(:data) do {"one" => "two", "three" => "four=five"} end
84
+ it_behaves_like "txt parser"
85
+ end
86
+
87
+ context "extra data" do
88
+ let(:data_in_txt) do "one=two\nfive\nthree=four\n" end
89
+ it_behaves_like "txt parser"
90
+ end
91
+ end
92
+
93
+ describe "scripts" do
94
+ let :cmd do "/tmp/foo.sh" end
95
+ let :data_in_txt do "one=two\nthree=four\n" end
96
+
97
+ before :each do
98
+ Facter::Util::Resolution.stubs(:exec).with(cmd).returns(data_in_txt)
99
+ File.stubs(:executable?).with(cmd).returns(true)
100
+ end
101
+
102
+ it "should return a hash of whatever is returned by the executable" do
103
+ pending("this test does not run on windows") if Facter::Util::Config.is_windows?
104
+ File.stubs(:file?).with(cmd).returns(true)
105
+ Facter::Util::Parser.parser_for(cmd).results.should == data
106
+ end
107
+
108
+ it "should not parse a directory" do
109
+ File.stubs(:file?).with(cmd).returns(false)
110
+ Facter::Util::Parser.parser_for(cmd).results.should be_nil
111
+ end
112
+
113
+ context "on Windows" do
114
+ let :cmd do "/tmp/foo.bat" end
115
+
116
+ before :each do
117
+ Facter::Util::Config.stubs(:is_windows?).returns(true)
118
+ end
119
+
120
+ let :parser do
121
+ Facter::Util::Parser.parser_for(cmd)
122
+ end
123
+
124
+ it "should return no results" do
125
+ parser.results.should be_nil
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "nothing parser" do
131
+ it "uses the nothing parser when there is no other parser" do
132
+ Facter::Util::Parser.parser_for("this.is.not.valid").results.should be_nil
133
+ end
134
+ end
135
+ end
@@ -4,6 +4,8 @@ require 'spec_helper'
4
4
  require 'facter/util/resolution'
5
5
 
6
6
  describe Facter::Util::Resolution do
7
+ include FacterSpec::ConfigHelper
8
+
7
9
  it "should require a name" do
8
10
  lambda { Facter::Util::Resolution.new }.should raise_error(ArgumentError)
9
11
  end
@@ -12,6 +14,12 @@ describe Facter::Util::Resolution do
12
14
  Facter::Util::Resolution.new("yay").name.should == "yay"
13
15
  end
14
16
 
17
+ it "should be able to set the value" do
18
+ resolve = Facter::Util::Resolution.new("yay")
19
+ resolve.value = "foo"
20
+ resolve.value.should == "foo"
21
+ end
22
+
15
23
  it "should have a method for setting the weight" do
16
24
  Facter::Util::Resolution.new("yay").should respond_to(:has_weight)
17
25
  end
@@ -43,6 +51,66 @@ describe Facter::Util::Resolution do
43
51
  res.limit.should == "testing"
44
52
  end
45
53
 
54
+
55
+ describe "when overriding environment variables" do
56
+ it "should execute the caller's block with the specified env vars" do
57
+ test_env = { "LANG" => "C", "FOO" => "BAR" }
58
+ Facter::Util::Resolution.with_env test_env do
59
+ test_env.keys.each do |key|
60
+ ENV[key].should == test_env[key]
61
+ end
62
+ end
63
+ end
64
+
65
+ it "should restore pre-existing environment variables to their previous values" do
66
+ orig_env = {}
67
+ new_env = {}
68
+ # an arbitrary sentinel value to use to temporarily set the environment vars to
69
+ sentinel_value = "Abracadabra"
70
+
71
+ # grab some values from the existing ENV (arbitrarily choosing 3 here)
72
+ ENV.keys.first(3).each do |key|
73
+ # save the original values so that we can test against them later
74
+ orig_env[key] = ENV[key]
75
+ # create bogus temp values for the chosen keys
76
+ new_env[key] = sentinel_value
77
+ end
78
+
79
+ # verify that, during the 'with_env', the new values are used
80
+ Facter::Util::Resolution.with_env new_env do
81
+ orig_env.keys.each do |key|
82
+ ENV[key].should == new_env[key]
83
+ end
84
+ end
85
+
86
+ # verify that, after the 'with_env', the old values are restored
87
+ orig_env.keys.each do |key|
88
+ ENV[key].should == orig_env[key]
89
+ end
90
+ end
91
+
92
+ it "should not be affected by a 'return' statement in the yield block" do
93
+ @sentinel_var = :resolution_test_foo.to_s
94
+
95
+ # the intent of this test case is to test a yield block that contains a return statement. However, it's illegal
96
+ # to use a return statement outside of a method, so we need to create one here to give scope to the 'return'
97
+ def handy_method()
98
+ ENV[@sentinel_var] = "foo"
99
+ new_env = { @sentinel_var => "bar" }
100
+
101
+ Facter::Util::Resolution.with_env new_env do
102
+ ENV[@sentinel_var].should == "bar"
103
+ return
104
+ end
105
+ end
106
+
107
+ handy_method()
108
+
109
+ ENV[@sentinel_var].should == "foo"
110
+
111
+ end
112
+ end
113
+
46
114
  describe "when setting the code" do
47
115
  before do
48
116
  Facter.stubs(:warnonce)
@@ -87,6 +155,27 @@ describe Facter::Util::Resolution do
87
155
  end
88
156
  end
89
157
 
158
+ describe 'callbacks when flushing facts' do
159
+ class FlushFakeError < StandardError; end
160
+
161
+ subject do
162
+ Facter::Util::Resolution.new("jeff")
163
+ end
164
+
165
+ context '#on_flush' do
166
+ it 'accepts a block with on_flush' do
167
+ subject.on_flush() { raise NotImplementedError }
168
+ end
169
+ end
170
+
171
+ context '#flush' do
172
+ it 'calls the block passed to on_flush' do
173
+ subject.on_flush() { raise FlushFakeError }
174
+ expect { subject.flush }.to raise_error FlushFakeError
175
+ end
176
+ end
177
+ end
178
+
90
179
  it "should be able to return a value" do
91
180
  Facter::Util::Resolution.new("yay").should respond_to(:value)
92
181
  end
@@ -96,6 +185,11 @@ describe Facter::Util::Resolution do
96
185
  @resolve = Facter::Util::Resolution.new("yay")
97
186
  end
98
187
 
188
+ it "should return any value that has been provided" do
189
+ @resolve.value = "foo"
190
+ @resolve.value.should == "foo"
191
+ end
192
+
99
193
  describe "and setcode has not been called" do
100
194
  it "should return nil" do
101
195
  Facter::Util::Resolution.expects(:exec).with(nil, nil).never
@@ -106,7 +200,7 @@ describe Facter::Util::Resolution do
106
200
  describe "and the code is a string" do
107
201
  describe "on windows" do
108
202
  before do
109
- Facter::Util::Config.stubs(:is_windows?).returns(true)
203
+ given_a_configuration_of(:is_windows => true)
110
204
  end
111
205
 
112
206
  it "should return the result of executing the code" do
@@ -125,7 +219,7 @@ describe Facter::Util::Resolution do
125
219
 
126
220
  describe "on non-windows systems" do
127
221
  before do
128
- Facter::Util::Config.stubs(:is_windows?).returns(false)
222
+ given_a_configuration_of(:is_windows => false)
129
223
  end
130
224
 
131
225
  it "should return the result of executing the code" do
@@ -490,14 +584,52 @@ describe Facter::Util::Resolution do
490
584
 
491
585
  # It's not possible, AFAICT, to mock %x{}, so I can't really test this bit.
492
586
  describe "when executing code" do
587
+ # set up some command strings, making sure we get the right version for both unix and windows
588
+ echo_command = Facter::Util::Config.is_windows? ? 'cmd.exe /c "echo foo"' : 'echo foo'
589
+ echo_env_var_command = Facter::Util::Config.is_windows? ? 'cmd.exe /c "echo %%%s%%"' : 'echo $%s'
590
+
493
591
  it "should deprecate the interpreter parameter" do
494
592
  Facter.expects(:warnonce).with("The interpreter parameter to 'exec' is deprecated and will be removed in a future version.")
495
593
  Facter::Util::Resolution.exec("/something", "/bin/perl")
496
594
  end
497
595
 
596
+ # execute a simple echo command
498
597
  it "should execute the binary" do
499
- test_command = Facter::Util::Config.is_windows? ? 'cmd.exe /c echo foo' : 'echo foo'
500
- Facter::Util::Resolution.exec(test_command).should == "foo"
598
+ Facter::Util::Resolution.exec(echo_command).should == "foo"
599
+ end
600
+
601
+ it "should override the LANG environment variable" do
602
+ Facter::Util::Resolution.exec(echo_env_var_command % 'LANG').should == "C"
603
+ end
604
+
605
+ it "should respect other overridden environment variables" do
606
+ Facter::Util::Resolution.with_env( {"FOO" => "foo"} ) do
607
+ Facter::Util::Resolution.exec(echo_env_var_command % 'FOO').should == "foo"
608
+ end
609
+ end
610
+
611
+ it "should restore overridden LANG environment variable after execution" do
612
+ # we're going to call with_env in a nested fashion, to make sure that the environment gets restored properly
613
+ # at each level
614
+ Facter::Util::Resolution.with_env( {"LANG" => "foo"} ) do
615
+ # Resolution.exec always overrides 'LANG' for its own execution scope
616
+ Facter::Util::Resolution.exec(echo_env_var_command % 'LANG').should == "C"
617
+ # But after 'exec' completes, we should see our value restored
618
+ ENV['LANG'].should == "foo"
619
+ # Now we'll do a nested call to with_env
620
+ Facter::Util::Resolution.with_env( {"LANG" => "bar"} ) do
621
+ # During 'exec' it should still be 'C'
622
+ Facter::Util::Resolution.exec(echo_env_var_command % 'LANG').should == "C"
623
+ # After exec it should be restored to our current value for this level of the nesting...
624
+ ENV['LANG'].should == "bar"
625
+ end
626
+ # Now we've dropped out of one level of nesting,
627
+ ENV['LANG'].should == "foo"
628
+ # Call exec one more time just for kicks
629
+ Facter::Util::Resolution.exec(echo_env_var_command % 'LANG').should == "C"
630
+ # One last check at our current nesting level.
631
+ ENV['LANG'].should == "foo"
632
+ end
501
633
  end
502
634
 
503
635
  context "when run on unix", :as_platform => :posix do