Esempio n. 1
0
    def test_fake_command_generation(self):
        class FakeChangeRecord(object):
            def __init__(self):
                self.__fqdn = 'prawf.unit.tests.'
                self._type = 'NOOP'
                self.value = 'prawf'
                self.ttl = 60

            @property
            def record(self):
                return self

            @property
            def fqdn(self):
                return self.__fqdn

        with requests_mock() as mock:
            mock.post(ANY, status_code=200, text='')

            provider = MythicBeastsProvider('test', {
                'unit.tests.': 'mypassword'
            })
            record = FakeChangeRecord()
            command = provider._compile_commands('ADD', record)
            self.assertEquals([], command)
Esempio n. 2
0
    def test_command_generation(self):
        zone = Zone('unit.tests.', [])
        zone.add_record(
            Record.new(zone, '', {
                'ttl': 60,
                'type': 'ALIAS',
                'value': 'alias.unit.tests.',
            }))
        zone.add_record(
            Record.new(
                zone, 'prawf-ns', {
                    'ttl': 300,
                    'type': 'NS',
                    'values': [
                        'alias.unit.tests.',
                        'alias2.unit.tests.',
                    ],
                }))
        zone.add_record(
            Record.new(zone, 'prawf-a', {
                'ttl': 60,
                'type': 'A',
                'values': [
                    '1.2.3.4',
                    '5.6.7.8',
                ],
            }))
        zone.add_record(
            Record.new(
                zone, 'prawf-aaaa', {
                    'ttl': 60,
                    'type': 'AAAA',
                    'values': [
                        'a:a::a',
                        'b:b::b',
                        'c:c::c:c',
                    ],
                }))
        zone.add_record(
            Record.new(zone, 'prawf-txt', {
                'ttl': 60,
                'type': 'TXT',
                'value': 'prawf prawf dyma prawf',
            }))
        zone.add_record(
            Record.new(zone, 'prawf-txt2', {
                'ttl': 60,
                'type': 'TXT',
                'value': 'v=DKIM1\\; k=rsa\\; p=prawf',
            }))
        with requests_mock() as mock:
            mock.post(ANY, status_code=200, text='')

            provider = MythicBeastsProvider('test',
                                            {'unit.tests.': 'mypassword'})

            plan = provider.plan(zone)
            changes = plan.changes
            generated_commands = []

            for change in changes:
                generated_commands.extend(
                    provider._compile_commands('ADD', change.new))

            expected_commands = [
                'ADD unit.tests 60 ANAME alias.unit.tests.',
                'ADD prawf-ns.unit.tests 300 NS alias.unit.tests.',
                'ADD prawf-ns.unit.tests 300 NS alias2.unit.tests.',
                'ADD prawf-a.unit.tests 60 A 1.2.3.4',
                'ADD prawf-a.unit.tests 60 A 5.6.7.8',
                'ADD prawf-aaaa.unit.tests 60 AAAA a:a::a',
                'ADD prawf-aaaa.unit.tests 60 AAAA b:b::b',
                'ADD prawf-aaaa.unit.tests 60 AAAA c:c::c:c',
                'ADD prawf-txt.unit.tests 60 TXT prawf prawf dyma prawf',
                'ADD prawf-txt2.unit.tests 60 TXT v=DKIM1; k=rsa; p=prawf',
            ]

            generated_commands.sort()
            expected_commands.sort()

            self.assertEquals(generated_commands, expected_commands)

            # Now test deletion
            existing = 'prawf-txt 300 TXT prawf prawf dyma prawf\n' \
                'prawf-txt2 300 TXT v=DKIM1; k=rsa; p=prawf\n' \
                'prawf-a 60 A 1.2.3.4'

            with requests_mock() as mock:
                mock.post(ANY, status_code=200, text=existing)
                wanted = Zone('unit.tests.', [])

                plan = provider.plan(wanted)
                changes = plan.changes
                generated_commands = []

                for change in changes:
                    generated_commands.extend(
                        provider._compile_commands('DELETE', change.existing))

            expected_commands = [
                'DELETE prawf-a.unit.tests 60 A 1.2.3.4',
                'DELETE prawf-txt.unit.tests 300 TXT prawf prawf dyma prawf',
                'DELETE prawf-txt2.unit.tests 300 TXT v=DKIM1; k=rsa; p=prawf',
            ]

            generated_commands.sort()
            expected_commands.sort()

            self.assertEquals(generated_commands, expected_commands)