0

I have a service that I'm publishing with avahi that could run on different machines.

And I have some code, that detecting and getting IP of that machines, and then starts using this service.

I wan't to automate tests, so I need to somehow fake several machines so they would have different IP addresses when avahi resolves them.

Is this possible?

class ReceiversManager(object):

    def __init__(self, name, on_message_callback):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        self.name = name
        self.on_message_callback = on_message_callback
        self.service_detector = AvahiServiceDetector(self.name, self.on_new_system)
        self.receivers = []
        self.addresses = []
        self.logger.setLevel(logging.DEBUG)

    def on_new_system(self, name, address, port):
        if not address in self.addresses:
            self.addresses.append(address)
            self.receivers.append(self.create_new_receiver(address, port))

    def create_new_receiver(self, address, port):
        self.logger.info('new address {}, new receiver created'.format(address))
        requester = ZmqReq(host='tcp://' + address, port=5559)
        id = self.create_id()
        reply = requester.execute(ArchiveCreator.CREATE_QUEUES_REQUEST + ' ' + id)
        recent_queue_name = ArchiveManager.create_queue_name(ArchiveManager.default_recent_queue_name, id)
        return DirectRabbitReceiver(host=address, queue_name=recent_queue_name,
                             start_immediately=True, on_receive_callback=self.on_message_callback)

    @staticmethod
    def create_id():
        return uuid.uuid5(uuid.NAMESPACE_DNS, ReceiversManager.get_baseboard_id() + ReceiversManager.get_disk_id()).hex

    @staticmethod
    def get_baseboard_id():
        info = dmidecode.QuerySection('baseboard')
        return info[info.keys()[1]]['data']['Serial Number']

    @staticmethod
    def get_disk_id():
        result = subprocess.check_output(['hdparm', '-I', '/dev/sda']).split()
        return result[result.index('Serial') + 2]

class AvahiServicePublisher():

    def __init__(self):
        self.group = None
        bus = dbus.SystemBus()
        server = dbus.Interface(
            bus.get_object(
                avahi.DBUS_NAME,
                avahi.DBUS_PATH_SERVER),
            avahi.DBUS_INTERFACE_SERVER)

        self.group = dbus.Interface(
            bus.get_object(avahi.DBUS_NAME,
                           server.EntryGroupNew()),
            avahi.DBUS_INTERFACE_ENTRY_GROUP)

    def publish(self, name, port, stype="_http._tcp", domain="", host="", text=""):
        self.group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                              name, stype, domain, host,
                              dbus.UInt16(port), text)

        self.group.Commit()

    def unpublish(self):
        self.group.Reset()
user1685095
  • 5,787
  • 9
  • 51
  • 100
  • [Mock](http://en.wikipedia.org/wiki/Mock_object) your interface to avahi? – 2rs2ts Feb 13 '14 at 15:39
  • I've added some code. Could you be more specific on how should I do that? I'm new to testing. – user1685095 Feb 13 '14 at 15:45
  • Which part are you testing? In general, if you are using mock tests, you would create "dummy" objects for everything but the part of the program you are testing. The "dummy" objects would take expected input and create expected output (hardcoded). If they received unexpected input, they can raise a warning and fail the test - and if the part of the program you are testing doesn't do what it's supposed to when the mocks give it the proper output, the test also fails. That's the general idea anyway. It helps you test only one thing. – 2rs2ts Feb 13 '14 at 15:56
  • I wan't to test ReceiversManager basically. It should see arbitraty number of machines in the network, that published the service through avahi. – user1685095 Feb 13 '14 at 16:04
  • @user1685095 I wish you'd stop putting an apostrophe in "want". It makes it look like "won't". – Nicu Stiurca Feb 13 '14 at 16:07
  • Oh, I noticed you're using `id` as a variable name. Don't do that, it's an internal function. – 2rs2ts Feb 13 '14 at 16:20
  • So which of the following classes are your own? `ZmqReq`, `ArchiveCreator`, `ArchiveManager`, `DirectRabbitReceiver`? – 2rs2ts Feb 13 '14 at 16:21
  • Sorry for "wan't". That's just my fingers error =) All of this classes have been written by me. All of them have their own tests. Probably what I wan't is not unit test actually, but functional test. – user1685095 Feb 13 '14 at 16:30

0 Answers0