summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-09-25 22:14:34 -0700
committerJohn Keiser <john@johnkeiser.com>2015-09-28 17:34:55 -0700
commitb8cffc2e35c662585acff290b27f072c60105479 (patch)
tree5ad0dec08499bf48c9b29e3122d9b96587c75ec7 /spec/support
parent68656820438b84d433ae9015b953790f67ddf0ec (diff)
downloadchef-b8cffc2e35c662585acff290b27f072c60105479.tar.gz
Add test for nonzero systemctl exit code
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/shared/unit/double_world.rb79
-rw-r--r--spec/support/shared/unit/mock_shellout.rb46
2 files changed, 46 insertions, 79 deletions
diff --git a/spec/support/shared/unit/double_world.rb b/spec/support/shared/unit/double_world.rb
deleted file mode 100644
index 1fdd8fcccb..0000000000
--- a/spec/support/shared/unit/double_world.rb
+++ /dev/null
@@ -1,79 +0,0 @@
-#
-# Author:: John Keiser <jkeiser@chef.io>
-# Copyright:: Copyright (c) 2015 John Keiser.
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-#
-# Mocks File and ShellOut.
-#
-# world.files["/etc/rc.d/init.d"] = "blah" means that /etc/rc.d/init.d is a file with contents "blah."
-# world.commands["shellme"] = "result" means that when shell_out! calls "shellme", it will return "result" in stdout.
-# world.commands["shellme"] = 10 means that when shell_out! calls "shellme", it will return exit code 10
-# world.commands["shellme"] = { code: 10, stdout: "result", stderr: "result2"}
-#
-shared_context "double world" do
- let(:world) { DoubleWorld.new }
- before do
- allow(::File).to receive(:exist?) { |f| world.files.has_key?(f) }
- allow(::File).to receive(:exists?) { |f| world.files.has_key?(f) }
- allow(::File).to receive(:executable?) { |f| world.files.has_key?(f) }
- allow(::File).to receive(:open) { |f| StringIO.new(world.files[f]) }
-
- allow(::Mixlib::ShellOut).to receive(:new) do |c|
- result = world.commands[c]
- case result
- when String
- result = { stdout: result }
- when Integer
- result = { exitstatus: result }
- when Hash
- else
- raise ArgumentError, "Result for command #{c} unexpected or undefined: #{result.inspect}"
- end
- DoubleShellout.new(result)
- end
- end
-
- private
-
- class DoubleWorld
- def initialize
- @files = {}
- @commands = {}
- end
- attr_reader :files
- attr_reader :commands
- end
-
- class DoubleShellout
- def initialize(properties)
- @properties = {
- stdout: "",
- stderr: "",
- exitstatus: 0
- }.merge(properties)
- end
- def method_missing(name, *args)
- @properties[name.to_sym]
- end
- def error?
- exitstatus != 0
- end
- def error!
- raise Mixlib::ShellOut::ShellCommandFailed, "Expected process to exit with 0, but received #{exitstatus}" if error?
- end
- end
-end
diff --git a/spec/support/shared/unit/mock_shellout.rb b/spec/support/shared/unit/mock_shellout.rb
new file mode 100644
index 0000000000..7c3e49ec82
--- /dev/null
+++ b/spec/support/shared/unit/mock_shellout.rb
@@ -0,0 +1,46 @@
+#
+# Author:: John Keiser <jkeiser@chef.io>
+# Copyright:: Copyright (c) 2015 John Keiser.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+# Mocks shellout results. Examples:
+# mock_shellout_command("systemctl --all", exitstatus: 1)
+#
+class MockShellout
+ module RSpec
+ def mock_shellout_command(command, **result)
+ allow(::Mixlib::ShellOut).to receive(:new).with(command, anything).and_return MockShellout.new(result)
+ end
+ end
+
+ def initialize(**properties)
+ @properties = {
+ stdout: "",
+ stderr: "",
+ exitstatus: 0
+ }.merge(properties)
+ end
+ def method_missing(name, *args)
+ @properties[name.to_sym]
+ end
+ def error?
+ exitstatus != 0
+ end
+ def error!
+ raise Mixlib::ShellOut::ShellCommandFailed, "Expected process to exit with 0, but received #{exitstatus}" if error?
+ end
+end