import unittest from command_lines.docker_command import capped class Capped(unittest.TestCase): def test_a_container_without_limits_gets_the_share_for_both(self): for given, expected in [ (["run", "--rm", "img", "true"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "--rm", "img", "true"]), (["-H", "unix:///d.sock", "run", "img"], ["-H", "unix:///d.sock", "run", "--memory", "6144m", "--memory-swap", "6144m", "img"]), (["container", "create", "img"], ["container", "create", "--memory", "6144m", "--memory-swap", "6144m", "img"]), (["--context", "run", "run", "img"], ["--context", "run", "run", "--memory", "6144m", "--memory-swap", "6144m", "img"]), (["run", "img", "python", "-m", "pytest"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "python", "-m", "pytest"]), (["run", "img", "grep", "-m", "1", "x"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "grep", "-m", "1", "x"]), (["run", "img", "git", "tag", "-m", "1.2.3"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "git", "tag", "-m", "1.2.3"]), (["run", "-m", "6143k", "img"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "-m", "6143k", "img"]), (["run", "img", "tool", "-m", "6"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "tool", "-m", "6"]), (["run", "img", "stress", "--memory", "2"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "stress", "--memory", "2"]), ]: with self.subTest(given): self.assertEqual(capped(given, "6144m"), expected) def test_a_container_s_own_limit_is_kept_and_sets_the_limit_it_lacks(self): for given, expected in [ (["run", "-m", "10g", "img"], ["run", "--memory-swap", "10g", "-m", "10g", "img"]), (["run", "-m", "6m", "img"], ["run", "--memory-swap", "6m", "-m", "6m", "img"]), (["run", "--memory=12g", "img"], ["run", "--memory-swap", "12g", "--memory=12g", "img"]), (["run", "--memory-swap", "2g", "img"], ["run", "--memory", "2g", "--memory-swap", "2g", "img"]), (["run", "-m", "4g", "--memory-swap", "-1", "img"], ["run", "--memory-swap", "4g", "-m", "4g", "img"]), (["run", "-m4g", "--memory-swap=0", "img", "python", "-m", "pytest"], ["run", "--memory-swap", "4g", "-m4g", "img", "python", "-m", "pytest"]), (["run", "img", "app", "-m", "512m"], ["run", "--memory-swap", "512m", "img", "app", "-m", "512m"]), ]: with self.subTest(given): self.assertEqual(capped(given, "6144m"), expected) def test_a_container_with_both_limits_or_another_docker_call_is_left_alone(self): for given in [["run", "-m", "0", "img"], ["run", "img", "grep", "-m", "0", "x"], ["run", "-m", "10g", "--memory-swap", "12g", "img"], ["ps"], ["build", "."], ["run", "--help"], ["compose", "run", "app"], ["exec", "box", "true"]]: with self.subTest(given): self.assertEqual(capped(given, "6144m"), given) if __name__ == "__main__": unittest.main()