Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for generateMountBindings and modifySandboxNamespaceOptions #1768

Merged
merged 1 commit into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More test cases such as HostNetwork: true for higher test coverage ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the opinion of this comment from Starop? @ZouRui89

},
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)
}
})
}
}
152 changes: 151 additions & 1 deletion cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,33 @@ func Test_modifyContainerNamespaceOptions(t *testing.T) {
tests := []struct {
name string
args args
want apitypes.HostConfig
}{
// TODO: Add test cases.
{
name: "normal test",
args: args{
nsOpts: &runtime.NamespaceOption{Network: runtime.NamespaceMode_NODE, Pid: runtime.NamespaceMode_NODE},
podSandboxID: "fakeSandBoxID",
hostConfig: &apitypes.HostConfig{PidMode: "host", IpcMode: "host", NetworkMode: "host"},
},
want: apitypes.HostConfig{PidMode: "host", IpcMode: "container:fakeSandBoxID", NetworkMode: "host"},
},
{
name: "nil test",
args: args{
nsOpts: nil,
podSandboxID: "fakeSandBoxID",
hostConfig: &apitypes.HostConfig{PidMode: "host", IpcMode: "host", NetworkMode: "host"},
},
want: apitypes.HostConfig{PidMode: "container:fakeSandBoxID", IpcMode: "container:fakeSandBoxID", NetworkMode: "container:fakeSandBoxID"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifyContainerNamespaceOptions(tt.args.nsOpts, tt.args.podSandboxID, tt.args.hostConfig)
if !reflect.DeepEqual(*tt.args.hostConfig, tt.want) {
t.Errorf("modifyContainerNamespaceOptions() = %v, want %v", *tt.args.hostConfig, tt.want)
}
})
}
}
Expand Down Expand Up @@ -1177,3 +1198,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)
}
})
}
}