From 947ad036c6cba05a8a0b601d223bda2cc4996752 Mon Sep 17 00:00:00 2001 From: Starnop Date: Wed, 1 Aug 2018 17:51:11 +0800 Subject: [PATCH] test: add test cases about CRI image Signed-off-by: Starnop --- cri/v1alpha1/cri_utils_test.go | 292 ++++++++------------------------- cri/v1alpha2/cri_utils_test.go | 86 +++++++++- 2 files changed, 154 insertions(+), 224 deletions(-) diff --git a/cri/v1alpha1/cri_utils_test.go b/cri/v1alpha1/cri_utils_test.go index 2a72fb035b..881d47719c 100644 --- a/cri/v1alpha1/cri_utils_test.go +++ b/cri/v1alpha1/cri_utils_test.go @@ -5,6 +5,7 @@ import ( "reflect" "strings" "testing" + "strconv" apitypes "github.com/alibaba/pouch/apis/types" "github.com/alibaba/pouch/daemon/mgr" @@ -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 } @@ -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 { @@ -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 } @@ -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) { @@ -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) { diff --git a/cri/v1alpha2/cri_utils_test.go b/cri/v1alpha2/cri_utils_test.go index 1a6051d0c1..08ee5f4305 100644 --- a/cri/v1alpha2/cri_utils_test.go +++ b/cri/v1alpha2/cri_utils_test.go @@ -3,6 +3,7 @@ package v1alpha2 import ( "fmt" "reflect" + "strconv" "strings" "testing" @@ -776,6 +777,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 } @@ -785,7 +788,39 @@ func Test_imageToCriImage(t *testing.T) { want *runtime.Image wantErr bool }{ - // TODO: Add test cases. + { + name: "normal test", + args: args{ + image: &apitypes.ImageInfo{ + ID: "image-id", + RepoTags: repoDigests, + RepoDigests: repoDigests, + Size: 1024, + Config: &apitypes.ContainerConfig{ + User: "foo", + Volumes: map[string]interface{}{"foo": "foo"}, + }, + }, + }, + want: &runtime.Image{ + Id: "image-id", + RepoTags: repoDigests, + RepoDigests: repoDigests, + Size_: uint64(1024), + Uid: &runtime.Int64Value{}, + Username: "foo", + Volumes: parseVolumesFromPouch(map[string]interface{}{"foo": "foo"}), + }, + wantErr: nil, + }, + { + name: "nil test", + args: args{ + image: &apitypes.ImageInfo{}, + }, + want: &runtime.Image{}, + wantErr: nil, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -832,6 +867,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 } @@ -841,7 +878,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) { @@ -865,7 +925,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) {