1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

Merge branch 'ns1_geo' of ssh://github.com/nsone/octodns into ns1_geo

This commit is contained in:
Steve Coursen
2017-11-14 13:14:52 -05:00
18 changed files with 992 additions and 95 deletions

177
tests/fixtures/digitalocean-page-1.json vendored Normal file
View File

@@ -0,0 +1,177 @@
{
"domain_records": [{
"id": 11189874,
"type": "NS",
"name": "@",
"data": "ns1.digitalocean.com",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189875,
"type": "NS",
"name": "@",
"data": "ns2.digitalocean.com",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189876,
"type": "NS",
"name": "@",
"data": "ns3.digitalocean.com",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189877,
"type": "NS",
"name": "under",
"data": "ns1.unit.tests",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189878,
"type": "NS",
"name": "under",
"data": "ns2.unit.tests",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189879,
"type": "SRV",
"name": "_srv._tcp",
"data": "foo-1.unit.tests",
"priority": 10,
"port": 30,
"ttl": 600,
"weight": 20,
"flags": null,
"tag": null
}, {
"id": 11189880,
"type": "SRV",
"name": "_srv._tcp",
"data": "foo-2.unit.tests",
"priority": 12,
"port": 30,
"ttl": 600,
"weight": 20,
"flags": null,
"tag": null
}, {
"id": 11189881,
"type": "TXT",
"name": "txt",
"data": "Bah bah black sheep",
"priority": null,
"port": null,
"ttl": 600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189882,
"type": "TXT",
"name": "txt",
"data": "have you any wool.",
"priority": null,
"port": null,
"ttl": 600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189883,
"type": "A",
"name": "@",
"data": "1.2.3.4",
"priority": null,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189884,
"type": "A",
"name": "@",
"data": "1.2.3.5",
"priority": null,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189885,
"type": "A",
"name": "www",
"data": "2.2.3.6",
"priority": null,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189886,
"type": "MX",
"name": "mx",
"data": "smtp-4.unit.tests",
"priority": 10,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189887,
"type": "MX",
"name": "mx",
"data": "smtp-2.unit.tests",
"priority": 20,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189888,
"type": "MX",
"name": "mx",
"data": "smtp-3.unit.tests",
"priority": 30,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}],
"links": {
"pages": {
"last": "https://api.digitalocean.com/v2/domains/unit.tests/records?page=2",
"next": "https://api.digitalocean.com/v2/domains/unit.tests/records?page=2"
}
},
"meta": {
"total": 21
}
}

89
tests/fixtures/digitalocean-page-2.json vendored Normal file
View File

@@ -0,0 +1,89 @@
{
"domain_records": [{
"id": 11189889,
"type": "MX",
"name": "mx",
"data": "smtp-1.unit.tests",
"priority": 40,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189890,
"type": "AAAA",
"name": "aaaa",
"data": "2601:644:500:e210:62f8:1dff:feb8:947a",
"priority": null,
"port": null,
"ttl": 600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189891,
"type": "CNAME",
"name": "cname",
"data": "unit.tests",
"priority": null,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189892,
"type": "A",
"name": "www.sub",
"data": "2.2.3.6",
"priority": null,
"port": null,
"ttl": 300,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189893,
"type": "TXT",
"name": "txt",
"data": "v=DKIM1;k=rsa;s=email;h=sha256;p=A/kinda+of/long/string+with+numb3rs",
"priority": null,
"port": null,
"ttl": 600,
"weight": null,
"flags": null,
"tag": null
}, {
"id": 11189894,
"type": "CAA",
"name": "@",
"data": "ca.unit.tests",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": 0,
"tag": "issue"
}, {
"id": 11189895,
"type": "CNAME",
"name": "included",
"data": "unit.tests",
"priority": null,
"port": null,
"ttl": 3600,
"weight": null,
"flags": null,
"tag": null
}],
"links": {
"pages": {
"first": "https://api.digitalocean.com/v2/domains/unit.tests/records?page=1",
"prev": "https://api.digitalocean.com/v2/domains/unit.tests/records?page=1"
}
},
"meta": {
"total": 21
}
}

View File

@@ -102,12 +102,12 @@ class TestManager(TestCase):
environ['YAML_TMP_DIR'] = tmpdir.dirname
tc = Manager(get_config_filename('simple.yaml')) \
.sync(dry_run=False)
self.assertEquals(20, tc)
self.assertEquals(21, tc)
# try with just one of the zones
tc = Manager(get_config_filename('simple.yaml')) \
.sync(dry_run=False, eligible_zones=['unit.tests.'])
self.assertEquals(14, tc)
self.assertEquals(15, tc)
# the subzone, with 2 targets
tc = Manager(get_config_filename('simple.yaml')) \
@@ -122,18 +122,18 @@ class TestManager(TestCase):
# Again with force
tc = Manager(get_config_filename('simple.yaml')) \
.sync(dry_run=False, force=True)
self.assertEquals(20, tc)
self.assertEquals(21, tc)
# Again with max_workers = 1
tc = Manager(get_config_filename('simple.yaml'), max_workers=1) \
.sync(dry_run=False, force=True)
self.assertEquals(20, tc)
self.assertEquals(21, tc)
# Include meta
tc = Manager(get_config_filename('simple.yaml'), max_workers=1,
include_meta=True) \
.sync(dry_run=False, force=True)
self.assertEquals(24, tc)
self.assertEquals(25, tc)
def test_eligible_targets(self):
with TemporaryDirectory() as tmpdir:
@@ -159,13 +159,13 @@ class TestManager(TestCase):
fh.write('---\n{}')
changes = manager.compare(['in'], ['dump'], 'unit.tests.')
self.assertEquals(14, len(changes))
self.assertEquals(15, len(changes))
# Compound sources with varying support
changes = manager.compare(['in', 'nosshfp'],
['dump'],
'unit.tests.')
self.assertEquals(13, len(changes))
self.assertEquals(14, len(changes))
with self.assertRaises(Exception) as ctx:
manager.compare(['nope'], ['dump'], 'unit.tests.')

View File

@@ -0,0 +1,241 @@
#
#
#
from __future__ import absolute_import, division, print_function, \
unicode_literals
from mock import Mock, call
from os.path import dirname, join
from requests import HTTPError
from requests_mock import ANY, mock as requests_mock
from unittest import TestCase
from octodns.record import Record
from octodns.provider.digitalocean import DigitalOceanClientNotFound, \
DigitalOceanProvider
from octodns.provider.yaml import YamlProvider
from octodns.zone import Zone
class TestDigitalOceanProvider(TestCase):
expected = Zone('unit.tests.', [])
source = YamlProvider('test', join(dirname(__file__), 'config'))
source.populate(expected)
# Our test suite differs a bit, add our NS and remove the simple one
expected.add_record(Record.new(expected, 'under', {
'ttl': 3600,
'type': 'NS',
'values': [
'ns1.unit.tests.',
'ns2.unit.tests.',
]
}))
for record in list(expected.records):
if record.name == 'sub' and record._type == 'NS':
expected._remove_record(record)
break
def test_populate(self):
provider = DigitalOceanProvider('test', 'token')
# Bad auth
with requests_mock() as mock:
mock.get(ANY, status_code=401,
text='{"id":"unauthorized",'
'"message":"Unable to authenticate you."}')
with self.assertRaises(Exception) as ctx:
zone = Zone('unit.tests.', [])
provider.populate(zone)
self.assertEquals('Unauthorized', ctx.exception.message)
# General error
with requests_mock() as mock:
mock.get(ANY, status_code=502, text='Things caught fire')
with self.assertRaises(HTTPError) as ctx:
zone = Zone('unit.tests.', [])
provider.populate(zone)
self.assertEquals(502, ctx.exception.response.status_code)
# Non-existant zone doesn't populate anything
with requests_mock() as mock:
mock.get(ANY, status_code=404,
text='{"id":"not_found","message":"The resource you '
'were accessing could not be found."}')
zone = Zone('unit.tests.', [])
provider.populate(zone)
self.assertEquals(set(), zone.records)
# No diffs == no changes
with requests_mock() as mock:
base = 'https://api.digitalocean.com/v2/domains/unit.tests/' \
'records?page='
with open('tests/fixtures/digitalocean-page-1.json') as fh:
mock.get('{}{}'.format(base, 1), text=fh.read())
with open('tests/fixtures/digitalocean-page-2.json') as fh:
mock.get('{}{}'.format(base, 2), text=fh.read())
zone = Zone('unit.tests.', [])
provider.populate(zone)
self.assertEquals(12, len(zone.records))
changes = self.expected.changes(zone, provider)
self.assertEquals(0, len(changes))
# 2nd populate makes no network calls/all from cache
again = Zone('unit.tests.', [])
provider.populate(again)
self.assertEquals(12, len(again.records))
# bust the cache
del provider._zone_records[zone.name]
def test_apply(self):
provider = DigitalOceanProvider('test', 'token')
resp = Mock()
resp.json = Mock()
provider._client._request = Mock(return_value=resp)
domain_after_creation = {
"domain_records": [{
"id": 11189874,
"type": "NS",
"name": "@",
"data": "ns1.digitalocean.com",
"priority": None,
"port": None,
"ttl": 3600,
"weight": None,
"flags": None,
"tag": None
}, {
"id": 11189875,
"type": "NS",
"name": "@",
"data": "ns2.digitalocean.com",
"priority": None,
"port": None,
"ttl": 3600,
"weight": None,
"flags": None,
"tag": None
}, {
"id": 11189876,
"type": "NS",
"name": "@",
"data": "ns3.digitalocean.com",
"priority": None,
"port": None,
"ttl": 3600,
"weight": None,
"flags": None,
"tag": None
}, {
"id": 11189877,
"type": "A",
"name": "@",
"data": "192.0.2.1",
"priority": None,
"port": None,
"ttl": 3600,
"weight": None,
"flags": None,
"tag": None
}],
"links": {},
"meta": {
"total": 4
}
}
# non-existant domain, create everything
resp.json.side_effect = [
DigitalOceanClientNotFound, # no zone in populate
DigitalOceanClientNotFound, # no domain during apply
domain_after_creation
]
plan = provider.plan(self.expected)
# No root NS, no ignored, no excluded, no unsupported
n = len(self.expected.records) - 7
self.assertEquals(n, len(plan.changes))
self.assertEquals(n, provider.apply(plan))
provider._client._request.assert_has_calls([
# created the domain
call('POST', '/domains', data={'ip_address': '192.0.2.1',
'name': 'unit.tests'}),
# get all records in newly created zone
call('GET', '/domains/unit.tests/records', {'page': 1}),
# delete the initial A record
call('DELETE', '/domains/unit.tests/records/11189877'),
# created at least one of the record with expected data
call('POST', '/domains/unit.tests/records', data={
'name': '_srv._tcp',
'weight': 20,
'data': 'foo-1.unit.tests.',
'priority': 10,
'ttl': 600,
'type': 'SRV',
'port': 30
}),
])
self.assertEquals(24, provider._client._request.call_count)
provider._client._request.reset_mock()
# delete 1 and update 1
provider._client.records = Mock(return_value=[
{
'id': 11189897,
'name': 'www',
'data': '1.2.3.4',
'ttl': 300,
'type': 'A',
},
{
'id': 11189898,
'name': 'www',
'data': '2.2.3.4',
'ttl': 300,
'type': 'A',
},
{
'id': 11189899,
'name': 'ttl',
'data': '3.2.3.4',
'ttl': 600,
'type': 'A',
}
])
# Domain exists, we don't care about return
resp.json.side_effect = ['{}']
wanted = Zone('unit.tests.', [])
wanted.add_record(Record.new(wanted, 'ttl', {
'ttl': 300,
'type': 'A',
'value': '3.2.3.4'
}))
plan = provider.plan(wanted)
self.assertEquals(2, len(plan.changes))
self.assertEquals(2, provider.apply(plan))
# recreate for update, and delete for the 2 parts of the other
provider._client._request.assert_has_calls([
call('POST', '/domains/unit.tests/records', data={
'data': '3.2.3.4',
'type': 'A',
'name': 'ttl',
'ttl': 300
}),
call('DELETE', '/domains/unit.tests/records/11189899'),
call('DELETE', '/domains/unit.tests/records/11189897'),
call('DELETE', '/domains/unit.tests/records/11189898')
], any_order=True)

View File

@@ -49,12 +49,12 @@ class TestYamlProvider(TestCase):
# We add everything
plan = target.plan(zone)
self.assertEquals(14, len(filter(lambda c: isinstance(c, Create),
self.assertEquals(15, len(filter(lambda c: isinstance(c, Create),
plan.changes)))
self.assertFalse(isfile(yaml_file))
# Now actually do it
self.assertEquals(14, target.apply(plan))
self.assertEquals(15, target.apply(plan))
self.assertTrue(isfile(yaml_file))
# There should be no changes after the round trip
@@ -64,15 +64,19 @@ class TestYamlProvider(TestCase):
# A 2nd sync should still create everything
plan = target.plan(zone)
self.assertEquals(14, len(filter(lambda c: isinstance(c, Create),
self.assertEquals(15, len(filter(lambda c: isinstance(c, Create),
plan.changes)))
with open(yaml_file) as fh:
data = safe_load(fh.read())
# '' has some of both
roots = sorted(data[''], key=lambda r: r['type'])
self.assertTrue('values' in roots[0]) # A
self.assertTrue('value' in roots[1]) # CAA
self.assertTrue('values' in roots[2]) # SSHFP
# these are stored as plural 'values'
for r in data['']:
self.assertTrue('values' in r)
self.assertTrue('values' in data['mx'])
self.assertTrue('values' in data['naptr'])
self.assertTrue('values' in data['_srv._tcp'])