def test_sticky_builds_affinity(): # Setup some mock objects for the response from the k8s API mock_k8s_api = _list_dind_pods_mock() build = Build(mock.MagicMock(), api=mock_k8s_api, name='test_build', namespace='build_namespace', repo_url=mock.MagicMock(), ref=mock.MagicMock(), build_image=mock.MagicMock(), image_name=mock.MagicMock(), push_secret=mock.MagicMock(), memory_limit=mock.MagicMock(), git_credentials=None, docker_host='http://mydockerregistry.local', node_selector=mock.MagicMock(), sticky_builds=True) affinity = build.get_affinity() assert isinstance(affinity, client.V1Affinity) assert affinity.node_affinity is not None assert affinity.pod_affinity is None assert affinity.pod_anti_affinity is None # One of the two nodes we have in our mock should be the preferred node assert affinity.node_affinity.preferred_during_scheduling_ignored_during_execution[ 0].preference.match_expressions[0].values[0] in ("node-a", "node-b")
def test_default_affinity(): # check that the default affinity is a pod anti-affinity mock_k8s_api = _list_dind_pods_mock() build = Build(mock.MagicMock(), api=mock_k8s_api, name='test_build', namespace='build_namespace', repo_url=mock.MagicMock(), ref=mock.MagicMock(), build_image=mock.MagicMock(), image_name=mock.MagicMock(), push_secret=mock.MagicMock(), memory_limit=mock.MagicMock(), git_credentials=None, docker_host='http://mydockerregistry.local', node_selector=mock.MagicMock()) affinity = build.get_affinity() assert isinstance(affinity, client.V1Affinity) assert affinity.node_affinity is None assert affinity.pod_affinity is None assert affinity.pod_anti_affinity is not None