privileged.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package common
  14. import (
  15. "fmt"
  16. "github.com/onsi/ginkgo"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. imageutils "k8s.io/kubernetes/test/utils/image"
  21. )
  22. // PrivilegedPodTestConfig is configuration struct for privileged pod test
  23. type PrivilegedPodTestConfig struct {
  24. f *framework.Framework
  25. privilegedPod string
  26. privilegedContainer string
  27. notPrivilegedContainer string
  28. pod *v1.Pod
  29. }
  30. var _ = framework.KubeDescribe("PrivilegedPod [NodeConformance]", func() {
  31. config := &PrivilegedPodTestConfig{
  32. f: framework.NewDefaultFramework("e2e-privileged-pod"),
  33. privilegedPod: "privileged-pod",
  34. privilegedContainer: "privileged-container",
  35. notPrivilegedContainer: "not-privileged-container",
  36. }
  37. ginkgo.It("should enable privileged commands [LinuxOnly]", func() {
  38. // Windows does not support privileged containers.
  39. ginkgo.By("Creating a pod with a privileged container")
  40. config.createPods()
  41. ginkgo.By("Executing in the privileged container")
  42. config.run(config.privilegedContainer, true)
  43. ginkgo.By("Executing in the non-privileged container")
  44. config.run(config.notPrivilegedContainer, false)
  45. })
  46. })
  47. func (c *PrivilegedPodTestConfig) run(containerName string, expectSuccess bool) {
  48. cmd := []string{"ip", "link", "add", "dummy1", "type", "dummy"}
  49. reverseCmd := []string{"ip", "link", "del", "dummy1"}
  50. stdout, stderr, err := c.f.ExecCommandInContainerWithFullOutput(
  51. c.privilegedPod, containerName, cmd...)
  52. msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
  53. if expectSuccess {
  54. framework.ExpectNoError(err, msg)
  55. // We need to clean up the dummy link that was created, as it
  56. // leaks out into the node level -- yuck.
  57. _, _, err := c.f.ExecCommandInContainerWithFullOutput(
  58. c.privilegedPod, containerName, reverseCmd...)
  59. framework.ExpectNoError(err,
  60. fmt.Sprintf("could not remove dummy1 link: %v", err))
  61. } else {
  62. framework.ExpectError(err, msg)
  63. }
  64. }
  65. func (c *PrivilegedPodTestConfig) createPodsSpec() *v1.Pod {
  66. isPrivileged := true
  67. notPrivileged := false
  68. return &v1.Pod{
  69. ObjectMeta: metav1.ObjectMeta{
  70. Name: c.privilegedPod,
  71. Namespace: c.f.Namespace.Name,
  72. },
  73. Spec: v1.PodSpec{
  74. Containers: []v1.Container{
  75. {
  76. Name: c.privilegedContainer,
  77. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  78. ImagePullPolicy: v1.PullIfNotPresent,
  79. SecurityContext: &v1.SecurityContext{Privileged: &isPrivileged},
  80. Command: []string{"/bin/sleep", "10000"},
  81. },
  82. {
  83. Name: c.notPrivilegedContainer,
  84. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  85. ImagePullPolicy: v1.PullIfNotPresent,
  86. SecurityContext: &v1.SecurityContext{Privileged: &notPrivileged},
  87. Command: []string{"/bin/sleep", "10000"},
  88. },
  89. },
  90. },
  91. }
  92. }
  93. func (c *PrivilegedPodTestConfig) createPods() {
  94. podSpec := c.createPodsSpec()
  95. c.pod = c.f.PodClient().CreateSync(podSpec)
  96. }