0

When I run my unit test case (written in ChefSpec) I get the following error:

Chef::Exceptions::CookbookNotFound: Cookbook azuredns not found. If you're loading azuredns from another cook book, make sure you configure the dependency in your metadata

Following are my spec file, recipe file and metadata file

azuredns/spec/get_azure_token_spec.rb

require 'chefspec'
require 'rest-client'

describe 'azuredns::get_azure_token' do
  let(:chef_run) do
    # Step into the provider
    runner = ChefSpec::SoloRunner.new(step_into: ['azuredns_token'])
    # Read test data from a json file
    file_path = File.expand_path('test_data.json', __dir__)
    file = File.open(file_path)
    contents = file.read
    node_attr = JSON.parse(contents)
    # Load test data into node object
    runner.node.consume_attributes(node_attr)
    runner.converge(described_recipe)
  end

  before(:each) do
    # Mock post method of RestClient
    allow(RestClient).to receive(:post)
      .and_return({ access_token: 'i-am-a-token' }.to_json)
  end

  it 'retrieves token' do
    expect(chef_run).to retrieve_azuredns_token('azure_token')
  end

  it 'varifies the expected value of azure_rest_token' do
    expect(chef_run.node['azure_rest_token']).to eq('Bearer i-am-a-token')
  end

  it 'does not retrieve token due to incorrect resource name' do
    expect(chef_run).to_not retrieve_azuredns_token('azure_token1')
  end

  it 'raises exception due to error in response' do
    # Mock post method of RestClient
    allow(RestClient).to receive(:post)
      .and_return({ error: 'invalid_grant' }.to_json)
    expect { chef_run }.to raise_error(Exception)
  end
end

azuredns/recipe/get_azure_token.rb

require 'rest_client'
require 'json'

cloud_name = node['workorder']['cloud']['ciName']
cloud = node['workorder']['services']['dns'][cloud_name]
dns_attributes = cloud['ciAttributes']

#  Establish connection and get a security token
token = azuredns_token 'azure_token' do
  tenant_id dns_attributes['tenant_id']
  client_id dns_attributes['client_id']
  client_secret dns_attributes['client_secret']
end

token.run_action(:retrieve)

azuredns/metadata.rb

name             'Azuredns'
maintainer       'Shaffan'
maintainer_email 'shaffan.chaudhry1@gmail.com'
license          'Apache License, Version 2.0'
description      'Installs/Configures Azure DNS'
version          '0.1.0'
depends          'azure'

Please help!

Shaffan
  • 53
  • 10

1 Answers1

1

Azuredns != azuredns :-)

Fix the name in your metadata. Chef, and pretty much everything from the UNIX world, is case sensitive.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • This is our coding convention to keep the first letter capital. – Shaffan Mar 01 '16 at 06:02
  • when I change my test case like this: describe "Azuredns::get_azure_token" I get this error: "No resource or method azuredns_token in get_azure_token.rb recipe" That is, it is unable to find my custom resource(LWRP) – Shaffan Mar 01 '16 at 06:16
  • Yes, because again, you would have to use `Azuredns_token`. It's *all* case sensitive which is why you should fix the name instead. – coderanger Mar 01 '16 at 06:45
  • I'm honestly not sure why we even allow uppercase in cookbook names, we should probably stop doing that. – coderanger Mar 01 '16 at 06:46
  • Again, your standard is wrong and you are fixing the wrong problem. You'll have more issues like this in the future. – coderanger Mar 01 '16 at 07:27
  • You are right. I had to capitalize the cookbook name in depends attr of metadata.rb file: 'depends "Azure"' instead of 'depends "azure"'. Also capitalize the Azure cookbook name in its metadata.rb file. Which is causing error in integration testing: Cookbook 'Azure' not found. – Shaffan Mar 01 '16 at 07:56
  • Is it really so hard to understand? Do not apply your naming convention for chef cookbooks. Period. – StephenKing Mar 01 '16 at 16:15