diff --git a/pkg/crc/machine/bundle/metadata.go b/pkg/crc/machine/bundle/metadata.go index a10c68a8a8..58a3dfed24 100644 --- a/pkg/crc/machine/bundle/metadata.go +++ b/pkg/crc/machine/bundle/metadata.go @@ -238,17 +238,20 @@ func GetCustomBundleName(bundleFilename string) string { return fmt.Sprintf("%s_%d%s", baseName, time.Now().Unix(), bundleExtension) } -func GetBundleNameFromURI(bundleURI string) string { +func GetBundleNameFromURI(bundleURI string) (string, error) { // the URI is expected to have been validated by validation.ValidateBundlePath first switch { case strings.HasPrefix(bundleURI, "docker://"): imageAndTag := strings.Split(path.Base(bundleURI), ":") - return constants.BundleForPreset(image.GetPresetName(imageAndTag[0]), imageAndTag[1]) + if len(imageAndTag) < 2 { + return "", fmt.Errorf("No tag found in bundle URI") + } + return constants.BundleForPreset(image.GetPresetName(imageAndTag[0]), imageAndTag[1]), nil case strings.HasPrefix(bundleURI, "http://"), strings.HasPrefix(bundleURI, "https://"): - return path.Base(bundleURI) + return path.Base(bundleURI), nil default: // local path - return filepath.Base(bundleURI) + return filepath.Base(bundleURI), nil } } diff --git a/pkg/crc/machine/bundle/metadata_test.go b/pkg/crc/machine/bundle/metadata_test.go index f2218103c6..e0f5a6e2f4 100644 --- a/pkg/crc/machine/bundle/metadata_test.go +++ b/pkg/crc/machine/bundle/metadata_test.go @@ -55,7 +55,7 @@ func jsonForBundleWithVersion(version, name string) string { { "name": "crc.qcow2", "format": "qcow2", - "size": "9", + "size": "9", "sha256sum": "245a0e5acd4f09000a9a5f37d731082ed1cf3fdcad1b5320cbe9b153c9fd82a4" } ], @@ -289,3 +289,27 @@ func TestGetBundleInfoFromNameInvalid(t *testing.T) { _, err = GetBundleInfoFromName("crc_nanoshift_libvirt_4.16.7_amd64_232.crcbundle") assert.Error(t, err) } + +func TestGetBundleNameFromURI(t *testing.T) { + // URI with no tag + bundleName, err := GetBundleNameFromURI("docker://quay.io/crcont/openshift-bundle") + assert.Equal(t, "", bundleName) + assert.Error(t, err) + + expectedBundleName := "crc_libvirt_4.17.3_amd64.crcbundle" + + // URI with tag + bundleName, err = GetBundleNameFromURI("docker://quay.io/crcont/openshift-bundle:4.17.3") + assert.Nil(t, err) + assert.Equal(t, expectedBundleName, bundleName) + + // HTTPs + bundleName, err = GetBundleNameFromURI("https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/bundles/openshift/4.17.3/crc_libvirt_4.17.3_amd64.crcbundle") + assert.Nil(t, err) + assert.Equal(t, bundleName, expectedBundleName, bundleName) + + // Local file + bundleName, err = GetBundleNameFromURI("/home/user/Downloads/crc_libvirt_4.17.3_amd64.crcbundle") + assert.Nil(t, err) + assert.Equal(t, bundleName, expectedBundleName, bundleName) +} diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go index cad83b8aea..b4463ab686 100644 --- a/pkg/crc/machine/start.go +++ b/pkg/crc/machine/start.go @@ -278,7 +278,11 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig) return nil, errors.Wrap(err, "Cannot determine if VM exists") } - bundleName := bundle.GetBundleNameWithoutExtension(bundle.GetBundleNameFromURI(startConfig.BundlePath)) + bundleNameFromURI, err := bundle.GetBundleNameFromURI(startConfig.BundlePath) + if err != nil { + return nil, errors.Wrap(err, "Error getting bundle name") + } + bundleName := bundle.GetBundleNameWithoutExtension(bundleNameFromURI) crcBundleMetadata, err := getCrcBundleInfo(startConfig.Preset, bundleName, startConfig.BundlePath, startConfig.EnableBundleQuayFallback) if err != nil { return nil, errors.Wrap(err, "Error getting bundle metadata") diff --git a/pkg/crc/preflight/preflight_checks_common.go b/pkg/crc/preflight/preflight_checks_common.go index 6d7e62d63b..1e7c7dcf17 100644 --- a/pkg/crc/preflight/preflight_checks_common.go +++ b/pkg/crc/preflight/preflight_checks_common.go @@ -86,7 +86,11 @@ var genericCleanupChecks = []Check{ func checkBundleExtracted(bundlePath string) func() error { return func() error { logging.Infof("Checking if %s exists", bundlePath) - bundleName := bundle.GetBundleNameFromURI(bundlePath) + bundleName, err := bundle.GetBundleNameFromURI(bundlePath) + if err != nil { + logging.Debugf("error getting bundle name from path %s: %v", bundlePath, err) + return err + } if _, err := bundle.Get(bundleName); err != nil { logging.Debugf("error getting bundle info for %s: %v", bundleName, err) return err diff --git a/pkg/crc/validation/validation.go b/pkg/crc/validation/validation.go index 3e8ee9496b..c330c4b4e3 100644 --- a/pkg/crc/validation/validation.go +++ b/pkg/crc/validation/validation.go @@ -83,13 +83,19 @@ func ValidateBundlePath(bundlePath string, preset crcpreset.Preset) error { } } - userProvidedBundle := bundle.GetBundleNameFromURI(bundlePath) + userProvidedBundle, err := bundle.GetBundleNameFromURI(bundlePath) + if err != nil { + return err + } bundleMismatchWarning(userProvidedBundle, preset) return nil } func ValidateBundle(bundlePath string, preset crcpreset.Preset) error { - bundleName := bundle.GetBundleNameFromURI(bundlePath) + bundleName, err := bundle.GetBundleNameFromURI(bundlePath) + if err != nil { + return err + } bundleMetadata, err := bundle.Get(bundleName) if err != nil { if bundlePath == constants.GetDefaultBundlePath(preset) {