Exemple #1
0
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = DNSDriver('none', 'none')
        self.tmp_file = tempfile.mkstemp()
        self.tmp_path = self.tmp_file[1]

    def test_export_zone_to_bind_format_slave_should_throw(self):
        zone = Zone(id=1, domain='example.com', type='slave', ttl=900,
                    driver=self.driver)
        self.assertRaises(ValueError, zone.export_to_bind_format)

    def test_export_zone_to_bind_format_success(self):
        zone = Zone(id=1, domain='example.com', type='master', ttl=900,
                    driver=self.driver)

        mock_records = []

        for values in MOCK_RECORDS_VALUES:
            values = values.copy()
            values['driver'] = self.driver
            values['zone'] = zone
            record = Record(**values)
            mock_records.append(record)

        self.driver.list_records = Mock()
        self.driver.list_records.return_value = mock_records

        result = self.driver.export_zone_to_bind_format(zone=zone)
        self.driver.export_zone_to_bind_zone_file(zone=zone,
                                                  file_path=self.tmp_path)

        with open(self.tmp_path, 'r') as fp:
            content = fp.read()

        lines1 = result.split('\n')
        lines2 = content.split('\n')

        for lines in [lines1, lines2]:
            self.assertEqual(len(lines), 2 + 1 + 9)
            self.assertRegexpMatches(lines[1], r'\$ORIGIN example\.com\.')
            self.assertRegexpMatches(lines[2], r'\$TTL 900')

            self.assertRegexpMatches(lines[4], r'www.example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(lines[5], r'www.example.com\.\s+900\s+IN\s+AAAA\s+2a01:4f8:121:3121::2')
            self.assertRegexpMatches(lines[6], r'www.example.com\.\s+123\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(lines[7], r'example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(lines[8], r'test1.example.com\.\s+900\s+IN\s+TXT\s+"test foo bar"')
            self.assertRegexpMatches(lines[9], r'test2.example.com\.\s+900\s+IN\s+TXT\s+"test \\"foo\\" \\"bar\\""')
            self.assertRegexpMatches(lines[10], r'example.com\.\s+900\s+IN\s+MX\s+10\s+mx.example.com')
            self.assertRegexpMatches(lines[11], r'example.com\.\s+900\s+IN\s+SRV\s+20\s+10 3333 example.com')
Exemple #2
0
 def setUp(self):
     self.driver = DNSDriver("none", "none")
     self.tmp_file = tempfile.mkstemp()
     self.tmp_path = self.tmp_file[1]
Exemple #3
0
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = DNSDriver("none", "none")
        self.tmp_file = tempfile.mkstemp()
        self.tmp_path = self.tmp_file[1]

    def test_export_zone_to_bind_format_slave_should_throw(self):
        zone = Zone(id=1,
                    domain="example.com",
                    type="slave",
                    ttl=900,
                    driver=self.driver)
        self.assertRaises(ValueError, zone.export_to_bind_format)

    def test_export_zone_to_bind_format_success(self):
        zone = Zone(id=1,
                    domain="example.com",
                    type="master",
                    ttl=900,
                    driver=self.driver)

        mock_records = []

        for values in MOCK_RECORDS_VALUES:
            values = values.copy()
            values["driver"] = self.driver
            values["zone"] = zone
            record = Record(**values)
            mock_records.append(record)

        self.driver.list_records = Mock()
        self.driver.list_records.return_value = mock_records

        now = datetime.datetime.utcnow()

        result = self.driver.export_zone_to_bind_format(zone=zone)
        self.driver.export_zone_to_bind_zone_file(zone=zone,
                                                  file_path=self.tmp_path)

        with open(self.tmp_path, "r") as fp:
            content = fp.read()

        lines1 = result.split("\n")
        lines2 = content.split("\n")

        date_str = "%s-%s-%s %s:%s:%s" % (
            now.year,
            zero_pad(now.month),
            zero_pad(now.day),
            zero_pad(now.hour),
            zero_pad(now.minute),
            zero_pad(now.second),
        )
        expected_header = "; Generated by Libcloud v%s on %s UTC" % (
            __version__,
            date_str,
        )

        self.assertEqual(lines1[0], expected_header)
        self.assertEqual(lines2[0], expected_header)

        for lines in [lines1, lines2]:
            self.assertEqual(len(lines), 2 + 1 + 9)
            assertRegex(self, lines[1], r"\$ORIGIN example\.com\.")
            assertRegex(self, lines[2], r"\$TTL 900")

            assertRegex(self, lines[4],
                        r"www.example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1")
            assertRegex(
                self,
                lines[5],
                r"www.example.com\.\s+900\s+IN\s+AAAA\s+2a01:4f8:121:3121::2",
            )
            assertRegex(self, lines[6],
                        r"www.example.com\.\s+123\s+IN\s+A\s+127\.0\.0\.1")
            assertRegex(self, lines[7],
                        r"example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1")
            assertRegex(
                self, lines[8],
                r'test1.example.com\.\s+900\s+IN\s+TXT\s+"test foo bar"')
            assertRegex(
                self,
                lines[9],
                r'test2.example.com\.\s+900\s+IN\s+TXT\s+"test \\"foo\\" \\"bar\\""',
            )
            assertRegex(
                self, lines[10],
                r"example.com\.\s+900\s+IN\s+MX\s+10\s+mx.example.com")
            assertRegex(
                self,
                lines[11],
                r"example.com\.\s+900\s+IN\s+SRV\s+20\s+10 3333 example.com",
            )

    def test_get_numeric_id(self):
        values = MOCK_RECORDS_VALUES[0].copy()
        values["driver"] = self.driver
        values["zone"] = None
        record = Record(**values)

        record.id = "abcd"
        result = record._get_numeric_id()
        self.assertEqual(result, "abcd")

        record.id = "1"
        result = record._get_numeric_id()
        self.assertEqual(result, 1)

        record.id = "12345"
        result = record._get_numeric_id()
        self.assertEqual(result, 12345)

        record.id = ""
        result = record._get_numeric_id()
        self.assertEqual(result, "")

        record.id = None
        result = record._get_numeric_id()
        self.assertEqual(result, "")
Exemple #4
0
 def __init__(self):
     DNSDriver.__init__(self, None)
Exemple #5
0
 def setUp(self):
     self.driver = DNSDriver('none', 'none')
     self.tmp_file = tempfile.mkstemp()
     self.tmp_path = self.tmp_file[1]
Exemple #6
0
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = DNSDriver('none', 'none')
        self.tmp_file = tempfile.mkstemp()
        self.tmp_path = self.tmp_file[1]

    def test_export_zone_to_bind_format_slave_should_throw(self):
        zone = Zone(id=1,
                    domain='example.com',
                    type='slave',
                    ttl=900,
                    driver=self.driver)
        self.assertRaises(ValueError, zone.export_to_bind_format)

    def test_export_zone_to_bind_format_success(self):
        zone = Zone(id=1,
                    domain='example.com',
                    type='master',
                    ttl=900,
                    driver=self.driver)

        mock_records = []

        for values in MOCK_RECORDS_VALUES:
            values = values.copy()
            values['driver'] = self.driver
            values['zone'] = zone
            record = Record(**values)
            mock_records.append(record)

        self.driver.list_records = Mock()
        self.driver.list_records.return_value = mock_records

        result = self.driver.export_zone_to_bind_format(zone=zone)
        self.driver.export_zone_to_bind_zone_file(zone=zone,
                                                  file_path=self.tmp_path)

        with open(self.tmp_path, 'r') as fp:
            content = fp.read()

        lines1 = result.split('\n')
        lines2 = content.split('\n')

        for lines in [lines1, lines2]:
            self.assertEqual(len(lines), 2 + 1 + 9)
            self.assertRegexpMatches(lines[1], r'\$ORIGIN example\.com\.')
            self.assertRegexpMatches(lines[2], r'\$TTL 900')

            self.assertRegexpMatches(
                lines[4], r'www.example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(
                lines[5],
                r'www.example.com\.\s+900\s+IN\s+AAAA\s+2a01:4f8:121:3121::2')
            self.assertRegexpMatches(
                lines[6], r'www.example.com\.\s+123\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(
                lines[7], r'example.com\.\s+900\s+IN\s+A\s+127\.0\.0\.1')
            self.assertRegexpMatches(
                lines[8],
                r'test1.example.com\.\s+900\s+IN\s+TXT\s+"test foo bar"')
            self.assertRegexpMatches(
                lines[9],
                r'test2.example.com\.\s+900\s+IN\s+TXT\s+"test \\"foo\\" \\"bar\\""'
            )
            self.assertRegexpMatches(
                lines[10],
                r'example.com\.\s+900\s+IN\s+MX\s+10\s+mx.example.com')
            self.assertRegexpMatches(
                lines[11],
                r'example.com\.\s+900\s+IN\s+SRV\s+20\s+10 3333 example.com')
Exemple #7
0
 def setUp(self):
     self.driver = DNSDriver("none", "none")
     self.tmp_file = tempfile.mkstemp()
     self.tmp_path = self.tmp_file[1]
Exemple #8
0
 def setUp(self):
     self.driver = DNSDriver('none', 'none')
     self.tmp_file = tempfile.mkstemp()
     self.tmp_path = self.tmp_file[1]
Exemple #9
0
 def __init__(self):
     DNSDriver.__init__(self, None)