Skip to content

Commit 28a1499

Browse files
committed
test: add unit test for generateMountBindings and modifySandboxNamespaceOptions
Signed-off-by: Zou Rui <[email protected]>
1 parent a115eb8 commit 28a1499

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed

cri/v1alpha1/cri_utils_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,3 +1327,133 @@ func Test_parseResourcesFromCRI(t *testing.T) {
13271327
})
13281328
}
13291329
}
1330+
1331+
func Test_generateMountBindings(t *testing.T) {
1332+
type args struct {
1333+
mounts []*runtime.Mount
1334+
}
1335+
tests := []struct {
1336+
name string
1337+
args args
1338+
want []string
1339+
}{
1340+
{
1341+
name: "propagation_private test",
1342+
args: args{
1343+
mounts: []*runtime.Mount{
1344+
{
1345+
ContainerPath: "container_path",
1346+
HostPath: "host_path",
1347+
Readonly: true,
1348+
SelinuxRelabel: true,
1349+
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
1350+
},
1351+
},
1352+
},
1353+
want: []string{"host_path:container_path:ro,Z"},
1354+
},
1355+
{
1356+
name: "propagation_bidirectinal test",
1357+
args: args{
1358+
mounts: []*runtime.Mount{
1359+
{
1360+
ContainerPath: "container_path",
1361+
HostPath: "host_path",
1362+
Readonly: true,
1363+
SelinuxRelabel: false,
1364+
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
1365+
},
1366+
},
1367+
},
1368+
want: []string{"host_path:container_path:ro,rshared"},
1369+
},
1370+
{
1371+
name: "propagation_host_to_container test",
1372+
args: args{
1373+
mounts: []*runtime.Mount{
1374+
{
1375+
ContainerPath: "container_path",
1376+
HostPath: "host_path",
1377+
Readonly: false,
1378+
SelinuxRelabel: true,
1379+
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
1380+
},
1381+
},
1382+
},
1383+
want: []string{"host_path:container_path:Z,rslave"},
1384+
},
1385+
{
1386+
name: "no_attrs test",
1387+
args: args{
1388+
mounts: []*runtime.Mount{
1389+
{
1390+
ContainerPath: "container_path",
1391+
HostPath: "host_path",
1392+
Readonly: false,
1393+
SelinuxRelabel: false,
1394+
},
1395+
},
1396+
},
1397+
want: []string{"host_path:container_path"},
1398+
},
1399+
}
1400+
for _, tt := range tests {
1401+
t.Run(tt.name, func(t *testing.T) {
1402+
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
1403+
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
1404+
}
1405+
})
1406+
}
1407+
}
1408+
1409+
func Test_modifySandboxNamespaceOptions(t *testing.T) {
1410+
type args struct {
1411+
nsOpts *runtime.NamespaceOption
1412+
hostConfig *apitypes.HostConfig
1413+
}
1414+
tests := []struct {
1415+
name string
1416+
args args
1417+
want *apitypes.HostConfig
1418+
}{
1419+
{
1420+
name: "nil test",
1421+
args: args{
1422+
nsOpts: nil,
1423+
hostConfig: &apitypes.HostConfig{
1424+
PidMode: namespaceModeHost,
1425+
IpcMode: namespaceModeHost,
1426+
NetworkMode: namespaceModeHost,
1427+
},
1428+
},
1429+
want: &apitypes.HostConfig{
1430+
PidMode: namespaceModeHost,
1431+
IpcMode: namespaceModeHost,
1432+
NetworkMode: namespaceModeHost,
1433+
},
1434+
},
1435+
{
1436+
name: "normal test",
1437+
args: args{
1438+
nsOpts: &runtime.NamespaceOption{
1439+
HostIpc: true,
1440+
HostPid: true,
1441+
HostNetwork: false,
1442+
},
1443+
hostConfig: &apitypes.HostConfig{},
1444+
},
1445+
want: &apitypes.HostConfig{
1446+
PidMode: namespaceModeHost,
1447+
IpcMode: namespaceModeHost,
1448+
},
1449+
},
1450+
}
1451+
for _, tt := range tests {
1452+
t.Run(tt.name, func(t *testing.T) {
1453+
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
1454+
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
1455+
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
1456+
}
1457+
})
1458+
}
1459+
}

cri/v1alpha2/cri_utils_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,132 @@ func Test_parseVolumesFromPouch(t *testing.T) {
11771177
})
11781178
}
11791179
}
1180+
1181+
func Test_generateMountBindings(t *testing.T) {
1182+
type args struct {
1183+
mounts []*runtime.Mount
1184+
}
1185+
tests := []struct {
1186+
name string
1187+
args args
1188+
want []string
1189+
}{
1190+
{
1191+
name: "propagation_private test",
1192+
args: args{
1193+
mounts: []*runtime.Mount{
1194+
{
1195+
ContainerPath: "container_path",
1196+
HostPath: "host_path",
1197+
Readonly: true,
1198+
SelinuxRelabel: true,
1199+
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
1200+
},
1201+
},
1202+
},
1203+
want: []string{"host_path:container_path:ro,Z"},
1204+
},
1205+
{
1206+
name: "propagation_bidirectinal test",
1207+
args: args{
1208+
mounts: []*runtime.Mount{
1209+
{
1210+
ContainerPath: "container_path",
1211+
HostPath: "host_path",
1212+
Readonly: true,
1213+
SelinuxRelabel: false,
1214+
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
1215+
},
1216+
},
1217+
},
1218+
want: []string{"host_path:container_path:ro,rshared"},
1219+
},
1220+
{
1221+
name: "propagation_host_to_container test",
1222+
args: args{
1223+
mounts: []*runtime.Mount{
1224+
{
1225+
ContainerPath: "container_path",
1226+
HostPath: "host_path",
1227+
Readonly: false,
1228+
SelinuxRelabel: true,
1229+
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
1230+
},
1231+
},
1232+
},
1233+
want: []string{"host_path:container_path:Z,rslave"},
1234+
},
1235+
{
1236+
name: "no_attrs test",
1237+
args: args{
1238+
mounts: []*runtime.Mount{
1239+
{
1240+
ContainerPath: "container_path",
1241+
HostPath: "host_path",
1242+
Readonly: false,
1243+
SelinuxRelabel: false,
1244+
},
1245+
},
1246+
},
1247+
want: []string{"host_path:container_path"},
1248+
},
1249+
}
1250+
for _, tt := range tests {
1251+
t.Run(tt.name, func(t *testing.T) {
1252+
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
1253+
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
1254+
}
1255+
})
1256+
}
1257+
}
1258+
1259+
func Test_modifySandboxNamespaceOptions(t *testing.T) {
1260+
type args struct {
1261+
nsOpts *runtime.NamespaceOption
1262+
hostConfig *apitypes.HostConfig
1263+
}
1264+
tests := []struct {
1265+
name string
1266+
args args
1267+
want *apitypes.HostConfig
1268+
}{
1269+
{
1270+
name: "nil test",
1271+
args: args{
1272+
nsOpts: &runtime.NamespaceOption{},
1273+
hostConfig: &apitypes.HostConfig{
1274+
IpcMode: namespaceModeHost,
1275+
PidMode: namespaceModeHost,
1276+
NetworkMode: namespaceModeHost,
1277+
},
1278+
},
1279+
want: &apitypes.HostConfig{
1280+
IpcMode: namespaceModeHost,
1281+
PidMode: namespaceModeHost,
1282+
NetworkMode: namespaceModeHost,
1283+
},
1284+
},
1285+
{
1286+
name: "normal test",
1287+
args: args{
1288+
nsOpts: &runtime.NamespaceOption{
1289+
Ipc: runtime.NamespaceMode_NODE,
1290+
Pid: runtime.NamespaceMode_NODE,
1291+
},
1292+
hostConfig: &apitypes.HostConfig{},
1293+
},
1294+
want: &apitypes.HostConfig{
1295+
IpcMode: namespaceModeHost,
1296+
PidMode: namespaceModeHost,
1297+
},
1298+
},
1299+
}
1300+
for _, tt := range tests {
1301+
t.Run(tt.name, func(t *testing.T) {
1302+
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
1303+
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
1304+
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
1305+
}
1306+
})
1307+
}
1308+
}

0 commit comments

Comments
 (0)