80a65b131adf6aa02fb6c61ebec802273138548f
[packages/precise/mcollective.git] / spec / unit / pluginpackager / agent_definition_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module PluginPackager
7     describe AgentDefinition do
8       before :each do
9         PluginPackager.expects(:get_metadata).once.returns({:name => "foo", :version => 1})
10       end
11
12       describe "#initialize" do
13
14         before do
15           AgentDefinition.any_instance.expects(:common)
16         end
17
18         it "should replace spaces in the package name with dashes" do
19           agent = AgentDefinition.new(".", "test package", nil, nil, nil, nil, [], {}, "agent")
20           agent.metadata[:name].should == "test-package"
21         end
22
23         it "should set dependencies if present" do
24           agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, [:name => "foo", :version => nil], {}, "agent")
25           agent.dependencies.should == [{:name => "foo", :version => nil}, {:name => "mcollective-common", :version => nil}]
26         end
27
28         it "should set mc name and version" do
29           agent = AgentDefinition.new(".", "test-package", nil, nil, nil, nil, [], {:mcname =>"pe-mcollective-common", :mcversion =>"1.2"}, "agent")
30           agent.mcname.should == "pe-mcollective-common"
31           agent.mcversion.should == "1.2"
32         end
33
34         it "should replace underscored with dashes in the name" do
35           agent = AgentDefinition.new(".", "test_package", nil, nil, nil, nil, [], {:mcname =>"pe-mcollective-common", :mcversion =>"1.2"}, "agent")
36           agent.metadata[:name].should == "test-package"
37         end
38
39         it "should replace whitespaces with a single dash in the name" do
40           agent = AgentDefinition.new(".", "test    package", nil, nil, nil, nil, [], {:mcname =>"pe-mcollective-common", :mcversion =>"1.2"}, "agent")
41           agent.metadata[:name].should == "test-package"
42         end
43       end
44
45       describe "#identify_packages" do
46         it "should attempt to identify all agent packages" do
47           AgentDefinition.any_instance.expects(:common).once.returns(:check)
48           AgentDefinition.any_instance.expects(:agent).once.returns(:check)
49           AgentDefinition.any_instance.expects(:client).once.returns(:check)
50
51           agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
52           agent.packagedata[:common].should == :check
53           agent.packagedata[:agent].should == :check
54           agent.packagedata[:client].should == :check
55         end
56       end
57
58       describe "#agent" do
59         before do
60           AgentDefinition.any_instance.expects(:client).once
61         end
62
63         it "should not populate the agent files if the agent directory is empty" do
64           AgentDefinition.any_instance.expects(:common).returns(nil)
65           PluginPackager.expects(:check_dir_present).returns(false)
66           agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
67           agent.packagedata[:agent].should == nil
68         end
69
70         it "should add the agent file if the agent directory and implementation file is present" do
71           AgentDefinition.any_instance.expects(:common).returns(nil)
72           PluginPackager.stubs(:check_dir_present).returns(true)
73           File.stubs(:join).with(".", "agent").returns("tmpdir/agent")
74           File.stubs(:join).with("tmpdir/agent", "*.ddl").returns("tmpdir/agent/*.ddl")
75           File.stubs(:join).with("tmpdir/agent", "*").returns("tmpdir/agent/*")
76           Dir.stubs(:glob).with("tmpdir/agent/*.ddl").returns([])
77           Dir.stubs(:glob).with("tmpdir/agent/*").returns(["implementation.rb"])
78
79           agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
80           agent.packagedata[:agent][:files].should == ["implementation.rb"]
81         end
82
83         it "should add common package as dependency if present" do
84           AgentDefinition.any_instance.expects(:common).returns({:files=> ["test.rb"]})
85           PluginPackager.expects(:check_dir_present).with("tmpdir/agent").returns(true)
86           File.stubs(:join).returns("/tmp")
87           File.stubs(:join).with(".", "agent").returns("tmpdir/agent")
88           File.stubs(:join).with(".", "aggregate").returns("tmpdir/aggregate")
89           Dir.stubs(:glob).returns([])
90
91           agent = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
92           agent.packagedata[:agent][:dependencies].should == [{:name => "mcollective-common", :version => nil}]
93           agent.packagedata[:agent][:plugindependency].should == {:name => "mcollective-foo-common", :version =>1, :iteration => 1}
94         end
95       end
96
97       describe "#common" do
98         it "should populate the common files if there are any" do
99           AgentDefinition.any_instance.expects(:agent)
100           AgentDefinition.any_instance.expects(:client)
101           File.expects(:join).with(".", "data", "**").returns("datadir")
102           File.expects(:join).with(".", "util", "**", "**").returns("utildir")
103           File.expects(:join).with(".", "agent", "*.ddl").returns("ddldir")
104           File.expects(:join).with(".", "validator", "**").returns("validatordir")
105           Dir.expects(:glob).with("datadir").returns(["data.rb"])
106           Dir.expects(:glob).with("utildir").returns(["util.rb"])
107           Dir.expects(:glob).with("validatordir").returns(["validator.rb"])
108           Dir.expects(:glob).with("ddldir").returns(["agent.ddl"])
109
110           common = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
111           common.packagedata[:common][:files].should == ["data.rb", "util.rb", "validator.rb", "agent.ddl"]
112         end
113
114         it "should raise an exception if the ddl file isn't present" do
115           File.expects(:join).with(".", "data", "**").returns("datadir")
116           File.expects(:join).with(".", "util", "**", "**").returns("utildir")
117           File.expects(:join).with(".", "agent", "*.ddl").returns("ddldir")
118           File.expects(:join).with(".", "agent").returns("ddldir")
119           File.expects(:join).with(".", "validator", "**").returns("validatordir")
120           Dir.expects(:glob).with("datadir").returns(["data.rb"])
121           Dir.expects(:glob).with("utildir").returns(["util.rb"])
122           Dir.expects(:glob).with("validatordir").returns(["validator.rb"])
123           Dir.expects(:glob).with("ddldir").returns([])
124
125           expect{
126             common = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
127           }.to raise_error(RuntimeError, "cannot create package - No ddl file found in ddldir")
128         end
129       end
130
131       describe "#client" do
132         before do
133           AgentDefinition.any_instance.expects(:agent).returns(nil)
134           File.expects(:join).with(".", "application").returns("clientdir")
135           File.expects(:join).with(".", "aggregate").returns("aggregatedir")
136         end
137
138         it "should populate client files if all directories are present" do
139           AgentDefinition.any_instance.expects(:common).returns(nil)
140           PluginPackager.expects(:check_dir_present).times(2).returns(true)
141           File.expects(:join).with("clientdir", "*").returns("clientdir/*")
142           File.expects(:join).with("aggregatedir", "*").returns("aggregatedir/*")
143           Dir.expects(:glob).with("clientdir/*").returns(["client.rb"])
144           Dir.expects(:glob).with("aggregatedir/*").returns(["aggregate.rb"])
145
146           client = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
147           client.packagedata[:client][:files].should == ["client.rb",  "aggregate.rb"]
148         end
149
150         it "should not populate client files if directories are not present" do
151           AgentDefinition.any_instance.expects(:common).returns(nil)
152           PluginPackager.expects(:check_dir_present).times(2).returns(false)
153
154           client = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
155           client.packagedata[:client].should == nil
156         end
157
158         it "should add common package as dependency if present" do
159           AgentDefinition.any_instance.expects(:common).returns("common")
160           PluginPackager.expects(:check_dir_present).times(2).returns(true)
161           File.expects(:join).with("clientdir", "*").returns("clientdir/*")
162           File.expects(:join).with("aggregatedir", "*").returns("aggregatedir/*")
163           Dir.expects(:glob).with("clientdir/*").returns(["client.rb"])
164           Dir.expects(:glob).with("aggregatedir/*").returns(["aggregate.rb"])
165
166           client = AgentDefinition.new(".", nil, nil, nil, nil, nil, [], {}, "agent")
167           client.packagedata[:client][:dependencies].should == [{:name => "mcollective-common", :version => nil}]
168           client.packagedata[:client][:plugindependency].should == {:name => "mcollective-foo-common", :version => 1, :iteration => 1}
169         end
170       end
171     end
172   end
173 end