-
Notifications
You must be signed in to change notification settings - Fork 558
Some refactoring of validation #3093
Some refactoring of validation #3093
Conversation
589fef8
to
1d6e48b
Compare
@@ -166,17 +167,3 @@ func validateVNET(a *Properties) error { | |||
|
|||
return nil | |||
} | |||
|
|||
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate code --> moved in common place
@@ -301,17 +301,3 @@ func isCustomVNET(a []*AgentPoolProfile) bool { | |||
|
|||
return false | |||
} | |||
|
|||
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID | |||
func GetVNETSubnetIDComponents(vnetSubnetID string) (string, string, string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate code --> moved in common place
@@ -156,17 +157,3 @@ func validateVNET(a *Properties) error { | |||
|
|||
return nil | |||
} | |||
|
|||
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID | |||
func GetVNETSubnetIDComponents(vnetSubnetID string) (string, string, string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate code --> moved in common place
@@ -37,3 +40,17 @@ func IP4BroadcastAddress(n *net.IPNet) net.IP { | |||
} | |||
return last | |||
} | |||
|
|||
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID | |||
func GetVNETSubnetIDComponents(vnetSubnetID string) (string, string, string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved duplicated function here
@@ -48,3 +56,29 @@ func Test_IP4BroadcastAddress(t *testing.T) { | |||
} | |||
} | |||
} | |||
|
|||
func Test_GetVNETSubnetIDComponents(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new unit test 🎉
@@ -275,17 +275,3 @@ func validateVNET(a *Properties) error { | |||
} | |||
return nil | |||
} | |||
|
|||
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID | |||
func GetVNETSubnetIDComponents(vnetSubnetID string) (string, string, string, string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate code --> moved in common place
660e6c5
to
889d3d3
Compare
pkg/api/common/net.go
Outdated
} | ||
submatches := re.FindStringSubmatch(vnetSubnetID) | ||
if len(submatches) != 4 { | ||
return "", "", "", "", fmt.Errorf("unable to find 4 submatches") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed the return here because return "", "", "", "", err
would return a nil error
pkg/api/common/net.go
Outdated
} | ||
submatches := re.FindStringSubmatch(vnetSubnetID) | ||
if len(submatches) != 5 { | ||
return "", "", "", "", fmt.Errorf("unable to match regexp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed from return "", "", "", "", err
because it was returning a nil error and if len(submatches) != 5
from if len(submatches) != 4
because we expect 5 matches, not 4 (the first one is the entire string).
This was a bug in all APIs except agentpoolonly v20180331 (the function was fixed in that one but not the others --> yay duplicate code) : validateVNET would never fail on invalid vnetID because it would find 5 submatches != 4 and exit with nil error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: what is the fifth regex substring match that we're not doing anything with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, you answered the question above.
Unless that regex package is weird, we probably have a sub-optimal expression, as we should be able to formulate a regex result that includes only that matched patterns and not a throwaway match of the entire string as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested on https://regex-golang.appspot.com/assets/html/index.html and got 4 submatches... I wonder if it's the single quotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm so this is because that's how the go regexp FindStringSubmatch
function works https://golang.org/pkg/regexp/#Regexp.FindStringSubmatch: FindSubmatch returns a slice of slices holding the text of the leftmost match of the regular expression in b and the matches, if any, of its subexpressions, as defined by the 'Submatch' descriptions in the package comment.
submatches[0]
is the "leftmost match of the regular expression"
@@ -94,23 +94,54 @@ func init() { | |||
labelKeyRegex = regexp.MustCompile(labelKeyFormat) | |||
} | |||
|
|||
func isValidEtcdVersion(etcdVersion string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved function to bottom of file
// "" is a valid etcdVersion that maps to DefaultEtcdVersion | ||
if etcdVersion == "" { | ||
return nil | ||
// Validate implements APIObject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved up
pkg/api/vlabs/validate.go
Outdated
if version == "" { | ||
return fmt.Errorf("the following user supplied OrchestratorProfile configuration is not supported: OrchestratorType: %s, OrchestratorRelease: %s, OrchestratorVersion: %s. Please check supported Release or Version for this build of acs-engine", o.OrchestratorType, o.OrchestratorRelease, o.OrchestratorVersion) | ||
return fmt.Errorf("the following OrchestratorProfile configuration is not supported: OrchestratorType: \"%s\", OrchestratorRelease: \"%s\", OrchestratorVersion: \"%s\". Please use one of the following versions: %v", o.OrchestratorType, o.OrchestratorRelease, o.OrchestratorVersion, common.GetAllSupportedKubernetesVersions()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return a list of supported versions to the user
@@ -29,103 +29,127 @@ const ( | |||
|
|||
func Test_OrchestratorProfile_Validate(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this unit test file are meant to make the current unit tests work with the refactoring of the validate.go file. A PR for improving and increasing unit test coverage of validation functions will follow.
/test all |
Codecov Report
@@ Coverage Diff @@
## master #3093 +/- ##
==========================================
+ Coverage 52.36% 52.61% +0.25%
==========================================
Files 103 103
Lines 15426 15413 -13
==========================================
+ Hits 8078 8110 +32
+ Misses 6621 6566 -55
- Partials 727 737 +10 |
unit test will pass once #3165 is merged |
a130888
to
2247a26
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the openshift translation files. Otherwise looks great!
pkg/api/common/net.go
Outdated
} | ||
submatches := re.FindStringSubmatch(vnetSubnetID) | ||
if len(submatches) != 5 { | ||
return "", "", "", "", fmt.Errorf("unable to match regexp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find!
pkg/api/common/net.go
Outdated
} | ||
submatches := re.FindStringSubmatch(vnetSubnetID) | ||
if len(submatches) != 5 { | ||
return "", "", "", "", fmt.Errorf("unable to match regexp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: what is the fifth regex substring match that we're not doing anything with?
pkg/api/common/net.go
Outdated
} | ||
submatches := re.FindStringSubmatch(vnetSubnetID) | ||
if len(submatches) != 5 { | ||
return "", "", "", "", fmt.Errorf("unable to match regexp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, you answered the question above.
Unless that regex package is weird, we probably have a sub-optimal expression, as we should be able to formulate a regex result that includes only that matched patterns and not a throwaway match of the entire string as well.
/approve |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: CecileRobertMichon, jackfrancis 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 |
What this PR does / why we need it: This PR is the first step towards increasing unit test coverage in validate.go. It separates validations in validate.go in functions that can be unit tested more easily. Functionality should be untouched as the PR mostly aims to move things around (as opposed to changing the validation itself). This refactor also makes it easier to identify gaps in validation. Those will be addressed in multiple follow up PRs.
Which issue this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close that issue when PR gets merged): fixes #Special notes for your reviewer:
If applicable:
Release note: