N°7783 - Relative paths cannot beused when the collector is not launched from its home directory #55
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Base information
Symptom (bug) / Objective (enhancement)
The json collector included in itop-data-collector-base may get its source information from a json file located on the server hosting the collector. In such case, the parameter jsonfile defines the name of the file to be used by the collector together with its path to access it. The path may be absolute or relative to the directory where the collector is deployed (let's call it ).
When a relative path is used to reference the json file (for instance data/myjsonfile.json) and when the collector is not launched from the directory , the json collector cannot locate the json file, logs the following error and stops.
Error:
PHP Warning: file_get_contents(data/myjsonfile.json): Failed to open stream: No such file or directory in /my-root-dir/my-collector/core/jsoncollector.class.inc.php on line xyz
...
[2025-01-02 13:31:26] [Error] [MyJsonCollector] Failed to get JSON file:
[2025-01-02 13:31:26] [Error] MyJsonCollector::Prepare() returned false
When can live with the bug by always specifying absolute paths in the configuration file. This implies that specific params.local.xml file must be provided for phpunit tests. The PR get rid of this requirement.
Reproduction procedure (bug)
Cause (bug)
The json collector doesn't handle the case where the source file may be defined with a relative path. See line 153 of core/jsoncollector.class.inc.php:
Proposed solution (bug and enhancement)
Add a test after the first read of the jsonfile. If it fails, retry with the APPROOT constant prepended to it.
Checklist before requesting a review
Checklist of things to do before PR is ready to merge