~newell-jensen/maas/1.5-bug-1373580

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_maasavahi.py

  • Committer: Gavin Panella
  • Date: 2013-11-14 15:05:03 UTC
  • mto: This revision was merged to the branch mainline in revision 1828.
  • Revision ID: gavin.panella@canonical.com-20131114150503-flg0tanjhngpl3rh
Purge Avahi, Zeroconf, and DBUS from MAAS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2012 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Tests for the Avahi export of MAAS."""
5
 
 
6
 
from __future__ import (
7
 
    absolute_import,
8
 
    print_function,
9
 
    unicode_literals,
10
 
    )
11
 
 
12
 
str = None
13
 
 
14
 
__metaclass__ = type
15
 
__all__ = []
16
 
 
17
 
from collections import defaultdict
18
 
 
19
 
import maasserver.maasavahi
20
 
from maasserver.maasavahi import (
21
 
    MAASAvahiService,
22
 
    setup_maas_avahi_service,
23
 
    )
24
 
from maasserver.models import Config
25
 
from maastesting.djangotestcase import DjangoTestCase
26
 
 
27
 
 
28
 
class MockZeroconfServiceFactory:
29
 
    """Factory used to track usage of the zeroconfservice module.
30
 
 
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.
34
 
    """
35
 
 
36
 
    def __init__(self):
37
 
        self.instances = []
38
 
 
39
 
    def __call__(self, *args, **kwargs):
40
 
        mock = MockZeroconfService(*args, **kwargs)
41
 
        self.instances.append(mock)
42
 
        return mock
43
 
 
44
 
 
45
 
class MockZeroconfService:
46
 
 
47
 
    def __init__(self, name, port, stype):
48
 
        self.name = name
49
 
        self.port = port
50
 
        self.stype = stype
51
 
        self.calls = []
52
 
 
53
 
    def publish(self):
54
 
        self.calls.append('publish')
55
 
 
56
 
    def unpublish(self):
57
 
        self.calls.append('unpublish')
58
 
 
59
 
 
60
 
class TestMAASAvahiService(DjangoTestCase):
61
 
 
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
65
 
        self.patch(
66
 
            Config.objects, '_config_changed_connections', defaultdict(set))
67
 
 
68
 
        mock_avahi = MockZeroconfServiceFactory()
69
 
        self.patch(
70
 
            maasserver.maasavahi, 'ZeroconfService', mock_avahi)
71
 
        return mock_avahi
72
 
 
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')
77
 
        service.publish()
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)
84
 
 
85
 
        # And published.
86
 
        self.assertEquals(['publish'], zeroconf.calls)
87
 
 
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')
92
 
        service.publish()
93
 
        service.publish()
94
 
 
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))
99
 
        self.assertEquals(
100
 
            ['publish', 'unpublish'], mock_avahi.instances[0].calls)
101
 
        self.assertEquals(
102
 
            ['publish'], mock_avahi.instances[1].calls)
103
 
 
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()
108
 
 
109
 
        # Name should have been published.
110
 
        self.assertEquals(1, len(mock_avahi.instances))
111
 
        self.assertEquals(
112
 
            'First Name MAAS Server', mock_avahi.instances[0].name)
113
 
 
114
 
        Config.objects.set_config('maas_name', 'Second Name')
115
 
 
116
 
        # A new publication should have occured.
117
 
        self.assertEquals(2, len(mock_avahi.instances))
118
 
        self.assertEquals(
119
 
            'Second Name MAAS Server', mock_avahi.instances[1].name)