Skip to content

Commit

Permalink
test: add test cases about modifyContainerConfig&containerNetns
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Aug 8, 2018
1 parent f913361 commit 7bf67bf
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
111 changes: 111 additions & 0 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,76 @@ func Test_modifyHostConfig(t *testing.T) {
}
}

func Test_modifyContainerConfig(t *testing.T) {
runAsUser := &runtime.Int64Value{int64(1)}
configUser := strconv.FormatInt(1, 10)

type args struct {
sc *runtime.LinuxContainerSecurityContext
config *apitypes.ContainerConfig
}
tests := []struct {
name string
args args
wantConfig *apitypes.ContainerConfig
}{
{
name: "Normal Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUser: runAsUser,
RunAsUsername: "foo",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: "foo",
},
},
{
name: "RunAsUser Nil Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUsername: "foo",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: "foo",
},
},
{
name: "RunAsUsername Empty Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUser: runAsUser,
RunAsUsername: "",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: configUser,
},
},
{
name: "Nil Test",
args: args{
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifyContainerConfig(tt.args.sc, tt.args.config)
if !reflect.DeepEqual(tt.args.config, tt.wantConfig) {
t.Errorf("modifyContainerConfig() config = %v, wantConfig %v", tt.args.config, tt.wantConfig)
return
}
})
}
}

func Test_applyContainerSecurityContext(t *testing.T) {
type args struct {
lc *runtime.LinuxContainerConfig
Expand Down Expand Up @@ -1129,6 +1199,47 @@ func Test_toCriContainer(t *testing.T) {
}
}

func Test_containerNetns(t *testing.T) {
type args struct {
container *mgr.Container
}
tests := []struct {
name string
args args
want string
}{
{
name: "Normal Test",
args: args{
container: &mgr.Container{
State: &apitypes.ContainerState{
Pid: int64(1001),
},
},
},
want: fmt.Sprintf("/proc/%v/ns/net", 1001),
},
{
name: "Pid EQ -1 Test",
args: args{
container: &mgr.Container{
State: &apitypes.ContainerState{
Pid: int64(-1),
},
},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := containerNetns(tt.args.container); got != tt.want {
t.Errorf("containerNetns() = %v, want %v", got, tt.want)
}
})
}
}

// Image related unit tests.
func Test_imageToCriImage(t *testing.T) {
type args struct {
Expand Down
111 changes: 111 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,76 @@ func Test_modifyHostConfig(t *testing.T) {
}
}

func Test_modifyContainerConfig(t *testing.T) {
runAsUser := &runtime.Int64Value{int64(1)}
configUser := strconv.FormatInt(1, 10)

type args struct {
sc *runtime.LinuxContainerSecurityContext
config *apitypes.ContainerConfig
}
tests := []struct {
name string
args args
wantConfig *apitypes.ContainerConfig
}{
{
name: "Normal Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUser: runAsUser,
RunAsUsername: "foo",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: "foo",
},
},
{
name: "RunAsUser Nil Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUsername: "foo",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: "foo",
},
},
{
name: "RunAsUsername Empty Test",
args: args{
sc: &runtime.LinuxContainerSecurityContext{
RunAsUser: runAsUser,
RunAsUsername: "",
},
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{
User: configUser,
},
},
{
name: "Nil Test",
args: args{
config: &apitypes.ContainerConfig{},
},
wantConfig: &apitypes.ContainerConfig{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifyContainerConfig(tt.args.sc, tt.args.config)
if !reflect.DeepEqual(tt.args.config, tt.wantConfig) {
t.Errorf("modifyContainerConfig() config = %v, wantConfig %v", tt.args.config, tt.wantConfig)
return
}
})
}
}

func Test_applyContainerSecurityContext(t *testing.T) {
type args struct {
lc *runtime.LinuxContainerConfig
Expand Down Expand Up @@ -1116,6 +1186,47 @@ func Test_toCriContainer(t *testing.T) {
}
}

func Test_containerNetns(t *testing.T) {
type args struct {
container *mgr.Container
}
tests := []struct {
name string
args args
want string
}{
{
name: "Normal Test",
args: args{
container: &mgr.Container{
State: &apitypes.ContainerState{
Pid: int64(1001),
},
},
},
want: fmt.Sprintf("/proc/%v/ns/net", 1001),
},
{
name: "Pid EQ -1 Test",
args: args{
container: &mgr.Container{
State: &apitypes.ContainerState{
Pid: int64(-1),
},
},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := containerNetns(tt.args.container); got != tt.want {
t.Errorf("containerNetns() = %v, want %v", got, tt.want)
}
})
}
}

// Image related unit tests.
func Test_imageToCriImage(t *testing.T) {
type args struct {
Expand Down

0 comments on commit 7bf67bf

Please sign in to comment.