Skip to content

Commit 1f7183e

Browse files
authored
Merge pull request #2030 from Starnop/cri-test-ImageUser
test: add test cases about CRI image
2 parents d6a4571 + 796d025 commit 1f7183e

File tree

2 files changed

+215
-240
lines changed

2 files changed

+215
-240
lines changed

cri/v1alpha1/cri_utils_test.go

Lines changed: 96 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,250 +1242,70 @@ 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+
imageUserInt := "1"
1247+
uid, _ := strconv.ParseInt(imageUserInt, 10, 64)
1248+
12451249
type args struct {
12461250
image *apitypes.ImageInfo
12471251
}
12481252
tests := []struct {
12491253
name string
12501254
args args
12511255
want *runtime.Image
1252-
wantErr bool
1256+
wantErr error
12531257
}{
12541258
{
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"},
1259+
name: "Normal Test",
1260+
args: args{
1261+
image: &apitypes.ImageInfo{
1262+
ID: "image-id",
1263+
RepoTags: repoDigests,
1264+
RepoDigests: repoDigests,
12871265
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{
13041266
Config: &apitypes.ContainerConfig{
1305-
User: "123456:dev",
1267+
User: imageUserInt,
13061268
},
1307-
ID: "1",
1308-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1309-
RepoTags: []string{"sldkfeio", "civlme"},
1310-
Size: 1024,
13111269
},
13121270
},
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)},
1271+
want: &runtime.Image{
1272+
Id: "image-id",
1273+
RepoTags: repoDigests,
1274+
RepoDigests: repoDigests,
1275+
Size_: uint64(1024),
1276+
Uid: &runtime.Int64Value{uid},
13191277
Username: "",
13201278
},
1321-
false,
1279+
wantErr: nil,
13221280
},
13231281
{
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"},
1282+
name: "ImageUID Nil Test",
1283+
args: args{
1284+
image: &apitypes.ImageInfo{
1285+
ID: "image-id",
1286+
RepoTags: repoDigests,
1287+
RepoDigests: repoDigests,
13331288
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{
1350-
Config: &apitypes.ContainerConfig{
1351-
User: "123456:dev",
1352-
},
1353-
ID: "1",
1354-
RepoDigests: []string{"asdlkfej", "vwoeifo"},
1355-
RepoTags: []string{"sldkfeio", "civlme"},
1356-
Size: 2048,
1357-
},
1358-
},
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: "",
1366-
},
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{
14421289
Config: &apitypes.ContainerConfig{
1443-
User: "-123456:dev",
1290+
User: "foo",
14441291
},
1445-
ID: "abc",
1446-
RepoDigests: []string{"1238", "4820940"},
1447-
RepoTags: []string{"sldkfeio", "civlme"},
1448-
Size: 2048,
14491292
},
14501293
},
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: "",
1294+
want: &runtime.Image{
1295+
Id: "image-id",
1296+
RepoTags: repoDigests,
1297+
RepoDigests: repoDigests,
1298+
Size_: uint64(1024),
1299+
Uid: &runtime.Int64Value{},
1300+
Username: "foo",
14581301
},
1459-
false,
1460-
},
1461-
{
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: "",
1481-
},
1482-
false,
1302+
wantErr: nil,
14831303
},
14841304
}
14851305
for _, tt := range tests {
14861306
t.Run(tt.name, func(t *testing.T) {
14871307
got, err := imageToCriImage(tt.args.image)
1488-
if (err != nil) != tt.wantErr {
1308+
if err != tt.wantErr {
14891309
t.Errorf("imageToCriImage() error = %v, wantErr %v", err, tt.wantErr)
14901310
return
14911311
}
@@ -1527,25 +1347,55 @@ func TestCriManager_ensureSandboxImageExists(t *testing.T) {
15271347
}
15281348

15291349
func Test_getUserFromImageUser(t *testing.T) {
1350+
imageUserInt := "1"
1351+
uid, _ := strconv.ParseInt(imageUserInt, 10, 64)
15301352
type args struct {
15311353
imageUser string
15321354
}
15331355
tests := []struct {
1534-
name string
1535-
args args
1536-
want *int64
1537-
want1 string
1356+
name string
1357+
args args
1358+
wantUID *int64
1359+
wantUserName string
15381360
}{
1539-
// TODO: Add test cases.
1361+
{
1362+
name: "Empty Test",
1363+
args: args{
1364+
imageUser: "",
1365+
},
1366+
wantUID: nil,
1367+
wantUserName: "",
1368+
},
1369+
{
1370+
name: "ParseInt Success Test",
1371+
args: args{
1372+
imageUser: imageUserInt,
1373+
},
1374+
wantUID: &uid,
1375+
wantUserName: "",
1376+
},
1377+
{
1378+
name: "ParseInt Fail Test",
1379+
args: args{
1380+
imageUser: "foo",
1381+
},
1382+
wantUID: nil,
1383+
wantUserName: "foo",
1384+
},
15401385
}
15411386
for _, tt := range tests {
15421387
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)
1388+
gotUID, gotUsername := getUserFromImageUser(tt.args.imageUser)
1389+
if (gotUID == nil && tt.wantUID != nil) || (gotUID != nil && tt.wantUID == nil) {
1390+
t.Errorf("getUserFromImageUser() gotUID = %v, wantUID %v", gotUID, tt.wantUID)
1391+
}
1392+
if gotUID != nil && tt.wantUID != nil {
1393+
if (*gotUID) != (*tt.wantUID) {
1394+
t.Errorf("getUserFromImageUser() gotUID = %v, wantUID %v", gotUID, tt.wantUID)
1395+
}
15461396
}
1547-
if got1 != tt.want1 {
1548-
t.Errorf("getUserFromImageUser() got1 = %v, want %v", got1, tt.want1)
1397+
if gotUsername != tt.wantUserName {
1398+
t.Errorf("getUserFromImageUser() gotUsername = %v, wantUserName %v", gotUsername, tt.wantUserName)
15491399
}
15501400
})
15511401
}
@@ -1560,7 +1410,27 @@ func Test_parseUserFromImageUser(t *testing.T) {
15601410
args args
15611411
want string
15621412
}{
1563-
// TODO: Add test cases.
1413+
{
1414+
name: "Empty Test",
1415+
args: args{
1416+
id: "",
1417+
},
1418+
want: "",
1419+
},
1420+
{
1421+
name: "user:group Test",
1422+
args: args{
1423+
id: "user:group",
1424+
},
1425+
want: "user",
1426+
},
1427+
{
1428+
name: "No Group Test",
1429+
args: args{
1430+
id: "user",
1431+
},
1432+
want: "user",
1433+
},
15641434
}
15651435
for _, tt := range tests {
15661436
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)