| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import os |
| 18 |
import re |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from cylc.flow.hostuserutil import ( |
| 23 |
get_fqdn_by_host, |
| 24 |
get_host, |
| 25 |
get_user, |
| 26 |
get_user_home, |
| 27 |
is_remote_host, |
| 28 |
is_remote_user |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
def test_is_remote_user_on_current_user(): |
| 33 |
"""is_remote_user with current user.""" |
| 34 |
assert not is_remote_user(None) |
| 35 |
assert not is_remote_user(os.getenv('USER')) |
| 36 |
|
| 37 |
|
| 38 |
def test_is_remote_host_on_localhost(monkeypatch): |
| 39 |
"""is_remote_host with localhost.""" |
| 40 |
assert not is_remote_host(None) |
| 41 |
assert not is_remote_host('localhost') |
| 42 |
assert not is_remote_host('localhost4.localhost42') |
| 43 |
assert not is_remote_host(os.getenv('HOSTNAME')) |
| 44 |
assert not is_remote_host(get_host()) |
| 45 |
|
| 46 |
|
| 47 |
def test_get_fqdn_by_host_on_bad_host(): |
| 48 |
"""get_fqdn_by_host bad host. |
| 49 |
|
| 50 |
Warning: |
| 51 |
This test can fail due to ISP/network configuration |
| 52 |
(for example ISP may reroute failed DNS to custom search page) |
| 53 |
e.g: https://www.virginmedia.com/help/advanced-network-error-search |
| 54 |
|
| 55 |
""" |
| 56 |
bad_host = 'nosuchhost.nosuchdomain.org' |
| 57 |
with pytest.raises(IOError) as exc: |
| 58 |
get_fqdn_by_host(bad_host) |
| 59 |
assert re.match( |
| 60 |
r"(\[Errno -2\] Name or service|" |
| 61 |
r"\[Errno 8\] nodename nor servname provided, or)" |
| 62 |
r" not known: '{}'".format(bad_host), |
| 63 |
str(exc.value) |
| 64 |
) |
| 65 |
assert exc.value.filename == bad_host |
| 66 |
|
| 67 |
|
| 68 |
def test_get_user(): |
| 69 |
"""get_user.""" |
| 70 |
assert os.getenv('USER') == get_user() |
| 71 |
|
| 72 |
|
| 73 |
def test_get_user_home(): |
| 74 |
"""get_user_home.""" |
| 75 |
assert os.getenv('HOME') == get_user_home() |