endpoint_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 devicemanager
  14. import (
  15. "path"
  16. "testing"
  17. "time"
  18. "github.com/stretchr/testify/require"
  19. pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
  20. )
  21. var (
  22. esocketName = "mock.sock"
  23. )
  24. func TestNewEndpoint(t *testing.T) {
  25. socket := path.Join("/tmp", esocketName)
  26. devs := []*pluginapi.Device{
  27. {ID: "ADeviceId", Health: pluginapi.Healthy},
  28. }
  29. p, e := esetup(t, devs, socket, "mock", func(n string, d []pluginapi.Device) {})
  30. defer ecleanup(t, p, e)
  31. }
  32. func TestRun(t *testing.T) {
  33. socket := path.Join("/tmp", esocketName)
  34. devs := []*pluginapi.Device{
  35. {ID: "ADeviceId", Health: pluginapi.Healthy},
  36. {ID: "AnotherDeviceId", Health: pluginapi.Healthy},
  37. {ID: "AThirdDeviceId", Health: pluginapi.Unhealthy},
  38. }
  39. updated := []*pluginapi.Device{
  40. {ID: "ADeviceId", Health: pluginapi.Unhealthy},
  41. {ID: "AThirdDeviceId", Health: pluginapi.Healthy},
  42. {ID: "AFourthDeviceId", Health: pluginapi.Healthy},
  43. }
  44. callbackCount := 0
  45. callbackChan := make(chan int)
  46. callback := func(n string, devices []pluginapi.Device) {
  47. // Should be called twice:
  48. // one for plugin registration, one for plugin update.
  49. if callbackCount > 2 {
  50. t.FailNow()
  51. }
  52. // Check plugin registration
  53. if callbackCount == 0 {
  54. require.Len(t, devices, 3)
  55. require.Equal(t, devices[0].ID, devs[0].ID)
  56. require.Equal(t, devices[1].ID, devs[1].ID)
  57. require.Equal(t, devices[2].ID, devs[2].ID)
  58. require.Equal(t, devices[0].Health, devs[0].Health)
  59. require.Equal(t, devices[1].Health, devs[1].Health)
  60. require.Equal(t, devices[2].Health, devs[2].Health)
  61. }
  62. // Check plugin update
  63. if callbackCount == 1 {
  64. require.Len(t, devices, 3)
  65. require.Equal(t, devices[0].ID, updated[0].ID)
  66. require.Equal(t, devices[1].ID, updated[1].ID)
  67. require.Equal(t, devices[2].ID, updated[2].ID)
  68. require.Equal(t, devices[0].Health, updated[0].Health)
  69. require.Equal(t, devices[1].Health, updated[1].Health)
  70. require.Equal(t, devices[2].Health, updated[2].Health)
  71. }
  72. callbackCount++
  73. callbackChan <- callbackCount
  74. }
  75. p, e := esetup(t, devs, socket, "mock", callback)
  76. defer ecleanup(t, p, e)
  77. go e.run()
  78. // Wait for the first callback to be issued.
  79. <-callbackChan
  80. p.Update(updated)
  81. // Wait for the second callback to be issued.
  82. <-callbackChan
  83. require.Equal(t, callbackCount, 2)
  84. }
  85. func TestAllocate(t *testing.T) {
  86. socket := path.Join("/tmp", esocketName)
  87. devs := []*pluginapi.Device{
  88. {ID: "ADeviceId", Health: pluginapi.Healthy},
  89. }
  90. callbackCount := 0
  91. callbackChan := make(chan int)
  92. p, e := esetup(t, devs, socket, "mock", func(n string, d []pluginapi.Device) {
  93. callbackCount++
  94. callbackChan <- callbackCount
  95. })
  96. defer ecleanup(t, p, e)
  97. resp := new(pluginapi.AllocateResponse)
  98. contResp := new(pluginapi.ContainerAllocateResponse)
  99. contResp.Devices = append(contResp.Devices, &pluginapi.DeviceSpec{
  100. ContainerPath: "/dev/aaa",
  101. HostPath: "/dev/aaa",
  102. Permissions: "mrw",
  103. })
  104. contResp.Devices = append(contResp.Devices, &pluginapi.DeviceSpec{
  105. ContainerPath: "/dev/bbb",
  106. HostPath: "/dev/bbb",
  107. Permissions: "mrw",
  108. })
  109. contResp.Mounts = append(contResp.Mounts, &pluginapi.Mount{
  110. ContainerPath: "/container_dir1/file1",
  111. HostPath: "host_dir1/file1",
  112. ReadOnly: true,
  113. })
  114. resp.ContainerResponses = append(resp.ContainerResponses, contResp)
  115. p.SetAllocFunc(func(r *pluginapi.AllocateRequest, devs map[string]pluginapi.Device) (*pluginapi.AllocateResponse, error) {
  116. return resp, nil
  117. })
  118. go e.run()
  119. // Wait for the callback to be issued.
  120. select {
  121. case <-callbackChan:
  122. break
  123. case <-time.After(time.Second):
  124. t.FailNow()
  125. }
  126. respOut, err := e.allocate([]string{"ADeviceId"})
  127. require.NoError(t, err)
  128. require.Equal(t, resp, respOut)
  129. }
  130. func esetup(t *testing.T, devs []*pluginapi.Device, socket, resourceName string, callback monitorCallback) (*Stub, *endpointImpl) {
  131. p := NewDevicePluginStub(devs, socket, resourceName, false)
  132. err := p.Start()
  133. require.NoError(t, err)
  134. e, err := newEndpointImpl(socket, resourceName, callback)
  135. require.NoError(t, err)
  136. return p, e
  137. }
  138. func ecleanup(t *testing.T, p *Stub, e *endpointImpl) {
  139. p.Stop()
  140. e.stop()
  141. }