avk
avk

Reputation: 61

Chef Inspec Test JSON output from an HTTP API

I am trying to create a inspec control where i need to check the status in a json file which gets downloaded when hitting a http url. When i hit http://localhost:5000/aaa/bbb/ccc/v1/healthCheck url a file gets downloaded(healthCheck.json). I am trying to execute the below code

control "file_check" do
  http_request = http('http://localhost:5000/aaa/bbb/ccc/v1/healthCheck')
  describe json(content: http_request.body) do
    its('status') { should eq 'success' }
  end
end

The error i am getting is

"results": [
            {
              "status": "failed",
              "code_desc": "Control Source Code Error a00972-http/controls/http.rb:5 ",
              "run_time": 0.0006573,
              "start_time": "2019-06-13T09:56:57+10:00",
              "message": "undefined method `body' for nil:NilClass",
              "exception": "RuntimeError",

Thanks in advance for the help.

Upvotes: 0

Views: 1331

Answers (1)

Mr.
Mr.

Reputation: 10122

according to the json resource, you are not accessing the json path correctly.

change:

its('status') { should eq 'success' }

to:

its(['results', 0, 'status']) { should eq 'success' }

please note that the value of results[0].success is failed

Upvotes: 1

Related Questions