Skip to content

Commit f195bae

Browse files
committed
test: add test cases about CRI image
Signed-off-by: Starnop <[email protected]>
1 parent 94baba4 commit f195bae

File tree

2 files changed

+180
-242
lines changed

2 files changed

+180
-242
lines changed

cri/v1alpha1/cri_utils_test.go

Lines changed: 84 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,8 @@ func Test_containerNetns(t *testing.T) {
12421242

12431243
// Image related unit tests.
12441244
func Test_imageToCriImage(t *testing.T) {
1245+
repoDigests := []string{"lastest", "dev", "v1.0"}
1246+
12451247
type args struct {
12461248
image *apitypes.ImageInfo
12471249
}
@@ -1252,234 +1254,36 @@ func Test_imageToCriImage(t *testing.T) {
12521254
wantErr bool
12531255
}{
12541256
{
1255-
"case1",
1256-
args{
1257-
&apitypes.ImageInfo{
1258-
Config: &apitypes.ContainerConfig{
1259-
User: "xiaoming",
1260-
},
1261-
ID: "1",
1262-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1263-
RepoTags: []string{"sldkfeio", "civlme"},
1264-
Size: 1024,
1265-
},
1266-
},
1267-
&runtime.Image{
1268-
Id: "1",
1269-
RepoTags: []string{"sldkfeio", "civlme"},
1270-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1271-
Size_: 1024,
1272-
Uid: &runtime.Int64Value{Value: 0},
1273-
Username: "xiaoming",
1274-
},
1275-
false,
1276-
},
1277-
{
1278-
"case2",
1279-
args{
1280-
&apitypes.ImageInfo{
1281-
Config: &apitypes.ContainerConfig{
1282-
User: "123456",
1283-
},
1284-
ID: "1",
1285-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1286-
RepoTags: []string{"sldkfeio", "civlme"},
1287-
Size: 1024,
1288-
},
1289-
},
1290-
&runtime.Image{
1291-
Id: "1",
1292-
RepoTags: []string{"sldkfeio", "civlme"},
1293-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1294-
Size_: 1024,
1295-
Uid: &runtime.Int64Value{Value: int64(123456)},
1296-
Username: "",
1297-
},
1298-
false,
1299-
},
1300-
{
1301-
"case3",
1302-
args{
1303-
&apitypes.ImageInfo{
1304-
Config: &apitypes.ContainerConfig{
1305-
User: "123456:dev",
1306-
},
1307-
ID: "1",
1308-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1309-
RepoTags: []string{"sldkfeio", "civlme"},
1310-
Size: 1024,
1311-
},
1312-
},
1313-
&runtime.Image{
1314-
Id: "1",
1315-
RepoTags: []string{"sldkfeio", "civlme"},
1316-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1317-
Size_: 1024,
1318-
Uid: &runtime.Int64Value{Value: int64(123456)},
1319-
Username: "",
1320-
},
1321-
false,
1322-
},
1323-
{
1324-
"case4",
1325-
args{
1326-
&apitypes.ImageInfo{
1327-
Config: &apitypes.ContainerConfig{
1328-
User: "123456:dev",
1329-
},
1330-
ID: "1",
1331-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1332-
RepoTags: []string{"sldkfeio", "civlme"},
1257+
name: "normal test",
1258+
args: args{
1259+
image: &apitypes.ImageInfo{
1260+
ID: "image-id",
1261+
RepoTags: repoDigests,
1262+
RepoDigests: repoDigests,
13331263
Size: 1024,
1334-
},
1335-
},
1336-
&runtime.Image{
1337-
Id: "1",
1338-
RepoTags: []string{"sldkfeio", "civlme"},
1339-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1340-
Size_: 1024,
1341-
Uid: &runtime.Int64Value{Value: int64(123456)},
1342-
Username: "",
1343-
},
1344-
false,
1345-
},
1346-
{
1347-
"case5",
1348-
args{
1349-
&apitypes.ImageInfo{
13501264
Config: &apitypes.ContainerConfig{
1351-
User: "123456:dev",
1265+
User: "foo",
1266+
Volumes: map[string]interface{}{"foo": "foo"},
13521267
},
1353-
ID: "1",
1354-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1355-
RepoTags: []string{"sldkfeio", "civlme"},
1356-
Size: 2048,
13571268
},
13581269
},
1359-
&runtime.Image{
1360-
Id: "1",
1361-
RepoTags: []string{"sldkfeio", "civlme"},
1362-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1363-
Size_: 2048,
1364-
Uid: &runtime.Int64Value{Value: int64(123456)},
1365-
Username: "",
1270+
want: &runtime.Image{
1271+
Id: "image-id",
1272+
RepoTags: repoDigests,
1273+
RepoDigests: repoDigests,
1274+
Size_: uint64(1024),
1275+
Uid: &runtime.Int64Value{},
1276+
Username: "foo",
13661277
},
1367-
false,
1368-
},
1369-
{
1370-
"case6",
1371-
args{
1372-
&apitypes.ImageInfo{
1373-
Config: &apitypes.ContainerConfig{
1374-
User: "123456:dev",
1375-
},
1376-
ID: "abc",
1377-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1378-
RepoTags: []string{"sldkfeio", "civlme"},
1379-
Size: 2048,
1380-
},
1381-
},
1382-
&runtime.Image{
1383-
Id: "abc",
1384-
RepoTags: []string{"sldkfeio", "civlme"},
1385-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1386-
Size_: 2048,
1387-
Uid: &runtime.Int64Value{Value: int64(123456)},
1388-
Username: "",
1389-
},
1390-
false,
1391-
},
1392-
{
1393-
"case7",
1394-
args{
1395-
&apitypes.ImageInfo{
1396-
Config: &apitypes.ContainerConfig{
1397-
User: "123456:dev",
1398-
},
1399-
ID: "abc",
1400-
RepoDigests: []string{"1238", "4820940"},
1401-
RepoTags: []string{"sldkfeio", "civlme"},
1402-
Size: 2048,
1403-
},
1404-
},
1405-
&runtime.Image{
1406-
Id: "abc",
1407-
RepoTags: []string{"sldkfeio", "civlme"},
1408-
RepoDigests: []string{"1238", "4820940"},
1409-
Size_: 2048,
1410-
Uid: &runtime.Int64Value{Value: int64(123456)},
1411-
Username: "",
1412-
},
1413-
false,
1414-
},
1415-
{
1416-
"case8",
1417-
args{
1418-
&apitypes.ImageInfo{
1419-
Config: &apitypes.ContainerConfig{
1420-
User: "123456:dev",
1421-
},
1422-
ID: "abc",
1423-
RepoDigests: []string{"1238", "4820940"},
1424-
RepoTags: []string{"sldkfeio", "civlme"},
1425-
Size: -2048,
1426-
},
1427-
},
1428-
&runtime.Image{
1429-
Id: "abc",
1430-
RepoTags: []string{"sldkfeio", "civlme"},
1431-
RepoDigests: []string{"1238", "4820940"},
1432-
Size_: 18446744073709549568,
1433-
Uid: &runtime.Int64Value{Value: int64(123456)},
1434-
Username: "",
1435-
},
1436-
false,
1437-
},
1438-
{
1439-
"case9",
1440-
args{
1441-
&apitypes.ImageInfo{
1442-
Config: &apitypes.ContainerConfig{
1443-
User: "-123456:dev",
1444-
},
1445-
ID: "abc",
1446-
RepoDigests: []string{"1238", "4820940"},
1447-
RepoTags: []string{"sldkfeio", "civlme"},
1448-
Size: 2048,
1449-
},
1450-
},
1451-
&runtime.Image{
1452-
Id: "abc",
1453-
RepoTags: []string{"sldkfeio", "civlme"},
1454-
RepoDigests: []string{"1238", "4820940"},
1455-
Size_: 2048,
1456-
Uid: &runtime.Int64Value{Value: int64(-123456)},
1457-
Username: "",
1458-
},
1459-
false,
1278+
wantErr: false,
14601279
},
14611280
{
1462-
"case10",
1463-
args{
1464-
&apitypes.ImageInfo{
1465-
Config: &apitypes.ContainerConfig{
1466-
User: "",
1467-
},
1468-
ID: "abc",
1469-
RepoDigests: []string{"1238", "4820940"},
1470-
RepoTags: []string{"xcuvk2", "cuvkwej23095489"},
1471-
Size: 2048,
1472-
},
1473-
},
1474-
&runtime.Image{
1475-
Id: "abc",
1476-
RepoTags: []string{"xcuvk2", "cuvkwej23095489"},
1477-
RepoDigests: []string{"1238", "4820940"},
1478-
Size_: 2048,
1479-
Uid: &runtime.Int64Value{Value: int64(0)},
1480-
Username: "",
1281+
name: "nil test",
1282+
args: args{
1283+
image: &apitypes.ImageInfo{},
14811284
},
1482-
false,
1285+
want: &runtime.Image{},
1286+
wantErr: false,
14831287
},
14841288
}
14851289
for _, tt := range tests {
@@ -1527,25 +1331,55 @@ func TestCriManager_ensureSandboxImageExists(t *testing.T) {
15271331
}
15281332

15291333
func Test_getUserFromImageUser(t *testing.T) {
1334+
imageUserInt := "1"
1335+
uid, _ := strconv.ParseInt(imageUserInt, 10, 64)
15301336
type args struct {
15311337
imageUser string
15321338
}
15331339
tests := []struct {
1534-
name string
1535-
args args
1536-
want *int64
1537-
want1 string
1340+
name string
1341+
args args
1342+
wantUID *int64
1343+
wantUserName string
15381344
}{
1539-
// TODO: Add test cases.
1345+
{
1346+
name: "Empty Test",
1347+
args: args{
1348+
imageUser: "",
1349+
},
1350+
wantUID: nil,
1351+
wantUserName: "",
1352+
},
1353+
{
1354+
name: "ParseInt Success Test",
1355+
args: args{
1356+
imageUser: imageUserInt,
1357+
},
1358+
wantUID: &uid,
1359+
wantUserName: "",
1360+
},
1361+
{
1362+
name: "ParseInt Fail Test",
1363+
args: args{
1364+
imageUser: "foo",
1365+
},
1366+
wantUID: nil,
1367+
wantUserName: "foo",
1368+
},
15401369
}
15411370
for _, tt := range tests {
15421371
t.Run(tt.name, func(t *testing.T) {
1543-
got, got1 := getUserFromImageUser(tt.args.imageUser)
1544-
if got != tt.want {
1545-
t.Errorf("getUserFromImageUser() got = %v, want %v", got, tt.want)
1372+
gotUID, gotUsername := getUserFromImageUser(tt.args.imageUser)
1373+
if (gotUID == nil && tt.wantUID != nil) || (gotUID != nil && tt.wantUID == nil) {
1374+
t.Errorf("getUserFromImageUser() gotUID = %v, wantUID %v", gotUID, tt.wantUID)
15461375
}
1547-
if got1 != tt.want1 {
1548-
t.Errorf("getUserFromImageUser() got1 = %v, want %v", got1, tt.want1)
1376+
if gotUID != nil && tt.wantUID != nil {
1377+
if (*gotUID) != (*tt.wantUID) {
1378+
t.Errorf("getUserFromImageUser() gotUID = %v, wantUID %v", gotUID, tt.wantUID)
1379+
}
1380+
}
1381+
if gotUsername != tt.wantUserName {
1382+
t.Errorf("getUserFromImageUser() gotUsername = %v, wantUserName %v", gotUsername, tt.wantUserName)
15491383
}
15501384
})
15511385
}
@@ -1560,7 +1394,27 @@ func Test_parseUserFromImageUser(t *testing.T) {
15601394
args args
15611395
want string
15621396
}{
1563-
// TODO: Add test cases.
1397+
{
1398+
name: "Empty Test",
1399+
args: args{
1400+
id: "",
1401+
},
1402+
want: "",
1403+
},
1404+
{
1405+
name: "user:group Test",
1406+
args: args{
1407+
id: "user:group",
1408+
},
1409+
want: "user",
1410+
},
1411+
{
1412+
name: "No Group Test",
1413+
args: args{
1414+
id: "user",
1415+
},
1416+
want: "user",
1417+
},
15641418
}
15651419
for _, tt := range tests {
15661420
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)