testvolumespec.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 testing
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  18. "k8s.io/kubernetes/pkg/client/testing/core"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/types"
  21. "k8s.io/kubernetes/pkg/volume"
  22. "k8s.io/kubernetes/pkg/watch"
  23. )
  24. // GetTestVolumeSpec returns a test volume spec
  25. func GetTestVolumeSpec(volumeName string, diskName api.UniqueVolumeName) *volume.Spec {
  26. return &volume.Spec{
  27. Volume: &api.Volume{
  28. Name: volumeName,
  29. VolumeSource: api.VolumeSource{
  30. GCEPersistentDisk: &api.GCEPersistentDiskVolumeSource{
  31. PDName: string(diskName),
  32. FSType: "fake",
  33. ReadOnly: false,
  34. },
  35. },
  36. },
  37. }
  38. }
  39. func CreateTestClient() *fake.Clientset {
  40. fakeClient := &fake.Clientset{}
  41. fakeClient.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
  42. obj := &api.PodList{}
  43. podNamePrefix := "mypod"
  44. namespace := "mynamespace"
  45. for i := 0; i < 5; i++ {
  46. podName := fmt.Sprintf("%s-%d", podNamePrefix, i)
  47. pod := api.Pod{
  48. Status: api.PodStatus{
  49. Phase: api.PodRunning,
  50. },
  51. ObjectMeta: api.ObjectMeta{
  52. Name: podName,
  53. Namespace: namespace,
  54. Labels: map[string]string{
  55. "name": podName,
  56. },
  57. },
  58. Spec: api.PodSpec{
  59. Containers: []api.Container{
  60. {
  61. Name: "containerName",
  62. Image: "containerImage",
  63. VolumeMounts: []api.VolumeMount{
  64. {
  65. Name: "volumeMountName",
  66. ReadOnly: false,
  67. MountPath: "/mnt",
  68. },
  69. },
  70. },
  71. },
  72. Volumes: []api.Volume{
  73. {
  74. Name: "volumeName",
  75. VolumeSource: api.VolumeSource{
  76. GCEPersistentDisk: &api.GCEPersistentDiskVolumeSource{
  77. PDName: "pdName",
  78. FSType: "ext4",
  79. ReadOnly: false,
  80. },
  81. },
  82. },
  83. },
  84. },
  85. }
  86. obj.Items = append(obj.Items, pod)
  87. }
  88. return true, obj, nil
  89. })
  90. fakeWatch := watch.NewFake()
  91. fakeClient.AddWatchReactor("*", core.DefaultWatchReactor(fakeWatch, nil))
  92. return fakeClient
  93. }
  94. // NewPod returns a test pod object
  95. func NewPod(uid, name string) *api.Pod {
  96. return &api.Pod{
  97. ObjectMeta: api.ObjectMeta{
  98. UID: types.UID(uid),
  99. Name: name,
  100. Namespace: name,
  101. },
  102. }
  103. }