Skip to content

Commit 9b7772d

Browse files
redbeampraveenkumar
authored andcommitted
bundle validation: expand regex
to allow more characters in bundle version field Issue #4409
1 parent 64042d8 commit 9b7772d

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

pkg/crc/machine/bundle/metadata.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,21 @@ func GetBundleNameFromURI(bundleURI string) string {
256256
func GetBundleInfoFromName(bundleName string) (*FilenameInfo, error) {
257257
var filenameInfo FilenameInfo
258258

259-
// crc_preset_driver_version_arch_customSuffix.crcbundle
260-
bundleNameRegex := regexp.MustCompile(`crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_([[:alnum:].-]+)_([[:alnum:]]+)(?:(?:_)([0-9]+))?\.crcbundle`)
261-
filenameParts := bundleNameRegex.FindStringSubmatch(bundleName)
259+
/*
260+
crc_preset_driver_version_arch_customSuffix.crcbundle
261+
262+
crc : Matches the fixed crc part
263+
(?:(?:_)([[:alpha:]]+))? : Matches the preset part (optional)
264+
([[:alpha:]]+) : Matches the next mandatory alphabetic part (e.g., libvirt)
265+
(%s = semverRegex) : Matches the version in SemVer format (e.g., 4.16.7 or 4.16.7-ec.2)
266+
([[:alnum:]]+) : Matches the architecture or platform part (e.g. amd64)
267+
(?:(?:_)([0-9]+))? : Optionally matches a trailing number after an underscore (e.g. 2345).
268+
\.crcbundle : Matches the file extension .crcbundle
269+
*/
270+
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-]+)*))?"
271+
bundleRegex := `crc(?:(?:_)([[:alpha:]]+))?_([[:alpha:]]+)_(%s)_([[:alnum:]]+)(?:(?:_)([0-9]+))?\.crcbundle`
272+
compiledRegex := regexp.MustCompile(fmt.Sprintf(bundleRegex, semverRegex))
273+
filenameParts := compiledRegex.FindStringSubmatch(bundleName)
262274

263275
if filenameParts == nil {
264276
return &filenameInfo, fmt.Errorf("bundle filename is in unrecognized format")

pkg/crc/machine/bundle/metadata_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,25 @@ func TestGetBundleInfoFromNameValid(t *testing.T) {
224224
{"crc_hyperv_4.18.0_arm64.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "arm64", ""},
225225
{"crc_libvirt_4.18.0-ec.2_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.18.0-ec.2", "amd64", ""},
226226

227-
{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
228-
{"crc_microshift_hyperv_4.18_x86.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", ""},
229-
{"crc_microshift_hyperv_4.18_x86_1233.crcbundle", preset.Microshift.String(), "hyperv", "4.18", "x86", "1233"},
230-
{"crc_hyperv_4.18_x86_4566.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", "4566"},
231-
{"crc_ABCdrv_4.18.0_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", "4566"},
232-
{"crc_ABCdrv_4.18.1.2_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", "4566"},
233-
{"crc_hyperv_4.18_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "x86", ""},
227+
{"crc_microshift_hyperv_4.18.0_x86.crcbundle", preset.Microshift.String(), "hyperv", "4.18.0", "x86", ""},
228+
{"crc_microshift_hyperv_4.18.0_x86_1233.crcbundle", preset.Microshift.String(), "hyperv", "4.18.0", "x86", "1233"},
229+
{"crc_hyperv_4.18.0_x86.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "x86", ""},
230+
{"crc_hyperv_4.18.0_x86_123.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.0", "x86", "123"},
234231
{"crc_ABCdrv_4.18.0_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", ""},
235-
{"crc_ABCdrv_4.18.1.2_x86.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.1.2", "x86", ""},
236-
{"crc_hyperv_4.18_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.18", "64bit", ""},
237-
{"crc_hyperv_4.1_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.1", "64bit", ""},
232+
{"crc_ABCdrv_4.18.0_x86_4566.crcbundle", preset.OpenShift.String(), "ABCdrv", "4.18.0", "x86", "4566"},
233+
{"crc_hyperv_4.18.1_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.18.1", "64bit", ""},
234+
{"crc_hyperv_4.1.1-rc.0_64bit.crcbundle", preset.OpenShift.String(), "hyperv", "4.1.1-rc.0", "64bit", ""},
238235

239236
{"crc_openshift_libvirt_4.16.7_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", ""},
240237
{"crc_openshift_libvirt_4.16.7_amd64_1.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "amd64", "1"},
241-
{"crc_openshift_libvirt_00.00.00.00_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", ""},
242-
{"crc_openshift_libvirt_00.00.00.00_amd64_100.crcbundle", preset.OpenShift.String(), "libvirt", "00.00.00.00", "amd64", "100"},
243238
{"crc_libvirt_4.16.7_intel.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", ""},
244239
{"crc_libvirt_4.16.7_intel_23.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "intel", "23"},
245240
{"crc_libvirt_4.16.7_64.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", ""},
246241
{"crc_libvirt_4.16.7_64_132.crcbundle", preset.OpenShift.String(), "libvirt", "4.16.7", "64", "132"},
247242
{"crc_microshift_libvirt_4.16.7_64.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", ""},
248243
{"crc_microshift_libvirt_4.16.7_64_123.crcbundle", preset.Microshift.String(), "libvirt", "4.16.7", "64", "123"},
249-
{"crc_libvirt_4_amd64.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", ""},
250-
{"crc_libvirt_4_amd64_0123.crcbundle", preset.OpenShift.String(), "libvirt", "4", "amd64", "0123"},
251-
{"crc_okd_libvirt_4_amd64.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", ""},
252-
{"crc_okd_libvirt_4_amd64_0123.crcbundle", preset.OKD.String(), "libvirt", "4", "amd64", "0123"},
244+
{"crc_okd_libvirt_4.0.0_amd64.crcbundle", preset.OKD.String(), "libvirt", "4.0.0", "amd64", ""},
245+
{"crc_okd_libvirt_4.0.0_amd64_0123.crcbundle", preset.OKD.String(), "libvirt", "4.0.0", "amd64", "0123"},
253246
}
254247

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

264+
// wrong version format
265+
_, err = GetBundleInfoFromName("crc_hyperv_4.18_x86.crcbundle")
266+
assert.Error(t, err)
267+
271268
// missing crc prefix
272269
_, err = GetBundleInfoFromName("libvirt_4.16.0_amd64.crcbundle")
273270
assert.Error(t, err)

0 commit comments

Comments
 (0)