Skip to content

Commit 718f6eb

Browse files
committed
test: add unit test for generateMountBindings and modifySandboxNamespaceOptions
Signed-off-by: Zou Rui <[email protected]>
1 parent 2f0b5cf commit 718f6eb

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed

cri/v1alpha1/cri_utils_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,3 +935,137 @@ func Test_parseVolumesFromPouch(t *testing.T) {
935935
})
936936
}
937937
}
938+
939+
func Test_generateMountBindings(t *testing.T) {
940+
type args struct {
941+
mounts []*runtime.Mount
942+
}
943+
tests := []struct {
944+
name string
945+
args args
946+
want []string
947+
}{
948+
{
949+
name: "propagation_private test",
950+
args: args{
951+
mounts: []*runtime.Mount{
952+
{
953+
ContainerPath: "container_path",
954+
HostPath: "host_path",
955+
Readonly: true,
956+
SelinuxRelabel: true,
957+
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
958+
},
959+
},
960+
},
961+
want: []string{"host_path:container_path:ro,Z"},
962+
},
963+
{
964+
name: "propagation_bidirectinal test",
965+
args: args{
966+
mounts: []*runtime.Mount{
967+
{
968+
ContainerPath: "container_path",
969+
HostPath: "host_path",
970+
Readonly: true,
971+
SelinuxRelabel: false,
972+
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
973+
},
974+
},
975+
},
976+
want: []string{"host_path:container_path:ro,rshared"},
977+
},
978+
{
979+
name: "propagation_host_to_container test",
980+
args: args{
981+
mounts: []*runtime.Mount{
982+
{
983+
ContainerPath: "container_path",
984+
HostPath: "host_path",
985+
Readonly: false,
986+
SelinuxRelabel: true,
987+
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
988+
},
989+
},
990+
},
991+
want: []string{"host_path:container_path:Z,rslave"},
992+
},
993+
{
994+
name: "no_attrs test",
995+
args: args{
996+
mounts: []*runtime.Mount{
997+
{
998+
ContainerPath: "container_path",
999+
HostPath: "host_path",
1000+
Readonly: false,
1001+
SelinuxRelabel: false,
1002+
Propagation: nil,
1003+
},
1004+
},
1005+
},
1006+
want: []string{"host_path:container_path"},
1007+
},
1008+
}
1009+
for _, tt := range tests {
1010+
t.Run(tt.name, func(t *testing.T) {
1011+
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
1012+
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
1013+
}
1014+
})
1015+
}
1016+
}
1017+
1018+
func Test_modifySandboxNamespaceOptions(t *testing.T) {
1019+
type args struct {
1020+
nsOpts *runtime.NamespaceOption
1021+
hostConfig *apitypes.HostConfig
1022+
}
1023+
tests := []struct {
1024+
name string
1025+
args args
1026+
want *apitypes.HostConfig
1027+
}{
1028+
{
1029+
name: "nil test",
1030+
args: args{
1031+
nsOpts: nil,
1032+
hostConfig: nil,
1033+
},
1034+
},
1035+
{
1036+
name: "ipc test",
1037+
args: args{
1038+
nsOpts: &runtime.NamespaceOption{
1039+
HostIpc: true,
1040+
},
1041+
hostConfig: nil,
1042+
},
1043+
},
1044+
{
1045+
name: "pid test",
1046+
args: args{
1047+
nsOpts: &runtime.NamespaceOption{
1048+
HostPid: true,
1049+
},
1050+
hostConfig: nil,
1051+
},
1052+
},
1053+
{
1054+
name: "network test",
1055+
args: args{
1056+
nsOpts: &runtime.NamespaceOption{
1057+
HostNetwork: true,
1058+
},
1059+
hostConfig: nil,
1060+
},
1061+
},
1062+
}
1063+
for _, tt := range tests {
1064+
t.Run(tt.name, func(t *testing.T) {
1065+
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
1066+
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
1067+
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
1068+
}
1069+
})
1070+
}
1071+
}

cri/v1alpha2/cri_utils_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,3 +935,147 @@ func Test_parseVolumesFromPouch(t *testing.T) {
935935
})
936936
}
937937
}
938+
939+
func Test_generateMountBindings(t *testing.T) {
940+
type args struct {
941+
mounts []*runtime.Mount
942+
}
943+
tests := []struct {
944+
name string
945+
args args
946+
want []string
947+
}{
948+
{
949+
name: "propagation_private test",
950+
args: args{
951+
mounts: []*runtime.Mount{
952+
{
953+
ContainerPath: "container_path",
954+
HostPath: "host_path",
955+
Readonly: true,
956+
SelinuxRelabel: true,
957+
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
958+
},
959+
},
960+
},
961+
want: []string{"host_path:container_path:ro,Z"},
962+
},
963+
{
964+
name: "propagation_bidirectinal test",
965+
args: args{
966+
mounts: []*runtime.Mount{
967+
{
968+
ContainerPath: "container_path",
969+
HostPath: "host_path",
970+
Readonly: true,
971+
SelinuxRelabel: false,
972+
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
973+
},
974+
},
975+
},
976+
want: []string{"host_path:container_path:ro,rshared"},
977+
},
978+
{
979+
name: "propagation_host_to_container test",
980+
args: args{
981+
mounts: []*runtime.Mount{
982+
{
983+
ContainerPath: "container_path",
984+
HostPath: "host_path",
985+
Readonly: false,
986+
SelinuxRelabel: true,
987+
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
988+
},
989+
},
990+
},
991+
want: []string{"host_path:container_path:Z,rslave"},
992+
},
993+
{
994+
name: "no_attrs test",
995+
args: args{
996+
mounts: []*runtime.Mount{
997+
{
998+
ContainerPath: "container_path",
999+
HostPath: "host_path",
1000+
Readonly: false,
1001+
SelinuxRelabel: false,
1002+
Propagation: nil,
1003+
},
1004+
},
1005+
},
1006+
want: []string{"host_path:container_path"},
1007+
},
1008+
}
1009+
for _, tt := range tests {
1010+
t.Run(tt.name, func(t *testing.T) {
1011+
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
1012+
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
1013+
}
1014+
})
1015+
}
1016+
}
1017+
1018+
func Test_modifySandboxNamespaceOptions(t *testing.T) {
1019+
type args struct {
1020+
nsOpts *runtime.NamespaceOption
1021+
hostConfig *apitypes.HostConfig
1022+
}
1023+
tests := []struct {
1024+
name string
1025+
args args
1026+
want *apitypes.HostConfig
1027+
}{
1028+
{
1029+
name: "nil test",
1030+
args: args{
1031+
nsOpts: nil,
1032+
hostConfig: nil,
1033+
},
1034+
want: nil,
1035+
},
1036+
{
1037+
name: "ipc test",
1038+
args: args{
1039+
nsOpts: &runtime.NamespaceOption{
1040+
Ipc: runtime.NamespaceMode_POD,
1041+
},
1042+
hostConfig: nil,
1043+
},
1044+
want: &apitypes.HostConfig{
1045+
IpcMode: namespaceModeHost,
1046+
},
1047+
},
1048+
{
1049+
name: "pid test",
1050+
args: args{
1051+
nsOpts: &runtime.NamespaceOption{
1052+
Pid: runtime.NamespaceMode_NODE,
1053+
},
1054+
hostConfig: nil,
1055+
},
1056+
want: &apitypes.HostConfig{
1057+
PidMode: namespaceModeHost,
1058+
},
1059+
},
1060+
{
1061+
name: "network test",
1062+
args: args{
1063+
nsOpts: &runtime.NamespaceOption{
1064+
Network: runtime.NamespaceMode_NODE,
1065+
},
1066+
hostConfig: nil,
1067+
},
1068+
want: &apitypes.HostConfig{
1069+
NetworkMode: namespaceModeHost,
1070+
},
1071+
},
1072+
}
1073+
for _, tt := range tests {
1074+
t.Run(tt.name, func(t *testing.T) {
1075+
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
1076+
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
1077+
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
1078+
}
1079+
})
1080+
}
1081+
}

0 commit comments

Comments
 (0)