Coverage for tests/subprocess_mock.py: 66%
67 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 09:04 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-30 09:04 +0000
1import random
2import subprocess
3from typing import NamedTuple
6class ProcMock(NamedTuple):
7 returncode: int
8 stdout: str
11def sha():
12 return hex(random.getrandbits(128))
15__rev_parse__ = sha()
16__hash_object__ = sha()
17__mktree = sha()
18__commit_tree = sha()
19__update_ref__ = sha()
22def check_output(cmd, **_):
23 text = ""
24 # repos.git
25 if "git diff" in cmd:
26 text = "some\nfiles\nthat\nwere\nchanged"
27 elif "git remote -v" in cmd and "grep https" in cmd:
28 text = "https://fake_remote.subprocessmock.com/fake_owner/fake_repo.git"
29 elif "git branch" in cmd and "grep" in cmd:
30 text = "subprocess_mock_fake_branch"
31 elif "rev-parse HEAD" in cmd:
32 text = __rev_parse__
33 elif "git log -1" in cmd:
34 text = "some_fake_git_commit_message\nfrom_subprocess_mock"
35 # jci
36 elif "cat /proc/self/cgroup" in cmd: 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true
37 text = "fake_pipe_id_from_subprocess_mock"
38 # remotes.git
39 elif "git hash-object" in cmd:
40 text = __hash_object__
41 elif "git mktree" in cmd: 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true
42 text = __mktree
43 elif "git commit-tree" in cmd: 43 ↛ 44line 43 didn't jump to line 44, because the condition on line 43 was never true
44 text = __commit_tree
45 elif "git update-ref" in cmd: 45 ↛ 47line 45 didn't jump to line 47, because the condition on line 45 was never false
46 text = __update_ref__
47 return text.encode()
50networks = {}
51names = {}
52containers = {}
55def cid(short=False):
56 n_chars = 12 if short else 64
57 return random.sample("0123456789abcdef" * 10, n_chars)
60def run(cmd, **_):
61 code, text = 0, ""
62 if "docker network create" in cmd: 62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true
63 name = cmd.split()[-1]
64 networks[name] = True
65 elif "docker network ls" in cmd: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true
66 name = cmd.split("grep")[1]
67 if name in networks:
68 text = f"{cid(short=True)} {name} bridge local"
69 else:
70 code = 1
71 elif "docker network rm" in cmd: 71 ↛ 72line 71 didn't jump to line 72, because the condition on line 71 was never true
72 name = text = cmd.split(" rm ")[1].split("|")[0].strip()
73 if name not in networks:
74 text = "No such net"
75 elif "docker stop -t 1" in cmd: 75 ↛ 76line 75 didn't jump to line 76, because the condition on line 75 was never true
76 name = text = cmd.split()[-1]
77 if name not in containers and name not in names:
78 cmd = 1
79 text = f"Error response from daemon: No such container: {name}"
80 elif "docker run -d" in cmd: 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true
81 name = cmd.split("--name")[1].strip().split()[0]
82 containers[name] = text = cid()
83 return ProcMock(returncode=code, stdout=text.encode())
86subprocess.check_output = check_output
87subprocess.run = run