Skip to content

Commit

Permalink
Merge branch 'jayennis22-master'. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo O Vieira committed Dec 19, 2014
2 parents acd4f79 + 1b6a428 commit 54d2fa2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
=========

1.3.1 (2014-12-19)
------------------

- Send update to DDNS only if IP address has changed

1.3.0 (2014-12-16)
------------------

Expand Down
2 changes: 1 addition & 1 deletion noipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

__title__ = "noipy"
__version_info__ = ('1', '3', '0')
__version_info__ = ('1', '3', '1')
__version__ = ".".join(__version_info__)
__author__ = "Pablo O Vieira"
__email__ = "email@povieira.com"
Expand Down
24 changes: 19 additions & 5 deletions noipy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import re
import getpass
import socket

try:
from . import dnsupdater
Expand Down Expand Up @@ -57,6 +58,15 @@ def get_ip():
return re.search(r'(\d{1,3}\.?){4}', content).group()


def get_dns_ip(dnsname):
"""Return the machine's current IP address in DNS.
"""
try:
return socket.gethostbyname(dnsname)
except:
return ""


def print_version():
print("== noipy DDNS updater tool v%s ==" % __version__)

Expand Down Expand Up @@ -138,12 +148,16 @@ def execute_update(args):
update_ddns = False

if update_ddns:
updater = provider_class(auth, args.hostname, updater_options)
ip_address = args.ip if args.ip else get_ip()
print("Updating hostname '%s' with IP address %s [provider: '%s']..."
% (args.hostname, ip_address, args.provider))
updater.update_dns(ip_address)
process_message = updater.status_message
if ip_address == get_dns_ip(args.hostname):
process_message = "No update required."
else:
updater = provider_class(auth, args.hostname, updater_options)
print("Updating hostname '%s' with IP address %s "
"[provider: '%s']..."
% (args.hostname, ip_address, args.provider))
updater.update_dns(ip_address)
process_message = updater.status_message

return exec_result, process_message

Expand Down
23 changes: 23 additions & 0 deletions test/test_noipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def test_duckdns_plugin(self):

def test_generic_plugin(self):
cmd_args = ['--provider', 'generic']

args = self.parser.parse_args(cmd_args)
result, status_message = main.execute_update(args)

self.assertTrue(result == main.EXECUTION_RESULT_NOK,
"An error should be flagged when --provider is "
"'generic' and --url is not specified")
Expand All @@ -102,8 +104,10 @@ def test_generic_plugin(self):
'--url', 'https://dynupdate.no-ip.com/nic/update',
'--provider', 'generic',
'-n', 'noipy.no-ip.org', self.test_ip]

args = self.parser.parse_args(cmd_args)
result, status_message = main.execute_update(args)

self.assertTrue(result == main.EXECUTION_RESULT_OK,
"Update with 'No-IP' using generic provider failed.")
self.assertTrue(status_message.startswith("ERROR:"),
Expand Down Expand Up @@ -187,6 +191,25 @@ def test_get_ip(self):

self.assertTrue(re.match(VALID_IP_REGEX, ip), 'get_ip() failed.')

def test_get_dns_ip(self):
ip = main.get_dns_ip('localhost')

self.assertTrue(ip == '127.0.0.1', 'get_dns_ip() failed.')

def test_unchanged_ip(self):
cmd_args = ['-u', 'username', '-p', 'password',
'--url', 'https://dynupdate.no-ip.com/nic/update',
'--provider', 'generic',
'-n', 'localhost', '127.0.0.1']

args = self.parser.parse_args(cmd_args)
result, status_message = main.execute_update(args)

self.assertTrue(result == main.EXECUTION_RESULT_OK,
"Update with unchanged IP failed.")
self.assertTrue(status_message == "No update required.",
"Status message should be 'No update required'")

def test_not_implemented_plugin(self):
auth = authinfo.ApiAuth('username', 'password')
hostname = "hostname"
Expand Down

0 comments on commit 54d2fa2

Please sign in to comment.