Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.

Commit 0092cba

Browse files
authored
Merge pull request #12 from adfinis-sygroup/fix-hostname-issue
Added functionality for hostnamectl
2 parents 768f2c2 + 21fde8e commit 0092cba

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

virtesk-tc-connectspice/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def nmcli_device_show():
5252
yield readfile('nmcli_device_show')
5353

5454

55+
@pytest.yield_fixture(scope='module')
56+
def hostnamectl_transient():
57+
yield readfile('hostnamectl_transient')
58+
59+
@pytest.yield_fixture(scope='module')
60+
def hostnamectl_transient_empty():
61+
yield readfile('hostnamectl_transient_empty')
62+
5563
def readfile(filename):
5664
with open(os.path.join(MOCK_DATA_PATH, filename), 'r') as f:
5765
return f.read()

virtesk-tc-connectspice/find_thinclient_identifier_nmcli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@ def extract_identifiers_from_nmcli():
3838
dhcp_hostname = get_dhcp_hostname_from_connection(value)
3939
if dhcp_hostname:
4040
hostnames.append(dhcp_hostname)
41+
hostnamectl_hostname = extract_hostname_from_hostnamectl()
42+
if hostnamectl_hostname and hostnamectl_hostname not in hostnames:
43+
hostnames.append(hostnamectl_hostname)
4144
return (hostnames, fixedips)
4245

4346

47+
def extract_hostname_from_hostnamectl():
48+
hostnamectl_output = subprocess.check_output(['hostnamectl'], env={'LC_ALL': 'C'})
49+
for line in hostnamectl_output.splitlines():
50+
if re.match('^Transient', line):
51+
key, value = get_line_key_value(line)
52+
return value
53+
54+
4455
def get_dhcp_hostname_from_connection(name):
4556
if name and name != '--':
4657
nmcli_output = subprocess.check_output(['nmcli', 'con', 'show', name], env={'LC_ALL': 'C'})

virtesk-tc-connectspice/test_find_thinclient_identifier_nmcli.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import find_thinclient_identifier_nmcli as find_nmcli
22

33

4-
def test_extract_identifiers_from_nmcli(mock, nmcli_device_show, nmcli_con_show, nmcli_con_show_empty):
4+
def test_extract_identifiers_from_nmcli(mock, nmcli_device_show, nmcli_con_show, nmcli_con_show_empty, hostnamectl_transient):
55
def my_check_output(cmd, **kwargs):
66
if cmd[:4] == ['nmcli', 'con', 'show', 'ADSY-EAP']:
77
return nmcli_con_show
88
elif cmd[:3] == ['nmcli', 'con', 'show']:
99
return nmcli_con_show_empty
1010
elif cmd[:3] == ['nmcli', 'device', 'show']:
1111
return nmcli_device_show
12+
elif cmd[:1] == ['hostnamectl']:
13+
return hostnamectl_transient
1214
mock.patch('find_thinclient_identifier_nmcli.subprocess.check_output', my_check_output)
1315
return_value = find_nmcli.extract_identifiers_from_nmcli()
1416

15-
assert return_value == (['a-hostname-appeared'], ['172.17.0.1', '10.9.5.185', '127.0.0.1'])
17+
assert return_value == (['a-hostname-appeared', 'transient-hostname'], ['172.17.0.1', '10.9.5.185', '127.0.0.1'])
1618

1719

1820
def test_get_dhcp_hostname_from_connection(mock, nmcli_con_show):
@@ -46,3 +48,17 @@ def test_get_active_connections(mock, active_connection):
4648
patched_check_output.return_value = active_connection
4749
return_value = find_nmcli.get_active_connections()
4850
assert return_value == 2
51+
52+
53+
def test_get_hostname_from_hostnamectl(mock, hostnamectl_transient):
54+
patched_check_output = mock.patch('find_thinclient_identifier_nmcli.subprocess.check_output')
55+
patched_check_output.return_value = hostnamectl_transient
56+
return_value = find_nmcli.extract_hostname_from_hostnamectl()
57+
assert return_value == 'transient-hostname'
58+
59+
60+
def test_get_hostname_from_hostnamectl_empty(mock, hostnamectl_transient_empty):
61+
patched_check_output = mock.patch('find_thinclient_identifier_nmcli.subprocess.check_output')
62+
patched_check_output.return_value = hostnamectl_transient_empty
63+
return_value = find_nmcli.extract_hostname_from_hostnamectl()
64+
assert return_value == None
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Static hostname: static-hostname
2+
Transient hostname: transient-hostname
3+
Icon name: computer-laptop
4+
Chassis: laptop
5+
Machine ID: 11111111111111111111111111111111
6+
Boot ID: 11111111111111111111111111111111
7+
Operating System: Ubuntu
8+
Kernel: Linux 4.4.0-53-generic
9+
Architecture: x86-64
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Static hostname: static-hostname
2+
Icon name: computer-laptop
3+
Chassis: laptop
4+
Machine ID: 11111111111111111111111111111111
5+
Boot ID: 11111111111111111111111111111111
6+
Operating System: Ubuntu
7+
Kernel: Linux 4.4.0-53-generic
8+
Architecture: x86-64

0 commit comments

Comments
 (0)