Skip to content

Add JSON schema based validation to mbed config script #5022

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

Merged
merged 6 commits into from Feb 1, 2018
Merged

Add JSON schema based validation to mbed config script #5022

merged 6 commits into from Feb 1, 2018

Conversation

ghost
Copy link

@ghost ghost commented Sep 5, 2017

Description

The current configuration system allows a user to specify various compile time configuration options for various targets within JSON files in the projects directory mbed_app.json for applications, and mbed_lib.json for libraries. The parameters will be converted to a collection of macros that are defined when compiling the code. The format of the JSON file is defined here.

Currently, the process of the checking the validity of the config file is done through a series of manual checks in tools/config/__init__.py. This PR will replace the manual checks with a solution that uses JSON schema to validate the configuration files.

Status

IN DEVELOPMENT

Todos

  • Define the JSON schema for app and lib library files
  • Remove remaining manual checks

Steps to test or reproduce

Test the current validation test cases still all pass:

Linux: $ PYTHONPATH=. python -m pytest tools/test/config/config_test.py

Windows: $ set PYTHONPATH=. && python -m pytest tools/test/config/config_test.py

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 5, 2017

cc @theotherjimmy

@theotherjimmy
Copy link
Contributor

@scartmell-arm Wow thanks!

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

Looks great so far! In it's current form, it's a huge improvement over the old validation mechanism. Be aware that #4984 may create a conflict with the test changes.

"pattern": "^([A-Za-z0-9_]+|[A-Za-z0-9_]+=[0-9]+|[A-Za-z0-9_]+=\\\".*\\\")$"
}
},
"__config_path": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this key something that we should allow in the configuration file? or is it something that the tools could add after validation?

Copy link
Author

Choose a reason for hiding this comment

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

I wasn't sure when I added it, it was in the allowable keys list, but isn't mentioned anywhere in the specification so it's probably fine to remove.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's added here

Copy link
Contributor

Choose a reason for hiding this comment

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

You could do the validation before adding that key.

Copy link
Author

Choose a reason for hiding this comment

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

That seems sensible, I'll do that.

"description": "Configuration file for an mbed library",
"type": "object",
"$id": "http://example.com/root.json",
"definitions": {
Copy link
Contributor

Choose a reason for hiding this comment

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

These definitions look almost the same. Could they be moved to another file, and imported?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that should be possible. I'll have a look at separating out the common parts of them, I don't think there are any differences between app and lib config parameter handling that needs to be handled in the validation.

@theotherjimmy
Copy link
Contributor

It looks like travis failed because it does not have the jsonschema package installed. Could you add that to the requirements.txt?

@theotherjimmy
Copy link
Contributor

theotherjimmy commented Sep 5, 2017

@scartmell-arm Looks like there's something missing from the schema: defining macros as evaluating to other macros. Do we need to add this to the documentation?

ConfigException: 'UVISOR_MODE=UVISOR_DISABLED' does not match '^([A-Za-z0-9_]+|[A-Za-z0-9_]+=[0-9]+|[A-Za-z0-9_]+=\\".*\\")$'
'MEM_FREE=free' does not match '^([A-Za-z0-9_]+|[A-Za-z0-9_]+=[0-9]+|[A-Za-z0-9_]+=\\".*\\")$','MEM_ALLOC=malloc' does not match '^([A-Za-z0-9_]+|[A-Za-z0-9_]+=[0-9]+|[A-Za-z0-9_]+=\\".*\\")$'

@ghost
Copy link
Author

ghost commented Sep 6, 2017

Looks like there's something missing from the schema: defining macros as evaluating to other macros. Do we need to add this to the documentation?

@theotherjimmy Yeah, that isn't currently included in the documentation. I have a feeling it's going to be a futile endeavour writing a regex pattern to cover the various valid macro forms so I'll also change that to something reasonable.

Also, wrt the documentation, it states:

However, if target_overrides is defined, config must also be defined.

A number of the tests, and potentially some of the actual application configurations don't follow this so I haven't currently added it to the schema. Whilst it might be correct, I can't see any harm in defining target_overrides without config being set.

@theotherjimmy
Copy link
Contributor

Yeah, that isn't currently included in the documentation.

I'll make a note to include that capability in the documentation.

I have a feeling it's going to be a futile endeavour writing a regex pattern to cover the various valid macro forms so I'll also change that to something reasonable.

Something reasonable like [A-Z0-9_]+(=.*)?? I think that's the best we have.

A number of the tests, and potentially some of the actual application configurations don't follow this so I haven't currently added it to the schema.

Good. That line is just wrong. Adding to the list of documentation updates.

@theotherjimmy
Copy link
Contributor

@scartmell-arm It looks like #4984 changed the way those test worked out from underneath you. Could you rebase to resolve those conflicts?

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

Was this an omission?

"title": "Mbed Library Schema",
"description": "Configuration file for an mbed library",
"type": "object",
"$id": "http://example.com/root.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is quite right.

"title": "Mbed Library Schema",
"description": "Configuration file for an mbed library",
"type": "object",
"$id": "http://example.com/root.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks off too.

@mbed-ci
Copy link

mbed-ci commented Oct 10, 2017

Build : FAILURE

Build number : 50
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

@mbed-ci
Copy link

mbed-ci commented Oct 10, 2017

Build : FAILURE

Build number : 59
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

@adbridge
Copy link
Contributor

/morph build

@adbridge
Copy link
Contributor

@theotherjimmy looks like this needs a re-review

@ghost
Copy link
Author

ghost commented Oct 16, 2017

It's currently breaking in CI because some examples have mbed-app.json that apparently aren't handled by the schema.

Specifically, mbed-cloud-client-example-sources-internal broke during the CI build because it's configuration contains "mbed-trace.enable": null which is being rejected as invalid, though I'm not entirely sure why.

@mbed-ci
Copy link

mbed-ci commented Oct 16, 2017

Build : FAILURE

Build number : 184
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

@theotherjimmy
Copy link
Contributor

@studavekar I can't find the failure in the above build. Could you take a look?

@ghost
Copy link
Author

ghost commented Oct 23, 2017

There's a related one here: Build 729

15:52:08 [K64F_WIFI4 IAR] [ERROR] None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas,None is not valid under any of the given schemas

@theotherjimmy
Copy link
Contributor

I think you're right. Should we allow None? I'm thinking not, but it would be fine, as we would treat it the same as False. Up to @sg- really.

@adbridge
Copy link
Contributor

@sg- Do you have an opinion on this ?

@sg-
Copy link
Contributor

sg- commented Oct 31, 2017

None in the past has led to tracebacks. Is that still the case? If so, no to None, if handled properly then it seems ok.

@theotherjimmy
Copy link
Contributor

theotherjimmy commented Oct 31, 2017

@sg- I don't think that None will lead to trace backs here, but I can't be sure without inspecting a large amount of code. I'd rather be safe and not allow it.

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 22, 2017

@sg- I don't think that None will lead to trace backs here, but I can't be sure without inspecting a large amount of code. I'd rather be safe and not allow it.

@scartmell-arm Can you update the schema based on the feedback above? As this has been idle for some time, I would recommend rebase as well.

@mbed-ci
Copy link

mbed-ci commented Nov 22, 2017

Build : SUCCESS

Build number : 575
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build

@cmonr cmonr added the needs: CI label Feb 1, 2018
@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

Build : SUCCESS

Build number : 1032
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build

@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 1, 2018

This is now ready for integration

@cmonr
Copy link
Contributor

cmonr commented Feb 1, 2018

@scartmell-arm Could you rebase this PR one last time? Looks like a conflict was recently introduced.
I'll keep an eye to push it through CI once the rebase is completed.

Steven Cartmell added 6 commits February 1, 2018 17:18
- Added app and lib JSON schema definition files which specify the valid
  keys and values that can be used in mbed library and application
  configuration files. The primary different between the app and lib
  schema is that the lib config requires a name key.

- Modified the expected error code in some of the test cases. The error
  message is now issued by the JSON validator.

- Added some validation code to the config script which checks the
  validity of the mbed_app.json file when it is initially loaded.

- Added some validation code to config script which checks each of the
  mbed_lib.json scripts when they are loaded.

- Removed manual checks for allowable config keys from within the mbed_app
  and mbed_lib json files.

- Removed the check_dict_types() function which was no longer being
  called.
- Removed the config_path setting from schema and moved the addition of the
config_path value after the validation is done.

- Altered the macro validation regex to be more lenient. Now verifies that
  if '=' is used in the macro definition that something comes after it.
- Moved all common definitions from schema_app and schema_lib into a
  separate definitions file.

- Changed the calling code to resolve multiple schema files correctly.
- Modified the expected error code in some of the test cases. The error
  message is now issued by the JSON validator.

- Stop validation code being called if no application configuration is
  given to validate.

- Change test mock code to check for specific calls, instead of number of
  calls.

- Derive absolute paths for JSON schema files before loading them, which
  seems to work better with the URI path resolution of the schema parser.
@cmonr
Copy link
Contributor

cmonr commented Feb 1, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

Build : SUCCESS

Build number : 1043
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5022/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build

@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

@mbed-ci
Copy link

mbed-ci commented Feb 1, 2018

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

I think the schema can be more restrictive, and therefore more helpful at pointing out errors.

"type": "object",
"patternProperties": {
"\\*": {
"type": "object",
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be the same as the pattern property below, "$ref": "#/target_override_entry"

"target_override_entry": {
"type": "object",
"patternProperties": {
"^\\S+$": {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should contain "$ref": "#/config_parameter_short", I think

Copy link
Contributor

@theotherjimmy theotherjimmy left a comment

Choose a reason for hiding this comment

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

Let's get this in, and I'll submit another PR to make it more strict. @cmonr If you could do the honors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants