7 describe ActionRunner do
10 @req.stubs(:agent).returns("spectester")
11 @req.stubs(:action).returns("tester")
13 command = "/bin/echo 1"
15 @runner = ActionRunner.new(command, @req, :json)
18 describe "#initialize" do
19 it "should set command" do
20 @runner.command.should == "/bin/echo 1"
23 it "should set agent" do
24 @runner.agent.should == "spectester"
27 it "should set action" do
28 @runner.action.should == "tester"
31 it "should set format" do
32 @runner.format.should == :json
35 it "should set request" do
36 @runner.request.should == @req
39 it "should set stdout" do
40 @runner.stdout.should == ""
43 it "should set stderr" do
44 @runner.stderr.should == ""
47 it "should set the command via path_to_command" do
48 ActionRunner.any_instance.expects(:path_to_command).with("rspec").once
49 ActionRunner.new("rspec", @req, :json)
54 it "should create a shell instance with correct settings" do
55 s = @runner.shell("test", "infile", "outfile")
57 s.command.should == "test infile outfile"
58 s.cwd.should == Dir.tmpdir
61 s.environment["MCOLLECTIVE_REQUEST_FILE"].should == "infile"
62 s.environment["MCOLLECTIVE_REPLY_FILE"].should == "outfile"
66 describe "#load_results" do
67 it "should call the correct format loader" do
69 req.expects(:agent).returns("spectester")
70 req.expects(:action).returns("tester")
72 runner = ActionRunner.new("/bin/echo 1", req, :foo)
73 runner.expects("load_foo_results").returns({:foo => :bar})
74 runner.load_results("/dev/null").should == {:foo => :bar}
77 it "should set all keys to Symbol" do
78 data = {"foo" => "bar", "bar" => "baz"}
79 Tempfile.open("mcollective_test", Dir.tmpdir) do |f|
83 results = @runner.load_results(f.path)
84 results.should == {:foo => "bar", :bar => "baz"}
89 describe "#load_json_results" do
90 it "should load data from a file" do
91 Tempfile.open("mcollective_test", Dir.tmpdir) do |f|
92 f.puts '{"foo":"bar","bar":"baz"}'
95 @runner.load_json_results(f.path).should == {"foo" => "bar", "bar" => "baz"}
100 it "should return empty data on JSON parse error" do
101 @runner.load_json_results("/dev/null").should == {}
104 it "should return empty data for missing files" do
105 @runner.load_json_results("/nonexisting").should == {}
108 it "should load complex data correctly" do
109 data = {"foo" => "bar", "bar" => {"one" => "two"}}
110 Tempfile.open("mcollective_test", Dir.tmpdir) do |f|
114 @runner.load_json_results(f.path).should == data
120 describe "#saverequest" do
121 it "should call the correct format serializer" do
123 req.expects(:agent).returns("spectester")
124 req.expects(:action).returns("tester")
126 runner = ActionRunner.new("/bin/echo 1", req, :foo)
128 runner.expects("save_foo_request").with(req).returns('{"foo":"bar"}')
130 runner.saverequest(req)
133 it "should save to a temp file" do
134 @req.expects(:to_json).returns({:foo => "bar"}.to_json)
135 fname = @runner.saverequest(@req).path
137 JSON.load(File.read(fname)).should == {"foo" => "bar"}
138 File.dirname(fname).should == Dir.tmpdir
142 describe "#save_json_request" do
143 it "should return correct json data" do
144 @req.expects(:to_json).returns({:foo => "bar"}.to_json)
145 @runner.save_json_request(@req).should == '{"foo":"bar"}'
149 describe "#canrun?" do
150 it "should correctly report executables" do
152 @runner.canrun?(File.join(ENV['SystemRoot'], "explorer.exe")).should == true
154 @runner.canrun?("/bin/true").should == true
158 it "should detect missing files" do
159 @runner.canrun?("/nonexisting").should == false
164 it "should return correct data" do
165 @runner.to_s.should == "spectester#tester command: /bin/echo 1"
169 describe "#tempfile" do
170 it "should return a TempFile" do
171 @runner.tempfile("foo").class.should == Tempfile
174 it "should contain the prefix in its name" do
175 @runner.tempfile("foo").path.should match(/foo/)
179 describe "#path_to_command" do
180 it "should return the command if it starts with separator" do
181 command = "#{File::SEPARATOR}rspec"
183 runner = ActionRunner.new(command , @req, :json)
184 runner.path_to_command(command).should == command
187 it "should find the first match in the libdir" do
188 Config.instance.expects(:libdir).returns(["#{File::SEPARATOR}libdir1", "#{File::SEPARATOR}libdir2"])
190 action_in_first_dir = File.join(File::SEPARATOR, "libdir1", "agent", "spectester", "action.sh")
191 action_in_last_dir = File.join(File::SEPARATOR, "libdir2", "agent", "spectester", "action.sh")
193 File.expects("exist?").with(action_in_first_dir).returns(true)
194 File.expects("exist?").with(action_in_last_dir).never
196 ActionRunner.new("action.sh", @req, :json).command.should == action_in_first_dir
199 it "should find the match even in the last libdir" do
200 Config.instance.expects(:libdir).returns(["#{File::SEPARATOR}libdir1", "#{File::SEPARATOR}libdir2"])
202 action_in_first_dir = File.join(File::SEPARATOR, "libdir1", "agent", "spectester", "action.sh")
203 action_in_last_dir = File.join(File::SEPARATOR, "libdir2", "agent", "spectester", "action.sh")
205 File.expects("exist?").with(action_in_first_dir).returns(false)
206 File.expects("exist?").with(action_in_last_dir).returns(true)
208 ActionRunner.new("action.sh", @req, :json).command.should == action_in_last_dir