ddc7225bc130e78b23fece7464abdd4be88184e8
[packages/precise/mcollective.git] / spec / unit / applications_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   describe Applications do
7     before do
8       tmpfile = Tempfile.new("mc_applications_spec")
9       path = tmpfile.path
10       tmpfile.close!
11
12       @tmpdir = FileUtils.mkdir_p(path)
13       @tmpdir = @tmpdir[0] if @tmpdir.is_a?(Array) # ruby 1.9.2
14       $LOAD_PATH << @tmpdir
15
16       @appsdir = File.join([@tmpdir, "mcollective", "application"])
17       FileUtils.mkdir_p(@appsdir)
18
19       FileUtils.cp(File.join([File.dirname(__FILE__), "..", "fixtures", "application", "test.rb"]), @appsdir)
20     end
21
22     after do
23       FileUtils.rm_r(@tmpdir)
24     end
25
26     describe "[]" do
27       it "should load the config" do
28         Applications.expects(:load_config).once
29         PluginManager.expects("[]").once
30         Applications["test"]
31       end
32
33       it "should return the correct stored application" do
34         app = mock("app")
35         app.stubs(:run)
36
37         Applications.expects(:load_config).once
38         PluginManager.expects("[]").with("test_application").once.returns(app)
39         Applications["test"].should == app
40       end
41     end
42
43     describe "#run" do
44       it "should load the configuration" do
45         app = mock("app")
46         app.stubs(:run)
47
48         Applications.expects(:load_config).once
49         Applications.expects(:load_application).once
50         PluginManager.expects("[]").once.returns(app)
51
52         Applications.run("test")
53       end
54
55       it "should load the application" do
56         app = mock("app")
57         app.stubs(:run)
58
59         Applications.expects(:load_config).once
60         Applications.expects(:load_application).with("test").once
61         PluginManager.expects("[]").once.returns(app)
62
63         Applications.run("test")
64       end
65
66       it "should invoke the application run method" do
67         app = mock("app")
68         app.stubs(:run).returns("hello world")
69
70         Applications.expects(:load_config).once
71         Applications.expects(:load_application)
72         PluginManager.expects("[]").once.returns(app)
73
74         Applications.run("test").should == "hello world"
75       end
76     end
77
78     describe "#load_application" do
79       it "should return the existing application if already loaded" do
80         app = mock("app")
81         app.stubs(:run)
82
83         PluginManager << {:type => "test_application", :class => app}
84
85         Applications.expects("load_config").never
86
87         Applications.load_application("test")
88       end
89
90       it "should load the config" do
91         Applications.expects("load_config").returns(true).once
92         Applications.load_application("test")
93       end
94
95       it "should load the correct class from disk" do
96         PluginManager.expects("loadclass").with("MCollective::Application::Test")
97         Applications.expects("load_config").returns(true).once
98
99         Applications.load_application("test")
100       end
101
102       it "should add the class to the plugin manager" do
103         Applications.expects("load_config").returns(true).once
104
105         PluginManager.expects("<<").with({:type => "test_application", :class => "MCollective::Application::Test"})
106
107         Applications.load_application("test")
108       end
109     end
110
111     describe "#list" do
112       it "should load the configuration" do
113         Applications.expects("load_config").returns(true).once
114         Config.instance.expects("libdir").returns([@tmpdir])
115         Applications.list
116       end
117
118       it "should add found applications to the list" do
119         Applications.expects("load_config").returns(true).once
120         Config.instance.expects("libdir").returns([@tmpdir])
121
122         Applications.list.should == ["test"]
123       end
124
125       it "should print a friendly error and exit on failure" do
126         Applications.expects("load_config").raises(Exception)
127         IO.any_instance.expects(:puts).with(regexp_matches(/Failed to generate application list/)).once
128
129         expect {
130           Applications.list.should
131         }.to raise_error(SystemExit)
132       end
133     end
134
135     describe "#filter_extra_options" do
136       it "should parse --config=x" do
137         ["--config=x --foo=bar -f -f bar", "--foo=bar --config=x -f -f bar"].each do |t|
138           Applications.filter_extra_options(t).should == "--config=x"
139         end
140       end
141
142       it "should parse --config x" do
143         ["--config x --foo=bar -f -f bar", "--foo=bar --config x -f -f bar"].each do |t|
144           Applications.filter_extra_options(t).should == "--config=x"
145         end
146       end
147
148       it "should parse -c x" do
149         ["-c x --foo=bar -f -f bar", "--foo=bar -c x -f -f bar"].each do |t|
150           Applications.filter_extra_options(t).should == "--config=x"
151         end
152       end
153     end
154   end
155 end