# Path: lighthouse/node.py
# class Node(object):
#     """
#     The class representing a member node of a cluster.
# 
#     Consists of a `port`, a `host` and a `peer`, plus methods for serializing
#     and deserializing themselves so that they can be transmitted back and
#     forth via discovery methods.
#     """
# 
#     def __init__(self, host, ip, port, peer=None, metadata=None):
#         self.port = port
#         self.host = host
#         self.ip = ip
#         self.peer = peer or Peer.current()
#         self.metadata = metadata or {}
# 
#     @property
#     def name(self):
#         """
#         Simple property for "naming" a node via the host and port.
#         """
#         return self.host + ":" + str(self.port)
# 
#     @classmethod
#     def current(cls, service, port):
#         """
#         Returns a Node instance representing the current service node.
# 
#         Collects the host and IP information for the current machine and
#         the port information from the given service.
#         """
#         host = socket.getfqdn()
#         return cls(
#             host=host,
#             ip=socket.gethostbyname(host),
#             port=port,
#             metadata=service.metadata
#         )
# 
#     def serialize(self):
#         """
#         Serializes the node data as a JSON map string.
#         """
#         return json.dumps({
#             "port": self.port,
#             "ip": self.ip,
#             "host": self.host,
#             "peer": self.peer.serialize() if self.peer else None,
#             "metadata": json.dumps(self.metadata or {}, sort_keys=True),
#         }, sort_keys=True)
# 
#     @classmethod
#     def deserialize(cls, value):
#         """
#         Creates a new Node instance via a JSON map string.
# 
#         Note that `port` and `ip` and are required keys for the JSON map,
#         `peer` and `host` are optional.  If `peer` is not present, the new Node
#         instance will use the current peer.  If `host` is not present, the
#         hostname of the given `ip` is looked up.
#         """
#         if getattr(value, "decode", None):
#             value = value.decode()
# 
#         logger.debug("Deserializing node data: '%s'", value)
#         parsed = json.loads(value)
# 
#         if "port" not in parsed:
#             raise ValueError("No port defined for node.")
#         if "ip" not in parsed:
#             raise ValueError("No IP address defined for node.")
#         if "host" not in parsed:
#             host, aliases, ip_list = socket.gethostbyaddr(parsed["ip"])
#             parsed["host"] = socket.get_fqdn(host)
#         if "peer" in parsed:
#             peer = Peer.deserialize(parsed["peer"])
#         else:
#             peer = None
# 
#         return cls(
#             parsed["host"], parsed["ip"], parsed["port"],
#             peer=peer, metadata=parsed.get("metadata")
#         )
# 
#     def __eq__(self, other):
#         """
#         Nodes are considered equal if their IPs and ports both match.
#         """
#         return bool(self.ip == other.ip and self.port == other.port)
# 
# Path: lighthouse/peer.py
# class Peer(object):
#     """
#     This class represents a host running a lighthouse reporter.
# 
#     When a reporter script tells its discovery method that a node is up, it
#     includes information about itself via this class so that writer scripts
#     reading that information can coordinate their peers.
# 
#     This is helpful for HAProxy as a way to generate "peers" config stanzas
#     so instances of HAProxy in a given cluster can share stick-table data.
#     """
# 
#     def __init__(self, name, ip, port=None):
#         self.name = name
#         self.ip = ip
#         self.port = port or DEFAULT_PEER_PORT
# 
#     @classmethod
#     def current(cls):
#         """
#         Helper method for getting the current peer of whichever host we're
#         running on.
#         """
#         name = socket.getfqdn()
#         ip = socket.gethostbyname(name)
# 
#         return cls(name, ip)
# 
#     def serialize(self):
#         """
#         Serializes the Peer data as a simple JSON map string.
#         """
#         return json.dumps({
#             "name": self.name,
#             "ip": self.ip,
#             "port": self.port
#         }, sort_keys=True)
# 
#     @classmethod
#     def deserialize(cls, value):
#         """
#         Generates a Peer instance via a JSON string of the sort generated
#         by `Peer.deserialize`.
# 
#         The `name` and `ip` keys are required to be present in the JSON map,
#         if the `port` key is not present the default is used.
#         """
#         parsed = json.loads(value)
# 
#         if "name" not in parsed:
#             raise ValueError("No peer name.")
#         if "ip" not in parsed:
#             raise ValueError("No peer IP.")
#         if "port" not in parsed:
#             parsed["port"] = DEFAULT_PEER_PORT
# 
#         return cls(parsed["name"], parsed["ip"], parsed["port"])
# 
#     def __hash__(self):
#         """
#         Hash method used to store peers in sets.
# 
#         Simply hashes the string <ip address>:<port>.
#         """
#         return hash(self.ip + ":" + str(self.port))
# 
#     def __eq__(self, other):
#         """
#         Peers are considered equal if their IP and port match.
#         """
#         return self.ip == other.ip and self.port == other.port


import json
    import unittest2 as unittest
    import unittest
from mock import patch
from lighthouse.node import Node
from lighthouse.peer import Peer
try:
except ImportError:




class NodeTests(unittest.TestCase):

    def test_constructor(self):

# Path: lighthouse/node.py
# class Node(object):
#     """
#     The class representing a member node of a cluster.
# 
#     Consists of a `port`, a `host` and a `peer`, plus methods for serializing
#     and deserializing themselves so that they can be transmitted back and
#     forth via discovery methods.
#     """
# 
#     def __init__(self, host, ip, port, peer=None, metadata=None):
#         self.port = port
#         self.host = host
#         self.ip = ip
#         self.peer = peer or Peer.current()
#         self.metadata = metadata or {}
# 
#     @property
#     def name(self):
#         """
#         Simple property for "naming" a node via the host and port.
#         """
#         return self.host + ":" + str(self.port)
# 
#     @classmethod
#     def current(cls, service, port):
#         """
#         Returns a Node instance representing the current service node.
# 
#         Collects the host and IP information for the current machine and
#         the port information from the given service.
#         """
#         host = socket.getfqdn()
#         return cls(
#             host=host,
#             ip=socket.gethostbyname(host),
#             port=port,
#             metadata=service.metadata
#         )
# 
#     def serialize(self):
#         """
#         Serializes the node data as a JSON map string.
#         """
#         return json.dumps({
#             "port": self.port,
#             "ip": self.ip,
#             "host": self.host,
#             "peer": self.peer.serialize() if self.peer else None,
#             "metadata": json.dumps(self.metadata or {}, sort_keys=True),
#         }, sort_keys=True)
# 
#     @classmethod
#     def deserialize(cls, value):
#         """
#         Creates a new Node instance via a JSON map string.
# 
#         Note that `port` and `ip` and are required keys for the JSON map,
#         `peer` and `host` are optional.  If `peer` is not present, the new Node
#         instance will use the current peer.  If `host` is not present, the
#         hostname of the given `ip` is looked up.
#         """
#         if getattr(value, "decode", None):
#             value = value.decode()
# 
#         logger.debug("Deserializing node data: '%s'", value)
#         parsed = json.loads(value)
# 
#         if "port" not in parsed:
#             raise ValueError("No port defined for node.")
#         if "ip" not in parsed:
#             raise ValueError("No IP address defined for node.")
#         if "host" not in parsed:
#             host, aliases, ip_list = socket.gethostbyaddr(parsed["ip"])
#             parsed["host"] = socket.get_fqdn(host)
#         if "peer" in parsed:
#             peer = Peer.deserialize(parsed["peer"])
#         else:
#             peer = None
# 
#         return cls(
#             parsed["host"], parsed["ip"], parsed["port"],
#             peer=peer, metadata=parsed.get("metadata")
#         )
# 
#     def __eq__(self, other):
#         """
#         Nodes are considered equal if their IPs and ports both match.
#         """
#         return bool(self.ip == other.ip and self.port == other.port)
# 
# Path: lighthouse/peer.py
# class Peer(object):
#     """
#     This class represents a host running a lighthouse reporter.
# 
#     When a reporter script tells its discovery method that a node is up, it
#     includes information about itself via this class so that writer scripts
#     reading that information can coordinate their peers.
# 
#     This is helpful for HAProxy as a way to generate "peers" config stanzas
#     so instances of HAProxy in a given cluster can share stick-table data.
#     """
# 
#     def __init__(self, name, ip, port=None):
#         self.name = name
#         self.ip = ip
#         self.port = port or DEFAULT_PEER_PORT
# 
#     @classmethod
#     def current(cls):
#         """
#         Helper method for getting the current peer of whichever host we're
#         running on.
#         """
#         name = socket.getfqdn()
#         ip = socket.gethostbyname(name)
# 
#         return cls(name, ip)
# 
#     def serialize(self):
#         """
#         Serializes the Peer data as a simple JSON map string.
#         """
#         return json.dumps({
#             "name": self.name,
#             "ip": self.ip,
#             "port": self.port
#         }, sort_keys=True)
# 
#     @classmethod
#     def deserialize(cls, value):
#         """
#         Generates a Peer instance via a JSON string of the sort generated
#         by `Peer.deserialize`.
# 
#         The `name` and `ip` keys are required to be present in the JSON map,
#         if the `port` key is not present the default is used.
#         """
#         parsed = json.loads(value)
# 
#         if "name" not in parsed:
#             raise ValueError("No peer name.")
#         if "ip" not in parsed:
#             raise ValueError("No peer IP.")
#         if "port" not in parsed:
#             parsed["port"] = DEFAULT_PEER_PORT
# 
#         return cls(parsed["name"], parsed["ip"], parsed["port"])
# 
#     def __hash__(self):
#         """
#         Hash method used to store peers in sets.
# 
#         Simply hashes the string <ip address>:<port>.
#         """
#         return hash(self.ip + ":" + str(self.port))
# 
#     def __eq__(self, other):
#         """
#         Peers are considered equal if their IP and port match.
#         """
#         return self.ip == other.ip and self.port == other.port


# Path: tests/node_tests.py
import json
    import unittest2 as unittest
    import unittest
from mock import patch
from lighthouse.node import Node
from lighthouse.peer import Peer

try:
except ImportError:




class NodeTests(unittest.TestCase):

    def test_constructor(self):

        peer = Peer("cluster04", 8888)