Update code from https://github.com/dmi-try/marionette-collective
[packages/precise/mcollective.git] / spec / unit / config_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe Config do
7     describe "#loadconfig" do
8       it "should fail when no libdir is set" do
9         File.expects(:exists?).with("/nonexisting").returns(true)
10         File.expects(:readlines).with("/nonexisting").returns([])
11         Config.instance.stubs(:set_config_defaults)
12         expect { Config.instance.loadconfig("/nonexisting") }.to raise_error("The /nonexisting config file does not specify a libdir setting, cannot continue")
13       end
14
15       it "should only test that libdirs are absolute paths" do
16         Util.expects(:absolute_path?).with("/one").returns(true)
17         Util.expects(:absolute_path?).with("/two").returns(true)
18         Util.expects(:absolute_path?).with("/three").returns(true)
19         Util.expects(:absolute_path?).with("four").returns(false)
20
21         File.stubs(:exists?).with("/nonexisting").returns(true)
22         File.stubs(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
23
24         ["/one:/two", "/three"].each do |path|
25           File.expects(:readlines).with("/nonexisting").returns(["libdir = #{path}"])
26
27           Config.instance.loadconfig("/nonexisting")
28
29           PluginManager.clear
30         end
31
32         File.expects(:readlines).with("/nonexisting").returns(["libdir = four"])
33
34         expect { Config.instance.loadconfig("/nonexisting") }.to raise_error(/should be absolute paths/)
35       end
36
37       it "should not allow any path like construct for identities" do
38         # Taken from puppet test cases
39         ['../foo', '..\\foo', './../foo', '.\\..\\foo',
40           '/foo', '//foo', '\\foo', '\\\\goo',
41           "test\0/../bar", "test\0\\..\\bar",
42           "..\\/bar", "/tmp/bar", "/tmp\\bar", "tmp\\bar",
43           " / bar", " /../ bar", " \\..\\ bar",
44           "c:\\foo", "c:/foo", "\\\\?\\UNC\\bar", "\\\\foo\\bar",
45           "\\\\?\\c:\\foo", "//?/UNC/bar", "//foo/bar",
46           "//?/c:/foo"
47         ].each do |input|
48           File.expects(:readlines).with("/nonexisting").returns(["identity = #{input}", "libdir=/nonexistinglib"])
49           File.expects(:exists?).with("/nonexisting").returns(true)
50           File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
51
52           expect {
53             Config.instance.loadconfig("/nonexisting")
54           }.to raise_error('Identities can only match /\w\.\-/')
55         end
56       end
57
58       it "should allow valid identities" do
59         ["foo", "foo_bar", "foo-bar", "foo-bar-123", "foo.bar", "foo_bar_123"].each do |input|
60           File.expects(:readlines).with("/nonexisting").returns(["identity = #{input}", "libdir=/nonexistinglib"])
61           File.expects(:exists?).with("/nonexisting").returns(true)
62           File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
63           PluginManager.stubs(:loadclass)
64           PluginManager.stubs("<<")
65
66           Config.instance.loadconfig("/nonexisting")
67         end
68       end
69
70       it "should set direct_addressing to true by default" do
71         File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib"])
72         File.expects(:exists?).with("/nonexisting").returns(true)
73         File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
74         PluginManager.stubs(:loadclass)
75         PluginManager.stubs("<<")
76
77         Config.instance.loadconfig("/nonexisting")
78         Config.instance.direct_addressing.should == true
79       end
80
81       it "should allow direct_addressing to be disabled in the config file" do
82         File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib", "direct_addressing=n"])
83         File.expects(:exists?).with("/nonexisting").returns(true)
84         File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
85         PluginManager.stubs(:loadclass)
86         PluginManager.stubs("<<")
87
88         Config.instance.loadconfig("/nonexisting")
89         Config.instance.direct_addressing.should == false
90       end
91
92       it "should not allow the syslog logger type on windows" do
93         Util.expects("windows?").returns(true).twice
94         File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib", "logger_type=syslog"])
95         File.expects(:exists?).with("/nonexisting").returns(true)
96         File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
97         PluginManager.stubs(:loadclass)
98         PluginManager.stubs("<<")
99
100         expect { Config.instance.loadconfig("/nonexisting") }.to raise_error("The sylog logger is not usable on the Windows platform")
101       end
102
103       it "should default to finding the help template in the same dir as the config file" do
104         path = File.join(File.dirname("/nonexisting"), "rpc-help.erb")
105
106         File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib"])
107         File.expects(:exists?).with("/nonexisting").returns(true)
108         PluginManager.stubs(:loadclass)
109         PluginManager.stubs("<<")
110
111         File.expects(:exists?).with(path).returns(true)
112
113         Config.instance.loadconfig("/nonexisting")
114         Config.instance.rpchelptemplate.should == path
115       end
116
117       it "should fall back to old behavior if the help template file does not exist in the config dir" do
118         path = File.join(File.dirname("/nonexisting"), "rpc-help.erb")
119
120         File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib"])
121         File.expects(:exists?).with("/nonexisting").returns(true)
122         File.expects(:exists?).with(path).returns(false)
123         PluginManager.stubs(:loadclass)
124         PluginManager.stubs("<<")
125
126         Config.instance.loadconfig("/nonexisting")
127         Config.instance.rpchelptemplate.should == "/etc/mcollective/rpc-help.erb"
128       end
129
130       it "should support multiple default_discovery_options" do
131         File.expects(:readlines).with("/nonexisting").returns(["default_discovery_options = 1", "default_discovery_options = 2", "libdir=/nonexistinglib"])
132         File.expects(:exists?).with("/nonexisting").returns(true)
133         File.expects(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
134         PluginManager.stubs(:loadclass)
135         PluginManager.stubs("<<")
136
137         Config.instance.loadconfig("/nonexisting")
138         Config.instance.default_discovery_options.should == ["1", "2"]
139       end
140     end
141
142     describe "#read_plugin_config_dir" do
143       before do
144         @plugindir = File.join("/", "nonexisting", "plugin.d")
145
146         File.stubs(:directory?).with(@plugindir).returns(true)
147
148         Config.instance.set_config_defaults("")
149       end
150
151       it "should not fail if the supplied directory is missing" do
152         File.expects(:directory?).with(@plugindir).returns(false)
153         Config.instance.read_plugin_config_dir(@plugindir)
154         Config.instance.pluginconf.should == {}
155       end
156
157       it "should skip files that do not match the expected filename pattern" do
158         Dir.expects(:new).with(@plugindir).returns(["foo.txt"])
159
160         IO.expects(:open).with(File.join(@plugindir, "foo.txt")).never
161
162         Config.instance.read_plugin_config_dir(@plugindir)
163       end
164
165       it "should load the config files" do
166         Dir.expects(:new).with(@plugindir).returns(["foo.cfg"])
167         File.expects(:open).with(File.join(@plugindir, "foo.cfg"), "r").returns([]).once
168         Config.instance.read_plugin_config_dir(@plugindir)
169       end
170
171       it "should set config parameters correctly" do
172         Dir.expects(:new).with(@plugindir).returns(["foo.cfg"])
173         File.expects(:open).with(File.join(@plugindir, "foo.cfg"), "r").returns(["rspec = test"])
174         Config.instance.read_plugin_config_dir(@plugindir)
175         Config.instance.pluginconf.should == {"foo.rspec" => "test"}
176       end
177
178       it "should override main config file" do
179         configfile = File.join(@plugindir, "foo.cfg")
180         servercfg = File.join(File.dirname(@plugindir), "server.cfg")
181
182         PluginManager.stubs(:loadclass)
183
184         File.stubs(:exists?).returns(true)
185         File.stubs(:directory?).with(@plugindir).returns(true)
186         File.stubs(:exists?).with(servercfg).returns(true)
187         File.expects(:readlines).with(servercfg).returns(["plugin.rspec.key = default", "libdir=/nonexisting"])
188         File.stubs(:directory?).with("/nonexisting").returns(true)
189
190         Dir.expects(:new).with(@plugindir).returns(["rspec.cfg"])
191         File.expects(:open).with(File.join(@plugindir, "rspec.cfg"), "r").returns(["key = overridden"])
192
193         Config.instance.loadconfig(servercfg)
194         Config.instance.pluginconf.should == {"rspec.key" => "overridden"}
195       end
196     end
197   end
198 end