fake_mirror_client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2015 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. "sync"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. cp "k8s.io/kubernetes/pkg/kubelet/checkpoint"
  20. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  21. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  22. )
  23. type FakeMirrorClient struct {
  24. mirrorPodLock sync.RWMutex
  25. // Note that a real mirror manager does not store the mirror pods in
  26. // itself. This fake manager does this to track calls.
  27. mirrorPods sets.String
  28. createCounts map[string]int
  29. deleteCounts map[string]int
  30. }
  31. func NewFakeMirrorClient() *FakeMirrorClient {
  32. m := FakeMirrorClient{}
  33. m.mirrorPods = sets.NewString()
  34. m.createCounts = make(map[string]int)
  35. m.deleteCounts = make(map[string]int)
  36. return &m
  37. }
  38. func (fmc *FakeMirrorClient) CreateMirrorPod(pod *v1.Pod) error {
  39. fmc.mirrorPodLock.Lock()
  40. defer fmc.mirrorPodLock.Unlock()
  41. podFullName := kubecontainer.GetPodFullName(pod)
  42. fmc.mirrorPods.Insert(podFullName)
  43. fmc.createCounts[podFullName]++
  44. return nil
  45. }
  46. // TODO (Robert Krawitz): Implement UID checking
  47. func (fmc *FakeMirrorClient) DeleteMirrorPod(podFullName string, _ *types.UID) (bool, error) {
  48. fmc.mirrorPodLock.Lock()
  49. defer fmc.mirrorPodLock.Unlock()
  50. fmc.mirrorPods.Delete(podFullName)
  51. fmc.deleteCounts[podFullName]++
  52. return true, nil
  53. }
  54. func (fmc *FakeMirrorClient) HasPod(podFullName string) bool {
  55. fmc.mirrorPodLock.RLock()
  56. defer fmc.mirrorPodLock.RUnlock()
  57. return fmc.mirrorPods.Has(podFullName)
  58. }
  59. func (fmc *FakeMirrorClient) NumOfPods() int {
  60. fmc.mirrorPodLock.RLock()
  61. defer fmc.mirrorPodLock.RUnlock()
  62. return fmc.mirrorPods.Len()
  63. }
  64. func (fmc *FakeMirrorClient) GetPods() []string {
  65. fmc.mirrorPodLock.RLock()
  66. defer fmc.mirrorPodLock.RUnlock()
  67. return fmc.mirrorPods.List()
  68. }
  69. func (fmc *FakeMirrorClient) GetCounts(podFullName string) (int, int) {
  70. fmc.mirrorPodLock.RLock()
  71. defer fmc.mirrorPodLock.RUnlock()
  72. return fmc.createCounts[podFullName], fmc.deleteCounts[podFullName]
  73. }
  74. type MockCheckpointManager struct {
  75. checkpoint map[string]*cp.Data
  76. }
  77. func (ckm *MockCheckpointManager) CreateCheckpoint(checkpointKey string, checkpoint checkpointmanager.Checkpoint) error {
  78. ckm.checkpoint[checkpointKey] = (checkpoint.(*cp.Data))
  79. return nil
  80. }
  81. func (ckm *MockCheckpointManager) GetCheckpoint(checkpointKey string, checkpoint checkpointmanager.Checkpoint) error {
  82. *(checkpoint.(*cp.Data)) = *(ckm.checkpoint[checkpointKey])
  83. return nil
  84. }
  85. func (ckm *MockCheckpointManager) RemoveCheckpoint(checkpointKey string) error {
  86. _, ok := ckm.checkpoint[checkpointKey]
  87. if ok {
  88. delete(ckm.checkpoint, "moo")
  89. }
  90. return nil
  91. }
  92. func (ckm *MockCheckpointManager) ListCheckpoints() ([]string, error) {
  93. var keys []string
  94. for key := range ckm.checkpoint {
  95. keys = append(keys, key)
  96. }
  97. return keys, nil
  98. }
  99. func NewMockCheckpointManager() checkpointmanager.CheckpointManager {
  100. return &MockCheckpointManager{checkpoint: make(map[string]*cp.Data)}
  101. }