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")
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)
21 File.stubs(:exists?).with("/nonexisting").returns(true)
22 File.stubs(:exists?).with(File.join(File.dirname("/nonexisting"), "rpc-help.erb")).returns(true)
24 ["/one:/two", "/three"].each do |path|
25 File.expects(:readlines).with("/nonexisting").returns(["libdir = #{path}"])
27 Config.instance.loadconfig("/nonexisting")
32 File.expects(:readlines).with("/nonexisting").returns(["libdir = four"])
34 expect { Config.instance.loadconfig("/nonexisting") }.to raise_error(/should be absolute paths/)
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",
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)
53 Config.instance.loadconfig("/nonexisting")
54 }.to raise_error('Identities can only match /\w\.\-/')
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("<<")
66 Config.instance.loadconfig("/nonexisting")
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("<<")
77 Config.instance.loadconfig("/nonexisting")
78 Config.instance.direct_addressing.should == true
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("<<")
88 Config.instance.loadconfig("/nonexisting")
89 Config.instance.direct_addressing.should == false
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("<<")
100 expect { Config.instance.loadconfig("/nonexisting") }.to raise_error("The sylog logger is not usable on the Windows platform")
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")
106 File.expects(:readlines).with("/nonexisting").returns(["libdir=/nonexistinglib"])
107 File.expects(:exists?).with("/nonexisting").returns(true)
108 PluginManager.stubs(:loadclass)
109 PluginManager.stubs("<<")
111 File.expects(:exists?).with(path).returns(true)
113 Config.instance.loadconfig("/nonexisting")
114 Config.instance.rpchelptemplate.should == path
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")
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("<<")
126 Config.instance.loadconfig("/nonexisting")
127 Config.instance.rpchelptemplate.should == "/etc/mcollective/rpc-help.erb"
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("<<")
137 Config.instance.loadconfig("/nonexisting")
138 Config.instance.default_discovery_options.should == ["1", "2"]
142 describe "#read_plugin_config_dir" do
144 @plugindir = File.join("/", "nonexisting", "plugin.d")
146 File.stubs(:directory?).with(@plugindir).returns(true)
148 Config.instance.set_config_defaults("")
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 == {}
157 it "should skip files that do not match the expected filename pattern" do
158 Dir.expects(:new).with(@plugindir).returns(["foo.txt"])
160 IO.expects(:open).with(File.join(@plugindir, "foo.txt")).never
162 Config.instance.read_plugin_config_dir(@plugindir)
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)
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"}
178 it "should override main config file" do
179 configfile = File.join(@plugindir, "foo.cfg")
180 servercfg = File.join(File.dirname(@plugindir), "server.cfg")
182 PluginManager.stubs(:loadclass)
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)
190 Dir.expects(:new).with(@plugindir).returns(["rspec.cfg"])
191 File.expects(:open).with(File.join(@plugindir, "rspec.cfg"), "r").returns(["key = overridden"])
193 Config.instance.loadconfig(servercfg)
194 Config.instance.pluginconf.should == {"rspec.key" => "overridden"}