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