From 76ac53feaa09f309428e9f0ddbfc8adaa4aa83a8 Mon Sep 17 00:00:00 2001 From: Joseph Gu Date: Fri, 24 Dec 2021 17:19:21 +0800 Subject: [PATCH] fix: potential error in combine (#525) add ut for combine and split --- engine/virt/helper.go | 6 ++++++ engine/virt/helper_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 engine/virt/helper_test.go diff --git a/engine/virt/helper.go b/engine/virt/helper.go index e93cc6a99..617a94e14 100644 --- a/engine/virt/helper.go +++ b/engine/virt/helper.go @@ -61,5 +61,11 @@ func splitUserImage(combined string) (user, imageName string, err error) { } func combineUserImage(user, imageName string) string { + if len(imageName) < 1 { + return "" + } + if len(user) < 1 { + return imageName + } return fmt.Sprintf("%s%s%s", user, sep, imageName) } diff --git a/engine/virt/helper_test.go b/engine/virt/helper_test.go new file mode 100644 index 000000000..727b27c1f --- /dev/null +++ b/engine/virt/helper_test.go @@ -0,0 +1,40 @@ +package virt + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCombineUserImage(t *testing.T) { + user := "user" + image := "image" + + combine := combineUserImage(user, image) + require.Equal(t, "user@image", combine) + u, i, err := splitUserImage(combine) + require.NoError(t, err) + require.Equal(t, user, u) + require.Equal(t, image, i) + + combine = combineUserImage("", image) + require.Equal(t, image, combine) + u, i, err = splitUserImage(combine) + require.NoError(t, err) + require.Equal(t, "", u) + require.Equal(t, image, i) + + combine = combineUserImage(user, "") + require.Equal(t, "", combine) + u, i, err = splitUserImage(combine) + require.Error(t, err) + + combine = combineUserImage("", "") + require.Equal(t, "", combine) + + u, i, err = splitUserImage("@") + require.Error(t, err) + + u, i, err = splitUserImage("hello@") + require.Error(t, err) +}