def test_apply(self): provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) # Create blank zone with requests_mock() as mock: mock.post(ANY, status_code=200, text='') provider.populate(zone) self.assertEquals(0, len(zone.records)) # Record change failed with requests_mock() as mock: mock.post(ANY, status_code=200, text='') provider.populate(zone) zone.add_record( Record.new(zone, 'prawf', { 'ttl': 300, 'type': 'TXT', 'value': 'prawf', })) plan = provider.plan(zone) with requests_mock() as mock: mock.post(ANY, status_code=400, text='NADD 300 TXT prawf') with self.assertRaises(Exception) as err: provider.apply(plan) self.assertEquals( 'Mythic Beasts could not action command: unit.tests ' 'ADD prawf.unit.tests 300 TXT prawf', err.exception.message) # Check deleting and adding/changing test record existing = 'prawf 300 TXT prawf prawf prawf\ndileu 300 TXT dileu' with requests_mock() as mock: mock.post(ANY, status_code=200, text=existing) # Mash up a new zone with records so a plan # is generated with changes and applied. For some reason # passing self.expected, or just changing each record's zone # doesn't work. Nor does this without a single add_record after wanted = Zone('unit.tests.', []) for record in list(self.expected.records): data = {'type': record._type} data.update(record.data) wanted.add_record(Record.new(wanted, record.name, data)) wanted.add_record( Record.new(wanted, 'prawf', { 'ttl': 60, 'type': 'TXT', 'value': 'prawf yw e', })) plan = provider.plan(wanted) # Octo ignores NS records (15-1) self.assertEquals( 1, len([c for c in plan.changes if isinstance(c, Update)])) self.assertEquals( 1, len([c for c in plan.changes if isinstance(c, Delete)])) self.assertEquals( 14, len([c for c in plan.changes if isinstance(c, Create)])) self.assertEquals(16, provider.apply(plan)) self.assertTrue(plan.exists)
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)