Skip to content

Commit

Permalink
test: add unit test for generateMountBindings and modifySandboxNamesp…
Browse files Browse the repository at this point in the history
…aceOptions

Signed-off-by: Zou Rui <[email protected]>
  • Loading branch information
ZouRui89 committed Aug 2, 2018
1 parent a115eb8 commit 28a1499
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 0 deletions.
130 changes: 130 additions & 0 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,133 @@ func Test_parseResourcesFromCRI(t *testing.T) {
})
}
}

func Test_generateMountBindings(t *testing.T) {
type args struct {
mounts []*runtime.Mount
}
tests := []struct {
name string
args args
want []string
}{
{
name: "propagation_private test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: true,
SelinuxRelabel: true,
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
},
},
},
want: []string{"host_path:container_path:ro,Z"},
},
{
name: "propagation_bidirectinal test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: true,
SelinuxRelabel: false,
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
},
},
},
want: []string{"host_path:container_path:ro,rshared"},
},
{
name: "propagation_host_to_container test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: false,
SelinuxRelabel: true,
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
},
},
},
want: []string{"host_path:container_path:Z,rslave"},
},
{
name: "no_attrs test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: false,
SelinuxRelabel: false,
},
},
},
want: []string{"host_path:container_path"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
}
})
}
}

func Test_modifySandboxNamespaceOptions(t *testing.T) {
type args struct {
nsOpts *runtime.NamespaceOption
hostConfig *apitypes.HostConfig
}
tests := []struct {
name string
args args
want *apitypes.HostConfig
}{
{
name: "nil test",
args: args{
nsOpts: nil,
hostConfig: &apitypes.HostConfig{
PidMode: namespaceModeHost,
IpcMode: namespaceModeHost,
NetworkMode: namespaceModeHost,
},
},
want: &apitypes.HostConfig{
PidMode: namespaceModeHost,
IpcMode: namespaceModeHost,
NetworkMode: namespaceModeHost,
},
},
{
name: "normal test",
args: args{
nsOpts: &runtime.NamespaceOption{
HostIpc: true,
HostPid: true,
HostNetwork: false,
},
hostConfig: &apitypes.HostConfig{},
},
want: &apitypes.HostConfig{
PidMode: namespaceModeHost,
IpcMode: namespaceModeHost,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
}
})
}
}
129 changes: 129 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,3 +1177,132 @@ func Test_parseVolumesFromPouch(t *testing.T) {
})
}
}

func Test_generateMountBindings(t *testing.T) {
type args struct {
mounts []*runtime.Mount
}
tests := []struct {
name string
args args
want []string
}{
{
name: "propagation_private test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: true,
SelinuxRelabel: true,
Propagation: runtime.MountPropagation_PROPAGATION_PRIVATE,
},
},
},
want: []string{"host_path:container_path:ro,Z"},
},
{
name: "propagation_bidirectinal test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: true,
SelinuxRelabel: false,
Propagation: runtime.MountPropagation_PROPAGATION_BIDIRECTIONAL,
},
},
},
want: []string{"host_path:container_path:ro,rshared"},
},
{
name: "propagation_host_to_container test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: false,
SelinuxRelabel: true,
Propagation: runtime.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
},
},
},
want: []string{"host_path:container_path:Z,rslave"},
},
{
name: "no_attrs test",
args: args{
mounts: []*runtime.Mount{
{
ContainerPath: "container_path",
HostPath: "host_path",
Readonly: false,
SelinuxRelabel: false,
},
},
},
want: []string{"host_path:container_path"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := generateMountBindings(tt.args.mounts); !reflect.DeepEqual(got, tt.want) {
t.Errorf("generateMountBindings() = %v, want %v", got, tt.want)
}
})
}
}

func Test_modifySandboxNamespaceOptions(t *testing.T) {
type args struct {
nsOpts *runtime.NamespaceOption
hostConfig *apitypes.HostConfig
}
tests := []struct {
name string
args args
want *apitypes.HostConfig
}{
{
name: "nil test",
args: args{
nsOpts: &runtime.NamespaceOption{},
hostConfig: &apitypes.HostConfig{
IpcMode: namespaceModeHost,
PidMode: namespaceModeHost,
NetworkMode: namespaceModeHost,
},
},
want: &apitypes.HostConfig{
IpcMode: namespaceModeHost,
PidMode: namespaceModeHost,
NetworkMode: namespaceModeHost,
},
},
{
name: "normal test",
args: args{
nsOpts: &runtime.NamespaceOption{
Ipc: runtime.NamespaceMode_NODE,
Pid: runtime.NamespaceMode_NODE,
},
hostConfig: &apitypes.HostConfig{},
},
want: &apitypes.HostConfig{
IpcMode: namespaceModeHost,
PidMode: namespaceModeHost,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifySandboxNamespaceOptions(tt.args.nsOpts, tt.args.hostConfig)
if !reflect.DeepEqual(tt.args.hostConfig, tt.want) {
t.Errorf("modifySandboxNamespaceOptions() = %v, want %v", tt.args.hostConfig, tt.want)
}
})
}
}

0 comments on commit 28a1499

Please sign in to comment.