0
當我運行我的單元測試用例(寫在ChefSpec)我得到以下錯誤:ChefSpec無法找到食譜
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
以下是我的規格文件,配方文件和元數據文件
azuredns /規格/ 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 /配方/ 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 '[email protected]'
license 'Apache License, Version 2.0'
description 'Installs/Configures Azure DNS'
version '0.1.0'
depends 'azure'
請幫幫忙!
這是我們的編碼慣例,以保持首字母大寫。 – Shaffan
當我更改我的測試案例是這樣的:describe「Azuredns :: get_azure_token」 我得到這個錯誤:「沒有資源或方法azuredns_token在get_azure_token.rb配方」 也就是說,它無法找到我的自定義資源(LWRP) – Shaffan
是的,因爲再一次,你將不得不使用'Azuredns_token'。這是* all *區分大小寫,這就是爲什麼您應該修復名稱。 – coderanger