Skip to content

Commit

Permalink
create getProductCode function
Browse files Browse the repository at this point in the history
add tests for TestModifyHorizonClientCode
add more tests for getProductName/getProductCode
  • Loading branch information
Matt Proud committed Dec 5, 2023
1 parent 0b65a48 commit 8aed0ef
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
29 changes: 28 additions & 1 deletion sdk/subproduct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,36 @@ func TestGetSubProductsDetailsInvalidMajorVersion(t *testing.T) {
assert.Empty(t, subProductDetails.Code, "Expected response to be empty")
}

func TestModifyHorizonClientCode(t *testing.T) {
productCode := "cart24fq4_lin_2309.1_tarball"
productCode = modifyHorizonClientCode(productCode)
assert.Equal(t, "cart+tarball", productCode)

productCode = "one_2"
productCode = modifyHorizonClientCode(productCode)
assert.Equal(t, "one", productCode)
}

func TestGetProductName(t *testing.T) {
reEndVersion := regexp.MustCompile(`[0-9]+.*`)
productName := "VMware ESXi 8.0 native ixgben ENS 1.18.2.0 NIC Driver for Intel Ethernet Controllers 82599, x520, x540, x550, and x552 family"
productName := "VMware vSphere Hypervisor (ESXi) 8.0U2"
productName = getProductName(productName, "vmware_vsphere", "", reEndVersion)
assert.Equal(t, "VMware vSphere Hypervisor (ESXi)", productName)

// Ensure drivers are unmodified
productName = "VMware ESXi 8.0 native ixgben ENS 1.18.2.0 NIC Driver for Intel Ethernet Controllers 82599, x520, x540, x550, and x552 family"
productName = getProductName(productName, "vmware_vsphere", "Driver CDs", reEndVersion)
assert.Equal(t, productName, "Driver - native ixgben ENS")
}

func TestGetProductCode(t *testing.T) {
reEndVersion := regexp.MustCompile(`[0-9]+.*`)
productCode := "ESXI80U2"
productCode = getProductCode(productCode, "vmware_vsphere", "", reEndVersion)
assert.Equal(t, "esxi", productCode)

// Ensure drivers are unmodified
productCode = "DT-ESXI80-INTEL-I40EN-2650-1OEM"
productCode = getProductCode(productCode, "vmware_vsphere", "Driver CDs", reEndVersion)
assert.Equal(t, "dt-esxi80-intel-i40en-2650-1oem", productCode)
}
54 changes: 29 additions & 25 deletions sdk/subproducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,11 @@ func (c *Client) GetSubProductsMap(slug string) (subProductMap map[string]SubPro

for _, dlgEdition := range dlgEditionsList {
for _, dlgList := range dlgEdition.DlgList {
productCode := strings.ToLower(dlgList.Code)
// productName := dlgList.Name
// Regex captures numbers and all text after
reEndVersion := regexp.MustCompile(`[0-9]+.*`)
// Regex detects numbers surrounded by - or _
reMidVersion := regexp.MustCompile(`(-|_)([0-9.]+)(-|_)`)


productName := getProductName(dlgList.Name, slug, dlgEdition.Name, reEndVersion)
// Horizon clients don't follow a common pattern for API naming. This block aligns the pattern
if strings.HasPrefix(productCode, "cart") {
productCode = ModifyHorizonClientCode(productCode)

} else {
// Check if product code has text after the version section
if ok := reMidVersion.MatchString(productCode); ok{
// replace version with + to allow for string to be split when searching
productCode = reMidVersion.ReplaceAllString(productCode, "+")
// remove versions prepended versions
reFpStrip := regexp.MustCompile(`(\+fp[0-9])|(\+hf[0-9])`)
productCode = reFpStrip.ReplaceAllString(productCode, "")
} else {
// when product ends with a version, remove all text after the first number
productCode = reEndVersion.ReplaceAllString(productCode, "")
productCode = strings.TrimSuffix(strings.TrimSuffix(productCode, "_"), "-")
}
}
productCode := getProductCode(strings.ToLower(dlgList.Code), slug, dlgEdition.Name, reEndVersion)

// Initalize the struct for a product code for the first time
if _, ok := subProductMap[productCode]; !ok {
Expand All @@ -106,6 +85,31 @@ func (c *Client) GetSubProductsMap(slug string) (subProductMap map[string]SubPro
return
}

func getProductCode(productCode, slug, dlgEditionName string, reEndVersion *regexp.Regexp) (string) {
productCode = strings.ToLower(productCode)
reMidVersion := regexp.MustCompile(`(-|_)([0-9.]+)(-|_)`)
// Horizon clients don't follow a common pattern for API naming. This block aligns the pattern
if strings.HasPrefix(productCode, "cart") {
return modifyHorizonClientCode(productCode)
}
if slug == "vmware_vsphere" && dlgEditionName == "Driver CDs" {
return productCode
}
// Check if product code has text after the version section
if ok := reMidVersion.MatchString(productCode); ok{
// replace version with + to allow for string to be split when searching
productCode = reMidVersion.ReplaceAllString(productCode, "+")
// remove versions prepended versions
reFpStrip := regexp.MustCompile(`(\+fp[0-9])|(\+hf[0-9])`)
productCode = reFpStrip.ReplaceAllString(productCode, "")
} else {
// when product ends with a version, remove all text after the first number
productCode = reEndVersion.ReplaceAllString(productCode, "")
productCode = strings.TrimSuffix(strings.TrimSuffix(productCode, "_"), "-")
}
return productCode
}

func getProductName(productName, slug, dlgEditionName string, reEndVersion *regexp.Regexp) (string) {
// Special case for Horizon due to inconsistent naming
if slug == "vmware_horizon" {
Expand All @@ -124,7 +128,7 @@ func getProductName(productName, slug, dlgEditionName string, reEndVersion *rege

}

func ModifyHorizonClientCode(productCode string) (string) {
func modifyHorizonClientCode(productCode string) (string) {
productCode = strings.Replace(productCode, "-", "_", 1)

// Remove version numbers at the start of the string only
Expand All @@ -133,7 +137,7 @@ func ModifyHorizonClientCode(productCode string) (string) {
if found != "" {
productCode = strings.Replace(productCode, found, "+", 1)
}
// Handle tarball not following pattern. Replace cart+lin_+tarball to cart+tarball
// Handle tarball not following pattern.
if strings.HasSuffix(productCode, "tarball") {
// productCode = strings.Replace(productCode, "lin_+", "", 1)
reHorizonTar := regexp.MustCompile(`lin_([0-9]+.*?)_`)
Expand Down

0 comments on commit 8aed0ef

Please sign in to comment.