Skip to content
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

feat(cli): add validate subcommand #165

Merged
merged 5 commits into from
May 7, 2024
Merged

Conversation

indirection42
Copy link
Member

@indirection42 indirection42 commented Apr 23, 2024

Resolve #55

Usage:

subway --config configs/config.yml validate
Note: since using subway validate --config configs/config.yml mentioned in #55 will require config being the option of the validate subcommand, I use the above form instead. If the former form is proper, a new run subcommand can be added.

Detailed validations performed

  • static checks: use garde crate's proc_macro to make validation more extendible. Including:
    • endpoints must be websocket URL format
    • ensure each method has only one param with inject=true
    • ensure each method has only one param with inject=true
  • dynamic(rely on outside state) checks:
    • simple endpoints connection check, just test if TCP handshake is a success

Example Success Output (with exit code 0):

2024-04-17T12:52:26.716236Z  INFO subway::extensions::client: Connected to endpoint: wss://acala-rpc-0.aca-api.network
2024-04-17T12:52:28.811641Z  INFO subway::extensions::client: Connected to endpoint: wss://acala-rpc.dwellir.com

Example Failure Output (with exit code 1):

2024-04-17T12:53:15.457831Z  INFO subway::extensions::client: Connected to endpoint: wss://acala-rpc-0.aca-api.network
2024-04-17T12:53:15.580627Z ERROR subway::extensions::client: Failed to connect to endpoint: wss://example.com, error: Networking or low-level protocol error: Connection rejected with status code: 200

Caused by:
    Connection rejected with status code: 200
2024-04-17T12:53:15.673427Z  INFO subway::extensions::client: Connected to endpoint: wss://acala-rpc.dwellir.com
Error: Unable to connect to all endpoints

Questions:

  1. I found middlewares and extensions are two sets, but they are intersected, should I do checks for the intersection?
  2. Should the validation test be performed in #[cfg(test)] (for developers, it makes sense), or we validate it individually in CI with an individual command?

src/cli.rs Outdated Show resolved Hide resolved
pub extensions: ExtensionsConfig,
// TODO: #[garde(custom(has_matched_extensions(&self.extensions)))]
Copy link
Member

Choose a reason for hiding this comment

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

what is this TODO for?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's for question 1 which I raised at the beginning of this PR's main comments.

}

#[tokio::test]
#[ignore = "This test is not support yet"]
Copy link
Member

Choose a reason for hiding this comment

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

why it is not yet supported?
if it is not yet implemented, then no need to have a not working test here

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, this is also related to Question 1 in this PR's main comment. If the question gets clarified, I will fix it.

assert!(test_fn(&invalid_params, &()).is_err());
// since test_fn is FnOnce, we need get a new one
let test_fn = validate_params_with_name(method_name);
assert!(test_fn(&another_invalid_params, &()).is_err());
Copy link
Member

Choose a reason for hiding this comment

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

best to break this into two different tests. and need a test for a valid one

@xlc
Copy link
Member

xlc commented Apr 24, 2024

Middleware depends on extensions and yes it will be helpful to ensure the required extensions are defined.
We can validate the default config in CI. The main use case is to validate the production config file, which is different for every team.

@indirection42 indirection42 force-pushed the feat-validate-subcommand branch from bf9a7bf to 200e86f Compare April 25, 2024 13:06
};
}

define_middleware_extension_mappings! {
Copy link
Member

Choose a reason for hiding this comment

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

this isn't very extensible as we need to define all the mapping here and it is very easy to make mistake when we introduce new extensions or make modifications.

should add a new validate method to the extension and call it instead. in that way the validation and use are in a same file and harder to modify one without modify others. and we should be able to do it in such way to ensure it is not possible to require an extension without validate it.

Copy link
Member Author

@indirection42 indirection42 Apr 26, 2024

Choose a reason for hiding this comment

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

Yeah, I agree. As you said, to make it not possible to use without validating it, I think the best way is to ensure it via type-safety. The process is like this: first we validate at some entrypoint which typically in the very beginning, then we wrap it into a new-type, and use this validated new-type all over the rest. Everywhere we use the new-type, we know the value has been validated. But it may take lots of changes in the codes, I will try to get a workaround.

Copy link
Member

Choose a reason for hiding this comment

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

let's remove this part for now so it is not blocking this PR and then we can do the extension validation in next PR

Copy link
Member Author

Choose a reason for hiding this comment

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

All right.

Copy link
Collaborator

@ermalkaleci ermalkaleci left a comment

Choose a reason for hiding this comment

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

can you move testing config files into another directory?

@indirection42
Copy link
Member Author

indirection42 commented Apr 26, 2024

can you move testing config files into another directory?

Yeah, I felt like doing so. Should I move the config_with_env.yml too?

@indirection42 indirection42 merged commit a1628f5 into master May 7, 2024
1 check passed
@indirection42 indirection42 deleted the feat-validate-subcommand branch May 7, 2024 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

validate subcommand to validate config
3 participants