kuberuntime_sandbox_test.go 3.6 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 kuberuntime
  14. import (
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. featuregatetesting "k8s.io/component-base/featuregate/testing"
  24. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  25. "k8s.io/kubernetes/pkg/features"
  26. containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  27. "k8s.io/kubernetes/pkg/kubelet/runtimeclass"
  28. rctest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing"
  29. "k8s.io/utils/pointer"
  30. )
  31. // TestCreatePodSandbox tests creating sandbox and its corresponding pod log directory.
  32. func TestCreatePodSandbox(t *testing.T) {
  33. fakeRuntime, _, m, err := createTestRuntimeManager()
  34. require.NoError(t, err)
  35. pod := newTestPod()
  36. fakeOS := m.osInterface.(*containertest.FakeOS)
  37. fakeOS.MkdirAllFn = func(path string, perm os.FileMode) error {
  38. // Check pod logs root directory is created.
  39. assert.Equal(t, filepath.Join(podLogsRootDirectory, pod.Namespace+"_"+pod.Name+"_12345678"), path)
  40. assert.Equal(t, os.FileMode(0755), perm)
  41. return nil
  42. }
  43. id, _, err := m.createPodSandbox(pod, 1)
  44. assert.NoError(t, err)
  45. assert.Contains(t, fakeRuntime.Called, "RunPodSandbox")
  46. sandboxes, err := fakeRuntime.ListPodSandbox(&runtimeapi.PodSandboxFilter{Id: id})
  47. assert.NoError(t, err)
  48. assert.Equal(t, len(sandboxes), 1)
  49. // TODO Check pod sandbox configuration
  50. }
  51. // TestCreatePodSandbox_RuntimeClass tests creating sandbox with RuntimeClasses enabled.
  52. func TestCreatePodSandbox_RuntimeClass(t *testing.T) {
  53. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RuntimeClass, true)()
  54. rcm := runtimeclass.NewManager(rctest.NewPopulatedClient())
  55. defer rctest.StartManagerSync(rcm)()
  56. fakeRuntime, _, m, err := createTestRuntimeManager()
  57. require.NoError(t, err)
  58. m.runtimeClassManager = rcm
  59. tests := map[string]struct {
  60. rcn *string
  61. expectedHandler string
  62. expectError bool
  63. }{
  64. "unspecified RuntimeClass": {rcn: nil, expectedHandler: ""},
  65. "valid RuntimeClass": {rcn: pointer.StringPtr(rctest.SandboxRuntimeClass), expectedHandler: rctest.SandboxRuntimeHandler},
  66. "missing RuntimeClass": {rcn: pointer.StringPtr("phantom"), expectError: true},
  67. }
  68. for name, test := range tests {
  69. t.Run(name, func(t *testing.T) {
  70. fakeRuntime.Called = []string{}
  71. pod := newTestPod()
  72. pod.Spec.RuntimeClassName = test.rcn
  73. id, _, err := m.createPodSandbox(pod, 1)
  74. if test.expectError {
  75. assert.Error(t, err)
  76. } else {
  77. assert.NoError(t, err)
  78. assert.Contains(t, fakeRuntime.Called, "RunPodSandbox")
  79. assert.Equal(t, test.expectedHandler, fakeRuntime.Sandboxes[id].RuntimeHandler)
  80. }
  81. })
  82. }
  83. }
  84. func newTestPod() *v1.Pod {
  85. return &v1.Pod{
  86. ObjectMeta: metav1.ObjectMeta{
  87. UID: "12345678",
  88. Name: "bar",
  89. Namespace: "new",
  90. },
  91. Spec: v1.PodSpec{
  92. Containers: []v1.Container{
  93. {
  94. Name: "foo",
  95. Image: "busybox",
  96. ImagePullPolicy: v1.PullIfNotPresent,
  97. },
  98. },
  99. },
  100. }
  101. }