-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_provision.py
117 lines (95 loc) · 3.32 KB
/
test_provision.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
from unittest import mock
import pytest
from hipercow import root
from hipercow.configure import configure
from hipercow.environment import environment_new
from hipercow.environment_engines import Pip
from hipercow.provision import provision, provision_history, provision_run
from hipercow.util import file_create
def test_provision_with_example_driver(tmp_path, mocker):
root.init(tmp_path)
r = root.open_root(tmp_path)
with (r.path / "requirements.txt").open("w") as f:
f.write("cowsay\n")
configure(r, "example")
mock_run = mock.MagicMock()
mocker.patch("subprocess.run", mock_run)
environment_new(r, "default", "pip")
provision(r, "default", [])
pr = Pip(r, "default")
venv_path = str(pr.path())
env = pr._envvars()
assert mock_run.call_count == 2
assert mock_run.mock_calls[0] == mock.call(
["python", "-m", "venv", venv_path],
env=os.environ,
check=True,
stderr=mock.ANY,
stdout=mock.ANY,
)
assert mock_run.mock_calls[1] == mock.call(
["pip", "install", "--verbose", "-r", "requirements.txt"],
env=os.environ | env,
check=True,
stderr=mock.ANY,
stdout=mock.ANY,
)
h = provision_history(r, "default")
assert len(h) == 1
assert h[0].result.error is None
id = h[0].data.id
with pytest.raises(Exception, match="has already been run"):
provision_run(r, "default", id)
r.path_provision_result("default", id).unlink()
h2 = provision_history(r, "default")
assert len(h) == 1
assert h2[0].result is None
def test_dont_create_on_second_provision(tmp_path, mocker):
root.init(tmp_path)
r = root.open_root(tmp_path)
file_create(r.path / "pyproject.toml")
configure(r, "example")
mock_run = mock.MagicMock()
mocker.patch("subprocess.run", mock_run)
environment_new(r, "default", "pip")
pr = Pip(r, "default")
pr.path().mkdir(parents=True)
provision(r, "default", [])
assert mock_run.call_count == 1
assert mock_run.mock_calls[0] == mock.call(
["pip", "install", "--verbose", "."],
env=os.environ | pr._envvars(),
check=True,
stderr=mock.ANY,
stdout=mock.ANY,
)
def test_record_provisioning_error(tmp_path, mocker):
root.init(tmp_path)
r = root.open_root(tmp_path)
file_create(r.path / "pyproject.toml")
configure(r, "example")
mock_run = mock.MagicMock(side_effect=Exception("some ghastly error"))
mocker.patch("subprocess.run", mock_run)
environment_new(r, "default", "pip")
pr = Pip(r, "default")
pr.path().mkdir(parents=True)
with pytest.raises(Exception, match="Provisioning failed"):
provision(r, "default", [])
assert mock_run.call_count == 1
assert mock_run.mock_calls[0] == mock.call(
["pip", "install", "--verbose", "."],
env=os.environ | pr._envvars(),
check=True,
stderr=mock.ANY,
stdout=mock.ANY,
)
h = provision_history(r, "default")
assert len(h) == 1
assert isinstance(h[0].result.error, Exception)
def test_throw_on_provision_if_no_environment(tmp_path):
root.init(tmp_path)
r = root.open_root(tmp_path)
configure(r, "example")
with pytest.raises(Exception, match="Environment 'default' does not exist"):
provision(r, "default", [])