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
def test_git_credentials_passed_to_podspec_upon_submit(): git_credentials = { 'client_id': 'my_username', 'access_token': 'my_access_token', } build = Build( mock.MagicMock(), api=mock.MagicMock(), name='test_build', namespace='build_namespace', repo_url=mock.MagicMock(), ref=mock.MagicMock(), git_credentials=git_credentials, builder_image=mock.MagicMock(), image_name=mock.MagicMock(), push_secret=mock.MagicMock(), memory_limit=mock.MagicMock(), docker_host='http://mydockerregistry.local', node_selector=mock.MagicMock()) with mock.patch.object(build, 'api') as api_patch, \ mock.patch.object(build.stop_event, 'is_set', return_value=True): build.submit() call_args_list = api_patch.create_namespaced_pod.call_args_list assert len(call_args_list) == 1 args = call_args_list[0][0] pod = args[1] assert len(pod.spec.containers) == 1 env = { env_var.name: env_var.value for env_var in pod.spec.containers[0].env } assert env['GIT_CREDENTIAL_ENV'] == git_credentials
def test_weko3_hosts_passed_to_podspec_upon_submit(): weko3_hosts = [{ 'hostname': ['https://test.jp'], 'file_base_url': 'https://test.jp/api/files', }] optional_envs = {'WEKO3_HOSTS_JSON': json.dumps(weko3_hosts)} 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(), git_credentials=None, optional_envs=optional_envs, build_image=mock.MagicMock(), image_name=mock.MagicMock(), push_secret=mock.MagicMock(), memory_limit=mock.MagicMock(), docker_host='http://mydockerregistry.local', node_selector=mock.MagicMock()) with mock.patch.object(build.stop_event, 'is_set', return_value=True): build.submit() call_args_list = mock_k8s_api.create_namespaced_pod.call_args_list assert len(call_args_list) == 1 args = call_args_list[0][0] pod = args[1] assert len(pod.spec.containers) == 1 env = { env_var.name: env_var.value for env_var in pod.spec.containers[0].env } assert json.loads(env['WEKO3_HOSTS_JSON']) == weko3_hosts
def test_git_credentials_passed_to_podspec_upon_submit(): git_credentials = """{ "client_id": "my_username", "access_token": "my_access_token", }""" mock_k8s_api = _list_dind_pods_mock() build = Build( mock.MagicMock(), api=mock_k8s_api, name="test_build", namespace="build_namespace", repo_url="repo", ref="ref", build_image="image", image_name="name", push_secret="", memory_limit=0, git_credentials=git_credentials, docker_host="http://mydockerregistry.local", node_selector={}, ) with mock.patch.object(build.stop_event, "is_set", return_value=True): build.submit() call_args_list = mock_k8s_api.create_namespaced_pod.call_args_list assert len(call_args_list) == 1 args = call_args_list[0][0] pod = args[1] assert len(pod.spec.containers) == 1 env = { env_var.name: env_var.value for env_var in pod.spec.containers[0].env } assert env["GIT_CREDENTIAL_ENV"] == git_credentials