testing_helper.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2017 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 predicates
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. storagev1 "k8s.io/api/storage/v1"
  18. )
  19. // FakePersistentVolumeClaimInfo declares a []v1.PersistentVolumeClaim type for testing.
  20. type FakePersistentVolumeClaimInfo []v1.PersistentVolumeClaim
  21. // GetPersistentVolumeClaimInfo gets PVC matching the namespace and PVC ID.
  22. func (pvcs FakePersistentVolumeClaimInfo) GetPersistentVolumeClaimInfo(namespace string, pvcID string) (*v1.PersistentVolumeClaim, error) {
  23. for _, pvc := range pvcs {
  24. if pvc.Name == pvcID && pvc.Namespace == namespace {
  25. return &pvc, nil
  26. }
  27. }
  28. return nil, fmt.Errorf("Unable to find persistent volume claim: %s/%s", namespace, pvcID)
  29. }
  30. // FakeNodeInfo declares a v1.Node type for testing.
  31. type FakeNodeInfo v1.Node
  32. // GetNodeInfo return a fake node info object.
  33. func (n FakeNodeInfo) GetNodeInfo(nodeName string) (*v1.Node, error) {
  34. node := v1.Node(n)
  35. return &node, nil
  36. }
  37. // FakeNodeListInfo declares a []v1.Node type for testing.
  38. type FakeNodeListInfo []v1.Node
  39. // GetNodeInfo returns a fake node object in the fake nodes.
  40. func (nodes FakeNodeListInfo) GetNodeInfo(nodeName string) (*v1.Node, error) {
  41. for _, node := range nodes {
  42. if node.Name == nodeName {
  43. return &node, nil
  44. }
  45. }
  46. return nil, fmt.Errorf("Unable to find node: %s", nodeName)
  47. }
  48. // FakePersistentVolumeInfo declares a []v1.PersistentVolume type for testing.
  49. type FakePersistentVolumeInfo []v1.PersistentVolume
  50. // GetPersistentVolumeInfo returns a fake PV object in the fake PVs by PV ID.
  51. func (pvs FakePersistentVolumeInfo) GetPersistentVolumeInfo(pvID string) (*v1.PersistentVolume, error) {
  52. for _, pv := range pvs {
  53. if pv.Name == pvID {
  54. return &pv, nil
  55. }
  56. }
  57. return nil, fmt.Errorf("Unable to find persistent volume: %s", pvID)
  58. }
  59. // FakeStorageClassInfo declares a []storagev1.StorageClass type for testing.
  60. type FakeStorageClassInfo []storagev1.StorageClass
  61. // GetStorageClassInfo returns a fake storage class object in the fake storage classes by name.
  62. func (classes FakeStorageClassInfo) GetStorageClassInfo(name string) (*storagev1.StorageClass, error) {
  63. for _, sc := range classes {
  64. if sc.Name == name {
  65. return &sc, nil
  66. }
  67. }
  68. return nil, fmt.Errorf("Unable to find storage class: %s", name)
  69. }