runtime_mock.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "context"
  16. "io"
  17. "time"
  18. "github.com/stretchr/testify/mock"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/client-go/tools/remotecommand"
  22. "k8s.io/client-go/util/flowcontrol"
  23. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  24. . "k8s.io/kubernetes/pkg/kubelet/container"
  25. "k8s.io/kubernetes/pkg/volume"
  26. )
  27. type Mock struct {
  28. mock.Mock
  29. }
  30. var _ Runtime = new(Mock)
  31. func (r *Mock) Start() error {
  32. args := r.Called()
  33. return args.Error(0)
  34. }
  35. func (r *Mock) Type() string {
  36. args := r.Called()
  37. return args.Get(0).(string)
  38. }
  39. func (r *Mock) Version() (Version, error) {
  40. args := r.Called()
  41. return args.Get(0).(Version), args.Error(1)
  42. }
  43. func (r *Mock) APIVersion() (Version, error) {
  44. args := r.Called()
  45. return args.Get(0).(Version), args.Error(1)
  46. }
  47. func (r *Mock) Status() (*RuntimeStatus, error) {
  48. args := r.Called()
  49. return args.Get(0).(*RuntimeStatus), args.Error(0)
  50. }
  51. func (r *Mock) GetPods(all bool) ([]*Pod, error) {
  52. args := r.Called(all)
  53. return args.Get(0).([]*Pod), args.Error(1)
  54. }
  55. func (r *Mock) SyncPod(pod *v1.Pod, status *PodStatus, secrets []v1.Secret, backOff *flowcontrol.Backoff) PodSyncResult {
  56. args := r.Called(pod, status, secrets, backOff)
  57. return args.Get(0).(PodSyncResult)
  58. }
  59. func (r *Mock) KillPod(pod *v1.Pod, runningPod Pod, gracePeriodOverride *int64) error {
  60. args := r.Called(pod, runningPod, gracePeriodOverride)
  61. return args.Error(0)
  62. }
  63. func (r *Mock) RunContainerInPod(container v1.Container, pod *v1.Pod, volumeMap map[string]volume.VolumePlugin) error {
  64. args := r.Called(pod, pod, volumeMap)
  65. return args.Error(0)
  66. }
  67. func (r *Mock) KillContainerInPod(container v1.Container, pod *v1.Pod) error {
  68. args := r.Called(pod, pod)
  69. return args.Error(0)
  70. }
  71. func (r *Mock) GetPodStatus(uid types.UID, name, namespace string) (*PodStatus, error) {
  72. args := r.Called(uid, name, namespace)
  73. return args.Get(0).(*PodStatus), args.Error(1)
  74. }
  75. func (r *Mock) ExecInContainer(containerID ContainerID, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize, timeout time.Duration) error {
  76. args := r.Called(containerID, cmd, stdin, stdout, stderr, tty)
  77. return args.Error(0)
  78. }
  79. func (r *Mock) AttachContainer(containerID ContainerID, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize) error {
  80. args := r.Called(containerID, stdin, stdout, stderr, tty)
  81. return args.Error(0)
  82. }
  83. func (r *Mock) GetContainerLogs(_ context.Context, pod *v1.Pod, containerID ContainerID, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) (err error) {
  84. args := r.Called(pod, containerID, logOptions, stdout, stderr)
  85. return args.Error(0)
  86. }
  87. func (r *Mock) PullImage(image ImageSpec, pullSecrets []v1.Secret, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
  88. args := r.Called(image, pullSecrets)
  89. return image.Image, args.Error(0)
  90. }
  91. func (r *Mock) GetImageRef(image ImageSpec) (string, error) {
  92. args := r.Called(image)
  93. return args.Get(0).(string), args.Error(1)
  94. }
  95. func (r *Mock) ListImages() ([]Image, error) {
  96. args := r.Called()
  97. return args.Get(0).([]Image), args.Error(1)
  98. }
  99. func (r *Mock) RemoveImage(image ImageSpec) error {
  100. args := r.Called(image)
  101. return args.Error(0)
  102. }
  103. func (r *Mock) PortForward(pod *Pod, port uint16, stream io.ReadWriteCloser) error {
  104. args := r.Called(pod, port, stream)
  105. return args.Error(0)
  106. }
  107. func (r *Mock) GarbageCollect(gcPolicy ContainerGCPolicy, ready bool, evictNonDeletedPods bool) error {
  108. args := r.Called(gcPolicy, ready, evictNonDeletedPods)
  109. return args.Error(0)
  110. }
  111. func (r *Mock) DeleteContainer(containerID ContainerID) error {
  112. args := r.Called(containerID)
  113. return args.Error(0)
  114. }
  115. func (r *Mock) ImageStats() (*ImageStats, error) {
  116. args := r.Called()
  117. return args.Get(0).(*ImageStats), args.Error(1)
  118. }
  119. // UpdatePodCIDR fulfills the cri interface.
  120. func (r *Mock) UpdatePodCIDR(c string) error {
  121. return nil
  122. }