예제 #1
0
파일: ipv4_tests.py 프로젝트: OSU-Net/cyder
    def test_freeip(self):
        system = System(name='foobar', ctnr=self.ctnr)
        system.save()

        r = Range.objects.create(
            start_str="10.0.33.1",
            end_str="10.0.33.3",
            network=self.s,
            ip_type='4',
        )
        self.ctnr.ranges.add(r)

        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        s = StaticInterface.objects.create(
            label="foo1", domain=self.d, ip_type='4',
            ip_str=str(r.get_next_ip()), system=system,
            mac="00:00:00:00:00:01")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.2")
        s = StaticInterface.objects.create(
            label="foo2", domain=self.d, ip_type='4',
            ip_str=str(r.get_next_ip()), system=system,
            mac="00:00:00:00:00:01")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.3")
        s = StaticInterface.objects.create(
            label="foo3", domain=self.d, ip_type='4',
            ip_str=str(r.get_next_ip()), system=system,
            mac="00:00:00:00:00:01")
        self.assertEqual(r.get_next_ip(), None)
예제 #2
0
    def test_freeip(self):
        system = System(name='foobar', ctnr=self.ctnr)
        system.save()

        r = Range.objects.create(
            start_str="10.0.33.1",
            end_str="10.0.33.3",
            network=self.s,
            ip_type='4',
        )
        self.ctnr.ranges.add(r)

        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        s = StaticInterface.objects.create(label="foo1",
                                           domain=self.d,
                                           ip_type='4',
                                           ip_str=str(r.get_next_ip()),
                                           system=system,
                                           mac="00:00:00:00:00:01")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.2")
        s = StaticInterface.objects.create(label="foo2",
                                           domain=self.d,
                                           ip_type='4',
                                           ip_str=str(r.get_next_ip()),
                                           system=system,
                                           mac="00:00:00:00:00:01")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.3")
        s = StaticInterface.objects.create(label="foo3",
                                           domain=self.d,
                                           ip_type='4',
                                           ip_str=str(r.get_next_ip()),
                                           system=system,
                                           mac="00:00:00:00:00:01")
        self.assertEqual(r.get_next_ip(), None)
예제 #3
0
    def setUp(self):
        self.system = System()
        self.system.save()
        d1 = create_fake_zone("oregonstate.com", suffix="")
        soa = d1.soa
        self.soa = soa

        v, _ = Vlan.objects.get_or_create(name="private", number=3)
        s, _ = Site.objects.get_or_create(name="phx1")
        s1, _ = Site.objects.get_or_create(name="corp", parent=s)
        d, _ = Domain.objects.get_or_create(name="phx1.oregonstate.com")
        d.soa = soa
        d.save()
        d1, _ = Domain.objects.get_or_create(name="corp.phx1.oregonstate.com")
        d1.soa = soa
        d1.save()
        d2, _ = Domain.objects.get_or_create(
            name="private.corp.phx1.oregonstate.com")
        d2.soa = soa
        d2.save()

        d, _ = Domain.objects.get_or_create(name="arpa")
        d, _ = Domain.objects.get_or_create(name="in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="15.in-addr.arpa")
        n = Network(network_str="15.0.0.0/8", ip_type="4")
        n.clean()
        n.site = s1
        n.vlan = v
        n.save()

        r = Range(start_str="15.0.0.0", end_str="15.0.0.10",
                  network=n, ip_type='4')
        r.clean()
        r.save()
예제 #4
0
파일: test_models.py 프로젝트: jirwin/cyder
    def setUp(self):
        self.g = create_fake_zone("gz", suffix="")
        self.c_g = create_fake_zone("coo.gz", suffix="")
        self.d = create_fake_zone("dz", suffix="")

        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.r1.save()

        self.s = System()
        self.s.save()
예제 #5
0
class DeleteStaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = "4"
        if name in ("arpa", "in-addr.arpa", "ip6.arpa"):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name="arpa")
        self.arpa.save()
        self.i_arpa = self.create_domain(name="in-addr.arpa")
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")

    def do_add(self, mac, label, domain, ip_str, system, ip_type="4"):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str, ip_type=ip_type, system=system)
        r.clean()
        r.save()
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_delete_basic(self):
        # Does deleting a system delete it's interfaces?
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        system = System()
        system.save()
        kwargs = {"mac": mac, "label": label, "domain": domain, "ip_str": ip_str, "system": system}
        self.do_add(**kwargs)
        self.assertTrue(StaticInterface.objects.filter(**kwargs))
        system.delete()
        self.assertFalse(StaticInterface.objects.filter(**kwargs))
예제 #6
0
 def do_add(self, mac, label, domain, ip_str, ip_type='4'):
     self.n = System()
     r = StaticInterface(mac=mac,
                         label=label,
                         domain=domain,
                         ip_str=ip_str,
                         ip_type=ip_type,
                         system=self.n)
     r.clean()
     r.save()
     repr(r)
     return r
예제 #7
0
 def test1_delete_basic(self):
     # Does deleting a system delete it's interfaces?
     mac = "11:22:33:44:55:66"
     label = "foo"
     domain = self.f_c
     ip_str = "10.0.0.2"
     system = System()
     system.save()
     kwargs = {"mac": mac, "label": label, "domain": domain, "ip_str": ip_str, "system": system}
     self.do_add(**kwargs)
     self.assertTrue(StaticInterface.objects.filter(**kwargs))
     system.delete()
     self.assertFalse(StaticInterface.objects.filter(**kwargs))
예제 #8
0
파일: A_tests.py 프로젝트: jirwin/cyder
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
예제 #9
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")
예제 #10
0
파일: auto_delete.py 프로젝트: jirwin/cyder
    def test_cleanup_intr(self):
        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.poo"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.poo"))
        self.assertFalse(Domain.objects.filter(name="z.foo.poo"))
        self.assertTrue(Domain.objects.filter(name="foo.poo"))

        Domain.objects.get_or_create(name="arpa")
        Domain.objects.get_or_create(name="in-addr.arpa")
        Domain.objects.get_or_create(name="10.in-addr.arpa")

        fqdn = "bar.x.y.z.foo.poo"
        label, the_domain = ensure_label_domain(fqdn)
        system = System()
        addr = StaticInterface(label=label,
                               domain=the_domain,
                               ip_type='4',
                               ip_str="10.2.3.4",
                               mac="00:11:22:33:44:55",
                               system=system)
        addr.save()
        self.assertFalse(prune_tree(the_domain))
        addr.delete()

        self.assertFalse(Domain.objects.filter(name="x.y.z.foo.poo"))
        self.assertFalse(Domain.objects.filter(name="y.z.foo.poo"))
        self.assertFalse(Domain.objects.filter(name="z.foo.poo"))
        self.assertTrue(Domain.objects.filter(name="foo.poo"))
예제 #11
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='ip6.arpa', ip_type='6')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="0", ip_type='6')
        self.r1.save()
        self.r2 = self.create_domain(name="1", ip_type='6')
        self.r2.save()
        self.n = System()
        self.n.clean()
        self.n.save()
예제 #12
0
 def do_add(self, mac, label, domain, ip_str, ip_type='4'):
     self.n = System()
     r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                         ip_type=ip_type, system=self.n)
     r.clean()
     r.save()
     repr(r)
     return r
예제 #13
0
파일: dirty_soa.py 프로젝트: jirwin/cyder
    def setUp(self):
        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.sr = self.r1.soa
        self.sr.dirty = False
        self.sr.save()

        self.dom = create_fake_zone("bgaz", suffix="")
        self.soa = self.dom.soa
        self.soa.dirty = False
        self.soa.save()

        self.rdom = create_fake_zone("123.in-addr.arpa", suffix="")
        self.rsoa = self.r1.soa
        self.rsoa.dirty = False
        self.rsoa.save()

        self.s = System()
        self.s.save()
예제 #14
0
    def setUp(self):
        self.g = create_fake_zone("gz", suffix="")
        self.c_g = create_fake_zone("coo.gz", suffix="")
        self.d = create_fake_zone("dz", suffix="")

        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.r1.save()

        self.s = System()
        self.s.save()
예제 #15
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()
        self.i6_arpa = self.create_domain(name='ip6.arpa')
        self.i6_arpa.save()

        self.r = Domain(name="ru")
        self.r.save()
        self.f_r = Domain(name="foo.ru")
        self.f_r.save()
        self.b_f_r = Domain(name="bar.foo.ru")
        self.b_f_r.save()

        self.f = Domain(name="fam")
        self.f.save()

        self._128 = self.create_domain(name='128', ip_type='4')
        self._128.save()

        self.s = System()
        self.s.save()
예제 #16
0
    def setUp(self):
        self.o = Domain(name="org")
        self.o.save()
        self.f_o = Domain(name="foo.org")
        self.f_o.save()
        self.s = System()

        Domain.objects.get_or_create(name="arpa")
        Domain.objects.get_or_create(name="in-addr.arpa")
        Domain.objects.get_or_create(name="10.in-addr.arpa")
        Domain.objects.get_or_create(name="172.in-addr.arpa")
        Domain.objects.get_or_create(name="192.in-addr.arpa")

        self.public, _ = View.objects.get_or_create(name="public")
        self.private, _ = View.objects.get_or_create(name="private")
예제 #17
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
예제 #18
0
파일: basic.py 프로젝트: alecdhuse/cyder
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")
예제 #19
0
파일: ipv4_tests.py 프로젝트: jirwin/cyder
    def test1_freeip(self):
        start_str = "10.0.33.1"
        end_str = "10.0.33.3"
        default_domain = self.d
        network = self.s
        rtype = 's'
        ip_type = '4'
        system = System()

        kwargs = {
            'start_str': start_str,
            'end_str': end_str,
            'default_domain': default_domain,
            'network': network,
            'rtype': rtype,
            'ip_type': ip_type
        }
        r = self.do_add(**kwargs)
        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        self.assertEqual(str(r.get_next_ip()), "10.0.33.1")
        s = StaticInterface(label="foo",
                            domain=self.d,
                            ip_type='4',
                            ip_str=str(r.get_next_ip()),
                            system=system,
                            mac="00:00:00:00:00:00")
        s.clean()
        s.save()
        self.assertEqual(str(r.get_next_ip()), "10.0.33.2")
        s = StaticInterface(label="foo",
                            domain=self.d,
                            ip_type='4',
                            ip_str=str(r.get_next_ip()),
                            system=system,
                            mac="00:00:00:00:00:00")
        s.clean()
        s.save()
        self.assertEqual(str(r.get_next_ip()), "10.0.33.3")
        s = StaticInterface(label="foo",
                            domain=self.d,
                            ip_type='4',
                            ip_str=str(r.get_next_ip()),
                            system=system,
                            mac="00:00:00:00:00:00")
        s.clean()
        s.save()
        self.assertEqual(r.get_next_ip(), None)
예제 #20
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='ip6.arpa', ip_type='6')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="0", ip_type='6')
        self.r1.save()
        self.r2 = self.create_domain(name="1", ip_type='6')
        self.r2.save()
        self.n = System()
        self.n.clean()
        self.n.save()
예제 #21
0
    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()
        self.i6_arpa = self.create_domain(name='ip6.arpa')
        self.i6_arpa.save()

        self.r = Domain(name="ru")
        self.r.save()
        self.f_r = Domain(name="foo.ru")
        self.f_r.save()
        self.b_f_r = Domain(name="bar.foo.ru")
        self.b_f_r.save()

        self.f = Domain(name="fam")
        self.f.save()

        self._128 = self.create_domain(name='128', ip_type='4')
        self._128.save()

        self.s = System()
        self.s.save()
예제 #22
0
 def test1_delete_basic(self):
     # Does deleting a system delete it's interfaces?
     mac = "11:22:33:44:55:66"
     label = "foo"
     domain = self.f_c
     ip_str = "10.0.0.2"
     system = System()
     system.save()
     kwargs = {
         'mac': mac,
         'label': label,
         'domain': domain,
         'ip_str': ip_str,
         'system': system
     }
     self.do_add(**kwargs)
     self.assertTrue(StaticInterface.objects.filter(**kwargs))
     system.delete()
     self.assertFalse(StaticInterface.objects.filter(**kwargs))
예제 #23
0
class NSTestsModels(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()
        self.i6_arpa = self.create_domain(name='ip6.arpa')
        self.i6_arpa.save()

        self.r = Domain(name="ru")
        self.r.save()
        self.f_r = Domain(name="foo.ru")
        self.f_r.save()
        self.b_f_r = Domain(name="bar.foo.ru")
        self.b_f_r.save()

        self.f = Domain(name="fam")
        self.f.save()

        self._128 = self.create_domain(name='128', ip_type='4')
        self._128.save()

        self.s = System()
        self.s.save()

    def do_add(self, domain, server):
        ns = Nameserver(domain=domain, server=server)
        ns.save()
        self.assertTrue(ns.__repr__())
        self.assertTrue(ns.details())
        ret = Nameserver.objects.filter(domain=domain, server=server)
        self.assertEqual(len(ret), 1)
        return ns

    def test_add_ns(self):
        data = {'domain': self.r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': 'ns5.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': u'ns3.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.b_f_r, 'server': 'n1.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.b_f_r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': 'asdf.asdf'}
        self.do_add(**data)

    def test_add_invalid(self):
        data = {'domain': self.f_r, 'server': 'ns3.foo.ru'}
        self.assertRaises(ValidationError, self.do_add, **data)

    def testtest_add_ns_in_domain(self):
        # Use an A record as a glue record.
        glue = AddressRecord(label='ns2', domain=self.r,
                             ip_str='128.193.1.10', ip_type='4')
        glue.clean()
        glue.save()
        data = {'domain': self.r, 'server': 'ns2.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)
        self.assertRaises(ValidationError, glue.delete)

        glue = AddressRecord(label='ns3', domain=self.f_r,
                             ip_str='128.193.1.10', ip_type='4')
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns3.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

    def test_disallow_name_update_of_glue_A(self):
        # Glue records should not be allowed to change their name.
        glue = AddressRecord(label='ns39', domain=self.f_r,
                             ip_str='128.193.1.77', ip_type='4')
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns39.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue.label = "ns22"
        self.assertRaises(ValidationError, glue.clean)

    def test_disallow_name_update_of_glue_Intr(self):
        # Glue records should not be allowed to change their name.
        glue = StaticInterface(label='ns24', domain=self.f_r,
                               ip_str='128.193.99.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns24.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue.label = "ns22"
        self.assertRaises(ValidationError, glue.clean)

    def test_disallow_delete_of_glue_intr(self):
        # Interface glue records should not be allowed to be deleted.
        glue = StaticInterface(label='ns24', domain=self.f_r,
                               ip_str='128.193.99.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns24.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        self.assertRaises(ValidationError, glue.delete)

    def test_manual_assign_of_glue(self):
        # Test that assigning a different glue record doesn't get overriden by
        # the auto assinging during the Nameserver's clean function.
        glue = StaticInterface(label='ns25', domain=self.f_r,
                               ip_str='128.193.99.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns25.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue2 = AddressRecord(label='ns25', domain=self.f_r,
                              ip_str='128.193.1.78', ip_type='4')
        glue2.clean()
        glue2.save()

        ns.clean()

        # Make sure things didn't get overriden.
        self.assertEqual(ns.glue, glue)

        ns.glue = glue2
        ns.save()
        # Refresh the object
        ns = Nameserver.objects.get(pk=ns.pk)
        # Again, Make sure things didn't get overriden.
        self.assertEqual(ns.glue, glue2)
        # Make sure we still can't delete.
        self.assertRaises(ValidationError, glue2.delete)
        self.assertRaises(ValidationError, ns.glue.delete)

        # We shuold be able to delelte the other one.
        glue.delete()

    def testtest_add_ns_in_domain_intr(self):
        # Use an Interface as a glue record.
        glue = StaticInterface(label='ns232', domain=self.r,
                               ip_str='128.193.99.10', ip_type='4',
                               system=self.s, mac="12:23:45:45:45:45")
        glue.clean()
        glue.save()
        data = {'domain': self.r, 'server': 'ns232.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)
        self.assertRaises(ValidationError, glue.delete)

        glue = StaticInterface(label='ns332', domain=self.f_r,
                               ip_str='128.193.1.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns332.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

    def test_add_ns_outside_domain(self):
        data = {'domain': self.f_r, 'server': 'ns2.ru'}
        ns = self.do_add(**data)
        self.assertFalse(ns.glue)

    def test_update_glue_to_no_intr(self):
        glue = StaticInterface(label='ns34', domain=self.r,
                               ip_str='128.193.1.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.save()
        data = {'domain': self.r, 'server': 'ns34.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)

        ns.server = "ns4.wee"
        ns.save()
        self.assertTrue(ns.glue is None)

    def test_update_glue_record_intr(self):
        # Glue records can't change their name.
        glue = StaticInterface(label='ns788', domain=self.r,
                               ip_str='128.193.1.10', ip_type='4',
                               system=self.s, mac="11:22:33:44:55:66")
        glue.save()
        data = {'domain': self.r, 'server': 'ns788.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        glue.label = "asdfasdf"
        self.assertRaises(ValidationError, glue.clean)

    def test_update_glue_to_no_glue(self):
        glue = AddressRecord(label='ns3', domain=self.r,
                             ip_str='128.193.1.10', ip_type='4')
        glue.save()
        data = {'domain': self.r, 'server': 'ns3.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)

        ns.server = "ns4.wee"
        ns.save()
        self.assertTrue(ns.glue is None)

    def test_delete_ns(self):
        glue = AddressRecord(label='ns4', domain=self.f_r,
                             ip_str='128.196.1.10', ip_type='4')
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns4.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

        ns.delete()
        nsret = Nameserver.objects.filter(
            server='ns2.foo.ru', domain=self.f_r)
        self.assertFalse(nsret)

    def test_invalid_create(self):
        glue = AddressRecord(label='ns2', domain=self.r,
                             ip_str='128.193.1.10', ip_type='4')
        glue.save()

        data = {'domain': self.r, 'server': 'ns2 .ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2$.ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2..ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2.ru '}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': ''}
        self.assertRaises(ValidationError, self.do_add, **data)

    def test_add_dup(self):
        data = {'domain': self.r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        self.assertRaises(ValidationError, self.do_add, **data)

    def _get_post_data(self, random_str):
        """Return a valid set of data"""
        return {
            'root_domain': '{0}.oregonstate.com'.format(random_str),
            'soa_primary': 'ns1.oregonstate.com',
            'soa_contact': 'noc.oregonstate.com',
            'nameserver_1': 'ns1.oregonstate.com',
            'ttl_1': '1234'
        }

    def test_bad_nameserver_soa_state_case_1_0(self):
        # This is Case 1
        root_domain = create_fake_zone("asdf10")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        a = AddressRecord(
            label='', domain=root_domain, ip_type="6", ip_str="1::")
        self.assertRaises(ValidationError, a.save)
        cn = CNAME(label='', domain=root_domain, target="asdf")
        self.assertRaises(ValidationError, cn.save)

    def test_bad_nameserver_soa_state_case_1_1(self):
        # This is Case 1
        root_domain = create_fake_zone("asdf111")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        self.assertRaises(ValidationError, a.save)
        cn = CNAME(label='', domain=cdomain, target="asdf")
        self.assertRaises(ValidationError, cn.save)

    def test_bad_nameserver_soa_state_case_1_2(self):
        # This is Case 1 ... with ptr's
        root_domain = create_fake_zone("12.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        ptr = PTR(name="asdf", ip_str="12.10.1.1", ip_type="4")
        self.assertRaises(ValidationError, ptr.save)

    def test_bad_nameserver_soa_state_case_1_3(self):
        # This is Case 1 ... with ptr's
        root_domain = create_fake_zone("13.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="10.13.in-addr.arpa")
        cdomain.soa = root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        ptr = PTR(name="asdf", ip_str="13.10.1.1", ip_type="4")
        self.assertRaises(ValidationError, ptr.save)

    def test_bad_nameserver_soa_state_case_1_4(self):
        # This is Case 1 ... with StaticInterfaces's
        reverse_root_domain = create_fake_zone("14.in-addr.arpa", suffix="")
        root_domain = create_fake_zone("asdf14")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="10.14.in-addr.arpa")
        cdomain.soa = reverse_root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        intr = StaticInterface(
            label="asdf", domain=root_domain, ip_str="14.10.1.1", ip_type="4",
            mac="11:22:33:44:55:66", system=self.s)
        self.assertRaises(ValidationError, intr.save)

    # See record.tests for the case a required view is deleted.
    def test_bad_nameserver_soa_state_case_2_0(self):
        # This is Case 2
        root_domain = create_fake_zone("asdf20")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        a = AddressRecord(
            label='', domain=root_domain, ip_type="6", ip_str="1::")
        a.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_1(self):
        # This is Case 2
        root_domain = create_fake_zone("asdf21")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        # Let's create a child domain and add a record there, then try to
        # delete the NS record
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        a.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_2(self):
        # This is Case 2 ... with ptrs
        root_domain = create_fake_zone("22.in-addr.arpa", suffix="")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        ptr = PTR(name="asdf", ip_str="22.1.1.1", ip_type="4")
        ptr.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_3(self):
        # This is Case 2 ... with ptrs
        root_domain = create_fake_zone("10.23.in-addr.arpa", suffix="")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        # Let's create a child domain and add a record there, then try to
        # delete the NS record
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        ptr = PTR(name="asdf", ip_str="23.10.1.1", ip_type="4")
        ptr.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_3_0(self):
        # This is Case 3
        root_domain = create_fake_zone("asdf30")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.
        a = AddressRecord(
            label='', domain=root_domain, ip_type="6", ip_str="1::")
        a.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)

    def test_bad_nameserver_soa_state_case_3_1(self):
        # This is Case 3
        root_domain = create_fake_zone("asdf31")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain pointed at an SOA record with no
        # records attached to it (esspecially no ns recods). It also has no
        # child domains.
        # Try case 3 but add a record to a child domain of root_domain
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.save()

        # Add a record to the domain.
        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        a.save()

        # Now try to add the domain to the zone that has no NS records at it's
        # root
        cdomain.soa = root_domain.soa

        self.assertRaises(ValidationError, cdomain.save)

    def test_bad_nameserver_soa_state_case_3_2(self):
        # This is Case 3 ... with ptrs
        root_domain = create_fake_zone("32.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.

        ptr = PTR(name="asdf", ip_str="32.1.1.1", ip_type="4")
        ptr.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)

    def test_bad_nameserver_soa_state_case_3_3(self):
        # This is Case 3 ... with ptrs
        root_domain = create_fake_zone("33.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain pointed at an SOA record with no
        # records attached to it (esspecially no ns recods). It also has no
        # child domains.
        # Try case 3 but add a record to a child domain of root_domain
        cdomain = Domain(name="10.33.in-addr.arpa")
        cdomain.save()

        # Add a record to the domain.
        ptr = PTR(name="asdf", ip_str="33.10.1.1", ip_type="4")
        ptr.save()

        # Now try to add the domain to the zone that has no NS records at it's
        # root
        cdomain.soa = root_domain.soa

        self.assertRaises(ValidationError, cdomain.save)
예제 #24
0
class V6StaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='ip6.arpa', ip_type='6')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="0", ip_type='6')
        self.r1.save()
        self.r2 = self.create_domain(name="1", ip_type='6')
        self.r2.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add(self, mac, label, domain, ip_str, ip_type='6'):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "12::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test2_create_basic(self):
        mac = "20:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "123::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test3_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "1234::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test4_create_basic(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "11::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test1_delete(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "112::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        r = self.do_add(**kwargs)
        self.do_delete(r)

    def test1_dup_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo3"
        domain = self.f_c
        ip_str = "1123::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_add_for_a_ptr(self):
        # Intr exists, then try ptr and A
        mac = "11:22:33:44:55:6e"
        label = "9988fooddfdf"
        domain = self.c
        ip_str = "111::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        ip_type = '6'
        i = self.do_add(**kwargs)
        i.clean()
        i.save()
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        self.assertRaises(ValidationError, a.clean)
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=i.fqdn)
        self.assertRaises(ValidationError, ptr.clean)

    def test2_bad_add_for_a_ptr(self):
        # PTR and A exist, then try add intr
        mac = "11:22:33:44:55:66"
        label = "9988fdfood"
        domain = self.c
        ip_str = "1112::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        ip_type = '6'
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        a.clean()
        a.save()
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=a.fqdn)
        ptr.clean()
        ptr.save()
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_reverse_domain(self):
        mac = "11:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "115::" + mac
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add(**kwargs)
        i.ip_str = "9111::"
        self.assertRaises(ValidationError, i.save)

    def test1_no_system(self):
        mac = "15:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "188::" + mac
        ip_type = '6'
        r = StaticInterface(label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=None)
        self.assertRaises(ValidationError, r.clean)
예제 #25
0
 def setUp(self):
     self.system = System()
     Domain.objects.get_or_create(name="com")
     Domain.objects.get_or_create(name="mozilla.com")
예제 #26
0
def migrate_dynamic_hosts():
    print "Migrating dynamic hosts."
    default, _ = Workgroup.objects.get_or_create(name='default')

    sys_value_keys = {"type": "Hardware Type",
                      "os": "Operating System",
                      "location": "Location",
                      "department": "Department",
                      "serial": "Serial Number",
                      "other_id": "Other ID",
                      "purchase_date": "Purchase Date",
                      "po_number": "PO Number",
                      "warranty_date": "Warranty Date",
                      "owning_unit": "Owning Unit",
                      "user_id": "User ID"}

    keys = ("id", "dynamic_range", "name", "workgroup", "enabled", "ha",
            "type", "os", "location", "department", "serial", "other_id",
            "purchase_date", "po_number", "warranty_date", "owning_unit",
            "user_id", "last_seen", "expire", "ttl", "last_update", "domain",
            "zone")

    sql = "SELECT %s FROM host WHERE ip = 0" % ", ".join(keys)

    count = 0
    cursor.execute(sql)
    for values in cursor.fetchall():
        items = dict(zip(keys, values))
        enabled = items['enabled']

        if len(items['ha']) == 0:
            mac = None
        elif len(items['ha']) == 12:
            if items['ha'] == '0' * 12:
                mac = None
                enabled = False
            else:
                mac = items['ha']
        else:
            stderr.write(
                'Host with id {} has invalid hardware address "{}"'.format(
                    items['id'], items['ha']))
            continue

        # TODO: Verify that there is no valid range/zone/workgroup with id 0
        r, c, w = None, None, default
        if items['dynamic_range']:
            try:
                r = maintain_find_range(items['dynamic_range'])
            except ObjectDoesNotExist:
                stderr.write(
                    'Could not create dynamic interface %s: Range %s '
                    'is in Maintain, but was not created in Cyder.' %
                    (items['ha'], items['dynamic_range']))

        if items['zone']:
            c = maintain_find_zone(items['zone'])

        if items['workgroup']:
            w = maintain_find_workgroup(items['workgroup'])

        if not all([r, c]):
            stderr.write('Trouble migrating host with mac {0}\n'
                         .format(items['ha']))
            continue

        s = System(name=items['name'])
        s.save()
        for key in sys_value_keys.keys():
            value = items[key].strip()
            if not value or value == '0':
                continue

            attr = Attribute.objects.get(
                name=fix_attr_name(sys_value_keys[key]))
            eav = SystemAV(entity=s, attribute=attr, value=value)
            eav.full_clean()
            eav.save()

        last_seen = items['last_seen'] or None
        if last_seen:
            last_seen = datetime.fromtimestamp(last_seen)

        intr = DynamicInterface(
            range=r, workgroup=w, ctnr=c, mac=mac, system=s,
            dhcp_enabled=enabled, last_seen=last_seen)
        try:
            intr.save(update_range_usage=False, commit=False)
        except ValidationError as e:
            try:
                intr.dhcp_enabled = False
                intr.save(update_range_usage=False)
                stderr.write(
                    'WARNING: Dynamic interface with MAC address {} has been '
                    'disabled\n'.format(intr.mac))
                stderr.write('    {}\n'.format(e))
            except ValidationError as e:
                stderr.write(
                    'WARNING: Could not create dynamic interface with MAC '
                    'address {}\n'.format(intr.mac))
                stderr.write('    {}\n'.format(e))
                intr = None

        if intr:
            count += 1
            if not count % 1000:
                print "%s valid hosts found so far." % count

    print "%s valid hosts found." % count
예제 #27
0
파일: A_tests.py 프로젝트: jirwin/cyder
class AStaticRegTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add_intr(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_add_a(self, label, domain, ip_str, ip_type='4'):
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        a.clean()
        a.save()
        return a

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_conflict_add_intr_first(self):
        # Add an intr and make sure A can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo4"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add_intr(**kwargs)
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.assertRaises(ValidationError, self.do_add_a, **kwargs)

    def test1_conflict_add_A_first(self):
        # Add an A and make sure an intr can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo5"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_a(**kwargs)
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.assertRaises(ValidationError, self.do_add_intr, **kwargs)

    def test2_conflict_add_intr_first(self):
        # Add an intr and update an existing A to conflict. Test for exception.
        mac = "12:22:33:44:55:66"
        label = "fo99"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add_intr(**kwargs)
        ip_str = "10.0.0.3"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        a = self.do_add_a(**kwargs)
        a.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, a.save)

    def test2_conflict_add_A_first(self):
        # Add an A and update and existing intr to conflict. Test for
        # exception.
        mac = "11:22:33:44:55:66"
        label = "foo98"
        domain = self.f_c
        ip_str = "10.0.0.2"
        # Add A
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_a(**kwargs)

        # Add Intr with diff IP
        ip_str = "10.0.0.3"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add_intr(**kwargs)

        # Conflict the IP on the intr
        intr.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, intr.save)
예제 #28
0
파일: dirty_soa.py 프로젝트: jirwin/cyder
class DirtySOATests(TestCase):
    def setUp(self):
        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.sr = self.r1.soa
        self.sr.dirty = False
        self.sr.save()

        self.dom = create_fake_zone("bgaz", suffix="")
        self.soa = self.dom.soa
        self.soa.dirty = False
        self.soa.save()

        self.rdom = create_fake_zone("123.in-addr.arpa", suffix="")
        self.rsoa = self.r1.soa
        self.rsoa.dirty = False
        self.rsoa.save()

        self.s = System()
        self.s.save()

    def test_print_soa(self):
        self.assertTrue(self.soa.bind_render_record() not in ('', None))
        self.assertTrue(self.rsoa.bind_render_record() not in ('', None))

    def generic_dirty(self, Klass, create_data, update_data, local_soa,
                      tdiff=1):
        Task.dns.all().delete()  # Delete all tasks
        local_soa.dirty = False
        local_soa.save()
        rec = Klass(**create_data)
        rec.full_clean()
        rec.save()
        self.assertTrue(rec.bind_render_record() not in ('', None))
        local_soa = SOA.objects.get(pk=local_soa.pk)
        self.assertTrue(local_soa.dirty)

        self.assertEqual(tdiff, Task.dns.all().count())

        # Now try updating
        Task.dns.all().delete()  # Delete all tasks
        local_soa.dirty = False
        local_soa.save()
        local_soa = SOA.objects.get(pk=local_soa.pk)
        self.assertFalse(local_soa.dirty)
        for k, v in update_data.iteritems():
            setattr(rec, k, v)
        rec.save()
        local_soa = SOA.objects.get(pk=local_soa.pk)
        self.assertTrue(local_soa.dirty)

        self.assertEqual(tdiff, Task.dns.all().count())

        # Now delete
        Task.dns.all().delete()  # Delete all tasks
        local_soa.dirty = False
        local_soa.save()
        local_soa = SOA.objects.get(pk=local_soa.pk)
        self.assertFalse(local_soa.dirty)
        rec.delete()
        local_soa = SOA.objects.get(pk=local_soa.pk)
        self.assertTrue(local_soa.dirty)

        self.assertEqual(tdiff, Task.dns.all().count())

    def test_dirty_a(self):
        create_data = {
            'label': 'asdf',
            'domain': self.dom,
            'ip_str': '10.2.3.1',
            'ip_type': '4'
        }
        update_data = {
            'label': 'asdfx',
        }
        self.generic_dirty(AddressRecord, create_data, update_data, self.soa)

    def test_dirty_intr(self):
        create_data = {
            'label': 'asdf1',
            'domain': self.dom,
            'ip_str': '10.2.3.1',
            'ip_type': '4',
            'system': self.s,
            'mac': '11:22:33:44:55:66'
        }
        update_data = {
            'label': 'asdfx1',
        }
        self.generic_dirty(StaticInterface, create_data, update_data, self.soa,
                           tdiff=2)

    def test_dirty_cname(self):
        create_data = {
            'label': 'asdf2',
            'domain': self.dom,
            'target': 'foo.bar.com',
        }
        update_data = {
            'label': 'asdfx2',
        }
        self.generic_dirty(CNAME, create_data, update_data, self.soa)

    def test_dirty_ptr(self):
        create_data = {
            'ip_str': '10.2.3.4',
            'ip_type': '4',
            'name': 'foo.bar.com',
        }
        update_data = {
            'label': 'asdfx2',
        }
        self.generic_dirty(PTR, create_data, update_data, local_soa=self.sr)

    def test_dirty_mx(self):
        create_data = {
            'label': '',
            'domain': self.dom,
            'priority': 10,
            'server': 'foo.bar.com',
        }
        update_data = {
            'label': 'asdfx3',
        }
        self.generic_dirty(MX, create_data, update_data, self.soa)

    def test_dirty_ns(self):
        create_data = {
            'domain': self.dom,
            'server': 'foo.bar.com',
        }
        update_data = {
            'label': 'asdfx4',
        }
        self.generic_dirty(Nameserver, create_data, update_data, self.soa)

    def test_dirty_soa(self):
        self.soa.dirty = False
        self.soa.refresh = 123
        self.soa.save()
        self.assertTrue(self.soa.dirty)

    def test_dirty_srv(self):
        create_data = {
            'label': '_asdf7',
            'domain': self.dom,
            'priority': 10,
            'port': 10,
            'weight': 10,
            'target': 'foo.bar.com',
        }
        update_data = {
            'label': '_asdfx4',
        }
        self.generic_dirty(SRV, create_data, update_data, self.soa)

    def test_dirty_txt(self):
        create_data = {
            'label': 'asdf8',
            'domain': self.dom,
            'txt_data': 'some shit',
        }
        update_data = {
            'label': 'asdfx5',
        }
        self.generic_dirty(TXT, create_data, update_data, self.soa)
예제 #29
0
class PTRStaticRegTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add_intr(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_add_ptr(self, label, domain, ip_str, ip_type='4'):
        ptr = PTR(name=label + '.' + domain.name, ip_str=ip_str,
                  ip_type=ip_type)
        ptr.clean()
        ptr.save()
        return ptr

    def test1_conflict_add_intr_first(self):
        # PTRdd an intr and make sure PTR can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo4"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add_intr(**kwargs)
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.assertRaises(ValidationError, self.do_add_ptr, **kwargs)

    def test1_conflict_add_PTR_first(self):
        # Add an PTR and make sure an intr can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo5"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_ptr(**kwargs)
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.assertRaises(ValidationError, self.do_add_intr, **kwargs)

    def test2_conflict_add_intr_first(self):
        # Add an intr and update an existing PTR to conflict. Test for exception.
        mac = "12:22:33:44:55:66"
        label = "fo99"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add_intr(**kwargs)
        ip_str = "10.0.0.3"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        ptr = self.do_add_ptr(**kwargs)
        ptr.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, ptr.clean)

    def test2_conflict_add_A_first(self):
        # Add an PTR and update and existing intr to conflict. Test for exception.
        mac = "11:22:33:44:55:66"
        label = "foo98"
        domain = self.f_c
        ip_str = "10.0.0.2"
        # Add PTR
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_ptr(**kwargs)

        # Add Intr with diff IP
        ip_str = "10.0.0.3"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add_intr(**kwargs)

        # Conflict the IP on the intr
        intr.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, intr.save)
예제 #30
0
    def gen_static(self):
        """
        Generates the Static Interface objects related to this zone's domain.

        .. note::
            Every static interface needs a system.

        :System uniqueness: hostname, mac, ip_str

        :StaticInterface uniqueness: hostname, mac, ip_str
        """
        from dhcp_migrate import migrate_zones

        if Ctnr.objects.count() <= 2:
            print "WARNING: Zones not migrated. Attempting to migrate now."
            migrate_zones()

        sys_value_keys = {"type": "Hardware Type",
                          "os": "Operating System",
                          "location": "Location",
                          "department": "Department",
                          "serial": "Serial Number",
                          "other_id": "Other ID",
                          "purchase_date": "Purchase Date",
                          "po_number": "PO Number",
                          "warranty_date": "Warranty Date",
                          "owning_unit": "Owning Unit",
                          "user_id": "User ID"}

        keys = ("host.id", "ip", "host.name", "zone.name", "workgroup",
                "enabled", "ha", "zone", "type", "os", "location",
                "department", "serial", "other_id", "purchase_date",
                "po_number", "warranty_date", "owning_unit", "user_id",
                "last_seen", "expire", "ttl", "last_update")

        sql = ("SELECT %s FROM host JOIN zone ON host.zone = zone.id "
               "WHERE ip != 0 AND domain = '%s';" %
               (", ".join(keys), self.domain_id))

        cursor.execute(sql)
        for values in cursor.fetchall():
            items = dict(zip(keys, values))
            ctnr = self.ctnr_from_zone_name(items['zone.name'])
            if ctnr is None:
                continue

            name = items['host.name']
            enabled = bool(items['enabled'])
            dns_enabled, dhcp_enabled = enabled, enabled
            ip = items['ip']
            ha = items['ha']
            if ip == 0:
                continue

            if len(ha) != 12 or ha == '0' * 12:
                ha = ""

            if ha == "":
                dhcp_enabled = False

            # check for duplicate
            static = StaticInterface.objects.filter(
                label=name, mac=(clean_mac(ha) or None), ip_str=long2ip(ip))
            if static:
                stderr.write("Ignoring host %s: already exists.\n"
                             % items['host.id'])
                continue

            # create system
            system = System(name=name)
            system.save()
            for key in sys_value_keys.keys():
                value = items[key].strip()
                if not value or value == '0':
                    continue
                attr = Attribute.objects.get(
                    name=fix_attr_name(sys_value_keys[key]))
                eav = SystemAV(entity=system, attribute=attr, value=value)
                eav.full_clean()
                eav.save()

            # check for workgroup
            if items['workgroup'] is not None:
                cursor.execute("SELECT name "
                               "FROM workgroup "
                               "WHERE id = {0}".format(items['workgroup']))
                wname = cursor.fetchone()[0]
                w, _ = Workgroup.objects.get_or_create(name=wname)
            else:
                w = None

            last_seen = items['last_seen'] or None
            if last_seen:
                last_seen = datetime.fromtimestamp(last_seen)

            static = StaticInterface(
                label=name, domain=self.domain, mac=(clean_mac(ha) or None),
                system=system, ip_str=long2ip(ip), ip_type='4',
                workgroup=w, ctnr=ctnr, ttl=items['ttl'],
                dns_enabled=dns_enabled, dhcp_enabled=dhcp_enabled,
                last_seen=last_seen)

            # create static interface
            try:
                static.save(update_range_usage=False)
            except ValidationError as e:
                try:
                    static.dhcp_enabled = False
                    static.dns_enabled = dns_enabled
                    static.save(update_range_usage=False)
                    stderr.write('WARNING: Static interface with IP {} has '
                                 'been disabled\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                except ValidationError as e:
                    stderr.write('WARNING: Could not create static interface '
                                 'with IP {}\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                    static = None
                    system.delete()

            if static:
                static.views.add(public)
                static.views.add(private)
예제 #31
0
class DeleteStaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")

    def do_add(self, mac, label, domain, ip_str, system, ip_type='4'):
        r = StaticInterface(mac=mac,
                            label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=system)
        r.clean()
        r.save()
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(AddressRecord.objects.filter(ip_str=ip_str,
                                                      fqdn=fqdn))

    def test1_delete_basic(self):
        # Does deleting a system delete it's interfaces?
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        system = System()
        system.save()
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str,
            'system': system
        }
        self.do_add(**kwargs)
        self.assertTrue(StaticInterface.objects.filter(**kwargs))
        system.delete()
        self.assertFalse(StaticInterface.objects.filter(**kwargs))
예제 #32
0
class NSTestsModels(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()
        self.i6_arpa = self.create_domain(name='ip6.arpa')
        self.i6_arpa.save()

        self.r = Domain(name="ru")
        self.r.save()
        self.f_r = Domain(name="foo.ru")
        self.f_r.save()
        self.b_f_r = Domain(name="bar.foo.ru")
        self.b_f_r.save()

        self.f = Domain(name="fam")
        self.f.save()

        self._128 = self.create_domain(name='128', ip_type='4')
        self._128.save()

        self.s = System()
        self.s.save()

    def do_add(self, domain, server):
        ns = Nameserver(domain=domain, server=server)
        ns.save()
        self.assertTrue(ns.__repr__())
        self.assertTrue(ns.details())
        ret = Nameserver.objects.filter(domain=domain, server=server)
        self.assertEqual(len(ret), 1)
        return ns

    def test_add_ns(self):
        data = {'domain': self.r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': 'ns5.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': u'ns3.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.b_f_r, 'server': 'n1.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.b_f_r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        data = {'domain': self.r, 'server': 'asdf.asdf'}
        self.do_add(**data)

    def test_add_invalid(self):
        data = {'domain': self.f_r, 'server': 'ns3.foo.ru'}
        self.assertRaises(ValidationError, self.do_add, **data)

    def testtest_add_ns_in_domain(self):
        # Use an A record as a glue record.
        glue = AddressRecord(label='ns2',
                             domain=self.r,
                             ip_str='128.193.1.10',
                             ip_type='4')
        glue.clean()
        glue.save()
        data = {'domain': self.r, 'server': 'ns2.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)
        self.assertRaises(ValidationError, glue.delete)

        glue = AddressRecord(label='ns3',
                             domain=self.f_r,
                             ip_str='128.193.1.10',
                             ip_type='4')
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns3.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

    def test_disallow_name_update_of_glue_A(self):
        # Glue records should not be allowed to change their name.
        glue = AddressRecord(label='ns39',
                             domain=self.f_r,
                             ip_str='128.193.1.77',
                             ip_type='4')
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns39.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue.label = "ns22"
        self.assertRaises(ValidationError, glue.clean)

    def test_disallow_name_update_of_glue_Intr(self):
        # Glue records should not be allowed to change their name.
        glue = StaticInterface(label='ns24',
                               domain=self.f_r,
                               ip_str='128.193.99.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns24.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue.label = "ns22"
        self.assertRaises(ValidationError, glue.clean)

    def test_disallow_delete_of_glue_intr(self):
        # Interface glue records should not be allowed to be deleted.
        glue = StaticInterface(label='ns24',
                               domain=self.f_r,
                               ip_str='128.193.99.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns24.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        self.assertRaises(ValidationError, glue.delete)

    def test_manual_assign_of_glue(self):
        # Test that assigning a different glue record doesn't get overriden by
        # the auto assinging during the Nameserver's clean function.
        glue = StaticInterface(label='ns25',
                               domain=self.f_r,
                               ip_str='128.193.99.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns25.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.glue, glue)

        glue2 = AddressRecord(label='ns25',
                              domain=self.f_r,
                              ip_str='128.193.1.78',
                              ip_type='4')
        glue2.clean()
        glue2.save()

        ns.clean()

        # Make sure things didn't get overriden.
        self.assertEqual(ns.glue, glue)

        ns.glue = glue2
        ns.save()
        # Refresh the object
        ns = Nameserver.objects.get(pk=ns.pk)
        # Again, Make sure things didn't get overriden.
        self.assertEqual(ns.glue, glue2)
        # Make sure we still can't delete.
        self.assertRaises(ValidationError, glue2.delete)
        self.assertRaises(ValidationError, ns.glue.delete)

        # We shuold be able to delelte the other one.
        glue.delete()

    def testtest_add_ns_in_domain_intr(self):
        # Use an Interface as a glue record.
        glue = StaticInterface(label='ns232',
                               domain=self.r,
                               ip_str='128.193.99.10',
                               ip_type='4',
                               system=self.s,
                               mac="12:23:45:45:45:45")
        glue.clean()
        glue.save()
        data = {'domain': self.r, 'server': 'ns232.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)
        self.assertRaises(ValidationError, glue.delete)

        glue = StaticInterface(label='ns332',
                               domain=self.f_r,
                               ip_str='128.193.1.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.clean()
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns332.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

    def test_add_ns_outside_domain(self):
        data = {'domain': self.f_r, 'server': 'ns2.ru'}
        ns = self.do_add(**data)
        self.assertFalse(ns.glue)

    def test_update_glue_to_no_intr(self):
        glue = StaticInterface(label='ns34',
                               domain=self.r,
                               ip_str='128.193.1.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.save()
        data = {'domain': self.r, 'server': 'ns34.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)

        ns.server = "ns4.wee"
        ns.save()
        self.assertTrue(ns.glue is None)

    def test_update_glue_record_intr(self):
        # Glue records can't change their name.
        glue = StaticInterface(label='ns788',
                               domain=self.r,
                               ip_str='128.193.1.10',
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        glue.save()
        data = {'domain': self.r, 'server': 'ns788.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        glue.label = "asdfasdf"
        self.assertRaises(ValidationError, glue.clean)

    def test_update_glue_to_no_glue(self):
        glue = AddressRecord(label='ns3',
                             domain=self.r,
                             ip_str='128.193.1.10',
                             ip_type='4')
        glue.save()
        data = {'domain': self.r, 'server': 'ns3.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)

        ns.server = "ns4.wee"
        ns.save()
        self.assertTrue(ns.glue is None)

    def test_delete_ns(self):
        glue = AddressRecord(label='ns4',
                             domain=self.f_r,
                             ip_str='128.196.1.10',
                             ip_type='4')
        glue.save()
        data = {'domain': self.f_r, 'server': 'ns4.foo.ru'}
        ns = self.do_add(**data)
        self.assertTrue(ns.glue)
        self.assertEqual(ns.server, ns.glue.fqdn)

        ns.delete()
        nsret = Nameserver.objects.filter(server='ns2.foo.ru', domain=self.f_r)
        self.assertFalse(nsret)

    def test_invalid_create(self):
        glue = AddressRecord(label='ns2',
                             domain=self.r,
                             ip_str='128.193.1.10',
                             ip_type='4')
        glue.save()

        data = {'domain': self.r, 'server': 'ns2 .ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2$.ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2..ru'}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': 'ns2.ru '}
        self.assertRaises(ValidationError, self.do_add, **data)
        data = {'domain': self.r, 'server': ''}
        self.assertRaises(ValidationError, self.do_add, **data)

    def test_add_dup(self):
        data = {'domain': self.r, 'server': 'ns2.moot.ru'}
        self.do_add(**data)

        self.assertRaises(ValidationError, self.do_add, **data)

    def _get_post_data(self, random_str):
        """Return a valid set of data"""
        return {
            'root_domain': '{0}.oregonstate.com'.format(random_str),
            'soa_primary': 'ns1.oregonstate.com',
            'soa_contact': 'noc.oregonstate.com',
            'nameserver_1': 'ns1.oregonstate.com',
            'ttl_1': '1234'
        }

    def test_bad_nameserver_soa_state_case_1_0(self):
        # This is Case 1
        root_domain = create_fake_zone("asdf10")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        a = AddressRecord(label='',
                          domain=root_domain,
                          ip_type="6",
                          ip_str="1::")
        self.assertRaises(ValidationError, a.save)
        cn = CNAME(label='', domain=root_domain, target="asdf")
        self.assertRaises(ValidationError, cn.save)

    def test_bad_nameserver_soa_state_case_1_1(self):
        # This is Case 1
        root_domain = create_fake_zone("asdf111")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        self.assertRaises(ValidationError, a.save)
        cn = CNAME(label='', domain=cdomain, target="asdf")
        self.assertRaises(ValidationError, cn.save)

    def test_bad_nameserver_soa_state_case_1_2(self):
        # This is Case 1 ... with ptr's
        root_domain = create_fake_zone("12.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        ptr = PTR(name="asdf", ip_str="12.10.1.1", ip_type="4")
        self.assertRaises(ValidationError, ptr.save)

    def test_bad_nameserver_soa_state_case_1_3(self):
        # This is Case 1 ... with ptr's
        root_domain = create_fake_zone("13.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="10.13.in-addr.arpa")
        cdomain.soa = root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        ptr = PTR(name="asdf", ip_str="13.10.1.1", ip_type="4")
        self.assertRaises(ValidationError, ptr.save)

    def test_bad_nameserver_soa_state_case_1_4(self):
        # This is Case 1 ... with StaticInterfaces's
        reverse_root_domain = create_fake_zone("14.in-addr.arpa", suffix="")
        root_domain = create_fake_zone("asdf14")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain at the root of a zone with no
        # other records in it.

        # Let's create a child domain and try to add a record there.
        cdomain = Domain(name="10.14.in-addr.arpa")
        cdomain.soa = reverse_root_domain.soa
        cdomain.save()

        # Adding a record shouldn't be allowed because there is no NS record on
        # the zone's root domain.
        intr = StaticInterface(label="asdf",
                               domain=root_domain,
                               ip_str="14.10.1.1",
                               ip_type="4",
                               mac="11:22:33:44:55:66",
                               system=self.s)
        self.assertRaises(ValidationError, intr.save)

    # See record.tests for the case a required view is deleted.
    def test_bad_nameserver_soa_state_case_2_0(self):
        # This is Case 2
        root_domain = create_fake_zone("asdf20")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        a = AddressRecord(label='',
                          domain=root_domain,
                          ip_type="6",
                          ip_str="1::")
        a.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_1(self):
        # This is Case 2
        root_domain = create_fake_zone("asdf21")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        # Let's create a child domain and add a record there, then try to
        # delete the NS record
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        a.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_2(self):
        # This is Case 2 ... with ptrs
        root_domain = create_fake_zone("22.in-addr.arpa", suffix="")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        ptr = PTR(name="asdf", ip_str="22.1.1.1", ip_type="4")
        ptr.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_2_3(self):
        # This is Case 2 ... with ptrs
        root_domain = create_fake_zone("10.23.in-addr.arpa", suffix="")
        self.assertEqual(root_domain.nameserver_set.count(), 1)
        ns = root_domain.nameserver_set.all()[0]

        # At his point we should have a domain at the root of a zone with one
        # ns record associated to the domain.

        # Let's create a child domain and add a record there, then try to
        # delete the NS record
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.soa = root_domain.soa
        cdomain.save()

        ptr = PTR(name="asdf", ip_str="23.10.1.1", ip_type="4")
        ptr.save()

        self.assertRaises(ValidationError, ns.delete)

    def test_bad_nameserver_soa_state_case_3_0(self):
        # This is Case 3
        root_domain = create_fake_zone("asdf30")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.
        a = AddressRecord(label='',
                          domain=root_domain,
                          ip_type="6",
                          ip_str="1::")
        a.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)

    def test_bad_nameserver_soa_state_case_3_1(self):
        # This is Case 3
        root_domain = create_fake_zone("asdf31")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain pointed at an SOA record with no
        # records attached to it (esspecially no ns recods). It also has no
        # child domains.
        # Try case 3 but add a record to a child domain of root_domain
        cdomain = Domain(name="test." + root_domain.name)
        cdomain.save()

        # Add a record to the domain.
        a = AddressRecord(label='', domain=cdomain, ip_type="6", ip_str="1::")
        a.save()

        # Now try to add the domain to the zone that has no NS records at it's
        # root
        cdomain.soa = root_domain.soa

        self.assertRaises(ValidationError, cdomain.save)

    def test_bad_nameserver_soa_state_case_3_2(self):
        # This is Case 3 ... with ptrs
        root_domain = create_fake_zone("32.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        soa = ns.domain.soa
        ns.domain.soa = None
        root_domain.soa = None  # Shit's getting cached
        ns.domain.save()
        soa.delete()

        # At his point we should have a domain pointed at no SOA record with no
        # records attached to it. It also has no child domains.

        # Add a record to the domain.

        ptr = PTR(name="asdf", ip_str="32.1.1.1", ip_type="4")
        ptr.save()

        s = SOA(primary="asdf.asdf", contact="asdf.asdf", description="asdf")
        s.save()
        root_domain.soa = s

        self.assertRaises(ValidationError, root_domain.save)

    def test_bad_nameserver_soa_state_case_3_3(self):
        # This is Case 3 ... with ptrs
        root_domain = create_fake_zone("33.in-addr.arpa", suffix="")
        for ns in root_domain.nameserver_set.all():
            ns.delete()

        # At his point we should have a domain pointed at an SOA record with no
        # records attached to it (esspecially no ns recods). It also has no
        # child domains.
        # Try case 3 but add a record to a child domain of root_domain
        cdomain = Domain(name="10.33.in-addr.arpa")
        cdomain.save()

        # Add a record to the domain.
        ptr = PTR(name="asdf", ip_str="33.10.1.1", ip_type="4")
        ptr.save()

        # Now try to add the domain to the zone that has no NS records at it's
        # root
        cdomain.soa = root_domain.soa

        self.assertRaises(ValidationError, cdomain.save)
예제 #33
0
class LibTestsRange(TestCase):

    def setUp(self):
        self.system = System()
        self.system.save()
        d1 = create_fake_zone("oregonstate.com", suffix="")
        soa = d1.soa
        self.soa = soa

        v, _ = Vlan.objects.get_or_create(name="private", number=3)
        s, _ = Site.objects.get_or_create(name="phx1")
        s1, _ = Site.objects.get_or_create(name="corp", parent=s)
        d, _ = Domain.objects.get_or_create(name="phx1.oregonstate.com")
        d.soa = soa
        d.save()
        d1, _ = Domain.objects.get_or_create(name="corp.phx1.oregonstate.com")
        d1.soa = soa
        d1.save()
        d2, _ = Domain.objects.get_or_create(
            name="private.corp.phx1.oregonstate.com")
        d2.soa = soa
        d2.save()

        d, _ = Domain.objects.get_or_create(name="arpa")
        d, _ = Domain.objects.get_or_create(name="in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="15.in-addr.arpa")
        n = Network(network_str="15.0.0.0/8", ip_type="4")
        n.clean()
        n.site = s1
        n.vlan = v
        n.save()

        r = Range(start_str="15.0.0.0", end_str="15.0.0.10",
                  network=n, ip_type='4')
        r.clean()
        r.save()

    def test1_create_ipv4_interface_from_range(self):
        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.1", range_end_str="15.0.0.3")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.1")

    def test2_create_ipv4_interface_from_range(self):
        # test soa inherit
        intr, errors = create_ipv4_intr_from_range(
                label="foo", system=self.system, mac="11:22:33:44:55:66",
                domain_name="superprivate.foo.corp.phx1.oregonstate.com",
                range_start_str="15.0.0.20", range_end_str="15.0.0.22")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.20")
        self.assertEqual(intr.domain.soa, self.soa)
        self.assertEqual(
            intr.domain.name, "superprivate.foo.corp.phx1.oregonstate.com")
        self.assertEqual(
            intr.domain.master_domain.name, "foo.corp.phx1.oregonstate.com")
        self.assertEqual(intr.domain.master_domain.soa, self.soa)

    def test3_create_ipv4_interface_from_range(self):
        # Test for an error when all the IP's are in use.
        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.2", range_end_str="15.0.0.5")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.2")

        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.2", range_end_str="15.0.0.5")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.3")

        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.2", range_end_str="15.0.0.5")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.4")

        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.2", range_end_str="15.0.0.5")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))
        self.assertEqual(intr.ip_str, "15.0.0.5")

        intr, errors = create_ipv4_intr_from_range(
                label="foo", domain_name="private.corp.phx1.oregonstate.com",
                system=self.system, mac="11:22:33:44:55:66",
                range_start_str="15.0.0.2", range_end_str="15.0.0.5")
        self.assertEqual(intr, None)
        self.assertTrue("ip" in errors)
예제 #34
0
class AuxAttrTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add(self, mac, label, domain, ip_str, ip_type='4'):
        self.n = System()
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_create(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        intr.update_attrs()

        def bad_get():
            intr.attrs.primary
        self.assertRaises(AttributeError, bad_get)
        x = StaticIntrKeyValue.objects.filter(key='primary', intr=intr)
        self.assertFalse(x)
        intr.attrs.primary = '1'
        self.assertEqual(intr.attrs.cache['primary'], '1')
        self.assertEqual(intr.attrs.primary, '1')
        x = StaticIntrKeyValue.objects.filter(key='primary', intr=intr)
        self.assertEqual(x[0].value, '1')

    def test6_create(self):
        mac = "24:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.update_attrs()
        intr.update_attrs()

        def bad_get():
            x = intr.attrs.primary
            return x
        self.assertRaises(AttributeError, bad_get)
        intr.attrs.primary = '1'
        self.assertEqual(intr.attrs.primary, '1')
        self.assertEqual(intr.attrs.cache['primary'], '1')

    def test2_create(self):
        mac = "13:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.attrs.primary = '2'
        self.assertEqual(intr.attrs.primary, '2')
        self.assertEqual(intr.attrs.cache['primary'], '2')
        del intr.attrs.primary

        def bad_get():
            intr.attrs.primary
        self.assertRaises(AttributeError, bad_get)
        intr.attrs.primary = '3'
        self.assertEqual(intr.attrs.primary, '3')
        self.assertEqual(intr.attrs.cache['primary'], '3')

    def test1_del(self):
        mac = "12:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.attrs.primary = '88'
        self.assertEqual(intr.attrs.primary, '88')
        del intr.attrs.primary

        def bad_get():
            intr.attrs.primary
        self.assertRaises(AttributeError, bad_get)

    def test3_create(self):
        mac = "19:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        intr.update_attrs()

        def bad_assign():
            intr.attrs.primary = 'a'
        self.assertRaises(ValidationError, bad_assign)

    def test1_existing_attrs(self):
        mac = "19:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        intr = self.do_add(**kwargs)
        StaticIntrKeyValue(key="foo", value="bar", intr=intr).save()
        StaticIntrKeyValue(
            key="interface_type", value="eth0", intr=intr).save()
        StaticIntrKeyValue(key="alias", value="5", intr=intr).save()
        intr.update_attrs()
        self.assertEqual(intr.attrs.alias, '5')
        self.assertEqual(intr.attrs.cache['alias'], '5')
        self.assertEqual(intr.attrs.interface_type, 'eth0')
        self.assertEqual(intr.attrs.cache['interface_type'], 'eth0')
        self.assertEqual(intr.attrs.foo, 'bar')
        self.assertEqual(intr.attrs.cache['foo'], 'bar')
예제 #35
0
class PTRStaticRegTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add_intr(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac,
                            label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_add_ptr(self, label, domain, ip_str, ip_type='4'):
        ptr = PTR(name=label + '.' + domain.name,
                  ip_str=ip_str,
                  ip_type=ip_type)
        ptr.clean()
        ptr.save()
        return ptr

    def test1_conflict_add_intr_first(self):
        # PTRdd an intr and make sure PTR can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo4"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add_intr(**kwargs)
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.assertRaises(ValidationError, self.do_add_ptr, **kwargs)

    def test1_conflict_add_PTR_first(self):
        # Add an PTR and make sure an intr can't exist.
        mac = "11:22:33:44:55:66"
        label = "foo5"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_ptr(**kwargs)
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.assertRaises(ValidationError, self.do_add_intr, **kwargs)

    def test2_conflict_add_intr_first(self):
        # Add an intr and update an existing PTR to conflict. Test for exception.
        mac = "12:22:33:44:55:66"
        label = "fo99"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add_intr(**kwargs)
        ip_str = "10.0.0.3"
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        ptr = self.do_add_ptr(**kwargs)
        ptr.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, ptr.clean)

    def test2_conflict_add_A_first(self):
        # Add an PTR and update and existing intr to conflict. Test for exception.
        mac = "11:22:33:44:55:66"
        label = "foo98"
        domain = self.f_c
        ip_str = "10.0.0.2"
        # Add PTR
        kwargs = {'label': label, 'domain': domain, 'ip_str': ip_str}
        self.do_add_ptr(**kwargs)

        # Add Intr with diff IP
        ip_str = "10.0.0.3"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add_intr(**kwargs)

        # Conflict the IP on the intr
        intr.ip_str = "10.0.0.2"
        self.assertRaises(ValidationError, intr.save)
예제 #36
0
class V6StaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='ip6.arpa', ip_type='6')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="0", ip_type='6')
        self.r1.save()
        self.r2 = self.create_domain(name="1", ip_type='6')
        self.r2.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add(self, mac, label, domain, ip_str, ip_type='6'):
        r = StaticInterface(mac=mac,
                            label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(AddressRecord.objects.filter(ip_str=ip_str,
                                                      fqdn=fqdn))

    def test1_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "12::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test2_create_basic(self):
        mac = "20:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "123::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test3_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "1234::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test4_create_basic(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "11::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test1_delete(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "112::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        r = self.do_add(**kwargs)
        self.do_delete(r)

    def test1_dup_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo3"
        domain = self.f_c
        ip_str = "1123::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_add_for_a_ptr(self):
        # Intr exists, then try ptr and A
        mac = "11:22:33:44:55:6e"
        label = "9988fooddfdf"
        domain = self.c
        ip_str = "111::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        ip_type = '6'
        i = self.do_add(**kwargs)
        i.clean()
        i.save()
        a = AddressRecord(label=label,
                          domain=domain,
                          ip_str=ip_str,
                          ip_type=ip_type)
        self.assertRaises(ValidationError, a.clean)
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=i.fqdn)
        self.assertRaises(ValidationError, ptr.clean)

    def test2_bad_add_for_a_ptr(self):
        # PTR and A exist, then try add intr
        mac = "11:22:33:44:55:66"
        label = "9988fdfood"
        domain = self.c
        ip_str = "1112::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        ip_type = '6'
        a = AddressRecord(label=label,
                          domain=domain,
                          ip_str=ip_str,
                          ip_type=ip_type)
        a.clean()
        a.save()
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=a.fqdn)
        ptr.clean()
        ptr.save()
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_reverse_domain(self):
        mac = "11:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "115::" + mac
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        i = self.do_add(**kwargs)
        i.ip_str = "9111::"
        self.assertRaises(ValidationError, i.save)

    def test1_no_system(self):
        mac = "15:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "188::" + mac
        ip_type = '6'
        r = StaticInterface(label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=None)
        self.assertRaises(ValidationError, r.clean)
예제 #37
0
파일: basic.py 프로젝트: alecdhuse/cyder
class StaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")

    def do_add(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        r.details()
        r.get_update_url()
        r.get_delete_url()
        r.get_detail_url()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add(**kwargs)
        i.update_attrs()
        self.assertEqual('None', i.interface_name())
        i.attrs.interface_type = "eth"
        self.assertEqual('None', i.interface_name())
        i.attrs.primary = "0"
        self.assertEqual('eth0', i.interface_name())
        i.attrs.alias = "0"
        self.assertEqual('eth0.0', i.interface_name())
        i.clean()

        def bad_assign():
            i.attrs.interface_type = "wee"
        self.assertRaises(ValidationError, bad_assign)

        def bad_assign2():
            i.attrs.alias = "wee"
        self.assertRaises(ValidationError, bad_assign2)

    def test2_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add(**kwargs)

        i.dhcp_enabled = False
        i.clean()
        i.save()
        i2 = StaticInterface.objects.get(pk=i.pk)
        self.assertFalse(i2.dhcp_enabled)

        i.dhcp_enabled = True
        i.clean()
        i.save()
        i3 = StaticInterface.objects.get(pk=i.pk)
        self.assertTrue(i3.dhcp_enabled)

    def test3_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test4_create_basic(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test5_create_basic(self):
        mac = "00:00:00:00:00:00"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

        # Change the mac by one.
        mac = "00:00:00:00:00:01"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)

    def test1_delete(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        r = self.do_add(**kwargs)
        self.do_delete(r)

    def test1_dup_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo3"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        self.do_add(**kwargs)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_add_for_a_ptr(self):
        # Intr exists, then try ptr and A
        mac = "11:22:33:44:55:66"
        label = "9988food"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        ip_type = '4'
        i = self.do_add(**kwargs)
        i.clean()
        i.save()
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        self.assertRaises(ValidationError, a.clean)
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=i.fqdn)
        self.assertRaises(ValidationError, ptr.clean)

    def test2_bad_add_for_a_ptr(self):
        # PTR and A exist, then try add intr
        mac = "11:22:33:44:55:66"
        label = "9988fdfood"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        ip_type = '4'
        a = AddressRecord(label=label, domain=domain, ip_str=ip_str,
                          ip_type=ip_type)
        a.clean()
        a.save()
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=a.fqdn)
        ptr.clean()
        ptr.save()
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_reverse_domain(self):
        mac = "11:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add(**kwargs)
        i.ip_str = "9.0.0.1"
        self.assertRaises(ValidationError, i.save)

    def test1_no_system(self):
        mac = "15:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        ip_type = '4'
        r = StaticInterface(label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=None, mac=mac)
        self.assertRaises(ValidationError, r.clean)
예제 #38
0
파일: free_ip.py 프로젝트: alecdhuse/cyder
class LibTestsFreeIP(TestCase):

    def setUp(self):
        self.system = System()
        self.system.save()

        d1 = create_fake_zone("oregonstate.com", suffix="")
        soa = d1.soa

        v, _ = Vlan.objects.get_or_create(name="private", number=3)
        s, _ = Site.objects.get_or_create(name="phx1")
        s1, _ = Site.objects.get_or_create(name="corp", parent=s)
        d, _ = Domain.objects.get_or_create(name="phx1.oregonstate.com")
        d.soa = soa
        d.save()
        d1, _ = Domain.objects.get_or_create(name="corp.phx1.oregonstate.com")
        d1.soa = soa
        d1.save()
        d2, _ = Domain.objects.get_or_create(
            name="private.corp.phx1.oregonstate.com")
        d2.soa = soa
        d2.save()

        d, _ = Domain.objects.get_or_create(name="arpa")
        d, _ = Domain.objects.get_or_create(name="in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="ip6.arpa")
        d, _ = Domain.objects.get_or_create(name="15.in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="2.in-addr.arpa")
        n = Network(network_str="15.0.0.0/8", ip_type="4")
        n.clean()
        n.site = s1
        n.vlan = v
        n.save()

        r = Range(start_str="15.0.0.0", end_str="15.0.0.10",
                  network=n, ip_type='4')
        r.clean()
        r.save()

    def test1_free_ip_count(self):
        # Add a bunch of interfaces and make sure the calc_free_ips function is
        # working
        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 4)
        intr, errors = create_ipv4_intr_from_range("foo",
                "private.corp.phx1.oregonstate.com", self.system,
                "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 3)

        intr, errors = create_ipv4_intr_from_range("foo",
                "private.corp.phx1.oregonstate.com", self.system,
                "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 2)

        intr, errors = create_ipv4_intr_from_range("foo",
                "private.corp.phx1.oregonstate.com", self.system,
                "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 1)

        intr, errors = create_ipv4_intr_from_range("foo",
                "private.corp.phx1.oregonstate.com", self.system,
                "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 0)
예제 #39
0
파일: free_ip.py 프로젝트: jirwin/cyder
class LibTestsFreeIP(TestCase):
    def setUp(self):
        self.system = System()
        self.system.save()

        d1 = create_fake_zone("oregonstate.com", suffix="")
        soa = d1.soa

        v, _ = Vlan.objects.get_or_create(name="private", number=3)
        s, _ = Site.objects.get_or_create(name="phx1")
        s1, _ = Site.objects.get_or_create(name="corp", parent=s)
        d, _ = Domain.objects.get_or_create(name="phx1.oregonstate.com")
        d.soa = soa
        d.save()
        d1, _ = Domain.objects.get_or_create(name="corp.phx1.oregonstate.com")
        d1.soa = soa
        d1.save()
        d2, _ = Domain.objects.get_or_create(
            name="private.corp.phx1.oregonstate.com")
        d2.soa = soa
        d2.save()

        d, _ = Domain.objects.get_or_create(name="arpa")
        d, _ = Domain.objects.get_or_create(name="in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="ip6.arpa")
        d, _ = Domain.objects.get_or_create(name="15.in-addr.arpa")
        d, _ = Domain.objects.get_or_create(name="2.in-addr.arpa")
        n = Network(network_str="15.0.0.0/8", ip_type="4")
        n.clean()
        n.site = s1
        n.vlan = v
        n.save()

        r = Range(start_str="15.0.0.0",
                  end_str="15.0.0.10",
                  network=n,
                  ip_type='4')
        r.clean()
        r.save()

    def test1_free_ip_count(self):
        # Add a bunch of interfaces and make sure the calc_free_ips function is
        # working
        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 4)
        intr, errors = create_ipv4_intr_from_range(
            "foo", "private.corp.phx1.oregonstate.com", self.system,
            "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 3)

        intr, errors = create_ipv4_intr_from_range(
            "foo", "private.corp.phx1.oregonstate.com", self.system,
            "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 2)

        intr, errors = create_ipv4_intr_from_range(
            "foo", "private.corp.phx1.oregonstate.com", self.system,
            "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 1)

        intr, errors = create_ipv4_intr_from_range(
            "foo", "private.corp.phx1.oregonstate.com", self.system,
            "11:22:33:44:55:66", "15.0.0.200", "15.0.0.204")
        intr.save()
        self.assertEqual(errors, None)
        self.assertTrue(isinstance(intr, StaticInterface))

        count = calc_free_ips_str("15.0.0.200", "15.0.0.204")
        self.assertEqual(count, 0)
예제 #40
0
class CNAMEStaticRegTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add_intr(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac, label=label, domain=domain, ip_str=ip_str,
                            ip_type=ip_type, system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(
            AddressRecord.objects.filter(ip_str=ip_str, fqdn=fqdn))

    def test1_delete_cname(self):
        mac = "11:22:33:44:55:66"
        label = "foo4"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add_intr(**kwargs)
        cn, _ = CNAME.objects.get_or_create(label='foo', domain=domain,
                                            target=label + "." + domain.name)
        self.assertRaises(ValidationError, i.delete)

    def test1_delete_override(self):
        mac = "12:22:33:44:55:66"
        label = "foo6"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {'mac': mac, 'label': label, 'domain': domain,
                  'ip_str': ip_str}
        i = self.do_add_intr(**kwargs)
        cn, _ = CNAME.objects.get_or_create(label='food', domain=domain,
                                            target=label + "." + domain.name)
        i.delete(check_cname=False)
예제 #41
0
def migrate_dynamic_hosts():
    print "Migrating dynamic hosts."
    default, _ = Workgroup.objects.get_or_create(name='default')

    sys_value_keys = {"type": "Hardware Type",
                      "os": "Operating System",
                      "location": "Location",
                      "department": "Department",
                      "serial": "Serial Number",
                      "other_id": "Other ID",
                      "purchase_date": "Purchase Date",
                      "po_number": "PO Number",
                      "warranty_date": "Warranty Date",
                      "owning_unit": "Owning Unit",
                      "user_id": "User ID"}

    keys = ("id", "dynamic_range", "name", "workgroup", "enabled", "ha",
            "type", "os", "location", "department", "serial", "other_id",
            "purchase_date", "po_number", "warranty_date", "owning_unit",
            "user_id", "last_seen", "expire", "ttl", "last_update", "domain",
            "zone")

    sql = "SELECT %s FROM host WHERE ip = 0" % ", ".join(keys)

    count = 0
    cursor.execute(sql)
    for values in cursor.fetchall():
        items = dict(zip(keys, values))
        enabled = items['enabled']

        if len(items['ha']) == 0:
            mac = None
        elif len(items['ha']) == 12:
            if items['ha'] == '0' * 12:
                mac = None
                enabled = False
            else:
                mac = items['ha']
        else:
            stderr.write(
                'Host with id {} has invalid hardware address "{}"'.format(
                    items['id'], items['ha']))
            continue

        # TODO: Verify that there is no valid range/zone/workgroup with id 0
        r, c, w = None, None, default
        if items['dynamic_range']:
            try:
                r = maintain_find_range(items['dynamic_range'])
            except ObjectDoesNotExist:
                stderr.write(
                    'Could not create dynamic interface %s: Range %s '
                    'is in Maintain, but was not created in Cyder.' %
                    (items['ha'], items['dynamic_range']))

        if items['zone']:
            c = maintain_find_zone(items['zone'])

        if items['workgroup']:
            w = maintain_find_workgroup(items['workgroup'])

        if not all([r, c]):
            stderr.write('Trouble migrating host with mac {0}\n'
                         .format(items['ha']))
            continue

        s = System(name=items['name'])
        s.save()
        for key in sys_value_keys.keys():
            value = items[key].strip()
            if not value or value == '0':
                continue

            attr = Attribute.objects.get(
                name=fix_attr_name(sys_value_keys[key]))
            eav = SystemAV(entity=s, attribute=attr, value=value)
            eav.full_clean()
            eav.save()

        last_seen = items['last_seen'] or None
        if last_seen:
            last_seen = datetime.fromtimestamp(last_seen)

        intr = DynamicInterface(
            range=r, workgroup=w, ctnr=c, mac=mac, system=s,
            dhcp_enabled=enabled, last_seen=last_seen)
        try:
            intr.save(update_range_usage=False, commit=False)
        except ValidationError as e:
            try:
                intr.dhcp_enabled = False
                intr.save(update_range_usage=False)
                stderr.write(
                    'WARNING: Dynamic interface with MAC address {} has been '
                    'disabled\n'.format(intr.mac))
                stderr.write('    {}\n'.format(e))
            except ValidationError as e:
                stderr.write(
                    'WARNING: Could not create dynamic interface with MAC '
                    'address {}\n'.format(intr.mac))
                stderr.write('    {}\n'.format(e))
                intr = None

        if intr:
            count += 1
            if not count % 1000:
                print "%s valid hosts found so far." % count

    print "%s valid hosts found." % count
예제 #42
0
class CNAMETests(cyder.base.tests.TestCase):

    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.g = create_fake_zone("gz", suffix="")
        self.c_g = create_fake_zone("coo.gz", suffix="")
        self.d = create_fake_zone("dz", suffix="")

        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.r1.save()

        self.s = System()
        self.s.save()

    def do_add(self, label, domain, data):
        cn = CNAME(label=label, domain=domain, target=data)
        cn.full_clean()
        cn.save()
        cn.save()
        self.assertTrue(cn.details())

        cs = CNAME.objects.filter(
            label=label, domain=domain, target=data)
        self.assertEqual(len(cs), 1)
        return cn

    def test_add(self):
        label = "foo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "boo"
        domain = self.c_g
        data = "foo.foo.com"
        self.do_add(label, domain, data)

        label = "fo1"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

        label = "hooo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test1_add_glob(self):
        label = "*foo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "*"
        domain = self.c_g
        data = "foo.foo.com"
        self.do_add(label, domain, data)

        label = "*.fo1"
        domain = self.g
        data = "foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

        label = "*sadfasfd-asdf"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test2_add_glob(self):
        label = "*coo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "*"
        domain = self.c_g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test_soa_condition(self):
        label = ""
        domain = self.c_g
        data = "foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

    def test_add_bad(self):
        label = ""
        domain = self.g
        data = "..foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

    def test_add_mx_with_cname(self):
        label = "cnamederp1"
        domain = self.c_g
        data = "foo.com"

        fqdn = label + '.' + domain.name
        mx_data = {'label': '', 'domain': self.c_g, 'server':
                   fqdn, 'priority': 2, 'ttl': 2222}
        mx = MX(**mx_data)
        mx.save()

        cn = CNAME(label=label, domain=domain, target=data)

        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = AddressRecord.objects.get_or_create(
            label=label, domain=dom, ip_type='4', ip_str="128.193.1.1")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_exists_upper_case(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = AddressRecord.objects.get_or_create(
            label=label, domain=dom, ip_type='4', ip_str="128.193.1.1")

        cn = CNAME(label=label.title(), domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(
            label=label, domain=dom, target=data
        )
        rec = AddressRecord(label=label, domain=dom, ip_str="128.193.1.1")

        self.assertRaises(ValidationError, rec.save)

    def test_srv_exists(self):
        label = "_testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = SRV.objects.get_or_create(
            label=label, domain=dom, target="asdf",
                                            port=2, priority=2, weight=4)

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_srv_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(
            label=label, domain=dom, target=data)

        rec = SRV(label=label, domain=dom, target="asdf",
                  port=2, priority=2, weight=4)

        self.assertRaises(ValidationError, rec.save)

    def test_txt_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = TXT.objects.get_or_create(
            label=label, domain=dom, txt_data="asdf")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_txt_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(
            label=label, domain=dom, target=data)
        cn.full_clean()
        cn.save()

        rec = TXT(label=label, domain=dom, txt_data="asdf1")

        self.assertRaises(ValidationError, rec.save)

    def test_mx_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = MX.objects.get_or_create(
            label=label, domain=dom, server="asdf",
                                            priority=123, ttl=123)

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_mx_cname_exists(self):
        # Duplicate test?
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(
            label=label, domain=dom, target=data)
        cn.full_clean()
        cn.save()

        rec = MX(label=label, domain=dom, server="asdf1",
                 priority=123, ttl=123)

        self.assertRaises(ValidationError, rec.save)

    def test_ns_exists(self):
        # Duplicate test?
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec = Nameserver(domain=dom, server="asdf1")
        rec.save()
        cn = CNAME(label='', domain=dom, target=data)
        self.assertRaises(ValidationError, cn.clean)

    def test_ns_cname_exists(self):
        # Duplicate test?
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(
            label='', domain=dom, target=data)
        cn.full_clean()
        cn.save()

        rec = Nameserver(domain=dom, server="asdf1")
        self.assertRaises(ValidationError, rec.save)

    def test_intr_exists(self):
        label = "tdfestyfoo"
        data = "waasdft"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        intr = StaticInterface(label=label, domain=dom, ip_str="10.0.0.1",
                               ip_type='4', system=self.s,
                               mac="11:22:33:44:55:66")
        intr.clean()
        intr.save()

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_intr_cname_exists(self):
        # Duplicate test?
        label = "tesafstyfoo"
        data = "wadfakt"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(
            label=label, domain=dom, target=data)
        cn.full_clean()
        cn.save()

        intr = StaticInterface(
            label=label, domain=dom, ip_str="10.0.0.2", ip_type='4',
            system=self.s, mac="00:11:22:33:44:55"
        )

        self.assertRaises(ValidationError, intr.clean)
        cn.label = "differentlabel"
        cn.save()
        intr.clean()
        intr.save()

    def test_ptr_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd')
        rec.full_clean()
        rec.save()

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_ptr_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(label=label, domain=dom, target=data)
        rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd')

        self.assertRaises(ValidationError, rec.clean)

    def test_cname_point_to_itself(self):
        label = "foopy"
        data = "foopy.what.cd"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.clean)
예제 #43
0
    def gen_static(self):
        """
        Generates the Static Interface objects related to this zone's domain.

        .. note::
            Every static interface needs a system.

        :System uniqueness: hostname, mac, ip_str

        :StaticInterface uniqueness: hostname, mac, ip_str
        """
        from dhcp_migrate import migrate_zones

        if Ctnr.objects.count() <= 2:
            print "WARNING: Zones not migrated. Attempting to migrate now."
            migrate_zones()

        sys_value_keys = {
            "type": "Hardware Type",
            "os": "Operating System",
            "location": "Location",
            "department": "Department",
            "serial": "Serial Number",
            "other_id": "Other ID",
            "purchase_date": "Purchase Date",
            "po_number": "PO Number",
            "warranty_date": "Warranty Date",
            "owning_unit": "Owning Unit",
            "user_id": "User ID"
        }

        keys = ("host.id", "ip", "host.name", "zone.name", "workgroup",
                "enabled", "ha", "zone", "type", "os", "location",
                "department", "serial", "other_id", "purchase_date",
                "po_number", "warranty_date", "owning_unit", "user_id",
                "last_seen", "expire", "ttl", "last_update")

        sql = ("SELECT %s FROM host JOIN zone ON host.zone = zone.id "
               "WHERE ip != 0 AND domain = '%s';" %
               (", ".join(keys), self.domain_id))

        cursor.execute(sql)
        for values in cursor.fetchall():
            items = dict(zip(keys, values))
            ctnr = self.ctnr_from_zone_name(items['zone.name'])
            if ctnr is None:
                continue

            name = items['host.name']
            enabled = bool(items['enabled'])
            dns_enabled, dhcp_enabled = enabled, enabled
            ip = items['ip']
            ha = items['ha']
            if ip == 0:
                continue

            if len(ha) != 12 or ha == '0' * 12:
                ha = ""

            if ha == "":
                dhcp_enabled = False

            # check for duplicate
            static = StaticInterface.objects.filter(label=name,
                                                    mac=(clean_mac(ha)
                                                         or None),
                                                    ip_str=long2ip(ip))
            if static:
                stderr.write("Ignoring host %s: already exists.\n" %
                             items['host.id'])
                continue

            # create system
            system = System(name=name)
            system.save()
            for key in sys_value_keys.keys():
                value = items[key].strip()
                if not value or value == '0':
                    continue
                attr = Attribute.objects.get(
                    name=fix_attr_name(sys_value_keys[key]))
                eav = SystemAV(entity=system, attribute=attr, value=value)
                eav.full_clean()
                eav.save()

            # check for workgroup
            if items['workgroup'] is not None:
                cursor.execute("SELECT name "
                               "FROM workgroup "
                               "WHERE id = {0}".format(items['workgroup']))
                wname = cursor.fetchone()[0]
                w, _ = Workgroup.objects.get_or_create(name=wname)
            else:
                w = None

            last_seen = items['last_seen'] or None
            if last_seen:
                last_seen = datetime.fromtimestamp(last_seen)

            static = StaticInterface(label=name,
                                     domain=self.domain,
                                     mac=(clean_mac(ha) or None),
                                     system=system,
                                     ip_str=long2ip(ip),
                                     ip_type='4',
                                     workgroup=w,
                                     ctnr=ctnr,
                                     ttl=items['ttl'],
                                     dns_enabled=dns_enabled,
                                     dhcp_enabled=dhcp_enabled,
                                     last_seen=last_seen)

            # create static interface
            try:
                static.save(update_range_usage=False)
            except ValidationError as e:
                try:
                    static.dhcp_enabled = False
                    static.dns_enabled = dns_enabled
                    static.save(update_range_usage=False)
                    stderr.write('WARNING: Static interface with IP {} has '
                                 'been disabled\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                except ValidationError as e:
                    stderr.write('WARNING: Could not create static interface '
                                 'with IP {}\n'.format(static.ip_str))
                    stderr.write('    {}\n'.format(e))
                    static = None
                    system.delete()

            if static:
                static.views.add(public)
                static.views.add(private)
예제 #44
0
class StaticInterTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()
        View.objects.get_or_create(name="private")

    def do_add(self, mac, label, domain, ip_str, ip_type='4'):
        r = StaticInterface(mac=mac,
                            label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=self.n)
        r.clean()
        r.save()
        r.details()
        r.get_update_url()
        r.get_delete_url()
        r.get_detail_url()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(AddressRecord.objects.filter(ip_str=ip_str,
                                                      fqdn=fqdn))

    def test1_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        i = self.do_add(**kwargs)
        i.update_attrs()
        self.assertEqual('None', i.interface_name())
        i.attrs.interface_type = "eth"
        self.assertEqual('None', i.interface_name())
        i.attrs.primary = "0"
        self.assertEqual('eth0', i.interface_name())
        i.attrs.alias = "0"
        self.assertEqual('eth0.0', i.interface_name())
        i.clean()

        def bad_assign():
            i.attrs.interface_type = "wee"

        self.assertRaises(ValidationError, bad_assign)

        def bad_assign2():
            i.attrs.alias = "wee"

        self.assertRaises(ValidationError, bad_assign2)

    def test2_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        i = self.do_add(**kwargs)

        i.dhcp_enabled = False
        i.clean()
        i.save()
        i2 = StaticInterface.objects.get(pk=i.pk)
        self.assertFalse(i2.dhcp_enabled)

        i.dhcp_enabled = True
        i.clean()
        i.save()
        i3 = StaticInterface.objects.get(pk=i.pk)
        self.assertTrue(i3.dhcp_enabled)

    def test3_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test4_create_basic(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test5_create_basic(self):
        mac = "00:00:00:00:00:00"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

        # Change the mac by one.
        mac = "00:00:00:00:00:01"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)

    def test1_delete(self):
        mac = "12:22:33:44:55:66"
        label = "foo1"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        r = self.do_add(**kwargs)
        self.do_delete(r)

    def test1_dup_create_basic(self):
        mac = "11:22:33:44:55:66"
        label = "foo3"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        self.do_add(**kwargs)
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_add_for_a_ptr(self):
        # Intr exists, then try ptr and A
        mac = "11:22:33:44:55:66"
        label = "9988food"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        ip_type = '4'
        i = self.do_add(**kwargs)
        i.clean()
        i.save()
        a = AddressRecord(label=label,
                          domain=domain,
                          ip_str=ip_str,
                          ip_type=ip_type)
        self.assertRaises(ValidationError, a.clean)
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=i.fqdn)
        self.assertRaises(ValidationError, ptr.clean)

    def test2_bad_add_for_a_ptr(self):
        # PTR and A exist, then try add intr
        mac = "11:22:33:44:55:66"
        label = "9988fdfood"
        domain = self.c
        ip_str = "10.0.0.1"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        ip_type = '4'
        a = AddressRecord(label=label,
                          domain=domain,
                          ip_str=ip_str,
                          ip_type=ip_type)
        a.clean()
        a.save()
        ptr = PTR(ip_str=ip_str, ip_type=ip_type, name=a.fqdn)
        ptr.clean()
        ptr.save()
        self.assertRaises(ValidationError, self.do_add, **kwargs)

    def test1_bad_reverse_domain(self):
        mac = "11:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        i = self.do_add(**kwargs)
        i.ip_str = "9.0.0.1"
        self.assertRaises(ValidationError, i.save)

    def test1_no_system(self):
        mac = "15:22:33:44:55:66"
        label = "8888foo"
        domain = self.f_c
        ip_str = "10.0.0.1"
        ip_type = '4'
        r = StaticInterface(label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=None,
                            mac=mac)
        self.assertRaises(ValidationError, r.clean)
예제 #45
0
파일: test_models.py 프로젝트: jirwin/cyder
class CNAMETests(cyder.base.tests.TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.g = create_fake_zone("gz", suffix="")
        self.c_g = create_fake_zone("coo.gz", suffix="")
        self.d = create_fake_zone("dz", suffix="")

        self.r1 = create_fake_zone("10.in-addr.arpa", suffix="")
        self.r1.save()

        self.s = System()
        self.s.save()

    def do_add(self, label, domain, data):
        cn = CNAME(label=label, domain=domain, target=data)
        cn.full_clean()
        cn.save()
        cn.save()
        self.assertTrue(cn.details())

        cs = CNAME.objects.filter(label=label, domain=domain, target=data)
        self.assertEqual(len(cs), 1)
        return cn

    def test_add(self):
        label = "foo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "boo"
        domain = self.c_g
        data = "foo.foo.com"
        self.do_add(label, domain, data)

        label = "fo1"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

        label = "hooo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test1_add_glob(self):
        label = "*foo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "*"
        domain = self.c_g
        data = "foo.foo.com"
        self.do_add(label, domain, data)

        label = "*.fo1"
        domain = self.g
        data = "foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

        label = "*sadfasfd-asdf"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test2_add_glob(self):
        label = "*coo"
        domain = self.g
        data = "foo.com"
        self.do_add(label, domain, data)

        label = "*"
        domain = self.c_g
        data = "foo.com"
        self.do_add(label, domain, data)

    def test_soa_condition(self):
        label = ""
        domain = self.c_g
        data = "foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

    def test_add_bad(self):
        label = ""
        domain = self.g
        data = "..foo.com"
        self.assertRaises(ValidationError, self.do_add, *(label, domain, data))

    def test_add_mx_with_cname(self):
        label = "cnamederp1"
        domain = self.c_g
        data = "foo.com"

        fqdn = label + '.' + domain.name
        mx_data = {
            'label': '',
            'domain': self.c_g,
            'server': fqdn,
            'priority': 2,
            'ttl': 2222
        }
        mx = MX(**mx_data)
        mx.save()

        cn = CNAME(label=label, domain=domain, target=data)

        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = AddressRecord.objects.get_or_create(label=label,
                                                     domain=dom,
                                                     ip_type='4',
                                                     ip_str="128.193.1.1")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_exists_upper_case(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = AddressRecord.objects.get_or_create(label=label,
                                                     domain=dom,
                                                     ip_type='4',
                                                     ip_str="128.193.1.1")

        cn = CNAME(label=label.title(), domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_address_record_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(label=label, domain=dom, target=data)
        rec = AddressRecord(label=label, domain=dom, ip_str="128.193.1.1")

        self.assertRaises(ValidationError, rec.save)

    def test_srv_exists(self):
        label = "_testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = SRV.objects.get_or_create(label=label,
                                           domain=dom,
                                           target="asdf",
                                           port=2,
                                           priority=2,
                                           weight=4)

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_srv_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(label=label, domain=dom, target=data)

        rec = SRV(label=label,
                  domain=dom,
                  target="asdf",
                  port=2,
                  priority=2,
                  weight=4)

        self.assertRaises(ValidationError, rec.save)

    def test_txt_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = TXT.objects.get_or_create(label=label,
                                           domain=dom,
                                           txt_data="asdf")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_txt_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(label=label,
                                            domain=dom,
                                            target=data)
        cn.full_clean()
        cn.save()

        rec = TXT(label=label, domain=dom, txt_data="asdf1")

        self.assertRaises(ValidationError, rec.save)

    def test_mx_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec, _ = MX.objects.get_or_create(label=label,
                                          domain=dom,
                                          server="asdf",
                                          priority=123,
                                          ttl=123)

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_mx_cname_exists(self):
        # Duplicate test?
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(label=label,
                                            domain=dom,
                                            target=data)
        cn.full_clean()
        cn.save()

        rec = MX(label=label,
                 domain=dom,
                 server="asdf1",
                 priority=123,
                 ttl=123)

        self.assertRaises(ValidationError, rec.save)

    def test_ns_exists(self):
        # Duplicate test?
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec = Nameserver(domain=dom, server="asdf1")
        rec.save()
        cn = CNAME(label='', domain=dom, target=data)
        self.assertRaises(ValidationError, cn.clean)

    def test_ns_cname_exists(self):
        # Duplicate test?
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(label='', domain=dom, target=data)
        cn.full_clean()
        cn.save()

        rec = Nameserver(domain=dom, server="asdf1")
        self.assertRaises(ValidationError, rec.save)

    def test_intr_exists(self):
        label = "tdfestyfoo"
        data = "waasdft"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        intr = StaticInterface(label=label,
                               domain=dom,
                               ip_str="10.0.0.1",
                               ip_type='4',
                               system=self.s,
                               mac="11:22:33:44:55:66")
        intr.clean()
        intr.save()

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_intr_cname_exists(self):
        # Duplicate test?
        label = "tesafstyfoo"
        data = "wadfakt"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn, _ = CNAME.objects.get_or_create(label=label,
                                            domain=dom,
                                            target=data)
        cn.full_clean()
        cn.save()

        intr = StaticInterface(label=label,
                               domain=dom,
                               ip_str="10.0.0.2",
                               ip_type='4',
                               system=self.s,
                               mac="00:11:22:33:44:55")

        self.assertRaises(ValidationError, intr.clean)
        cn.label = "differentlabel"
        cn.save()
        intr.clean()
        intr.save()

    def test_ptr_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd')
        rec.full_clean()
        rec.save()

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.full_clean)

    def test_ptr_cname_exists(self):
        label = "testyfoo"
        data = "wat"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        CNAME.objects.get_or_create(label=label, domain=dom, target=data)
        rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd')

        self.assertRaises(ValidationError, rec.clean)

    def test_cname_point_to_itself(self):
        label = "foopy"
        data = "foopy.what.cd"
        dom, _ = Domain.objects.get_or_create(name="cd")
        dom, _ = Domain.objects.get_or_create(name="what.cd")

        cn = CNAME(label=label, domain=dom, target=data)
        self.assertRaises(ValidationError, cn.clean)
예제 #46
0
class AuxAttrTests(TestCase):
    def create_domain(self, name, ip_type=None, delegated=False):
        if ip_type is None:
            ip_type = '4'
        if name in ('arpa', 'in-addr.arpa', 'ip6.arpa'):
            pass
        else:
            name = ip_to_domain_name(name, ip_type=ip_type)
        d = Domain(name=name, delegated=delegated)
        d.clean()
        self.assertTrue(d.is_reverse)
        return d

    def setUp(self):
        self.arpa = self.create_domain(name='arpa')
        self.arpa.save()
        self.i_arpa = self.create_domain(name='in-addr.arpa')
        self.i_arpa.save()

        self.c = Domain(name="ccc")
        self.c.save()
        self.f_c = Domain(name="foo.ccc")
        self.f_c.save()
        self.r1 = self.create_domain(name="10")
        self.r1.save()
        self.n = System()
        self.n.clean()
        self.n.save()

    def do_add(self, mac, label, domain, ip_str, ip_type='4'):
        self.n = System()
        r = StaticInterface(mac=mac,
                            label=label,
                            domain=domain,
                            ip_str=ip_str,
                            ip_type=ip_type,
                            system=self.n)
        r.clean()
        r.save()
        repr(r)
        return r

    def do_delete(self, r):
        ip_str = r.ip_str
        fqdn = r.fqdn
        r.delete()
        self.assertFalse(AddressRecord.objects.filter(ip_str=ip_str,
                                                      fqdn=fqdn))

    def test1_create(self):
        mac = "11:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        intr.update_attrs()

        def bad_get():
            intr.attrs.primary

        self.assertRaises(AttributeError, bad_get)
        x = StaticIntrKeyValue.objects.filter(key='primary', intr=intr)
        self.assertFalse(x)
        intr.attrs.primary = '1'
        self.assertEqual(intr.attrs.cache['primary'], '1')
        self.assertEqual(intr.attrs.primary, '1')
        x = StaticIntrKeyValue.objects.filter(key='primary', intr=intr)
        self.assertEqual(x[0].value, '1')

    def test6_create(self):
        mac = "24:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.update_attrs()
        intr.update_attrs()

        def bad_get():
            x = intr.attrs.primary
            return x

        self.assertRaises(AttributeError, bad_get)
        intr.attrs.primary = '1'
        self.assertEqual(intr.attrs.primary, '1')
        self.assertEqual(intr.attrs.cache['primary'], '1')

    def test2_create(self):
        mac = "13:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.attrs.primary = '2'
        self.assertEqual(intr.attrs.primary, '2')
        self.assertEqual(intr.attrs.cache['primary'], '2')
        del intr.attrs.primary

        def bad_get():
            intr.attrs.primary

        self.assertRaises(AttributeError, bad_get)
        intr.attrs.primary = '3'
        self.assertEqual(intr.attrs.primary, '3')
        self.assertEqual(intr.attrs.cache['primary'], '3')

    def test1_del(self):
        mac = "12:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        intr.update_attrs()
        intr.attrs.primary = '88'
        self.assertEqual(intr.attrs.primary, '88')
        del intr.attrs.primary

        def bad_get():
            intr.attrs.primary

        self.assertRaises(AttributeError, bad_get)

    def test3_create(self):
        mac = "19:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        intr.update_attrs()

        def bad_assign():
            intr.attrs.primary = 'a'

        self.assertRaises(ValidationError, bad_assign)

    def test1_existing_attrs(self):
        mac = "19:22:33:44:55:66"
        label = "foo"
        domain = self.f_c
        ip_str = "10.0.0.2"
        kwargs = {
            'mac': mac,
            'label': label,
            'domain': domain,
            'ip_str': ip_str
        }
        intr = self.do_add(**kwargs)
        StaticIntrKeyValue(key="foo", value="bar", intr=intr).save()
        StaticIntrKeyValue(key="interface_type", value="eth0",
                           intr=intr).save()
        StaticIntrKeyValue(key="alias", value="5", intr=intr).save()
        intr.update_attrs()
        self.assertEqual(intr.attrs.alias, '5')
        self.assertEqual(intr.attrs.cache['alias'], '5')
        self.assertEqual(intr.attrs.interface_type, 'eth0')
        self.assertEqual(intr.attrs.cache['interface_type'], 'eth0')
        self.assertEqual(intr.attrs.foo, 'bar')
        self.assertEqual(intr.attrs.cache['foo'], 'bar')