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

Add validation for parameters passed with command #3259

Merged

Conversation

devang-gaur
Copy link
Contributor

kind bug

What does does this PR do / why we need it:
Adds Validation on parameters passed with odo config set commands

Which issue(s) this PR fixes:

Fixes #3036

@dharmit
Copy link
Member

dharmit commented May 29, 2020

Couple of things:

$ odo config set ports "8080/TCP/UDP" -f
E0529 11:36:57.513401    6790 config.go:690] Validation failed for the provided arguments, Error: unable to parse the port string 8080/TCP/UDP
 ✗  unknown parameter :'' is not a parameter in local odo config

IMO, we should simply print the error message. Details about error number, line where it occurred, etc. should not be shown to the user.

Also, why is it showing the error unknown parameter :'' is not a parameter in local odo config? I'm little confused there. Error: unable to parse the port string 8080/TCP/UDP looks good, however.

IMO, below should fail because I've not provided TCP or UDP to port 9090:

$ odo config set ports "8080/TCP,9090" -f
 ✓  Local config successfully updated

Run `odo push --config` command to apply changes to the cluster

$ odo config view
COMPONENT SETTINGS
------------------------------------------------
PARAMETER         CURRENT_VALUE
Type              nodejs
Application       app
Project           default
SourceType        local
Ref               
SourceLocation    ./
Ports             8080/TCP,9090
Name              nodejs-nodejs-ex-wcxj
MinMemory         
MaxMemory         
DebugPort         
Ignore            
MinCPU            
MaxCPU

Another thing from code point of view - please add a comment describing what IsValidLArgumentList is doing.

@devang-gaur
Copy link
Contributor Author

@dharmit

odo config set ports "8080/TCP,9090" -f

is not failing because the port validator function accepts it.

I'm using existing validation utilities so that the validation behavior remains uniform. Should I create a different validator ?

https://github.com/openshift/odo/blob/6bd0d73dc67e014e34f7e92a35dab26734163afc/pkg/odo/util/validation/validators.go#L60

@devang-gaur devang-gaur force-pushed the validate_params branch 3 times, most recently from d74ba3f to 5ad2ab2 Compare May 29, 2020 12:35
@dharmit
Copy link
Member

dharmit commented Jun 1, 2020

Should I create a different validator ?

Nope. My bad. Should have read the comments that said it's a valid value.

@girishramnani
Copy link
Contributor

girishramnani commented Jun 1, 2020

If I remember correctly not providing the protocol in <PORT>TCP/UDP defaults to TCP I think in most tools

@dharmit
Copy link
Member

dharmit commented Jun 1, 2020

If I remember correctly not providing the protocol in <PORT>TCP/UDP defaults to TCP I think in most tools

Behind the scenes, it does. I tried creating a URL for port without protocol info in config.yaml and it works. It doesn't show up in odo config view, however. Not an issue, IMO.

@devang-gaur
Copy link
Contributor Author

devang-gaur commented Jun 1, 2020

If I remember correctly not providing the protocol in <PORT>TCP/UDP defaults to TCP I think in most tools

Behind the scenes, it does. I tried creating a URL for port without protocol info in config.yaml and it works. It doesn't show up in odo config view, however. Not an issue, IMO.

I think it would help to be clear with the user if we specify that in odo config view

@dharmit
Copy link
Member

dharmit commented Jun 3, 2020

I think it would help to be clear with the user if we specify that in odo config view

Or, it could be mentioned in the CLI documentation or actual documentation. If we'd want to make it clear in the output of odo config view, we would have to modify the validation function.

case "ports", "debugport":
err = validation.PortsValidator(args[1])
default:
if len(args) < 2 || len(args[1]) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t this check be at the top?

var err error
switch param {
case "ports", "debugport":
err = validation.PortsValidator(args[1])
Copy link
Contributor

@girishramnani girishramnani Jun 10, 2020

Choose a reason for hiding this comment

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

What would happen if I provided odo config set ports without any value for the ports? Probably would panic due to args[1] I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

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

param, ok := asLocallySupportedParameter(args[0])

if !ok {
return errors.Errorf("the parameter provided : %v, is not present in the supported parameters list.", args[0])
Copy link
Contributor

@girishramnani girishramnani Jun 10, 2020

Choose a reason for hiding this comment

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

There is an error string convention that error don’t start with capitals and doesn’t end with a fullstop

param, ok := asLocallySupportedParameter(args[0])

if !ok {
err = errors.Errorf("the parameter provided : %v, is not present in the supported parameters list", args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

I would just say: "provided parameter '%v' is not a supported parameter"

if !ok {
err = errors.Errorf("the parameter provided : %v, is not present in the supported parameters list", args[0])
} else if len(args) < 2 || len(args[1]) == 0 {
err = errors.Errorf("no argument provided for parameter %v", param)
Copy link
Contributor

Choose a reason for hiding this comment

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

"no value" instead of "argument"

}

if err != nil {
err = errors.Errorf("validation failed for the provided arguments, Error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would output error as "validation failed for the provided arguments: %v"

@@ -730,3 +731,30 @@ func (lci *LocalConfigInfo) GetOSSourcePath() (path string, err error) {

return sourceOSPath, err
}

// IsValidArgumentList validates the argument list for `odo config` command
func IsValidArgumentList(args []string) error {
Copy link
Contributor

@metacosm metacosm Jun 11, 2020

Choose a reason for hiding this comment

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

This function should actually be merged with the Args function that is passed to the command struct when it's created since otherwise the check on args length will never occur (as it's done in that function). Actually, there would be merits to check the args there instead of in Complete… See: https://github.com/openshift/odo/blob/6bd0d73dc67e014e34f7e92a35dab26734163afc/pkg/odo/cli/config/set.go#L96-L103

@girishramnani
Copy link
Contributor

@dev-gaur CI failing

@girishramnani
Copy link
Contributor

CI seem to fail but code is good hence, please fix the tests if need be.
/approve

@openshift-ci-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: girishramnani

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label Jun 19, 2020
@codecov
Copy link

codecov bot commented Jun 22, 2020

Codecov Report

Merging #3259 into master will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3259      +/-   ##
==========================================
+ Coverage   46.28%   46.45%   +0.16%     
==========================================
  Files         112      112              
  Lines       11200    11237      +37     
==========================================
+ Hits         5184     5220      +36     
+ Misses       5518     5513       -5     
- Partials      498      504       +6     
Impacted Files Coverage Δ
pkg/config/config.go 45.64% <100.00%> (ø)
pkg/sync/sync.go 43.56% <0.00%> (-4.96%) ⬇️
pkg/util/file_indexer.go 7.50% <0.00%> (-2.50%) ⬇️
pkg/devfile/adapters/kubernetes/utils/utils.go 52.48% <0.00%> (-1.93%) ⬇️
pkg/devfile/adapters/docker/component/adapter.go 74.01% <0.00%> (-0.99%) ⬇️
pkg/devfile/adapters/docker/component/utils.go 68.83% <0.00%> (-0.91%) ⬇️
pkg/preference/preference.go 61.87% <0.00%> (-0.63%) ⬇️
pkg/lclient/client.go 0.00% <0.00%> (ø)
pkg/lclient/fakeclient.go 82.11% <0.00%> (ø)
pkg/testingutil/devfile.go 0.00% <0.00%> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f736e3b...d6bf379. Read the comment docs.

@mohammedzee1000
Copy link
Contributor

/kind bug

@openshift-ci-robot openshift-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Jun 30, 2020
err = errors.Errorf("the parameter provided : %v, is not supported", args[0])
}

switch param {
Copy link
Contributor

Choose a reason for hiding this comment

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

+100, I feel happy when i see switch statement instead of multiple if else

@@ -730,3 +731,31 @@ func (lci *LocalConfigInfo) GetOSSourcePath() (path string, err error) {

return sourceOSPath, err
}

// IsValidArgumentList validates the argument list for `odo config` command
func IsValidArgumentList(args []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should not be in that file as it only deals with command-related code, not config-related functionality per se. My advice would be to put it with the set command and unexport it.

param, ok := asLocallySupportedParameter(args[0])

if !ok {
err = errors.Errorf("the parameter provided : %v, is not supported", args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

The wording is awkward. See my previous comment on this topic.

}

var err error
param, ok := asLocallySupportedParameter(args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

Moving the enclosing function to the set command will require exporting this function.

@girishramnani
Copy link
Contributor

@dev-gaur any update on the comments made by chris?

Copy link
Contributor

@metacosm metacosm left a comment

Choose a reason for hiding this comment

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

LGTM

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label Jul 4, 2020
@girishramnani
Copy link
Contributor

/retest

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

3 similar comments
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit c8bff4b into redhat-developer:master Jul 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"odo config set" doesn't validate port values and thus "odo describe" panics
9 participants