test_docker_memory_check.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. from unittest import mock
  3. import pytest
  4. from tools import docker_memory_check
  5. @pytest.mark.parametrize(
  6. ("option", "expected"),
  7. (
  8. ("always", True),
  9. ("never", False),
  10. ),
  11. )
  12. def test_should_use_color_forced(option, expected):
  13. assert docker_memory_check.should_use_color(option) is expected
  14. def test_should_use_color_determined_by_CI_variable_missing():
  15. with mock.patch.dict(os.environ, clear=True):
  16. assert docker_memory_check.should_use_color("auto") is True
  17. def test_should_use_color_determined_by_CI_variable_empty():
  18. with mock.patch.dict(os.environ, {"CI": ""}):
  19. assert docker_memory_check.should_use_color("auto") is True
  20. def test_should_use_color_determined_by_CI_variable_present():
  21. with mock.patch.dict(os.environ, {"CI": ""}):
  22. assert docker_memory_check.should_use_color("1") is False
  23. def test_color_using_color():
  24. ret = docker_memory_check.color("hello hello", "\033[33m", use_color=True)
  25. assert ret == "\033[33mhello hello\033[m"
  26. def test_color_not_using_color():
  27. ret = docker_memory_check.color("hello hello", "\033[33m", use_color=False)
  28. assert ret == "hello hello"
  29. def test_check_ignored_file_does_not_exist(capsys, tmp_path):
  30. json_file = tmp_path.joinpath("settings.json")
  31. assert docker_memory_check.main(("--settings-file", str(json_file))) == 0
  32. out, err = capsys.readouterr()
  33. assert out == err == ""
  34. def test_check_ignored_file_is_not_json(capsys, tmp_path):
  35. json_file = tmp_path.joinpath("settings.json")
  36. json_file.write_text("not json")
  37. assert docker_memory_check.main(("--settings-file", str(json_file))) == 0
  38. out, err = capsys.readouterr()
  39. assert out == err == ""
  40. def test_check_ignored_file_missing_field(capsys, tmp_path):
  41. json_file = tmp_path.joinpath("settings.json")
  42. json_file.write_text("{}")
  43. assert docker_memory_check.main(("--settings-file", str(json_file))) == 0
  44. out, err = capsys.readouterr()
  45. assert out == err == ""
  46. def test_check_ignored_memory_limit_not_int(capsys, tmp_path):
  47. json_file = tmp_path.joinpath("settings.json")
  48. json_file.write_text('{"memoryMiB": "lots"}')
  49. assert docker_memory_check.main(("--settings-file", str(json_file))) == 0
  50. out, err = capsys.readouterr()
  51. assert out == err == ""
  52. def test_check_has_enough_configured_memory(capsys, tmp_path):
  53. json_file = tmp_path.joinpath("settings.json")
  54. json_file.write_text('{"memoryMiB": 9001}')
  55. args = ("--settings-file", str(json_file), "--memory-minimum", "8092")
  56. assert docker_memory_check.main(args) == 0
  57. out, err = capsys.readouterr()
  58. assert out == err == ""
  59. def test_check_insufficient_configured_memory(capsys, tmp_path):
  60. json_file = tmp_path.joinpath("settings.json")
  61. json_file.write_text('{"memoryMiB": 7000}')
  62. args = ("--settings-file", str(json_file), "--memory-minimum=8092", "--color=never")
  63. assert docker_memory_check.main(args) == 0
  64. out, err = capsys.readouterr()
  65. assert out == ""
  66. assert (
  67. err
  68. == """\
  69. WARNING: docker is configured with less than the recommended minimum memory!
  70. - open Docker.app and adjust the memory in Settings -> Resources
  71. - current memory (MiB): 7000
  72. - recommended minimum (MiB): 8092
  73. """
  74. )