Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
make zone choice random, add delay for import as it is rate limited
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Dec 28, 2023
1 parent 3b17a03 commit 4a8626f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
47 changes: 37 additions & 10 deletions tests/test_dns_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import sys
import uuid
import time
import random
import tempfile

fp = open('/dev/null', 'rb')
Expand All @@ -14,31 +16,40 @@
# test IMPORT EXPORT

cf = None
zone_name = None
zone_id = None

def test_cloudflare():
global cf
cf = CloudFlare.CloudFlare()
assert isinstance(cf, CloudFlare.CloudFlare)

zone_name = None
zone_id = None

def test_find_zone():
global zone_name, zone_id
# grab the first zone identifier
params = {'per_page':1}
# grab a random zone identifier from the first 10 zones
params = {'per_page':10}
zones = cf.zones.get(params=params)
assert len(zones) == 1
zone_name = zones[0]['name']
zone_id = zones[0]['id']
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
zone_id = zones[n]['id']
assert len(zone_id) == 32
print('zone: %s %s' % (zone_id, zone_name))
print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr)

def test_dns_import():
# IMPORT
# create a zero length file
fp = tempfile.TemporaryFile(mode='w+b')
fp.seek(0)
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fp})
while True:
try:
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fp})
break
except CloudFlare.exceptions.CloudFlareAPIError as e:
print('cf.zones.dns_records.import: returned %d "%s"' % (int(e), str(e))) # 429 or 99998
time.sleep(.5) # This is sadly needed as import seems to be rate limited
fp.seek(0)
# {"recs_added": 0, "recs_added_by_type": {}, "total_records_parsed": 0}
assert len(results) > 0
assert results['recs_added'] == 0
Expand All @@ -64,7 +75,14 @@ def test_dns_import_with_debug():
# create a zero length file
fp = tempfile.TemporaryFile(mode='w+b')
fp.seek(0)
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fp})
while True:
try:
results = cf.zones.dns_records.import_.post(zone_id, files={'file':fp})
break
except CloudFlare.exceptions.CloudFlareAPIError as e:
print('cf.zones.dns_records.import: returned %d "%s"' % (int(e), str(e))) # 429 or 99998
time.sleep(.5) # This is sadly needed as import seems to be rate limited
fp.seek(0)
# {"recs_added": 0, "recs_added_by_type": {}, "total_records_parsed": 0}
assert len(results) > 0
assert results['recs_added'] == 0
Expand All @@ -78,3 +96,12 @@ def test_dns_export_with_debug():
assert isinstance(dns_records, str)
assert 'SOA' in dns_records
assert 'NS' in dns_records

if __name__ == '__main__':
test_cloudflare()
test_find_zone()
test_dns_import()
test_dns_export()
test_cloudflare_with_debug()
test_dns_import_with_debug()
test_dns_export_with_debug()
23 changes: 13 additions & 10 deletions tests/test_dns_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
import os
import sys
import uuid
import random

sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

# test GET POST PUT PATCH & DELETE - but not in that order

cf = None
zone_name = None
zone_id = None

def test_cloudflare():
global cf
cf = CloudFlare.CloudFlare()
assert isinstance(cf, CloudFlare.CloudFlare)

zone_name = None
zone_id = None

def test_find_zone():
global zone_name, zone_id
# grab the first zone identifier
params = {'per_page':1}
# grab a random zone identifier from the first 10 zones
params = {'per_page':10}
zones = cf.zones.get(params=params)
assert len(zones) == 1
zone_name = zones[0]['name']
zone_id = zones[0]['id']
assert len(zones) > 0 and len(zones) <= 10
n = random.randrange(len(zones))
zone_name = zones[n]['name']
zone_id = zones[n]['id']
assert len(zone_id) == 32
print('zone: %s %s' % (zone_id, zone_name))
print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr)

dns_name = None
dns_type = None
Expand All @@ -42,7 +45,7 @@ def test_dns_records():
dns_content1 = 'temp pytest element 1'
dns_content2 = 'temp pytest element 2'
dns_content3 = 'temp pytest element 3'
print('dns_record: %s' % (dns_name))
print('dns_record: %s' % (dns_name), file=sys.stderr)

def test_dns_records_get1():
# GET
Expand All @@ -63,7 +66,7 @@ def test_dns_records_post():

dns_id = dns_result['id']
assert len(dns_id) == 32
print('dns_record: %s %s' % (dns_name, dns_id))
print('dns_record: %s %s' % (dns_name, dns_id), file=sys.stderr)

def test_dns_records_get2():
# GET
Expand Down

0 comments on commit 4a8626f

Please sign in to comment.