Resubmit: Use SystemdStrategy for Debian in the hostname module (#76929)

* add DebianStrategy tests
* ensure hostname can be changed by using become
* use Systemd strat for debian and Base for generic.
* add test to ensure all strategies are available

Signed-off-by: Thomas Sjögren <konstruktoid@users.noreply.github.com>
pull/76995/head
Thomas Sjögren 4 years ago committed by GitHub
parent 522f9d1050
commit b145732973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- hostname - Fix Debian strategy KeyError, use `SystemdStrategy`
(https://github.com/ansible/ansible/issues/76124)

@ -1,3 +1,4 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2013, Hiroaki Nakamura <hnakamur@gmail.com>
@ -83,9 +84,9 @@ from ansible.module_utils.six import PY3, text_type
STRATS = {
'alpine': 'Alpine',
'debian': 'Debian',
'debian': 'Systemd',
'freebsd': 'FreeBSD',
'generic': 'Generic',
'generic': 'Base',
'macos': 'Darwin',
'macosx': 'Darwin',
'darwin': 'Darwin',

@ -0,0 +1,20 @@
---
- name: Test DebianStrategy by setting hostname
become: 'yes'
hostname:
use: debian
name: "{{ ansible_distribution_release }}-bebop.ansible.example.com"
- name: Test DebianStrategy by getting current hostname
command: hostname
register: get_hostname
- name: Test DebianStrategy by verifying /etc/hostname content
command: grep -v '^#' /etc/hostname
register: grep_hostname
- name: Test DebianStrategy using assertions
assert:
that:
- "'{{ ansible_distribution_release }}-bebop.ansible.example.com' in get_hostname.stdout"
- "'{{ ansible_distribution_release }}-bebop.ansible.example.com' in grep_hostname.stdout"

@ -30,6 +30,7 @@
always:
# Reset back to original hostname
- name: Move back original file if it existed
become: 'yes'
command: mv -f {{ _hostname_file }}.orig {{ _hostname_file }}
when: hn_stat.stat.exists | default(False)
@ -40,6 +41,7 @@
when: not hn_stat.stat.exists | default(True)
- name: Reset back to original hostname
become: 'yes'
hostname:
name: "{{ original.stdout }}"
register: revert

@ -1,4 +1,5 @@
- name: Run hostname module for real now
become: 'yes'
hostname:
name: crocodile.ansible.test.doesthiswork.net.example.com
register: hn2
@ -8,6 +9,7 @@
register: current_after_hn2
- name: Run hostname again to ensure it does not change
become: 'yes'
hostname:
name: crocodile.ansible.test.doesthiswork.net.example.com
register: hn3

@ -38,6 +38,19 @@ class TestHostname(ModuleTestCase):
m.return_value.write.called,
msg='%s called write, should not have' % str(cls))
def test_all_named_strategies_exist(self):
"""Loop through the STRATS and see if anything is missing."""
for _name, prefix in hostname.STRATS.items():
classname = "%sStrategy" % prefix
cls = getattr(hostname, classname, None)
if cls is None:
self.assertFalse(
cls is None, "%s is None, should be a subclass" % classname
)
else:
self.assertTrue(issubclass(cls, hostname.BaseStrategy))
class TestRedhatStrategy(ModuleTestCase):
def setUp(self):

Loading…
Cancel
Save