2016-07-24 07:13:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-22 16:42:17 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
|
|
|
from tests.support.unit import TestCase, skipIf
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.mock import (
|
2016-07-24 07:13:51 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.libcloud_dns as libcloud_dns
|
2017-04-26 03:34:52 +00:00
|
|
|
from salt.modules.libcloud_dns import _simple_record, _simple_zone
|
2016-07-24 07:13:51 +00:00
|
|
|
|
|
|
|
|
2017-04-29 00:48:04 +00:00
|
|
|
class DNSTestZone(object):
|
2016-07-24 09:43:06 +00:00
|
|
|
def __init__(self, id, domain):
|
2016-07-24 07:13:51 +00:00
|
|
|
self.id = id
|
2017-04-26 03:34:52 +00:00
|
|
|
self.type = 'master'
|
|
|
|
self.ttl = 4400
|
2016-07-24 09:43:06 +00:00
|
|
|
self.domain = domain
|
2017-04-26 03:34:52 +00:00
|
|
|
self.extra = {}
|
2016-07-24 07:13:51 +00:00
|
|
|
|
|
|
|
|
2017-04-29 00:48:04 +00:00
|
|
|
class DNSTestRecord(object):
|
|
|
|
def __init__(self, id, name, record_type, data):
|
2016-07-24 07:13:51 +00:00
|
|
|
self.id = id
|
2016-07-24 07:34:29 +00:00
|
|
|
self.name = name
|
2017-04-29 00:48:04 +00:00
|
|
|
self.type = record_type
|
2017-04-26 03:34:52 +00:00
|
|
|
self.ttl = 4400
|
2016-07-24 07:13:51 +00:00
|
|
|
self.data = data
|
2017-04-28 22:28:28 +00:00
|
|
|
self.zone = DNSTestZone('test', 'domain')
|
2017-04-26 03:34:52 +00:00
|
|
|
self.extra = {}
|
2016-07-24 07:13:51 +00:00
|
|
|
|
2017-03-24 03:05:19 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class LibcloudDnsModuleTestCase(TestCase, LoaderModuleMockMixin):
|
2016-07-24 09:16:48 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
test_records = {
|
2017-04-29 00:48:04 +00:00
|
|
|
'zone1': [_simple_record(DNSTestRecord(0, 'www', 'A', '127.0.0.1'))]
|
2017-04-17 10:29:06 +00:00
|
|
|
}
|
2017-03-24 03:05:19 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def list_zones(profile):
|
2017-04-29 00:48:04 +00:00
|
|
|
return [_simple_zone(DNSTestZone('zone1', 'test.com'))]
|
2016-07-24 09:43:06 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def list_records(zone_id, profile):
|
|
|
|
return test_records[zone_id]
|
2017-03-24 03:05:19 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def create_record(*args):
|
|
|
|
return True
|
2016-07-24 09:48:11 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def delete_record(*args):
|
|
|
|
return True
|
2016-07-24 07:13:51 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def create_zone(*args):
|
|
|
|
return True
|
2016-07-24 09:48:11 +00:00
|
|
|
|
2017-04-17 10:29:06 +00:00
|
|
|
def delete_zone(*args):
|
|
|
|
return True
|
2017-03-22 16:42:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
libcloud_dns: {
|
|
|
|
'__salt__': {
|
|
|
|
'libcloud_dns.list_zones': list_zones,
|
|
|
|
'libcloud_dns.list_records': list_records,
|
|
|
|
'libcloud_dns.create_record': create_record,
|
|
|
|
'libcloud_dns.delete_record': delete_record,
|
|
|
|
'libcloud_dns.create_zone': create_zone,
|
|
|
|
'libcloud_dns.delete_zone': delete_zone
|
2016-07-24 07:13:51 +00:00
|
|
|
}
|
2017-03-22 16:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 07:13:51 +00:00
|
|
|
|
2016-07-24 09:06:34 +00:00
|
|
|
def test_present_record_exists(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:06:34 +00:00
|
|
|
Try and create a record that already exists
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_present('www', 'test.com', 'A', '127.0.0.1', 'test')
|
2017-03-24 04:13:03 +00:00
|
|
|
self.assertTrue(result)
|
2016-07-24 09:06:34 +00:00
|
|
|
|
|
|
|
def test_present_record_does_not_exist(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:06:34 +00:00
|
|
|
Try and create a record that already exists
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_present('mail', 'test.com', 'A', '127.0.0.1', 'test')
|
2017-03-24 04:13:03 +00:00
|
|
|
self.assertTrue(result)
|
2016-07-24 09:06:34 +00:00
|
|
|
|
2016-07-24 09:16:48 +00:00
|
|
|
def test_absent_record_exists(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:16:48 +00:00
|
|
|
Try and deny a record that already exists
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_absent('www', 'test.com', 'A', '127.0.0.1', 'test')
|
2017-03-24 04:13:03 +00:00
|
|
|
self.assertTrue(result)
|
2016-07-24 09:16:48 +00:00
|
|
|
|
|
|
|
def test_absent_record_does_not_exist(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:16:48 +00:00
|
|
|
Try and deny a record that already exists
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_absent('mail', 'test.com', 'A', '127.0.0.1', 'test')
|
2017-03-24 04:13:03 +00:00
|
|
|
self.assertTrue(result)
|
2016-07-24 09:16:48 +00:00
|
|
|
|
|
|
|
def test_present_zone_not_found(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:06:34 +00:00
|
|
|
Assert that when you try and ensure present state for a record to a zone that doesn't exist
|
|
|
|
it fails gracefully
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_present('mail', 'notatest.com', 'A', '127.0.0.1', 'test')
|
2016-07-24 09:30:32 +00:00
|
|
|
self.assertFalse(result['result'])
|
2016-07-24 07:13:51 +00:00
|
|
|
|
2016-07-24 09:16:48 +00:00
|
|
|
def test_absent_zone_not_found(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:16:48 +00:00
|
|
|
Assert that when you try and ensure absent state for a record to a zone that doesn't exist
|
|
|
|
it fails gracefully
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
|
|
|
result = libcloud_dns.record_absent('mail', 'notatest.com', 'A', '127.0.0.1', 'test')
|
2016-07-24 09:30:32 +00:00
|
|
|
self.assertFalse(result['result'])
|
2016-07-24 09:16:48 +00:00
|
|
|
|
2016-07-24 09:43:06 +00:00
|
|
|
def test_zone_present(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:43:06 +00:00
|
|
|
Assert that a zone is present (that did not exist)
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2017-03-24 04:13:03 +00:00
|
|
|
result = libcloud_dns.zone_present('testing.com', 'master', 'test1')
|
|
|
|
self.assertTrue(result)
|
2016-07-24 09:43:06 +00:00
|
|
|
|
2016-07-24 09:45:06 +00:00
|
|
|
def test_zone_already_present(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:45:06 +00:00
|
|
|
Assert that a zone is present (that did exist)
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2017-03-24 04:13:03 +00:00
|
|
|
result = libcloud_dns.zone_present('test.com', 'master', 'test1')
|
|
|
|
self.assertTrue(result)
|
2016-07-24 09:45:06 +00:00
|
|
|
|
2016-07-24 09:48:11 +00:00
|
|
|
def test_zone_absent(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:48:11 +00:00
|
|
|
Assert that a zone that did exist is absent
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2017-03-24 04:13:03 +00:00
|
|
|
result = libcloud_dns.zone_absent('test.com', 'test1')
|
|
|
|
self.assertTrue(result)
|
2016-07-24 09:48:11 +00:00
|
|
|
|
|
|
|
def test_zone_already_absent(self):
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2016-07-24 09:48:11 +00:00
|
|
|
Assert that a zone that did not exist is absent
|
2017-03-22 16:42:17 +00:00
|
|
|
'''
|
2017-03-24 04:13:03 +00:00
|
|
|
result = libcloud_dns.zone_absent('testing.com', 'test1')
|
|
|
|
self.assertTrue(result)
|