Hi chef-users email list,

I am resending this as Charles Johnson indicated that my rpevious communication was formatted as a winmail.dat attachment by our Exchange Server (Thank you Charles).

I am trying to implement unit testing for a cookbook wrapper to HAProxy that I put together. I’m trying to define an environment and stubbed node within that environment, in order to test the search and template rendering functionality of my cookbook.

If I include chefspec/server in my spec_helper file, and attempt to create the node and set the running nodes environment to ‘staging’, a la:

context ‘with env, role and one node’ do
let(:node_env) { ‘staging’ }
let(:chef_run) do
ChefSpec::Runner.new(platform: platform, version: platform_version) do |node|
# Create a new environment (you could also use a different :let block or :before block)
env = Chef::Environment.new
env.name node_env

    # Stub the node to return this environment
    node.stub(:chef_environment).and_return(env.name)

    # Stub any calls to Environment.load to return this environment
    Chef::Environment.stub(:load).and_return(env)
  end.converge(described_recipe)
end

before do
  ChefSpec::Server.create_environment(node_env, { description: 'Staging env' })
  ChefSpec::Server.create_role('smp', { default_attributes: {} })
  ChefSpec::Server.create_node('smp1.stg.prv', {
    run_list: ['role[smp]'],
    chef_environment: node_env,
    normal: { fqdn: '127.0.0.1', hostname: 'smp1.stg.prv', ipaddress: '127.0.0.1' }
  })
end

it 'Renders a template in staging with staging values' do
  expect(chef_run).to render_file('/etc/haproxy/haproxy.cfg').with_content(/smp1.stg.prv/)
end

end

All of my test report:

Net::HTTPServerException:
404 "Not Found “

From what I can tell via Google, the Chef gem version, Chef Zero gem version and chef spec gem version are all somewhat temperamental. Do the Chef-DK Chef Zero and Chef-DK ChefSpec not play nice? Should I create a Bundler file with “proper” Gem version and bundle install it? Does the old chef_solo syntax work, and if so, can anyone point me to a web resource that uses only chef_solo syntax?

Really, any help anyone can provide would be greatly appreciated on my part.

Thanks all.
Joseph Hammerman

I have created a pastebin here with my code and the versions of what I believe are the relevant tools:

bash-3.2$ cat spec/default_spec.rb require ‘spec_helper.rb’ haproxy_config_f - Pastebin.com

bash-3.2$ cat spec/default_spec.rb require ‘spec_helper.rb’ haproxy_config_f - Pastebin.com
bash-3.2$ cat spec/default_spec.rb require ‘spec_helper.rb’ haproxy_config_file = ‘/etc/haproxy/haproxy.cfg’ describe ‘sm-haproxy-websockets::default’ do
View on pastebin.com Preview by Yahoo

Thanks in advance for any assistance that anyone on the list is able to provide.
Joseph Hammerman

On Monday, November 10, 2014 3:19 PM, Joseph Hammerman josephhammerman@yahoo.com wrote:

Hi chef-users email list,

I am resending this as Charles Johnson indicated that my rpevious communication was formatted as a winmail.dat attachment by our Exchange Server (Thank you Charles).

I am trying to implement unit testing for a cookbook wrapper to HAProxy that I put together. I’m trying to define an environment and stubbed node within that environment, in order to test the search and template rendering functionality of my cookbook.

If I include chefspec/server in my spec_helper file, and attempt to create the node and set the running nodes environment to ‘staging’, a la:

context ‘with env, role and one node’ do
let(:node_env) { ‘staging’ }
let(:chef_run) do
ChefSpec::Runner.new(platform: platform, version: platform_version) do |node|
# Create a new environment (you could also use a different :let block or :before block)
env = Chef::Environment.new
env.name node_env

    # Stub the node to return this environment
    node.stub(:chef_environment).and_return(env.name)

    # Stub any calls to Environment.load to return this environment
    Chef::Environment.stub(:load).and_return(env)
  end.converge(described_recipe)
end

before do
  ChefSpec::Server.create_environment(node_env, { description: 'Staging env' })
  ChefSpec::Server.create_role('smp', { default_attributes: {} })
  ChefSpec::Server.create_node('smp1.stg.prv', {
    run_list: ['role[smp]'],
    chef_environment: node_env,
    normal: { fqdn: '127.0.0.1', hostname: 'smp1.stg.prv', ipaddress: '127.0.0.1' }
  })
end

it 'Renders a template in staging with staging values' do
  expect(chef_run).to render_file('/etc/haproxy/haproxy.cfg').with_content(/smp1.stg.prv/)
end

end

All of my test report:

Net::HTTPServerException:
404 "Not Found “

From what I can tell via Google, the Chef gem version, Chef Zero gem version and chef spec gem version are all somewhat temperamental. Do the Chef-DK Chef Zero and Chef-DK ChefSpec not play nice? Should I create a Bundler file with “proper” Gem version and bundle install it? Does the old chef_solo syntax work, and if so, can anyone point me to a web resource that uses only chef_solo syntax?

Really, any help anyone can provide would be greatly appreciated on my part.

Thanks all.
Joseph Hammerman

On Monday, November 10, 2014 at 12:19 PM, Joseph Hammerman wrote:

Hi chef-users email list,

I am resending this as Charles Johnson indicated that my rpevious communication was formatted as a winmail.dat attachment by our Exchange Server (Thank you Charles).

I am trying to implement unit testing for a cookbook wrapper to HAProxy that I put together. I’m trying to define an environment and stubbed node within that environment, in order to test the search and template rendering functionality of my cookbook.

If I include chefspec/server in my spec_helper file, and attempt to create the node and set the running nodes environment to ‘staging’, a la:

context 'with env, role and one node' do
let(:node_env) { 'staging' }
let(:chef_run) do
ChefSpec::Runner.new(platform: platform, version: platform_version) do |node|

Create a new environment (you could also use a different :let block or :before block)

env = Chef::Environment.new
env.name (http://env.name) node_env

Stub the node to return this environment

node.stub(:chef_environment).and_return(env.name (http://env.name))

Stub any calls to Environment.load to return this environment

Chef::Environment.stub(:load).and_return(env)
end.converge(described_recipe)
end

before do
ChefSpec::Server.create_environment(node_env, { description: 'Staging env' })
ChefSpec::Server.create_role('smp', { default_attributes: {} })
ChefSpec::Server.create_node('smp1.stg.prv', {
run_list: ['role[smp]'],
chef_environment: node_env,
normal: { fqdn: '127.0.0.1', hostname: 'smp1.stg.prv', ipaddress: '127.0.0.1' }
})
end

it 'Renders a template in staging with staging values' do
expect(chef_run).to render_file('/etc/haproxy/haproxy.cfg').with_content(/smp1.stg.prv/)
end
end

All of my test report:

Net::HTTPServerException:
404 "Not Found “

From what I can tell via Google, the Chef gem version, Chef Zero gem version and chef spec gem version are all somewhat temperamental. Do the Chef-DK Chef Zero and Chef-DK ChefSpec not play nice? Should I create a Bundler file with “proper” Gem version and bundle install it? Does the old chef_solo syntax work, and if so, can anyone point me to a web resource that uses only chef_solo syntax?

Really, any help anyone can provide would be greatly appreciated on my part.

Thanks all.

Joseph Hammerman
The error message is a little sparse, but maybe you’re just missing a stub somewhere which is causing the chef code to try to pull down data from a server? Using the -b option to rspec will give you a full trace which should help pinpoint which method is making the HTTP call.

HTH,

--
Daniel DeLeo