1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the Avahi export of MAAS."""
6
from __future__ import (
17
from collections import defaultdict
19
import maasserver.maasavahi
20
from maasserver.maasavahi import (
22
setup_maas_avahi_service,
24
from maasserver.models import Config
25
from maastesting.djangotestcase import DjangoTestCase
28
class MockZeroconfServiceFactory:
29
"""Factory used to track usage of the zeroconfservice module.
31
An instance is meant to be patched as
32
maasserver.maasavahi.ZeroconfService. It will register instances
33
created, as well as the parameters and methods called on each instance.
39
def __call__(self, *args, **kwargs):
40
mock = MockZeroconfService(*args, **kwargs)
41
self.instances.append(mock)
45
class MockZeroconfService:
47
def __init__(self, name, port, stype):
54
self.calls.append('publish')
57
self.calls.append('unpublish')
60
class TestMAASAvahiService(DjangoTestCase):
62
def setup_mock_avahi(self):
63
# Unregister other signals from Config, otherwise
64
# the one registered in urls.py, will interfere with these tests
66
Config.objects, '_config_changed_connections', defaultdict(set))
68
mock_avahi = MockZeroconfServiceFactory()
70
maasserver.maasavahi, 'ZeroconfService', mock_avahi)
73
def test_publish_exports_name_over_avahi(self):
74
mock_avahi = self.setup_mock_avahi()
75
service = MAASAvahiService()
76
Config.objects.set_config('maas_name', 'My Test')
78
# One ZeroconfService should have been created
79
self.assertEquals(1, len(mock_avahi.instances))
80
zeroconf = mock_avahi.instances[0]
81
self.assertEquals('My Test MAAS Server', zeroconf.name)
82
self.assertEquals(80, zeroconf.port)
83
self.assertEquals('_maas._tcp', zeroconf.stype)
86
self.assertEquals(['publish'], zeroconf.calls)
88
def test_publish_twice_unpublishes_first(self):
89
mock_avahi = self.setup_mock_avahi()
90
service = MAASAvahiService()
91
Config.objects.set_config('maas_name', 'My Test')
95
# Two ZeroconfService should have been created. The first
96
# should have been published, and unpublished,
97
# while the second one should have one publish call.
98
self.assertEquals(2, len(mock_avahi.instances))
100
['publish', 'unpublish'], mock_avahi.instances[0].calls)
102
['publish'], mock_avahi.instances[1].calls)
104
def test_setup_maas_avahi_service(self):
105
mock_avahi = self.setup_mock_avahi()
106
Config.objects.set_config('maas_name', 'First Name')
107
setup_maas_avahi_service()
109
# Name should have been published.
110
self.assertEquals(1, len(mock_avahi.instances))
112
'First Name MAAS Server', mock_avahi.instances[0].name)
114
Config.objects.set_config('maas_name', 'Second Name')
116
# A new publication should have occured.
117
self.assertEquals(2, len(mock_avahi.instances))
119
'Second Name MAAS Server', mock_avahi.instances[1].name)