Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Make body be JSON string and fix tests #3

Merged
merged 1 commit into from
Sep 7, 2021

Conversation

ofedoren
Copy link
Member

@ofedoren ofedoren commented Sep 1, 2021

I've changed the way the body is represented since it fixes the problem with https://github.com/theforeman/foreman_host_reports/blob/ab78021ca47d1b5676fd9c964206df975dc8ba3d/app/controllers/api/v2/host_reports_controller.rb#L50-L53. The problem is that if the body is not a string then it tries to generate one and the generated one cannot be JSON.parsed again. Also, in this way Rails won't parse body into parameters and thus we don't need to change the default behaviour by potentially breaking stuff. Since this plugin is aimed to be used only in foreman_host_reports plugin context only, I guess we can afford such change.

Also, made rubocop happy and fixed few tests.

P.S. With this change the report is being saved properly and parsed for view. There is also no additional parsing before save on the Foreman plugin side since the body is a string. The main concern I can see is that the body seems to be double JSON.dumped.

@ofedoren
Copy link
Member Author

ofedoren commented Sep 2, 2021

Thanks, @ekohl, updated.

format: "puppet",
version: 1,
host: @body["host"],
reported_at: @body["reported_at"],
status: 0,
proxy: @body["proxy"],
body: @body,
body: @body.to_json,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is a TODO in build_report_root that hints at this. Is it something that should be done in that method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was not implemented yet:

    # TODO add body string serialization and metric with total time
    # and for tests there must be an option to turn this off

My thinking was that JSON in JSON is very hard to read so I was thinking to have some flag so in tests we could generate it without JSON in string so fixtures are easy to read and compare. This would need to be HTTP parameter so it can be used in integration tests.

Could you implement this?

test/puppet_processor_test.rb Outdated Show resolved Hide resolved
test/puppet_processor_test.rb Outdated Show resolved Hide resolved
@ofedoren
Copy link
Member Author

ofedoren commented Sep 4, 2021

@lzap, updated. I've made json_body param to be true by default, since it's more internal param for test purposes and can be changed via http[s]://fqdn/host_reports/puppet[ansible]?json_body=false.

Copy link
Member

@lzap lzap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests will look still nice while we optimize for maximum efficiency.

@lzap lzap merged commit 31af1ef into theforeman:develop Sep 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants