Skip to content

Commit

Permalink
wakeonlan: Add integration tests and improvements (ansible#26254)
Browse files Browse the repository at this point in the history
This PR includes:
- Checkmode improvements
- Integration tests
- A fix for python3
- PEP8 fixes

This backports improvements from the win_wakeonlan module.
  • Loading branch information
dagwieers authored and gundalow committed Jul 7, 2017
1 parent 9a9b1db commit f8982dc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
49 changes: 25 additions & 24 deletions lib/ansible/modules/remote_management/wakeonlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
DOCUMENTATION = r'''
---
module: wakeonlan
version_added: '2.2'
Expand All @@ -43,18 +42,19 @@
description:
- UDP port to use for magic Wake-on-LAN packet.
default: 7
author: "Dag Wieers (@dagwieers)"
author:
- Dag Wieers (@dagwieers)
todo:
- Add arping support to check whether the system is up (before and after)
- Enable check-mode support (when we have arping support)
- Does not have SecureOn password support
notes:
- This module sends a magic packet, without knowing whether it worked
- Only works if the target system was properly configured for Wake-on-LAN (in the BIOS and/or the OS)
- Some BIOSes have a different (configurable) Wake-on-LAN boot order (i.e. PXE first) when turned off
- Some BIOSes have a different (configurable) Wake-on-LAN boot order (i.e. PXE first).
'''

EXAMPLES = '''
EXAMPLES = r'''
- name: Send a magic Wake-on-LAN packet to 00:00:5E:00:53:66
wakeonlan:
mac: '00:00:5E:00:53:66'
Expand All @@ -67,7 +67,7 @@
delegate_to: localhost
'''

RETURN='''
RETURN = r'''
# Default return values
'''

Expand Down Expand Up @@ -97,39 +97,40 @@ def wakeonlan(module, mac, broadcast, port):
module.fail_json(msg="Incorrect MAC address format: %s" % mac_orig)

# Create payload for magic packet
data = ''
data = b''
padding = ''.join(['FFFFFFFFFFFF', mac * 20])
for i in range(0, len(padding), 2):
data = ''.join([data, struct.pack('B', int(padding[i: i + 2], 16))])
data = b''.join([data, struct.pack('B', int(padding[i: i + 2], 16))])

# Broadcast payload to network
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
try:
sock.sendto(data, (broadcast, port))
except socket.error:
e = get_exception()
if not module.check_mode:

# Broadcast payload to network
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
try:
sock.sendto(data, (broadcast, port))
except socket.error:
e = get_exception()
sock.close()
module.fail_json(msg=str(e))
sock.close()
module.fail_json(msg=str(e))
sock.close()


def main():
module = AnsibleModule(
argument_spec = dict(
mac = dict(type='str', required=True),
broadcast = dict(type='str', default='255.255.255.255'),
port = dict(type='int', default=7),
argument_spec=dict(
mac=dict(type='str', required=True),
broadcast=dict(type='str', default='255.255.255.255'),
port=dict(type='int', default=7),
),
supports_check_mode = True,
supports_check_mode=True,
)

mac = module.params['mac']
broadcast = module.params['broadcast']
port = module.params['port']

if not module.check_mode:
wakeonlan(module, mac, broadcast, port)
wakeonlan(module, mac, broadcast, port)

module.exit_json(changed=True)

Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/wakeonlan/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
posix/ci/group2
9 changes: 9 additions & 0 deletions test/integration/targets/wakeonlan/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- name: Send a magic Wake-on-LAN packet to 00:00:5E:00:53:66
wakeonlan:
mac: 00:00:5E:00:53:66
broadcast: 192.0.2.255

- name: Send a magic Wake-on-LAN packet on port 9 to 00-00-5E-00-53-66
wakeonlan:
mac: 00-00-5E-00-53-66
port: 9
1 change: 0 additions & 1 deletion test/sanity/pep8/legacy-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ lib/ansible/modules/remote_management/hpilo/hpilo_boot.py
lib/ansible/modules/remote_management/hpilo/hpilo_facts.py
lib/ansible/modules/remote_management/hpilo/hponcfg.py
lib/ansible/modules/remote_management/stacki/stacki_host.py
lib/ansible/modules/remote_management/wakeonlan.py
lib/ansible/modules/source_control/bzr.py
lib/ansible/modules/source_control/gitlab_group.py
lib/ansible/modules/source_control/gitlab_project.py
Expand Down

0 comments on commit f8982dc

Please sign in to comment.