salt/tests/unit/states/test_libcloud_dns.py

143 lines
4.2 KiB
Python
Raw Normal View History

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
from tests.support.mock import (
2016-07-24 07:13:51 +00:00
NO_MOCK,
NO_MOCK_REASON
)
import salt.states.libcloud_dns as libcloud_dns
2016-07-24 07:13:51 +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
2016-07-24 09:43:06 +00:00
self.domain = domain
2016-07-24 07:13:51 +00:00
class DNSTestRecord(object):
2016-07-24 07:34:29 +00:00
def __init__(self, id, name, type, data):
2016-07-24 07:13:51 +00:00
self.id = id
2016-07-24 07:34:29 +00:00
self.name = name
2016-07-24 07:13:51 +00:00
self.type = type
self.data = data
@skipIf(NO_MOCK, NO_MOCK_REASON)
class LibcloudDnsModuleTestCase(TestCase, LoaderModuleMockMixin):
2016-07-24 07:13:51 +00:00
def setup_loader_modules(self):
test_records = {
'zone1': [DNSTestRecord(0, 'www', 'A', '127.0.0.1')]
}
2017-03-24 03:05:19 +00:00
def list_zones(profile):
return [DNSTestZone('zone1', 'test.com')]
2016-07-24 07:13:51 +00:00
def list_records(zone_id, profile):
return test_records[zone_id]
2016-07-24 09:06:34 +00:00
def create_record(*args):
return True
2016-07-24 09:16:48 +00:00
def delete_record(*args):
return True
2016-07-24 09:43:06 +00:00
def create_zone(*args):
return True
2016-07-24 09:48:11 +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')
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')
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)