Skip to content

Commit

Permalink
test: add test cases about CRI image
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Aug 1, 2018
1 parent 012886d commit 947ad03
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 224 deletions.
292 changes: 71 additions & 221 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strings"
"testing"
"strconv"

apitypes "github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/mgr"
Expand Down Expand Up @@ -807,6 +808,8 @@ func Test_toCriContainer(t *testing.T) {

// Image related unit tests.
func Test_imageToCriImage(t *testing.T) {
repoDigests := []string{"lastest", "dev", "v1.0"}

type args struct {
image *apitypes.ImageInfo
}
Expand All @@ -817,234 +820,36 @@ func Test_imageToCriImage(t *testing.T) {
wantErr bool
}{
{
"case1",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "xiaoming",
},
ID: "1",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 1024,
},
},
&runtime.Image{
Id: "1",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 1024,
Uid: &runtime.Int64Value{Value: 0},
Username: "xiaoming",
},
false,
},
{
"case2",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456",
},
ID: "1",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 1024,
},
},
&runtime.Image{
Id: "1",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 1024,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case3",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
},
ID: "1",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 1024,
},
},
&runtime.Image{
Id: "1",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 1024,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case4",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
},
ID: "1",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
name: "normal test",
args: args{
image: &apitypes.ImageInfo{
ID: "image-id",
RepoTags: repoDigests,
RepoDigests: repoDigests,
Size: 1024,
},
},
&runtime.Image{
Id: "1",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 1024,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case5",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
},
ID: "1",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 2048,
},
},
&runtime.Image{
Id: "1",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 2048,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case6",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
},
ID: "abc",
RepoDigests: []string{"asdlkfej", "vwoeifo"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 2048,
},
},
&runtime.Image{
Id: "abc",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"asdlkfej", "vwoeifo"},
Size_: 2048,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case7",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
User: "foo",
Volumes: map[string]interface{}{"foo": "foo"},
},
ID: "abc",
RepoDigests: []string{"1238", "4820940"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 2048,
},
},
&runtime.Image{
Id: "abc",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"1238", "4820940"},
Size_: 2048,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
want: &runtime.Image{
Id: "image-id",
RepoTags: repoDigests,
RepoDigests: repoDigests,
Size_: uint64(1024),
Uid: &runtime.Int64Value{},
Username: "foo",
},
false,
},
{
"case8",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "123456:dev",
},
ID: "abc",
RepoDigests: []string{"1238", "4820940"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: -2048,
},
},
&runtime.Image{
Id: "abc",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"1238", "4820940"},
Size_: 18446744073709549568,
Uid: &runtime.Int64Value{Value: int64(123456)},
Username: "",
},
false,
},
{
"case9",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "-123456:dev",
},
ID: "abc",
RepoDigests: []string{"1238", "4820940"},
RepoTags: []string{"sldkfeio", "civlme"},
Size: 2048,
},
},
&runtime.Image{
Id: "abc",
RepoTags: []string{"sldkfeio", "civlme"},
RepoDigests: []string{"1238", "4820940"},
Size_: 2048,
Uid: &runtime.Int64Value{Value: int64(-123456)},
Username: "",
},
false,
wantErr: nil,
},
{
"case10",
args{
&apitypes.ImageInfo{
Config: &apitypes.ContainerConfig{
User: "",
},
ID: "abc",
RepoDigests: []string{"1238", "4820940"},
RepoTags: []string{"xcuvk2", "cuvkwej23095489"},
Size: 2048,
},
},
&runtime.Image{
Id: "abc",
RepoTags: []string{"xcuvk2", "cuvkwej23095489"},
RepoDigests: []string{"1238", "4820940"},
Size_: 2048,
Uid: &runtime.Int64Value{Value: int64(0)},
Username: "",
name: "nil test",
args: args{
image: &apitypes.ImageInfo{},
},
false,
want: &runtime.Image{},
wantErr: nil,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -1092,6 +897,8 @@ func TestCriManager_ensureSandboxImageExists(t *testing.T) {
}

func Test_getUserFromImageUser(t *testing.T) {
imageUserInt := "1"
uid, _ := strconv.ParseInt(imageUserInt, 10, 64)
type args struct {
imageUser string
}
Expand All @@ -1101,7 +908,30 @@ func Test_getUserFromImageUser(t *testing.T) {
want *int64
want1 string
}{
// TODO: Add test cases.
{
name: "Empty Test",
args: args{
imageUser: "",
},
want: nil,
want1: "",
},
{
name: "ParseInt Success Test",
args: args{
imageUser: imageUserInt,
},
want: &uid,
want1: "",
},
{
name: "ParseInt Fail Test",
args: args{
imageUser: "foo",
},
want: nil,
want1: "foo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -1125,7 +955,27 @@ func Test_parseUserFromImageUser(t *testing.T) {
args args
want string
}{
// TODO: Add test cases.
{
name: "Empty Test",
args: args{
id: "",
},
want: "",
},
{
name: "user:group Test",
args: args{
id: "user:group",
},
want: "user",
},
{
name: "No Group Test",
args: args{
id: "user",
},
want: "user",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 947ad03

Please sign in to comment.