pods_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // +build integration,!no-etcd
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package pods
  15. import (
  16. "fmt"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. "k8s.io/kubernetes/pkg/client/restclient"
  21. client "k8s.io/kubernetes/pkg/client/unversioned"
  22. "k8s.io/kubernetes/test/integration"
  23. "k8s.io/kubernetes/test/integration/framework"
  24. )
  25. func TestPodUpdateActiveDeadlineSeconds(t *testing.T) {
  26. _, s := framework.RunAMaster(nil)
  27. defer s.Close()
  28. ns := framework.CreateTestingNamespace("pod-activedeadline-update", s, t)
  29. defer framework.DeleteTestingNamespace(ns, s, t)
  30. client := client.NewOrDie(&restclient.Config{Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}})
  31. var (
  32. iZero = int64(0)
  33. i30 = int64(30)
  34. i60 = int64(60)
  35. iNeg = int64(-1)
  36. )
  37. prototypePod := func() *api.Pod {
  38. return &api.Pod{
  39. ObjectMeta: api.ObjectMeta{
  40. Name: "xxx",
  41. },
  42. Spec: api.PodSpec{
  43. Containers: []api.Container{
  44. {
  45. Name: "fake-name",
  46. Image: "fakeimage",
  47. },
  48. },
  49. },
  50. }
  51. }
  52. cases := []struct {
  53. name string
  54. original *int64
  55. update *int64
  56. valid bool
  57. }{
  58. {
  59. name: "no change, nil",
  60. original: nil,
  61. update: nil,
  62. valid: true,
  63. },
  64. {
  65. name: "no change, set",
  66. original: &i30,
  67. update: &i30,
  68. valid: true,
  69. },
  70. {
  71. name: "change to positive from nil",
  72. original: nil,
  73. update: &i60,
  74. valid: true,
  75. },
  76. {
  77. name: "change to smaller positive",
  78. original: &i60,
  79. update: &i30,
  80. valid: true,
  81. },
  82. {
  83. name: "change to larger positive",
  84. original: &i30,
  85. update: &i60,
  86. valid: false,
  87. },
  88. {
  89. name: "change to negative from positive",
  90. original: &i30,
  91. update: &iNeg,
  92. valid: false,
  93. },
  94. {
  95. name: "change to negative from nil",
  96. original: nil,
  97. update: &iNeg,
  98. valid: false,
  99. },
  100. // zero is not allowed, must be a positive integer
  101. {
  102. name: "change to zero from positive",
  103. original: &i30,
  104. update: &iZero,
  105. valid: false,
  106. },
  107. {
  108. name: "change to nil from positive",
  109. original: &i30,
  110. update: nil,
  111. valid: false,
  112. },
  113. }
  114. for i, tc := range cases {
  115. pod := prototypePod()
  116. pod.Spec.ActiveDeadlineSeconds = tc.original
  117. pod.ObjectMeta.Name = fmt.Sprintf("activedeadlineseconds-test-%v", i)
  118. if _, err := client.Pods(ns.Name).Create(pod); err != nil {
  119. t.Errorf("Failed to create pod: %v", err)
  120. }
  121. pod.Spec.ActiveDeadlineSeconds = tc.update
  122. _, err := client.Pods(ns.Name).Update(pod)
  123. if tc.valid && err != nil {
  124. t.Errorf("%v: failed to update pod: %v", tc.name, err)
  125. } else if !tc.valid && err == nil {
  126. t.Errorf("%v: unexpected allowed update to pod", tc.name)
  127. }
  128. integration.DeletePodOrErrorf(t, client, ns.Name, pod.Name)
  129. }
  130. }
  131. func TestPodReadOnlyFilesystem(t *testing.T) {
  132. _, s := framework.RunAMaster(nil)
  133. defer s.Close()
  134. isReadOnly := true
  135. ns := framework.CreateTestingNamespace("pod-readonly-root", s, t)
  136. defer framework.DeleteTestingNamespace(ns, s, t)
  137. client := client.NewOrDie(&restclient.Config{Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}})
  138. pod := &api.Pod{
  139. ObjectMeta: api.ObjectMeta{
  140. Name: "xxx",
  141. },
  142. Spec: api.PodSpec{
  143. Containers: []api.Container{
  144. {
  145. Name: "fake-name",
  146. Image: "fakeimage",
  147. SecurityContext: &api.SecurityContext{
  148. ReadOnlyRootFilesystem: &isReadOnly,
  149. },
  150. },
  151. },
  152. },
  153. }
  154. if _, err := client.Pods(ns.Name).Create(pod); err != nil {
  155. t.Errorf("Failed to create pod: %v", err)
  156. }
  157. integration.DeletePodOrErrorf(t, client, ns.Name, pod.Name)
  158. }