Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Some refactoring of validation #3093

Merged
merged 21 commits into from
Jun 8, 2018

Conversation

CecileRobertMichon
Copy link
Contributor

@CecileRobertMichon CecileRobertMichon commented May 25, 2018

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:

  • documentation
  • unit tests
  • tested backward compatibility (ie. deploy with previous version, upgrade with this branch)

Release note:


@@ -166,17 +167,3 @@ func validateVNET(a *Properties) error {

return nil
}

// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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

}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 4 {
return "", "", "", "", fmt.Errorf("unable to find 4 submatches")
Copy link
Contributor Author

@CecileRobertMichon CecileRobertMichon Jun 2, 2018

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

}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 5 {
return "", "", "", "", fmt.Errorf("unable to match regexp")
Copy link
Contributor Author

@CecileRobertMichon CecileRobertMichon Jun 2, 2018

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.

Copy link
Member

Choose a reason for hiding this comment

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

Good find!

Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

@CecileRobertMichon CecileRobertMichon Jun 8, 2018

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

Copy link
Contributor Author

@CecileRobertMichon CecileRobertMichon Jun 8, 2018

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 {
Copy link
Contributor Author

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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved up

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())
Copy link
Contributor Author

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) {
Copy link
Contributor Author

@CecileRobertMichon CecileRobertMichon Jun 4, 2018

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.

@CecileRobertMichon
Copy link
Contributor Author

/test all

@codecov
Copy link

codecov bot commented Jun 4, 2018

Codecov Report

Merging #3093 into master will increase coverage by 0.25%.
The diff coverage is 52.03%.

@@            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

@CecileRobertMichon
Copy link
Contributor Author

unit test will pass once #3165 is merged

Copy link
Member

@jackfrancis jackfrancis left a 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!

}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 5 {
return "", "", "", "", fmt.Errorf("unable to match regexp")
Copy link
Member

Choose a reason for hiding this comment

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

Good find!

}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 5 {
return "", "", "", "", fmt.Errorf("unable to match regexp")
Copy link
Member

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?

}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 5 {
return "", "", "", "", fmt.Errorf("unable to match regexp")
Copy link
Member

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.

@jackfrancis
Copy link
Member

/approve
/lgtm

@acs-bot acs-bot removed the lgtm label Jun 8, 2018
@jackfrancis
Copy link
Member

/approve
/lgtm

@acs-bot acs-bot added the lgtm label Jun 8, 2018
@acs-bot
Copy link

acs-bot commented Jun 8, 2018

[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:
  • OWNERS [CecileRobertMichon,jackfrancis]

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

@CecileRobertMichon CecileRobertMichon merged commit 132003a into Azure:master Jun 8, 2018
@ghost ghost removed the in progress label Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants