Skip to content

Commit

Permalink
fix: potential error in combine (#525)
Browse files Browse the repository at this point in the history
add ut for combine and split
  • Loading branch information
Aceralon authored Dec 24, 2021
1 parent f1e3e4c commit 76ac53f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions engine/virt/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
40 changes: 40 additions & 0 deletions engine/virt/helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 76ac53f

Please sign in to comment.