containerd/pkg/server/helpers_selinux_test.go
Yanqiang Miao 0c3304e006 Support selinux options/label
Support selinux optios/label

Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
2017-08-31 19:20:12 +08:00

72 lines
1.9 KiB
Go

// +build selinux
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"testing"
"github.com/opencontainers/selinux/go-selinux"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
func TestInitSelinuxOpts(t *testing.T) {
if !selinux.GetEnabled() {
return
}
for desc, test := range map[string]struct {
selinuxOpt *runtime.SELinuxOption
processLabel string
mountLabels []string
}{
"testNullValue": {
selinuxOpt: nil,
processLabel: "",
mountLabels: []string{"", ""},
},
"testEmptyString": {
selinuxOpt: &runtime.SELinuxOption{
User: "",
Role: "",
Type: "",
Level: "",
},
processLabel: ":::",
mountLabels: []string{":object_r:container_file_t:", ":object_r:svirt_sandbox_file_t:"},
},
"testUser": {
selinuxOpt: &runtime.SELinuxOption{
User: "user_u",
Role: "user_r",
Type: "user_t",
Level: "s0:c1,c2",
},
processLabel: "user_u:user_r:user_t:s0:c1,c2",
mountLabels: []string{"user_u:object_r:container_file_t:s0:c1,c2", "user_u:object_r:svirt_sandbox_file_t:s0:c1,c2"},
},
} {
t.Logf("TestCase %q", desc)
processLabel, mountLabel, err := initSelinuxOpts(test.selinuxOpt)
assert.NoError(t, err)
assert.Equal(t, test.processLabel, processLabel)
assert.Contains(t, test.mountLabels, mountLabel)
}
}