Mock Python-On-Whales

huangapple go评论88阅读模式
英文:

Mock Python-On-Whales

问题

  1. check_container.py
  2. ```python
  3. from python_on_whales import docker
  4. class Container:
  5. def __init__(self, name):
  6. self.name = name
  7. def is_running(self):
  8. container_found = self.search_container()
  9. if container_found:
  10. for container in self.get_container_state():
  11. if container.state.status == "running":
  12. return True
  13. else:
  14. return False
  15. else:
  16. return False
  17. def search_container(self):
  18. return docker.ps(all, filters={"name": self.name})
  19. def get_container_state(self):
  20. return docker.ps(all, filters={"name": self.name})

test_check_container.py

  1. from unittest import TestCase, mock
  2. from unittest.mock import patch
  3. from src.check_container import Container
  4. class Test(TestCase):
  5. @patch("src.check_container.Container.search_container")
  6. @patch("src.check_container.Container.get_container_state")
  7. def test_check_container(self, mock_container_state, mock_container):
  8. mock_container.return_value = [{"id": '123456789', "name": 'busybox'}]
  9. mock_container_state.return_value = [{"status": "running"}]
  10. container = Container("busybox")
  11. container_running = container.is_running()
  12. assert container_running is True
  1. 你的代码中有一些HTML实体字符我已经去掉了它们以便代码能够正确运行希望这能帮助你解决问题
英文:

With a Pyhton script I want to determine, if a docker container is running. For this, I use python-on-whales. Something this way:

check_container.py

  1. from python_on_whales import docker
  2. class Container:
  3. def __init__(self, name):
  4. self.name = name
  5. def is_running(self):
  6. container_found = self.search_container()
  7. if container_found:
  8. for container in self.get_container_state():
  9. if container.state.status == "running":
  10. return True
  11. else:
  12. return False
  13. else:
  14. return False
  15. def search_container(self):
  16. return docker.ps(all, filters={"name": self.name})
  17. def get_container_state(self):
  18. return docker.ps(all, filters={"name": self.name})

Now, I want to test this in an unit test.

I tried different ways, but I didn't found a correct way. Mostly I got an error "Object is not iterable" or an assertion error (None instead True)

This is an example... one of may. Mock Python-On-Whales
test_check_container.py

  1. from unittest import TestCase, mock
  2. from unittest.mock import patch
  3. from src.check_container import Container
  4. class Test(TestCase):
  5. @patch("src.check_container.Container.search_container")
  6. @patch("src.check_container.Container.get_container_state")
  7. def test_check_container(self, mock_container_state, mock_container):
  8. mock_container.return_value = mock.Mock([{"id": '123456789', "name": 'busybox'}])
  9. mock_container_state.return_value = mock.MagicMock([{"status": "running"}])
  10. container = Container("busybox")
  11. container_running = container.is_running()
  12. assert container_running is True

Anyone an idea, how to mock the "docker.ps" calls and to solve my issue.

答案1

得分: 0

以下是翻译好的代码部分:

  1. have thought too far. With the following approach I was able to optimize the code and also make the test a bit easier.
  2. check_container.py
  3. from python_on_whales import docker
  4. class Container:
  5. def __init__(self, name):
  6. self.name = name
  7. def is_running(self):
  8. if docker.ps(all=True, filters={"name": self.name, "status": "running"}):
  9. return True
  10. else:
  11. return False
  12. test_check_container.py
  13. from unittest import TestCase
  14. from unittest.mock import patch
  15. from python_on_whales import docker
  16. from src.check_container import Container
  17. class TestDockerContainerRunning(TestCase):
  18. @patch("src.check_container.docker.ps", return_value=True)
  19. def test_container_running_mock(self, mock_container_state):
  20. assert Container("Foo").is_running() is True
  21. With this few line of codes it's possible to check if a Docker container is running.
  22. Another way is to run a Docker container for the test and remove it after:
  23. def test_container_running(self):
  24. with docker.run("busybox", ["sleep", "infinity"], detach=True, remove=True, name="busybox") as c:
  25. assert Container("busybox").is_running() is True
英文:

have thought too far. With the following approach I was able to optimize the code and also make the test a bit easier.

check_container.py

  1. from python_on_whales import docker
  2. class Container:
  3. def __init__(self, name):
  4. self.name = name
  5. def is_running(self):
  6. if docker.ps(all=True, filters={"name": self.name, "status": "running"}):
  7. return True
  8. else:
  9. return False

test_check_container.py

  1. from unittest import TestCase
  2. from unittest.mock import patch
  3. from python_on_whales import docker
  4. from src.check_container import Container
  5. class TestDockerContainerRunning(TestCase):
  6. @patch("src.check_container.docker.ps", return_value=True)
  7. def test_container_running_mock(self, mock_container_state):
  8. assert Container("Foo").is_running() is True

With this few line of codes it's possible to check if an Docker container is running.

Another way is to run a docker container for the test and remove it after:

  1. def test_container_running(self):
  2. with docker.run("busybox", ["sleep", "infinity"], detach=True, remove=True, name="busybox") as c:
  3. assert Container("busybox").is_running() is True

huangapple
  • 本文由 发表于 2023年2月14日 03:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定