Skip to content

Commit 3aa7cd9

Browse files
committed
Update mocks used to test sticky builds
1 parent c0cfcb0 commit 3aa7cd9

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

binderhub/tests/test_build.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,31 @@ async def test_build(app, needs_build, needs_launch, always_build, slug, pytestc
7171
assert r.url.startswith(final['url'])
7272

7373

74+
def _list_dind_pods_mock():
75+
"""Mock list of DIND pods"""
76+
mock_response = mock.MagicMock()
77+
mock_response.read.return_value = json.dumps(
78+
{
79+
"items": [
80+
{
81+
"spec": {"nodeName": name},
82+
}
83+
for name in ["node-a", "node-b"]
84+
]
85+
}
86+
)
87+
mock_k8s_api = mock.MagicMock()
88+
mock_k8s_api.list_namespaced_pod.return_value = mock_response
89+
return mock_k8s_api
90+
91+
7492
def test_default_affinity():
7593
# check that the default affinity is a pod anti-affinity
94+
95+
mock_k8s_api = _list_dind_pods_mock()
96+
7697
build = Build(
77-
mock.MagicMock(), api=mock.MagicMock(), name='test_build',
98+
mock.MagicMock(), api=mock_k8s_api, name='test_build',
7899
namespace='build_namespace', repo_url=mock.MagicMock(),
79100
ref=mock.MagicMock(), build_image=mock.MagicMock(),
80101
image_name=mock.MagicMock(), push_secret=mock.MagicMock(),
@@ -92,14 +113,7 @@ def test_default_affinity():
92113

93114
def test_sticky_builds_affinity():
94115
# Setup some mock objects for the response from the k8s API
95-
Pod = namedtuple("Pod", "spec")
96-
PodSpec = namedtuple("PodSpec", "node_name")
97-
PodList = namedtuple("PodList", "items")
98-
99-
mock_k8s_api = mock.MagicMock()
100-
mock_k8s_api.list_namespaced_pod.return_value = PodList(
101-
[Pod(PodSpec("node-a")), Pod(PodSpec("node-b"))],
102-
)
116+
mock_k8s_api = _list_dind_pods_mock()
103117

104118
build = Build(
105119
mock.MagicMock(), api=mock_k8s_api, name='test_build',
@@ -127,19 +141,21 @@ def test_git_credentials_passed_to_podspec_upon_submit():
127141
'client_id': 'my_username',
128142
'access_token': 'my_access_token',
129143
}
144+
145+
mock_k8s_api = _list_dind_pods_mock()
146+
130147
build = Build(
131-
mock.MagicMock(), api=mock.MagicMock(), name='test_build',
148+
mock.MagicMock(), api=mock_k8s_api, name='test_build',
132149
namespace='build_namespace', repo_url=mock.MagicMock(), ref=mock.MagicMock(),
133150
git_credentials=git_credentials, build_image=mock.MagicMock(),
134151
image_name=mock.MagicMock(), push_secret=mock.MagicMock(),
135152
memory_limit=mock.MagicMock(), docker_host='http://mydockerregistry.local',
136153
node_selector=mock.MagicMock())
137154

138-
with mock.patch.object(build, 'api') as api_patch, \
139-
mock.patch.object(build.stop_event, 'is_set', return_value=True):
155+
with mock.patch.object(build.stop_event, 'is_set', return_value=True):
140156
build.submit()
141157

142-
call_args_list = api_patch.create_namespaced_pod.call_args_list
158+
call_args_list = mock_k8s_api.create_namespaced_pod.call_args_list
143159
assert len(call_args_list) == 1
144160

145161
args = call_args_list[0][0]

0 commit comments

Comments
 (0)