|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2017-2022 - Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Test ``session`` commands.""" |
| 19 | + |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from renku.core.errors import ParameterError |
| 25 | +from renku.core.plugin.session import supported_session_providers |
| 26 | +from renku.core.session.docker import DockerSessionProvider |
| 27 | +from renku.core.session.session import session_list, session_start, session_stop |
| 28 | + |
| 29 | + |
| 30 | +def fake_start( |
| 31 | + self, |
| 32 | + image_name, |
| 33 | + project_name, |
| 34 | + config, |
| 35 | + client, |
| 36 | + cpu_request, |
| 37 | + mem_request, |
| 38 | + disk_request, |
| 39 | + gpu_request, |
| 40 | +): |
| 41 | + return "0xdeadbeef" |
| 42 | + |
| 43 | + |
| 44 | +def fake_stop(self, project_name, session_name, stop_all): |
| 45 | + if session_name == "missing_session": |
| 46 | + return False |
| 47 | + return True |
| 48 | + |
| 49 | + |
| 50 | +def fake_find_image(self, image_name, config): |
| 51 | + if image_name == "missing_image": |
| 52 | + return False |
| 53 | + return True |
| 54 | + |
| 55 | + |
| 56 | +def fake_build_image(self, image_descriptor, image_name, config): |
| 57 | + return |
| 58 | + |
| 59 | + |
| 60 | +def fake_session_list(self, project_name, config): |
| 61 | + return ["0xdeadbeef"] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("provider", ["docker"]) |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "parameters,result", |
| 67 | + [ |
| 68 | + ({}, "0xdeadbeef"), |
| 69 | + ({"image_name": "fixed_image"}, "0xdeadbeef"), |
| 70 | + ({"image_name": "missing_image"}, ParameterError), |
| 71 | + ], |
| 72 | +) |
| 73 | +@patch.multiple( |
| 74 | + DockerSessionProvider, session_start=fake_start, find_image=fake_find_image, build_image=fake_build_image |
| 75 | +) |
| 76 | +def test_session_start(run_shell, client, provider, parameters, result, client_database_injection_manager): |
| 77 | + provider_implementation = next(filter(lambda x: x[1] == provider, supported_session_providers()), None) |
| 78 | + assert provider_implementation is not None |
| 79 | + |
| 80 | + with client_database_injection_manager(client): |
| 81 | + if not isinstance(result, str) and issubclass(result, Exception): |
| 82 | + with pytest.raises(result): |
| 83 | + session_start(provider=provider, config_path=None, **parameters) |
| 84 | + else: |
| 85 | + assert session_start(provider=provider, config_path=None, **parameters) == result |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("provider", ["docker"]) |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "parameters,result", |
| 91 | + [ |
| 92 | + ({"session_name": "0xdeadbeef"}, None), |
| 93 | + ({"session_name": "0xdeadbeef", "stop_all": True}, None), |
| 94 | + ({"session_name": "missing_session"}, ParameterError), |
| 95 | + ], |
| 96 | +) |
| 97 | +@patch.object(DockerSessionProvider, "session_stop", fake_stop) |
| 98 | +def test_session_stop(run_shell, client, provider, parameters, result, client_database_injection_manager): |
| 99 | + provider_implementation = next(filter(lambda x: x[1] == provider, supported_session_providers()), None) |
| 100 | + assert provider_implementation is not None |
| 101 | + |
| 102 | + with client_database_injection_manager(client): |
| 103 | + if result is not None and issubclass(result, Exception): |
| 104 | + with pytest.raises(result): |
| 105 | + session_stop(provider=provider, **parameters) |
| 106 | + else: |
| 107 | + session_stop(provider=provider, **parameters) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize("provider,result", [("docker", ["0xdeadbeef"]), ("no_provider", ParameterError)]) |
| 111 | +@patch.object(DockerSessionProvider, "session_list", fake_session_list) |
| 112 | +def test_session_list(run_shell, client, provider, result, client_database_injection_manager): |
| 113 | + with client_database_injection_manager(client): |
| 114 | + if not isinstance(result, list) and issubclass(result, Exception): |
| 115 | + with pytest.raises(result): |
| 116 | + session_list(provider=provider, config_path=None) |
| 117 | + else: |
| 118 | + assert session_list(provider=provider, config_path=None) == result |
0 commit comments