How to test a bash code snippet


#1

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every
chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this application

(gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/
xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’
}

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use
ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I
changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the
/vagrant box (it’s some old coder I need to maintain).

Thanks!


#2

This isn’t directly related to your testing but you can just run bundle check and it will tell you if any gems need to be installed or upgraded. If you’re only testing for xpath 0.1.4’s existence then if another gem gets upgraded you won’t be able to boot your application.

On Monday, July 8, 2013 at 10:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’ }

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the /vagrant box (it’s some old coder I need to maintain).

Thanks!


#3

Thanks Daniel - that’s makes good sense and I"ll update code for that.

I’m still not sure the best way to test the actual bash code being run by
chef_run though?

On Mon, Jul 8, 2013 at 11:56 PM, Daniel Condomitti daniel@condomitti.comwrote:

This isn’t directly related to your testing but you can just run bundle check and it will tell you if any gems need to be installed or upgraded.
If you’re only testing for xpath 0.1.4’s existence then if another gem gets
upgraded you won’t be able to boot your application.

On Monday, July 8, 2013 at 10:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every
chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this

application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/
xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’
}

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use
ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I
changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the
/vagrant box (it’s some old coder I need to maintain).

Thanks!


#4

To test if it executes, you will need to use the execute resource, not the bash resource. You can probably switch back to execute, and add source ~/.rvm/scripts/rvm before calling rvm use …

To properly test the guard, you will want to have a look at the recent guard evaluation work that was added.

On Monday, July 8, 2013 at 7:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’ }

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the /vagrant box (it’s some old coder I need to maintain).

Thanks!


#5

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without this
it erred not being able to see rvm (non apt package).

On Tue, Jul 9, 2013 at 6:21 AM, John Dewey john@dewey.ws wrote:

To test if it executes, you will need to use the execute resource, not the
bash resource. You can probably switch back to execute, and add source ~/.rvm/scripts/rvm before calling rvm use …

To properly test the guard, you will want to have a look at the recent
guard evaluation work that was added.
https://github.com/acrmp/chefspec#evaluating-guards

On Monday, July 8, 2013 at 7:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every
chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this

application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/
xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’
}

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use
ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I
changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the
/vagrant box (it’s some old coder I need to maintain).

Thanks!


#6

Right, thats why you want to source the rvm script, before calling rvm.

code <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

command = <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose

EOH

expect(chef_run).to execute_command(command)

On Monday, July 8, 2013 at 3:39 PM, Rudi wrote:

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without this it erred not being able to see rvm (non apt package).

On Tue, Jul 9, 2013 at 6:21 AM, John Dewey <john@dewey.ws (mailto:john@dewey.ws)> wrote:

To test if it executes, you will need to use the execute resource, not the bash resource. You can probably switch back to execute, and add source ~/.rvm/scripts/rvm before calling rvm use …

To properly test the guard, you will want to have a look at the recent guard evaluation work that was added.
https://github.com/acrmp/chefspec#evaluating-guards

On Monday, July 8, 2013 at 7:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’ }

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the /vagrant box (it’s some old coder I need to maintain).

Thanks!


#7

Very nice! Thanks man.

On Tue, Jul 9, 2013 at 6:46 AM, John Dewey john@dewey.ws wrote:

Right, thats why you want to source the rvm script, before calling rvm.

code <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

command = <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

expect(chef_run).to execute_command(command)

On Monday, July 8, 2013 at 3:39 PM, Rudi wrote:

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without this
it erred not being able to see rvm (non apt package).

On Tue, Jul 9, 2013 at 6:21 AM, John Dewey john@dewey.ws wrote:

To test if it executes, you will need to use the execute resource, not the
bash resource. You can probably switch back to execute, and add source ~/.rvm/scripts/rvm before calling rvm use …

To properly test the guard, you will want to have a look at the recent
guard evaluation work that was added.
https://github.com/acrmp/chefspec#evaluating-guards

On Monday, July 8, 2013 at 7:56 AM, Rudi wrote:

Hi,

I’m using chefspec with chef 11 and have this working piece of code but
don’t know how to test it.

I hope this is the correct place to ask for any suggestions on this?

I also have a crude file exists check so that bundle doesn’t run every
chef_run.

bash “install with bundler” do
user "vagrant"
cwd "/vagrant"
flags "–login"
code <<-EOH
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

xpath is the last in the list of 125 required gems for this

application (gem list --local)

If this gem is here we assume bundle has already run successfully.

not_if { ::File.exists?(’/home/vagrant/.rvm/gems/ruby-1.9.2-p290/gems/
xpath-0.1.4’) }
end

Prior to this code I was testing the execute command with this:

describe ‘main::install_bundle’ do
let (:chef_run) { ChefSpec::ChefRunner.new.converge ‘main::install_bundle’
}

it “installs with bundler” do
expect(chef_run).to execute_command(“cd /vagrant && rvm use
ruby-1.9.2-p290 && bundle --verbose”)
end
end

But I need the “–login” flag so that rvm can select ruby 1.9.2 so I
changed to the bash resource.

FYI …chef is running with ruby 1.9.3 but the Rails app uses 1.9.2 on the
/vagrant box (it’s some old coder I need to maintain).

Thanks!


#8

On Monday, July 8, 2013 at 3:47 PM, Rudi wrote:

Very nice! Thanks man.

On Tue, Jul 9, 2013 at 6:46 AM, John Dewey <john@dewey.ws (mailto:john@dewey.ws)> wrote:

Right, thats why you want to source the rvm script, before calling rvm.

code <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

command = <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose

EOH

expect(chef_run).to execute_command(command)

On Monday, July 8, 2013 at 3:39 PM, Rudi wrote:

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without this it erred not being able to see rvm (non apt package).

The fact that an execute resource runs bin/sh as an intermediate step is an implementation detail (admittedly, one we’re unlikely to change), so adding the --login flag to the bash resource is better chef code, in my opinion.

I’d encourage you to submit a patch to chefspec to support script/bash resources if you’re able.


Daniel DeLeo


#9

I’d encourage you to submit a patch to chefspec to support script/bash
resources if you’re able.

I like the idea too but I’m not really able to (just not smart enough to be
honest)

What I was initially looking for was something like

expect(chef_run).to execute_bash(command)

But I don’t think that exists right?

On Tue, Jul 9, 2013 at 7:51 AM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, July 8, 2013 at 3:47 PM, Rudi wrote:

Very nice! Thanks man.

On Tue, Jul 9, 2013 at 6:46 AM, John Dewey john@dewey.ws wrote:

Right, thats why you want to source the rvm script, before calling rvm.

code <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

command = <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

expect(chef_run).to execute_command(command)

On Monday, July 8, 2013 at 3:39 PM, Rudi wrote:

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without this
it erred not being able to see rvm (non apt package).

The fact that an execute resource runs bin/sh as an intermediate step is
an implementation detail (admittedly, one we’re unlikely to change), so
adding the --login flag to the bash resource is better chef code, in my
opinion.

I’d encourage you to submit a patch to chefspec to support script/bash
resources if you’re able.


Daniel DeLeo


#10

You might be looking for:

expct(chef_run).to execute_bash_script(command)

Ref
https://github.com/acrmp/chefspec/blob/master/lib/chefspec/matchers/script.rb#L5

-M

On Monday, July 8, 2013, Rudi wrote:

I’d encourage you to submit a patch to chefspec to support script/bash
resources if you’re able.

I like the idea too but I’m not really able to (just not smart enough to
be honest)

What I was initially looking for was something like

expect(chef_run).to execute_bash(command)

But I don’t think that exists right?

On Tue, Jul 9, 2013 at 7:51 AM, Daniel DeLeo <dan@kallistec.com<javascript:_e({}, ‘cvml’, ‘dan@kallistec.com’);>

wrote:

On Monday, July 8, 2013 at 3:47 PM, Rudi wrote:

Very nice! Thanks man.

On Tue, Jul 9, 2013 at 6:46 AM, John Dewey <john@dewey.ws<javascript:_e({}, ‘cvml’, ‘john@dewey.ws’);>

wrote:

Right, thats why you want to source the rvm script, before calling rvm.

code <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

command = <<-EOH.gsub(/^\s+/, ‘’)
source ~/.rvm/scripts/rvm
rvm use ruby-1.9.2-p290
bundle --verbose
EOH

expect(chef_run).to execute_command(command)

On Monday, July 8, 2013 at 3:39 PM, Rudi wrote:

Hey John,

can probably switch back to execute

The reason I left was I needed to pass in the “–login” flag. Without
this it erred not being able to see rvm (non apt package).

The fact that an execute resource runs bin/sh as an intermediate step is
an implementation detail (admittedly, one we’re unlikely to change), so
adding the --login flag to the bash resource is better chef code, in my
opinion.

I’d encourage you to submit a patch to chefspec to support script/bash
resources if you’re able.


Daniel DeLeo