~rvb/maas/maas-1.7-3399

« back to all changes in this revision

Viewing changes to src/provisioningserver/dhcp/tests/test_omshell.py

Backports all the changes from trunk up to revision 3379 (i.e. all the changes before the change to the isolation level).

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from maastesting.factory import factory
24
24
from maastesting.fakemethod import FakeMethod
25
25
from maastesting.fixtures import TempDirectory
 
26
from maastesting.matchers import MockCalledOnceWith
26
27
from maastesting.testcase import MAASTestCase
27
28
from mock import (
28
29
    ANY,
220
221
        self.assertIsNone(shell.remove(ip_address))
221
222
 
222
223
 
 
224
class Test_Omshell_nullify_lease(MAASTestCase):
 
225
    """Tests for Omshell.nullify_lease"""
 
226
 
 
227
    def test__calls_omshell_correctly(self):
 
228
        server_address = factory.make_string()
 
229
        shared_key = factory.make_string()
 
230
        ip_address = factory.make_ipv4_address()
 
231
        shell = Omshell(server_address, shared_key)
 
232
 
 
233
        # Instead of calling a real omshell, we'll just record the
 
234
        # parameters passed to Popen.
 
235
        run = self.patch(shell, '_run')
 
236
        run.return_value = (0, '\nends = 00:00:00:00')
 
237
        expected_script = dedent("""\
 
238
            server {server}
 
239
            key omapi_key {key}
 
240
            connect
 
241
            new lease
 
242
            set ip-address = {ip}
 
243
            open
 
244
            set ends = 00:00:00:00
 
245
            update
 
246
            """)
 
247
        expected_script = expected_script.format(
 
248
            server=server_address, key=shared_key, ip=ip_address)
 
249
        shell.nullify_lease(ip_address)
 
250
        self.assertThat(run, MockCalledOnceWith(expected_script))
 
251
 
 
252
    def test__considers_nonexistent_lease_a_success(self):
 
253
        server_address = factory.make_string()
 
254
        shared_key = factory.make_string()
 
255
        ip_address = factory.make_ipv4_address()
 
256
        shell = Omshell(server_address, shared_key)
 
257
 
 
258
        output = (
 
259
            "obj: <null>\nobj: lease\nobj: lease\n"
 
260
            "can't open object: not found\nobj: lease\n")
 
261
        self.patch(shell, '_run').return_value = (0, output)
 
262
        shell.nullify_lease(ip_address)  # No exception.
 
263
        self.assertThat(shell._run, MockCalledOnceWith(ANY))
 
264
 
 
265
    def test__catches_invalid_error(self):
 
266
        server_address = factory.make_string()
 
267
        shared_key = factory.make_string()
 
268
        ip_address = factory.make_ipv4_address()
 
269
        shell = Omshell(server_address, shared_key)
 
270
 
 
271
        output = "obj: <null>\nobj: lease\ninvalid value."
 
272
        self.patch(shell, '_run').return_value = (0, output)
 
273
        self.assertRaises(
 
274
            ExternalProcessError, shell.nullify_lease, ip_address)
 
275
 
 
276
    def test__catches_failed_update(self):
 
277
        server_address = factory.make_string()
 
278
        shared_key = factory.make_string()
 
279
        ip_address = factory.make_ipv4_address()
 
280
        shell = Omshell(server_address, shared_key)
 
281
 
 
282
        # make "ends" different to what we asked, so the post-run check
 
283
        # should fail.
 
284
        output = dedent("""\
 
285
            obj: <null>
 
286
            obj: lease
 
287
            obj: lease
 
288
            ip-address = 0a:00:00:72
 
289
            state = 00:00:00:01
 
290
            subnet = 00:00:00:03
 
291
            pool = 00:00:00:04
 
292
            hardware-address = 00:16:3e:06:45:5e
 
293
            hardware-type = 00:00:00:01
 
294
            ends = 00:00:00:FF
 
295
            starts = "T@v'"
 
296
            tstp = 54:41:1e:e7
 
297
            tsfp = 00:00:00:00
 
298
            atsfp = 00:00:00:00
 
299
            cltt = "T@v'"
 
300
            flags = 00
 
301
            """)
 
302
        self.patch(shell, '_run').return_value = (0, output)
 
303
        self.assertRaises(
 
304
            ExternalProcessError, shell.nullify_lease, ip_address)
 
305
 
 
306
 
223
307
class Test_generate_omapi_key(MAASTestCase):
224
308
    """Tests for omshell.generate_omapi_key"""
225
309