test_device.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import pytest
  2. from render import Render
  3. @pytest.fixture
  4. def mock_values():
  5. return {
  6. "images": {
  7. "test_image": {
  8. "repository": "nginx",
  9. "tag": "latest",
  10. }
  11. },
  12. }
  13. def test_add_device(mock_values):
  14. render = Render(mock_values)
  15. c1 = render.add_container("test_container", "test_image")
  16. c1.healthcheck.disable()
  17. c1.devices.add_device("/h/dev/sda", "/c/dev/sda")
  18. c1.devices.add_device("/h/dev/sdb", "/c/dev/sdb", "rwm")
  19. output = render.render()
  20. assert output["services"]["test_container"]["devices"] == ["/h/dev/sda:/c/dev/sda", "/h/dev/sdb:/c/dev/sdb:rwm"]
  21. def test_devices_without_host(mock_values):
  22. render = Render(mock_values)
  23. c1 = render.add_container("test_container", "test_image")
  24. c1.healthcheck.disable()
  25. with pytest.raises(Exception):
  26. c1.devices.add_device("", "/c/dev/sda")
  27. def test_devices_without_container(mock_values):
  28. render = Render(mock_values)
  29. c1 = render.add_container("test_container", "test_image")
  30. c1.healthcheck.disable()
  31. with pytest.raises(Exception):
  32. c1.devices.add_device("/h/dev/sda", "")
  33. def test_add_duplicate_device(mock_values):
  34. render = Render(mock_values)
  35. c1 = render.add_container("test_container", "test_image")
  36. c1.healthcheck.disable()
  37. c1.devices.add_device("/h/dev/sda", "/c/dev/sda")
  38. with pytest.raises(Exception):
  39. c1.devices.add_device("/h/dev/sda", "/c/dev/sda")
  40. def test_add_device_with_invalid_container_path(mock_values):
  41. render = Render(mock_values)
  42. c1 = render.add_container("test_container", "test_image")
  43. c1.healthcheck.disable()
  44. with pytest.raises(Exception):
  45. c1.devices.add_device("/h/dev/sda", "c/dev/sda")
  46. def test_add_device_with_invalid_host_path(mock_values):
  47. render = Render(mock_values)
  48. c1 = render.add_container("test_container", "test_image")
  49. c1.healthcheck.disable()
  50. with pytest.raises(Exception):
  51. c1.devices.add_device("h/dev/sda", "/c/dev/sda")
  52. def test_add_disallowed_device(mock_values):
  53. render = Render(mock_values)
  54. c1 = render.add_container("test_container", "test_image")
  55. c1.healthcheck.disable()
  56. with pytest.raises(Exception):
  57. c1.devices.add_device("/dev/dri", "/c/dev/sda")
  58. def test_add_device_with_invalid_cgroup_perm(mock_values):
  59. render = Render(mock_values)
  60. c1 = render.add_container("test_container", "test_image")
  61. c1.healthcheck.disable()
  62. with pytest.raises(Exception):
  63. c1.devices.add_device("/h/dev/sda", "/c/dev/sda", "invalid")
  64. def test_automatically_add_gpu_devices(mock_values):
  65. mock_values["resources"] = {"gpus": {"use_all_gpus": True}}
  66. render = Render(mock_values)
  67. c1 = render.add_container("test_container", "test_image")
  68. c1.healthcheck.disable()
  69. output = render.render()
  70. assert output["services"]["test_container"]["devices"] == ["/dev/dri:/dev/dri"]
  71. assert output["services"]["test_container"]["group_add"] == [44, 107, 568]
  72. def test_automatically_add_gpu_devices_and_kfd(mock_values):
  73. mock_values["resources"] = {"gpus": {"use_all_gpus": True, "kfd_device_exists": True}}
  74. render = Render(mock_values)
  75. c1 = render.add_container("test_container", "test_image")
  76. c1.healthcheck.disable()
  77. output = render.render()
  78. assert output["services"]["test_container"]["devices"] == ["/dev/dri:/dev/dri", "/dev/kfd:/dev/kfd"]
  79. assert output["services"]["test_container"]["group_add"] == [44, 107, 568]
  80. def test_remove_gpu_devices(mock_values):
  81. mock_values["resources"] = {"gpus": {"use_all_gpus": True}}
  82. render = Render(mock_values)
  83. c1 = render.add_container("test_container", "test_image")
  84. c1.healthcheck.disable()
  85. c1.devices.remove_devices()
  86. output = render.render()
  87. assert "devices" not in output["services"]["test_container"]
  88. assert output["services"]["test_container"]["group_add"] == [568]
  89. def test_add_usb_bus(mock_values):
  90. render = Render(mock_values)
  91. c1 = render.add_container("test_container", "test_image")
  92. c1.healthcheck.disable()
  93. c1.devices.add_usb_bus()
  94. output = render.render()
  95. assert output["services"]["test_container"]["devices"] == ["/dev/bus/usb:/dev/bus/usb"]
  96. def test_add_usb_bus_disallowed(mock_values):
  97. render = Render(mock_values)
  98. c1 = render.add_container("test_container", "test_image")
  99. c1.healthcheck.disable()
  100. with pytest.raises(Exception):
  101. c1.devices.add_device("/dev/bus/usb", "/dev/bus/usb")
  102. def test_add_snd_device(mock_values):
  103. render = Render(mock_values)
  104. c1 = render.add_container("test_container", "test_image")
  105. c1.healthcheck.disable()
  106. c1.add_snd_device()
  107. output = render.render()
  108. assert output["services"]["test_container"]["devices"] == ["/dev/snd:/dev/snd"]
  109. assert output["services"]["test_container"]["group_add"] == [29, 568]
  110. def test_add_tun_device(mock_values):
  111. render = Render(mock_values)
  112. c1 = render.add_container("test_container", "test_image")
  113. c1.healthcheck.disable()
  114. c1.add_tun_device()
  115. output = render.render()
  116. assert output["services"]["test_container"]["devices"] == ["/dev/net/tun:/dev/net/tun"]