server_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2018 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 podresources
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/stretchr/testify/mock"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/kubernetes/pkg/kubelet/apis/podresources/v1alpha1"
  22. )
  23. type mockProvider struct {
  24. mock.Mock
  25. }
  26. func (m *mockProvider) GetPods() []*v1.Pod {
  27. args := m.Called()
  28. return args.Get(0).([]*v1.Pod)
  29. }
  30. func (m *mockProvider) GetDevices(podUID, containerName string) []*v1alpha1.ContainerDevices {
  31. args := m.Called(podUID, containerName)
  32. return args.Get(0).([]*v1alpha1.ContainerDevices)
  33. }
  34. func (m *mockProvider) UpdateAllocatedDevices() {
  35. m.Called()
  36. }
  37. func TestListPodResources(t *testing.T) {
  38. podName := "pod-name"
  39. podNamespace := "pod-namespace"
  40. podUID := types.UID("pod-uid")
  41. containerName := "container-name"
  42. devs := []*v1alpha1.ContainerDevices{
  43. {
  44. ResourceName: "resource",
  45. DeviceIds: []string{"dev0", "dev1"},
  46. },
  47. }
  48. for _, tc := range []struct {
  49. desc string
  50. pods []*v1.Pod
  51. devices []*v1alpha1.ContainerDevices
  52. expectedResponse *v1alpha1.ListPodResourcesResponse
  53. }{
  54. {
  55. desc: "no pods",
  56. pods: []*v1.Pod{},
  57. devices: []*v1alpha1.ContainerDevices{},
  58. expectedResponse: &v1alpha1.ListPodResourcesResponse{},
  59. },
  60. {
  61. desc: "pod without devices",
  62. pods: []*v1.Pod{
  63. {
  64. ObjectMeta: metav1.ObjectMeta{
  65. Name: podName,
  66. Namespace: podNamespace,
  67. UID: podUID,
  68. },
  69. Spec: v1.PodSpec{
  70. Containers: []v1.Container{
  71. {
  72. Name: containerName,
  73. },
  74. },
  75. },
  76. },
  77. },
  78. devices: []*v1alpha1.ContainerDevices{},
  79. expectedResponse: &v1alpha1.ListPodResourcesResponse{
  80. PodResources: []*v1alpha1.PodResources{
  81. {
  82. Name: podName,
  83. Namespace: podNamespace,
  84. Containers: []*v1alpha1.ContainerResources{
  85. {
  86. Name: containerName,
  87. Devices: []*v1alpha1.ContainerDevices{},
  88. },
  89. },
  90. },
  91. },
  92. },
  93. },
  94. {
  95. desc: "pod with devices",
  96. pods: []*v1.Pod{
  97. {
  98. ObjectMeta: metav1.ObjectMeta{
  99. Name: podName,
  100. Namespace: podNamespace,
  101. UID: podUID,
  102. },
  103. Spec: v1.PodSpec{
  104. Containers: []v1.Container{
  105. {
  106. Name: containerName,
  107. },
  108. },
  109. },
  110. },
  111. },
  112. devices: devs,
  113. expectedResponse: &v1alpha1.ListPodResourcesResponse{
  114. PodResources: []*v1alpha1.PodResources{
  115. {
  116. Name: podName,
  117. Namespace: podNamespace,
  118. Containers: []*v1alpha1.ContainerResources{
  119. {
  120. Name: containerName,
  121. Devices: devs,
  122. },
  123. },
  124. },
  125. },
  126. },
  127. },
  128. } {
  129. t.Run(tc.desc, func(t *testing.T) {
  130. m := new(mockProvider)
  131. m.On("GetPods").Return(tc.pods)
  132. m.On("GetDevices", string(podUID), containerName).Return(tc.devices)
  133. m.On("UpdateAllocatedDevices").Return()
  134. server := NewPodResourcesServer(m, m)
  135. resp, err := server.List(context.TODO(), &v1alpha1.ListPodResourcesRequest{})
  136. if err != nil {
  137. t.Errorf("want err = %v, got %q", nil, err)
  138. }
  139. if tc.expectedResponse.String() != resp.String() {
  140. t.Errorf("want resp = %s, got %s", tc.expectedResponse.String(), resp.String())
  141. }
  142. })
  143. }
  144. }