mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Add unit test for get security option functions.
This commit is contained in:
		@@ -42,7 +42,7 @@ func makeContainerConfig(sConfig *runtimeApi.PodSandboxConfig, name, image strin
 | 
				
			|||||||
// TestListContainers creates several containers and then list them to check
 | 
					// TestListContainers creates several containers and then list them to check
 | 
				
			||||||
// whether the correct metadatas, states, and labels are returned.
 | 
					// whether the correct metadatas, states, and labels are returned.
 | 
				
			||||||
func TestListContainers(t *testing.T) {
 | 
					func TestListContainers(t *testing.T) {
 | 
				
			||||||
	ds, _, _ := newTestDockerSevice()
 | 
						ds, _, _ := newTestDockerService()
 | 
				
			||||||
	podName, namespace := "foo", "bar"
 | 
						podName, namespace := "foo", "bar"
 | 
				
			||||||
	containerName, image := "sidecar", "logger"
 | 
						containerName, image := "sidecar", "logger"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -91,7 +91,7 @@ func TestListContainers(t *testing.T) {
 | 
				
			|||||||
// TestContainerStatus tests the basic lifecycle operations and verify that
 | 
					// TestContainerStatus tests the basic lifecycle operations and verify that
 | 
				
			||||||
// the status returned reflects the operations performed.
 | 
					// the status returned reflects the operations performed.
 | 
				
			||||||
func TestContainerStatus(t *testing.T) {
 | 
					func TestContainerStatus(t *testing.T) {
 | 
				
			||||||
	ds, _, fClock := newTestDockerSevice()
 | 
						ds, _, fClock := newTestDockerService()
 | 
				
			||||||
	sConfig := makeSandboxConfig("foo", "bar", "1", 0)
 | 
						sConfig := makeSandboxConfig("foo", "bar", "1", 0)
 | 
				
			||||||
	labels := map[string]string{"abc.xyz": "foo"}
 | 
						labels := map[string]string{"abc.xyz": "foo"}
 | 
				
			||||||
	annotations := map[string]string{"foo.bar.baz": "abc"}
 | 
						annotations := map[string]string{"foo.bar.baz": "abc"}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,7 +48,7 @@ func makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid string, atte
 | 
				
			|||||||
// TestListSandboxes creates several sandboxes and then list them to check
 | 
					// TestListSandboxes creates several sandboxes and then list them to check
 | 
				
			||||||
// whether the correct metadatas, states, and labels are returned.
 | 
					// whether the correct metadatas, states, and labels are returned.
 | 
				
			||||||
func TestListSandboxes(t *testing.T) {
 | 
					func TestListSandboxes(t *testing.T) {
 | 
				
			||||||
	ds, _, _ := newTestDockerSevice()
 | 
						ds, _, _ := newTestDockerService()
 | 
				
			||||||
	name, namespace := "foo", "bar"
 | 
						name, namespace := "foo", "bar"
 | 
				
			||||||
	configs := []*runtimeApi.PodSandboxConfig{}
 | 
						configs := []*runtimeApi.PodSandboxConfig{}
 | 
				
			||||||
	for i := 0; i < 3; i++ {
 | 
						for i := 0; i < 3; i++ {
 | 
				
			||||||
@@ -86,7 +86,7 @@ func TestListSandboxes(t *testing.T) {
 | 
				
			|||||||
// TestSandboxStatus tests the basic lifecycle operations and verify that
 | 
					// TestSandboxStatus tests the basic lifecycle operations and verify that
 | 
				
			||||||
// the status returned reflects the operations performed.
 | 
					// the status returned reflects the operations performed.
 | 
				
			||||||
func TestSandboxStatus(t *testing.T) {
 | 
					func TestSandboxStatus(t *testing.T) {
 | 
				
			||||||
	ds, _, fClock := newTestDockerSevice()
 | 
						ds, _, fClock := newTestDockerService()
 | 
				
			||||||
	labels := map[string]string{"label": "foobar1"}
 | 
						labels := map[string]string{"label": "foobar1"}
 | 
				
			||||||
	annotations := map[string]string{"annotation": "abc"}
 | 
						annotations := map[string]string{"annotation": "abc"}
 | 
				
			||||||
	config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)
 | 
						config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -78,8 +78,6 @@ type DockerLegacyService interface {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type dockerService struct {
 | 
					type dockerService struct {
 | 
				
			||||||
	// TODO: Current seccomp implementation is very docker specific. Move this somewhere else
 | 
					 | 
				
			||||||
	// after we define more general seccomp api.
 | 
					 | 
				
			||||||
	seccompProfileRoot string
 | 
						seccompProfileRoot string
 | 
				
			||||||
	client             dockertools.DockerInterface
 | 
						client             dockertools.DockerInterface
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,7 @@ import (
 | 
				
			|||||||
	"k8s.io/kubernetes/pkg/util/clock"
 | 
						"k8s.io/kubernetes/pkg/util/clock"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newTestDockerSevice() (*dockerService, *dockertools.FakeDockerClient, *clock.FakeClock) {
 | 
					func newTestDockerService() (*dockerService, *dockertools.FakeDockerClient, *clock.FakeClock) {
 | 
				
			||||||
	fakeClock := clock.NewFakeClock(time.Time{})
 | 
						fakeClock := clock.NewFakeClock(time.Time{})
 | 
				
			||||||
	c := dockertools.NewFakeDockerClientWithClock(fakeClock)
 | 
						c := dockertools.NewFakeDockerClientWithClock(fakeClock)
 | 
				
			||||||
	return &dockerService{client: c}, c, fakeClock
 | 
						return &dockerService{client: c}, c, fakeClock
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,10 @@ import (
 | 
				
			|||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"k8s.io/kubernetes/pkg/api"
 | 
				
			||||||
 | 
						runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
 | 
				
			||||||
 | 
						"k8s.io/kubernetes/pkg/security/apparmor"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLabelsAndAnnotationsRoundTrip(t *testing.T) {
 | 
					func TestLabelsAndAnnotationsRoundTrip(t *testing.T) {
 | 
				
			||||||
@@ -32,3 +36,114 @@ func TestLabelsAndAnnotationsRoundTrip(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, expectedLabels, actualLabels)
 | 
						assert.Equal(t, expectedLabels, actualLabels)
 | 
				
			||||||
	assert.Equal(t, expectedAnnotations, actualAnnotations)
 | 
						assert.Equal(t, expectedAnnotations, actualAnnotations)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TestGetContainerSecurityOpts tests the logic of generating container security options from sandbox annotations.
 | 
				
			||||||
 | 
					// The actual profile loading logic is tested in dockertools.
 | 
				
			||||||
 | 
					// TODO: Migrate the corresponding test to dockershim.
 | 
				
			||||||
 | 
					func TestGetContainerSecurityOpts(t *testing.T) {
 | 
				
			||||||
 | 
						containerName := "bar"
 | 
				
			||||||
 | 
						makeConfig := func(annotations map[string]string) *runtimeApi.PodSandboxConfig {
 | 
				
			||||||
 | 
							return makeSandboxConfigWithLabelsAndAnnotations("pod", "ns", "1234", 1, nil, annotations)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							msg          string
 | 
				
			||||||
 | 
							config       *runtimeApi.PodSandboxConfig
 | 
				
			||||||
 | 
							expectedOpts []string
 | 
				
			||||||
 | 
						}{{
 | 
				
			||||||
 | 
							msg:          "No security annotations",
 | 
				
			||||||
 | 
							config:       makeConfig(nil),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp unconfined",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompContainerAnnotationKeyPrefix + containerName: "unconfined",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp default",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompContainerAnnotationKeyPrefix + containerName: "docker/default",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: nil,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp pod default",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompPodAnnotationKey: "docker/default",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: nil,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "AppArmor runtime/default",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								apparmor.ContainerAnnotationKeyPrefix + containerName: apparmor.ProfileRuntimeDefault,
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "AppArmor local profile",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								apparmor.ContainerAnnotationKeyPrefix + containerName: apparmor.ProfileNamePrefix + "foo",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined", "apparmor=foo"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "AppArmor and seccomp profile",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompContainerAnnotationKeyPrefix + containerName: "docker/default",
 | 
				
			||||||
 | 
								apparmor.ContainerAnnotationKeyPrefix + containerName:   apparmor.ProfileNamePrefix + "foo",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: []string{"apparmor=foo"},
 | 
				
			||||||
 | 
						}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i, test := range tests {
 | 
				
			||||||
 | 
							opts, err := getContainerSecurityOpts(containerName, test.config, "test/seccomp/profile/root")
 | 
				
			||||||
 | 
							assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							for _, opt := range test.expectedOpts {
 | 
				
			||||||
 | 
								assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TestGetSandboxSecurityOpts tests the logic of generating sandbox security options from sandbox annotations.
 | 
				
			||||||
 | 
					func TestGetSandboxSecurityOpts(t *testing.T) {
 | 
				
			||||||
 | 
						makeConfig := func(annotations map[string]string) *runtimeApi.PodSandboxConfig {
 | 
				
			||||||
 | 
							return makeSandboxConfigWithLabelsAndAnnotations("pod", "ns", "1234", 1, nil, annotations)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							msg          string
 | 
				
			||||||
 | 
							config       *runtimeApi.PodSandboxConfig
 | 
				
			||||||
 | 
							expectedOpts []string
 | 
				
			||||||
 | 
						}{{
 | 
				
			||||||
 | 
							msg:          "No security annotations",
 | 
				
			||||||
 | 
							config:       makeConfig(nil),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp default",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompPodAnnotationKey: "docker/default",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: nil,
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp unconfined",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompPodAnnotationKey: "unconfined",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: []string{"seccomp=unconfined"},
 | 
				
			||||||
 | 
						}, {
 | 
				
			||||||
 | 
							msg: "Seccomp pod and container profile",
 | 
				
			||||||
 | 
							config: makeConfig(map[string]string{
 | 
				
			||||||
 | 
								api.SeccompContainerAnnotationKeyPrefix + "test-container": "unconfined",
 | 
				
			||||||
 | 
								api.SeccompPodAnnotationKey:                                "docker/default",
 | 
				
			||||||
 | 
							}),
 | 
				
			||||||
 | 
							expectedOpts: nil,
 | 
				
			||||||
 | 
						}}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i, test := range tests {
 | 
				
			||||||
 | 
							opts, err := getSandboxSecurityOpts(test.config, "test/seccomp/profile/root")
 | 
				
			||||||
 | 
							assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							for _, opt := range test.expectedOpts {
 | 
				
			||||||
 | 
								assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user