def ensure_domain_specified_in_flags_exists(self):
        """Make sure the domain in the FLAGS exists."""
        self.driver = RsDnsDriver(raise_if_zone_missing=False)
        assert_not_equal(None, self.driver.default_dns_zone)

        def zone_found():
            zones = self.driver.get_dns_zones()
            print("Retrieving zones.")
            for zone in zones:
                print("zone %s" % zone)
                if zone.name == self.driver.default_dns_zone.name:
                    self.driver.default_dns_zone.id = zone.id
                    global DNS_DOMAIN_ID
                    DNS_DOMAIN_ID = zone.id
                    return True
            return False

        if zone_found():
            return
        self.create_domain_if_needed()
        for i in range(5):
            if zone_found():
                return
        self.fail("""Could not find default dns zone.
                  This happens when they clear the staging DNS service of data.
                  To fix it, manually run the tests as follows:
                  $ ADD_DOMAINS=True python int_tests.py
                  and if all goes well the tests will create a new domain
                  record.""")
 def ensure_domain_specified_in_flags_exists(self):
     """Make sure the domain in the FLAGS exists."""
     self.driver = RsDnsDriver(raise_if_zone_missing=False)
     assert_not_equal(None, self.driver.default_dns_zone)
     def zone_found():
         zones = self.driver.get_dns_zones()
         print("Retrieving zones.")
         for zone in zones:
             print("zone %s" % zone)
             if zone.name == self.driver.default_dns_zone.name:
                 self.driver.default_dns_zone.id = zone.id
                 global DNS_DOMAIN_ID
                 DNS_DOMAIN_ID = zone.id
                 return True
         return False
     if zone_found():
         return
     self.create_domain_if_needed()
     for i in range(5):
         if zone_found():
             return
     self.fail("""Could not find default dns zone.
               This happens when they clear the staging DNS service of data.
               To fix it, manually run the tests as follows:
               $ ADD_DOMAINS=True python int_tests.py
               and if all goes well the tests will create a new domain
               record.""")
class RsDnsDriverTests(object):
    """Tests the RS DNS Driver."""
    def create_domain_if_needed(self):
        """Adds the domain specified in the flags."""
        print("Creating domain %s" % self.driver.default_dns_zone.name)
        future = self.driver.dns_client.domains.create(
            self.driver.default_dns_zone.name)
        while not future.ready:
            time.sleep(2)
        print("Got something: %s" % future.resource)
        with open('/home/vagrant/dns_resource.txt', 'w') as f:
            f.write('%r\n' % future.result[0].id)
        global DNS_DOMAIN_ID
        DNS_DOMAIN_ID = future.result[0].id
        print("The domain should have been created with id=%s" % DNS_DOMAIN_ID)

    @test
    @time_out(2 * 60)
    def ensure_domain_specified_in_flags_exists(self):
        """Make sure the domain in the FLAGS exists."""
        self.driver = RsDnsDriver(raise_if_zone_missing=False)
        assert_not_equal(None, self.driver.default_dns_zone)

        def zone_found():
            zones = self.driver.get_dns_zones()
            print("Retrieving zones.")
            for zone in zones:
                print("zone %s" % zone)
                if zone.name == self.driver.default_dns_zone.name:
                    self.driver.default_dns_zone.id = zone.id
                    global DNS_DOMAIN_ID
                    DNS_DOMAIN_ID = zone.id
                    return True
            return False

        if zone_found():
            return
        self.create_domain_if_needed()
        for i in range(5):
            if zone_found():
                return
        self.fail("""Could not find default dns zone.
                  This happens when they clear the staging DNS service of data.
                  To fix it, manually run the tests as follows:
                  $ ADD_DOMAINS=True python int_tests.py
                  and if all goes well the tests will create a new domain
                  record.""")

    @test(depends_on=[ensure_domain_specified_in_flags_exists],
          enabled=WHITE_BOX and FLAGS.dns_domain_name != "dbaas.rackspace.com")
    def delete_all_entries(self):
        """Deletes all entries under the default domain."""
        list = self.driver.get_entries()
        for entry in list:
            if entry.type == "A":
                self.driver.delete_entry(name=entry.name,
                                         type=entry.type,
                                         dns_zone=entry.dns_zone)
        # It takes awhile for them to be deleted.
        poll_until(lambda: self.driver.get_entries_by_name(TEST_NAME),
                   lambda list: len(list) == 0,
                   sleep_time=4,
                   time_out=60)

    @test(depends_on=[delete_all_entries])
    def create_test_entry(self):
        fullname = TEST_NAME
        entry = DnsEntry(name=fullname,
                         content=TEST_CONTENT,
                         type="A",
                         ttl=3600)
        self.driver.create_entry(entry)
        list = None
        for i in range(500):
            list = self.driver.get_entries_by_name(name=fullname)
            if len(list) > 0:
                break
            time.sleep(1)
        print("This is the list: %r" % list)
        assert_equal(1, len(list))
        list2 = self.driver.get_entries_by_content(content=TEST_CONTENT)
        assert_equal(1, len(list2))

    @test(depends_on=[delete_all_entries])
    def create_test_rsdns_entry(self):
        """Create an entry using the RsDnsInstanceEntryFactory."""
        instance = {'uuid': '000136c0-effa-4711-a747-a5b9fbfcb3bd', 'id': '10'}
        ip = "10.100.2.7"
        factory = RsDnsInstanceEntryFactory(dns_domain_id=DNS_DOMAIN_ID)
        entry = factory.create_entry(instance)
        entry.content = ip
        self.driver.create_entry(entry)
        entries = self.driver.get_entries_by_name(name=entry.name)
        assert_equal(1, len(entries))
        assert_equal(ip, entries[0].content)
        assert_equal(FLAGS.dns_ttl, entries[0].ttl)

    @test(depends_on=[create_test_entry])
    def delete_test_entry(self):
        fullname = TEST_NAME
        self.driver.delete_entry(fullname, "A")
        # It takes awhile for them to be deleted.
        poll_until(lambda: self.driver.get_entries_by_name(TEST_NAME),
                   lambda list: len(list) == 0,
                   sleep_time=2,
                   time_out=60)
class RsDnsDriverTests(object):
    """Tests the RS DNS Driver."""

    def create_domain_if_needed(self):
        """Adds the domain specified in the flags."""
        print("Creating domain %s" % self.driver.default_dns_zone.name)
        future = self.driver.dns_client.domains.create(
            self.driver.default_dns_zone.name)
        while not future.ready:
            time.sleep(2)
        print("Got something: %s" % future.resource)
        with open('/home/vagrant/dns_resource.txt', 'w') as f:
            f.write('%r\n' % future.result[0].id)
        global DNS_DOMAIN_ID
        DNS_DOMAIN_ID = future.result[0].id
        print("The domain should have been created with id=%s" % DNS_DOMAIN_ID)

    @test
    @time_out(2 * 60)
    def ensure_domain_specified_in_flags_exists(self):
        """Make sure the domain in the FLAGS exists."""
        self.driver = RsDnsDriver(raise_if_zone_missing=False)
        assert_not_equal(None, self.driver.default_dns_zone)
        def zone_found():
            zones = self.driver.get_dns_zones()
            print("Retrieving zones.")
            for zone in zones:
                print("zone %s" % zone)
                if zone.name == self.driver.default_dns_zone.name:
                    self.driver.default_dns_zone.id = zone.id
                    global DNS_DOMAIN_ID
                    DNS_DOMAIN_ID = zone.id
                    return True
            return False
        if zone_found():
            return
        self.create_domain_if_needed()
        for i in range(5):
            if zone_found():
                return
        self.fail("""Could not find default dns zone.
                  This happens when they clear the staging DNS service of data.
                  To fix it, manually run the tests as follows:
                  $ ADD_DOMAINS=True python int_tests.py
                  and if all goes well the tests will create a new domain
                  record.""")

    @test(depends_on=[ensure_domain_specified_in_flags_exists],
          enabled=WHITE_BOX and FLAGS.dns_domain_name != "dbaas.rackspace.com")
    def delete_all_entries(self):
        """Deletes all entries under the default domain."""
        list = self.driver.get_entries()
        for entry in list:
            if entry.type == "A":
                self.driver.delete_entry(name=entry.name, type=entry.type,
                                         dns_zone=entry.dns_zone)
        # It takes awhile for them to be deleted.
        poll_until(lambda : self.driver.get_entries_by_name(TEST_NAME),
                         lambda list : len(list) == 0,
                         sleep_time=4, time_out=60)

    @test(depends_on=[delete_all_entries])
    def create_test_entry(self):
        fullname = TEST_NAME
        entry = DnsEntry(name=fullname, content=TEST_CONTENT, type="A",
                         ttl=3600)
        self.driver.create_entry(entry)
        list = None
        for i in range(500):
            list = self.driver.get_entries_by_name(name=fullname)
            if len(list) > 0:
                break
            time.sleep(1)
        print("This is the list: %r" % list)
        assert_equal(1, len(list))
        list2 = self.driver.get_entries_by_content(content=TEST_CONTENT)
        assert_equal(1, len(list2))

    @test(depends_on=[delete_all_entries])
    def create_test_rsdns_entry(self):
        """Create an entry using the RsDnsInstanceEntryFactory."""
        instance = {'uuid': '000136c0-effa-4711-a747-a5b9fbfcb3bd', 'id': '10'}
        ip = "10.100.2.7"
        factory = RsDnsInstanceEntryFactory(dns_domain_id=DNS_DOMAIN_ID)
        entry = factory.create_entry(instance)
        entry.content = ip
        self.driver.create_entry(entry)
        entries = self.driver.get_entries_by_name(name=entry.name)
        assert_equal(1, len(entries))
        assert_equal(ip, entries[0].content)
        assert_equal(FLAGS.dns_ttl, entries[0].ttl)

    @test(depends_on=[create_test_entry])
    def delete_test_entry(self):
        fullname = TEST_NAME
        self.driver.delete_entry(fullname, "A")
        # It takes awhile for them to be deleted.
        poll_until(lambda : self.driver.get_entries_by_name(TEST_NAME),
                         lambda list : len(list) == 0,
                         sleep_time=2, time_out=60)