-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting error "undefined method `body' for nil:NilClass" when I am trying to test Azure Load Balancer with inspec using the API calls. #206
Comments
Looking at this now. |
I was able to replicate this with InSpec 3.9.3: I used this Terraform config to spin up a basic Azure cluster (including a load balancer). I then used this code: inspec.yml
example.rb # encoding: utf-8
# copyright: 2018, The Authors
title 'sample section'
# you add controls here
control "file_check" do
http_request = http('https://management.azure.com/subscriptions/<my_subscription_id>/resourceGroups/nellresourcegroup/providers/Microsoft.Network/loadBalancers/NellLB/backendAddressPools/BackEndAddressPool?api-version=2018-08-01',
headers: {
'Accept' => 'application/json',
'Authorization' => 'Bearer <my_bearer_token>'
})
describe json(content: http_request.body) do
its('name') { should eq 'hello' }
end
end When running this control, I received the same error.
|
Now to build InSpec from master and see if it is fixed in InSpec 4. |
Confirmed - this is still an issue with the most recent master - digging into this more!
|
This is where that error is being generated - in the instance_eval method used in the Rule object begin
instance_eval(&block)
rescue StandardError => e
# We've encountered an exception while trying to eval the code inside the
# control block. We need to prevent the exception from bubbling up, and
# fail the control. Controls are failed by having a failed resource within
# them; but since our control block is unsafe (and opaque) to us, let's
# make a dummy and fail that.
location = block.source_location.compact.join(':')
describe 'Control Source Code Error' do
# Rubocop thinks we are raising an exception - we're actually calling RSpec's fail()
its(location) { fail e.message } # rubocop: disable Style/SignalException
end |
I believe the way to solve this is to handle the case where an http response does not have a body. Working on that. |
Though I'm still wondering how @worker can be nil in this block of code:
Many of the http resource methods depend on worker NOT being nil - and I don't think we want to write handling for each one of them. |
Aha! The problem is here:
In the case of the control being run on the Azure LB, opts is nil (though I'm not sure why). That means that this method (required to instantiate the @worker)
Will return an error, which means @worker will not be created. I went through some of this thought process when working on a similar issue, I don't know why opts is being set to nil, but it's something we need to at least add handling for. |
This issue should be resolved by using the inspec-azure resource pack. |
I am trying to test Azure Load Balancer with inspec using the API calls. My control file looks like below:
Control file:
Profile file inspec.yml
When running the test I am getting below error:
× file_check: /home/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/newtest/test/integration/new_profile/controls/basic.rb:13
× Control Source Code Error /home/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/new-test/test/integration/new_profile/controls/basic.rb:13
undefined method `body' for nil:NilClass
Seems like nothing is stored in http_request, Can anyone suggest where is the issue?
Originally posted by @nsr99 in inspec/inspec#2394 (comment)
The text was updated successfully, but these errors were encountered: