summaryrefslogtreecommitdiffstats
path: root/ipatests/test_xmlrpc/test_location_plugin.py
blob: 97e97a2bc9ec910b65c3fc5b551e226fc52e53b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# Copyright (C) 2016  FreeIPA Contributors see COPYING for license
#
from __future__ import absolute_import

import pytest

from ipalib import errors, api
from ipatests.test_xmlrpc.tracker.location_plugin import LocationTracker
from ipatests.test_xmlrpc.tracker.server_plugin import ServerTracker
from ipatests.test_xmlrpc.xmlrpc_test import (
    XMLRPC_test,
    raises_exact,
)
from ipapython.dnsutil import DNSName


@pytest.fixture(scope='class', params=[u'location1', u'sk\xfa\u0161ka.idna'])
def location(request):
    tracker = LocationTracker(request.param)
    return tracker.make_fixture(request)


@pytest.fixture(scope='class')
def location_invalid(request):
    tracker = LocationTracker(u'invalid..location')
    return tracker


@pytest.fixture(scope='class')
def location_absolute(request):
    tracker = LocationTracker(u'invalid.absolute.')
    return tracker.make_fixture(request)


@pytest.fixture(scope='class')
def server(request):
    tracker = ServerTracker(api.env.host)
    return tracker


@pytest.mark.tier1
class TestNonexistentIPALocation(XMLRPC_test):
    def test_retrieve_nonexistent(self, location):
        location.ensure_missing()
        command = location.make_retrieve_command()
        with raises_exact(errors.NotFound(
                reason=u'%s: location not found' % location.idnsname)):
            command()

    def test_update_nonexistent(self, location):
        location.ensure_missing()
        command = location.make_update_command(updates=dict(
            description=u'Nope'))
        with raises_exact(errors.NotFound(
                reason=u'%s: location not found' % location.idnsname)):
            command()

    def test_delete_nonexistent(self, location):
        location.ensure_missing()
        command = location.make_delete_command()
        with raises_exact(errors.NotFound(
                reason=u'%s: location not found' % location.idnsname)):
            command()

@pytest.mark.tier1
class TestInvalidIPALocations(XMLRPC_test):
    def test_invalid_name(self, location_invalid):
        command = location_invalid.make_create_command()
        with raises_exact(errors.ConversionError(
                name=u'name',
                error=u"empty DNS label")):
            command()

    def test_invalid_absolute(self, location_absolute):
        command = location_absolute.make_create_command()
        with raises_exact(errors.ValidationError(
                name=u'name', error=u'must be relative')):
            command()


@pytest.mark.tier1
class TestCRUD(XMLRPC_test):
    def test_create_duplicate(self, location):
        location.ensure_exists()
        command = location.make_create_command()
        with raises_exact(errors.DuplicateEntry(
                message=u'location with name "%s" already exists' %
                        location.idnsname)):
            command()

    def test_retrieve_simple(self, location):
        location.retrieve()

    def test_retrieve_all(self, location):
        location.retrieve(all=True)

    def test_search_simple(self, location):
        location.find()

    def test_search_all(self, location):
        location.find(all=True)

    def test_update_simple(self, location):
        location.update(dict(
                description=u'Updated description',
            ),
            expected_updates=dict(
                description=[u'Updated description'],
            ))
        location.retrieve()

    def test_try_rename(self, location):
        location.ensure_exists()
        command = location.make_update_command(
            updates=dict(setattr=u'idnsname=changed'))
        with raises_exact(errors.NotAllowedOnRDN()):
            command()

    def test_delete_location(self, location):
        location.delete()


@pytest.mark.tier1
class TestLocationsServer(XMLRPC_test):

    def test_add_nonexistent_location_to_server(self, server):
        nonexistent_loc = DNSName(u'nonexistent-location')
        command = server.make_update_command(
            updates=dict(
                ipalocation_location=nonexistent_loc,
            )
        )
        with raises_exact(errors.NotFound(
                reason=u"{location}: location not found".format(
                    location=nonexistent_loc
                ))):
            command()

    def test_add_location_to_server(self, location, server):
        location.ensure_exists()
        server.update(
            dict(ipalocation_location=location.idnsname_obj),
            expected_updates=dict(
                ipalocation_location=[location.idnsname_obj],
            )
        )
        location.add_server_to_location(server.server_name)
        location.retrieve()

    def test_retrieve(self, server):
        server.retrieve()

    def test_retrieve_all(self, server):
        server.retrieve(all=True)

    def test_search_server_with_location(self, location, server):
        command = server.make_find_command(
            server.server_name, in_location=location.idnsname_obj)
        result = command()
        server.check_find(result)

    def test_search_server_with_location_with_all(self, location, server):
        command = server.make_find_command(
            server.server_name, in_location=location.idnsname_obj, all=True)
        result = command()
        server.check_find(result, all=True)

    def test_search_server_without_location(self, location, server):
        command = server.make_find_command(
            server.server_name, not_in_location=location.idnsname_obj)
        result = command()
        server.check_find_nomatch(result)

    def test_add_location_to_server_custom_weight(self, location, server):
        location.ensure_exists()
        server.update(
            dict(
                ipalocation_location=location.idnsname_obj,
                ipalocationweight=200,
            ),
            expected_updates=dict(
                ipalocation_location=[location.idnsname_obj],
                ipalocationweight=[u'200'],
            )
        )
        # remove invalid data from the previous test
        location.remove_server_from_location(server.server_name)

        location.add_server_to_location(server.server_name, weight=200)
        location.retrieve()

    def test_remove_location_from_server(self, location, server):
        server.update(dict(ipalocation_location=None))
        location.remove_server_from_location(server.server_name)
        location.retrieve()

    def test_remove_location_weight_from_server(self, location, server):
        server.update(dict(ipalocationweight=None))
        location.retrieve()