Skip to content

Commit

Permalink
bundle validation: expand regex
Browse files Browse the repository at this point in the history
to allow more characters in bundle version field

Issue #4409
  • Loading branch information
redbeam authored and praveenkumar committed Oct 24, 2024
1 parent 64042d8 commit 9b7772d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
18 changes: 15 additions & 3 deletions pkg/crc/machine/bundle/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,21 @@ func GetBundleNameFromURI(bundleURI string) string {
func GetBundleInfoFromName(bundleName string) (*FilenameInfo, error) {
var filenameInfo FilenameInfo

// crc_preset_driver_version_arch_customSuffix.crcbundle
bundleNameRegex := regexp.MustCompile(`crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_([[:alnum:].-]+)_([[:alnum:]]+)(?:(?:_)([0-9]+))?\.crcbundle`)
filenameParts := bundleNameRegex.FindStringSubmatch(bundleName)
/*
crc_preset_driver_version_arch_customSuffix.crcbundle
crc : Matches the fixed crc part
(?:(?:_)([[:alpha:]]+))? : Matches the preset part (optional)
([[:alpha:]]+) : Matches the next mandatory alphabetic part (e.g., libvirt)
(%s = semverRegex) : Matches the version in SemVer format (e.g., 4.16.7 or 4.16.7-ec.2)
([[:alnum:]]+) : Matches the architecture or platform part (e.g. amd64)
(?:(?:_)([0-9]+))? : Optionally matches a trailing number after an underscore (e.g. 2345).
\.crcbundle : Matches the file extension .crcbundle
*/
semverRegex := "(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-(?:(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?:[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?"
bundleRegex := `crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_(%s)_([[:alnum:]]+)(?:(?:_)([0-9]+))?\.crcbundle`
compiledRegex := regexp.MustCompile(fmt.Sprintf(bundleRegex, semverRegex))
filenameParts := compiledRegex.FindStringSubmatch(bundleName)

if filenameParts == nil {
return &filenameInfo, fmt.Errorf("bundle filename is in unrecognized format")
Expand Down
29 changes: 13 additions & 16 deletions pkg/crc/machine/bundle/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,32 +224,25 @@ func TestGetBundleInfoFromNameValid(t *testing.T) {
{"crc_hyperv_4.18.0_arm64.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "arm64", ""},
{"crc_libvirt_4.18.0-ec.2_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.18.0-ec.2", "amd64", ""},

{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
{"crc_microshift_hyperv_4.18_x86.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", ""},
{"crc_microshift_hyperv_4.18_x86_1233.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", "1233"},
{"crc_hyperv_4.18_x86_4566.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", "4566"},
{"crc_ABCdrv_4.18.0_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", "4566"},
{"crc_ABCdrv_4.18.1.2_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", "4566"},
{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
{"crc_microshift_hyperv_4.18.0_x86.crcbundle", preset.Microshift.String(), "hyperv", "4.18.0", "x86", ""},
{"crc_microshift_hyperv_4.18.0_x86_1233.crcbundle", preset.Microshift.String(), "hyperv", "4.18.0", "x86", "1233"},
{"crc_hyperv_4.18.0_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "x86", ""},
{"crc_hyperv_4.18.0_x86_123.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "x86", "123"},
{"crc_ABCdrv_4.18.0_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", ""},
{"crc_ABCdrv_4.18.1.2_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", ""},
{"crc_hyperv_4.18_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "64bit", ""},
{"crc_hyperv_4.1_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.1", "64bit", ""},
{"crc_ABCdrv_4.18.0_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", "4566"},
{"crc_hyperv_4.18.1_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.1", "64bit", ""},
{"crc_hyperv_4.1.1-rc.0_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.1.1-rc.0", "64bit", ""},

{"crc_openshift_libvirt_4.16.7_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", ""},
{"crc_openshift_libvirt_4.16.7_amd64_1.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", "1"},
{"crc_openshift_libvirt_00.00.00.00_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", ""},
{"crc_openshift_libvirt_00.00.00.00_amd64_100.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", "100"},
{"crc_libvirt_4.16.7_intel.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", ""},
{"crc_libvirt_4.16.7_intel_23.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", "23"},
{"crc_libvirt_4.16.7_64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", ""},
{"crc_libvirt_4.16.7_64_132.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", "132"},
{"crc_microshift_libvirt_4.16.7_64.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", ""},
{"crc_microshift_libvirt_4.16.7_64_123.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", "123"},
{"crc_libvirt_4_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", ""},
{"crc_libvirt_4_amd64_0123.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", "0123"},
{"crc_okd_libvirt_4_amd64.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", ""},
{"crc_okd_libvirt_4_amd64_0123.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", "0123"},
{"crc_okd_libvirt_4.0.0_amd64.crcbundle", preset.OKD.String(), "libvirt", "4.0.0", "amd64", ""},
{"crc_okd_libvirt_4.0.0_amd64_0123.crcbundle", preset.OKD.String(), "libvirt", "4.0.0", "amd64", "0123"},
}

for _, parts := range valid {
Expand All @@ -268,6 +261,10 @@ func TestGetBundleInfoFromNameInvalid(t *testing.T) {
_, err := GetBundleInfoFromName("crc_libvirt_amd64.crcbundle")
assert.Error(t, err)

// wrong version format
_, err = GetBundleInfoFromName("crc_hyperv_4.18_x86.crcbundle")
assert.Error(t, err)

// missing crc prefix
_, err = GetBundleInfoFromName("libvirt_4.16.0_amd64.crcbundle")
assert.Error(t, err)
Expand Down

0 comments on commit 9b7772d

Please sign in to comment.