| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import tempfile |
| 6 |
import unittest |
| 7 |
|
| 8 |
from unittest.mock import patch, MagicMock, mock_open |
| 9 |
|
| 10 |
from ansible.module_utils.common._utils import get_all_subclasses |
| 11 |
from ansible.modules import hostname |
| 12 |
|
| 13 |
|
| 14 |
class TestHostname: |
| 15 |
@patch('os.path.isfile') |
| 16 |
def test_stategy_get_never_writes_in_check_mode(self, isfile, set_module_args): |
| 17 |
isfile.return_value = True |
| 18 |
|
| 19 |
set_module_args({'name': 'fooname', '_ansible_check_mode': True}) |
| 20 |
subclasses = get_all_subclasses(hostname.BaseStrategy) |
| 21 |
module = MagicMock() |
| 22 |
for cls in subclasses: |
| 23 |
instance = cls(module) |
| 24 |
|
| 25 |
instance.module.run_command = MagicMock() |
| 26 |
instance.module.run_command.return_value = (0, '', '') |
| 27 |
|
| 28 |
m = mock_open() |
| 29 |
builtins = 'builtins' |
| 30 |
with patch('%s.open' % builtins, m): |
| 31 |
instance.get_permanent_hostname() |
| 32 |
instance.get_current_hostname() |
| 33 |
assert not m.return_value.write.called |
| 34 |
|
| 35 |
def test_all_named_strategies_exist(self): |
| 36 |
"""Loop through the STRATS and see if anything is missing.""" |
| 37 |
for _name, prefix in hostname.STRATS.items(): |
| 38 |
classname = "%sStrategy" % prefix |
| 39 |
cls = getattr(hostname, classname, None) |
| 40 |
|
| 41 |
assert cls is not None |
| 42 |
|
| 43 |
assert issubclass(cls, hostname.BaseStrategy) |
| 44 |
|
| 45 |
|
| 46 |
class TestRedhatStrategy(unittest.TestCase): |
| 47 |
def setUp(self): |
| 48 |
super(TestRedhatStrategy, self).setUp() |
| 49 |
self.testdir = tempfile.mkdtemp(prefix='ansible-test-hostname-') |
| 50 |
self.network_file = os.path.join(self.testdir, "network") |
| 51 |
|
| 52 |
def tearDown(self): |
| 53 |
super(TestRedhatStrategy, self).tearDown() |
| 54 |
shutil.rmtree(self.testdir, ignore_errors=True) |
| 55 |
|
| 56 |
@property |
| 57 |
def instance(self): |
| 58 |
self.module = MagicMock() |
| 59 |
instance = hostname.RedHatStrategy(self.module) |
| 60 |
instance.NETWORK_FILE = self.network_file |
| 61 |
return instance |
| 62 |
|
| 63 |
def test_get_permanent_hostname_missing(self): |
| 64 |
self.assertIsNone(self.instance.get_permanent_hostname()) |
| 65 |
self.assertTrue(self.module.fail_json.called) |
| 66 |
self.module.fail_json.assert_called_with( |
| 67 |
"Unable to locate HOSTNAME entry in %s" % self.network_file |
| 68 |
) |
| 69 |
|
| 70 |
def test_get_permanent_hostname_line_missing(self): |
| 71 |
with open(self.network_file, "w") as f: |
| 72 |
f.write("# some other content\n") |
| 73 |
self.assertIsNone(self.instance.get_permanent_hostname()) |
| 74 |
self.module.fail_json.assert_called_with( |
| 75 |
"Unable to locate HOSTNAME entry in %s" % self.network_file |
| 76 |
) |
| 77 |
|
| 78 |
def test_get_permanent_hostname_existing(self): |
| 79 |
with open(self.network_file, "w") as f: |
| 80 |
f.write( |
| 81 |
"some other content\n" |
| 82 |
"HOSTNAME=foobar\n" |
| 83 |
"more content\n" |
| 84 |
) |
| 85 |
self.assertEqual(self.instance.get_permanent_hostname(), "foobar") |
| 86 |
|
| 87 |
def test_get_permanent_hostname_existing_whitespace(self): |
| 88 |
with open(self.network_file, "w") as f: |
| 89 |
f.write( |
| 90 |
"some other content\n" |
| 91 |
" HOSTNAME=foobar \n" |
| 92 |
"more content\n" |
| 93 |
) |
| 94 |
self.assertEqual(self.instance.get_permanent_hostname(), "foobar") |
| 95 |
|
| 96 |
def test_set_permanent_hostname_missing(self): |
| 97 |
self.instance.set_permanent_hostname("foobar") |
| 98 |
with open(self.network_file) as f: |
| 99 |
self.assertEqual(f.read(), "HOSTNAME=foobar\n") |
| 100 |
|
| 101 |
def test_set_permanent_hostname_line_missing(self): |
| 102 |
with open(self.network_file, "w") as f: |
| 103 |
f.write("# some other content\n") |
| 104 |
self.instance.set_permanent_hostname("foobar") |
| 105 |
with open(self.network_file) as f: |
| 106 |
self.assertEqual(f.read(), "# some other content\nHOSTNAME=foobar\n") |
| 107 |
|
| 108 |
def test_set_permanent_hostname_existing(self): |
| 109 |
with open(self.network_file, "w") as f: |
| 110 |
f.write( |
| 111 |
"some other content\n" |
| 112 |
"HOSTNAME=spam\n" |
| 113 |
"more content\n" |
| 114 |
) |
| 115 |
self.instance.set_permanent_hostname("foobar") |
| 116 |
with open(self.network_file) as f: |
| 117 |
self.assertEqual( |
| 118 |
f.read(), |
| 119 |
"some other content\n" |
| 120 |
"HOSTNAME=foobar\n" |
| 121 |
"more content\n" |
| 122 |
) |
| 123 |
|
| 124 |
def test_set_permanent_hostname_existing_whitespace(self): |
| 125 |
with open(self.network_file, "w") as f: |
| 126 |
f.write( |
| 127 |
"some other content\n" |
| 128 |
" HOSTNAME=spam \n" |
| 129 |
"more content\n" |
| 130 |
) |
| 131 |
self.instance.set_permanent_hostname("foobar") |
| 132 |
with open(self.network_file) as f: |
| 133 |
self.assertEqual( |
| 134 |
f.read(), |
| 135 |
"some other content\n" |
| 136 |
"HOSTNAME=foobar\n" |
| 137 |
"more content\n" |
| 138 |
) |