Tabiko
Tabiko

Reputation: 353

Chefspec and stubbing shell_out commands

I've written a library method in my cookbook which would read the /etc/fstab file and modify it if there are certain options missing for mounts.

When I try to write Chefspec tests, they all fail to returns the stubbed information, but rather read the local /etc/fstab on my computer.

I'm wondering what would be the correct syntax to stub a sample fstab which I expect on production servers and pass that information to the library method when running Chefspec.

The library method is in libraries/fstab_quota.rb file:

    module ApplicationQuota
      module Fstab
        def read_fstab
          cmd = shell_out('cat /etc/fstab')
          cmd.run_command

          cmd.stdout.split("\n").each do |fs|
            # Logic here
          end
      end
    end
[Chef::Recipe, Chef::Resource].each  do |l|
  l.send :include, ::ApplicationQuota::Fstab
end

I'd then call the library method inside the cookbook recipe:

# Fstab returns either an Array if there are modifications or false if 
# no modifications have been made
fstab = read_fstab

file '/etc/fstab' do
  content fstab.join("\n")
  owner "root"
  group "root"
  mode "644"
  action :create
  only_if { fstab }
end

Upvotes: 0

Views: 3046

Answers (1)

Tejay Cardon
Tejay Cardon

Reputation: 4223

Stub the shell_out command itself.

my_double = double('shellout_double')
allow(my_double).to receive(:run_command)
allow(my_double).to receive(:stdout).and_return(.....)
allow(Mixlib:ShellOut).to receive(:shell_out).with("your command").and_return(my_double)

Upvotes: 0

Related Questions