示例#1
0
def test_update():
    handle, path = tempfile.mkstemp()
    orig = open(path, "r+")
    orig.write("HEADER\n\n;" + SLAM_HDR1 + ";" + SLAM_HDR2
        + "useless content\n;"+ SLAM_FTR + "FOOTER\n")
    orig.flush()
    orig.seek(0)
    conf = generator.BindConfig.create(outputfile=None)
    conf.output = orig

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="addr", pool=models.Pool(dns_record="A"))
    addr2 = models.Address(addr="rdda", pool=models.Pool(dns_record="A"))
    addr3 = models.Address(addr="addr2", pool=models.Pool(dns_record="AAAA"))
    conf.updateconf([(None,
        [(host1, addr1, [], ""), (host1, addr2, [], ""),
            (host2, addr3, [], "")])])

    orig.seek(0)
    res = orig.read()
    assert(res == "HEADER\n\n;" + SLAM_HDR1 + ";" + SLAM_HDR2
        + "host1\t1D\tIN\tA\taddr\n"
        + "host1\t1D\tIN\tA\trdda\n"
        + "host2\t1D\tIN\tAAAA\taddr2\n"
        + ";" + SLAM_FTR + "FOOTER\n")

    orig.close()
    os.unlink(path)
示例#2
0
def test_bind():
    hdr = _strstream("stuff\n@ 1D IN SOA foo bar " +
        datetime.date.today().strftime("%Y%m%d") + "02 a b c d\n")
    out = StringIO.StringIO()

    conf = generator.BindConfig.create(outputfile=None)
    conf.output = out
    conf.header = hdr
    conf.footer = None

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="addr", pool=models.Pool(dns_record="A"))
    addr2 = models.Address(addr="rdda", pool=models.Pool(dns_record="A"))
    addr3 = models.Address(addr="addr2", pool=models.Pool(dns_record="AAAA"))
    conf.createconf([(models.Pool.create(name="tstp", definition="1.2.3.0/24"),
        [(host1, addr1, [], ""), (host1, addr2, [], ""),
            (host2, addr3, [], "")])])

    assert(out.getvalue() == "stuff\n@ 1D IN SOA foo bar "
        + datetime.date.today().strftime("%Y%m%d") + "03 a b c d\n\n;"
        + SLAM_HDR1 + ";" + SLAM_HDR2
        + "; Pool tstp (range: 1.2.3.0/24)\n"
        + "host1\t1D\tIN\tA\taddr\n"
        + "host1\t1D\tIN\tA\trdda\n"
        + "host2\t1D\tIN\tAAAA\taddr2\n"
        + ";" + SLAM_FTR)
示例#3
0
def test_checkfile():
    handle, path1 = tempfile.mkstemp()
    checkf1 = open(path1, "w")
    handle, path2 = tempfile.mkstemp()
    checkf2 = open(path2, "w")

    checkf1.write("host1\t1d\tin\ta\taddr\n"
        + "host1\t1d\tin\ta\trdda\n"
        + "host2\t1d\tin\taaaa\taddr2\n")
    checkf2.write("host142\t1d\tin\ta\taddr\n"
        + "42host1\t1d\tin\ta\trdda\n"
        + "host\t1d\tin\taaaa\taddr2\n")
    checkf1.close()
    checkf2.close()

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="addr", pool=models.Pool(dns_record="A"))
    addr2 = models.Address(addr="rdda", pool=models.Pool(dns_record="A"))
    addr3 = models.Address(addr="addr42", pool=models.Pool(dns_record="A"))
    addr4 = models.Address(addr="rdda42", pool=models.Pool(dns_record="A"))


    conf = generator.BindConfig.create(outputfile="-")
    conf.createconf([(None, [(host1, addr1, [], ""), (host2, addr2, [], "")])])

    conf = generator.BindConfig.create(outputfile="-", checkfile=[path2])
    dup = conf.createconf([(None, [(host1, addr3, [], ""),
        (host2, addr4, [], "")])])
    assert len(dup) == 0

    conf = generator.BindConfig.create(outputfile="-", checkfile=[path1])
    dup = conf.createconf([(None, [(host1, addr1, [], ""),
        (host2, addr2, [], "")])])
    assert len(dup) == 4

    conf = generator.BindConfig.create(outputfile="-",
        checkfile=[path1, path2])
    dup = conf.createconf([(None, [(host1, addr1, [], ""),
        (host2, addr2, [], "")])])
    assert len(dup) == 6

    os.unlink(path1)
    os.unlink(path2)

    # other generators
    _generator_checkfile(generator.RevBindConfig,
        "117.103.168.192.in-addr.arpa. 1D IN PTR test.domain.\n"
        + "118.103.168.192.in-addr.arpa. 1D IN PTR host1.domain.")
    _generator_checkfile(generator.DhcpdConfig,
        "host test { fixed-address test; }\n"
        + "host host1 { fixed-address host1; }")
    _generator_checkfile(generator.QuattorConfig,
        "escape(\"test\"),\"1.2.3.4\"\n"
        + "escape(\"host2\"),\"1.2.2.2\"")
示例#4
0
def test_quattor():
    conf = generator.QuattorConfig.create(outputfile=None)
    out = StringIO.StringIO()
    conf.output = out

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="192.168.50.30", macaddr="01:12:34:56:78:9a")
    addr2 = models.Address(addr="192.168.42.137")
    conf.createconf([(None, [(host1, addr1, [], ""), (host2, addr2, [], "")])])

    assert(out.getvalue() == "\n#" + SLAM_HDR1 + "#" + SLAM_HDR2
        + 'escape("host1"),"192.168.50.30",\n'
        + 'escape("host2"),"192.168.42.137",\n'
        + "#" + SLAM_FTR)
示例#5
0
def test_dhcp():
    conf = generator.DhcpdConfig.create(outputfile=None, domain="foo.example")
    out = StringIO.StringIO()
    conf.output = out

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="192.168.50.30", macaddr="01:12:34:56:78:9a")
    addr2 = models.Address(addr="192.168.42.137")
    conf.createconf([(None, [(host1, addr1, [], ""), (host2, addr2, [], "")])])

    assert(out.getvalue() == "\n#" + SLAM_HDR1 + "#" + SLAM_HDR2
        + "option domain-name \"foo.example\";\n"
        + "host host1 { hardware ethernet 01:12:34:56:78:9a; "
        + "fixed-address host1; }\n"
        + "#" + SLAM_FTR)
示例#6
0
def test_revbind():
    conf = generator.RevBindConfig.create(outputfile=None, timeout="6H")
    out = StringIO.StringIO()
    conf.output = out
    conf.header = None
    conf.footer = None

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="192.168.50.30",
        pool=models.Pool(addr_range_type="ip4", dns_record="A"))
    addr2 = models.Address(addr="fe80:1234:5678:9abc:def0:1234:5678:9abc",
        pool=models.Pool(addr_range_type="ip6", dns_record="AAAA"))
    conf.createconf([(None, [(host1, addr1, [], ""), (host2, addr2, [], "")])])

    print("@@@" + out.getvalue() + "@@@")
    assert(out.getvalue() == "\n;" + SLAM_HDR1 + ";" + SLAM_HDR2
        + "30.50.168.192.in-addr.arpa.\t6H\tIN\tPTR\thost1\n"
        + "c.b.a.9.8.7.6.5.4.3.2.1.0.f.e.d.c.b.a.9.8.7.6.5.4.3.2.1.0.8.e.f"
        + ".ip6.arpa.\t6H\tIN\tPTR\thost2\n"
        + ";" + SLAM_FTR)
示例#7
0
def _generator_checkfile(cls, content):
    handle, path = tempfile.mkstemp()
    checkf = open(path, "w")
    checkf.write(content)
    checkf.close()

    host1 = models.Host(name="host1")
    host2 = models.Host(name="host2")
    addr1 = models.Address(addr="addr", pool=models.Pool(dns_record="A"))
    addr2 = models.Address(addr="rdda", pool=models.Pool(dns_record="A"))

    conf = cls.create(outputfile="-")
    dup = conf.createconf([(None, [(host1, addr1, [], ""),
        (host2, addr2, [], "")])])
    assert len(dup) == 0

    conf = cls.create(outputfile="-", checkfile=[path])
    dup = conf.createconf([(None, [(host1, addr1, [], ""),
        (host2, addr2, [], "")])])
    assert len(dup) == 1

    os.unlink(path)
示例#8
0
def create_host(host,
                pool=None,
                address=None,
                mac=None,
                random=False,
                alias=None,
                category=None,
                serial="",
                inventory="",
                duration=None,
                nodns=False):
    """Create a new host and assign it the first element of addesses or
    automatically one from the given pool, eventually random."""
    #validation
    if not host:
        raise MissingParameterError(
            "You must provide a name for the new host.")
    if not isValidHostname(hostname=host):
        raise PropertyFormatError(
            "You must provide a valid name (without space, special character) for the new host."
        )

    if models.Host.objects.filter(name=host):
        raise DuplicateObjectError("Host as the host name [" + host +
                                   "] already exists.")
    #anomalie9
    if models.Alias.objects.filter(name=host):
        raise DuplicateObjectError("A alias as the host name [" + host +
                                   "] already exists.")
    if alias:
        for alia in alias:
            if not isValidHostname(hostname=alia):
                raise PropertyFormatError(
                    "You must provide a valid alias name (without space, special character) for the new host."
                )
            if models.Alias.objects.filter(name=alia):
                raise DuplicateObjectError("A alias as alias name [" +
                                           str(alia) + "] already exists.")
            if models.Host.objects.filter(name=alia):
                raise DuplicateObjectError("A Host as alias name [" +
                                           str(alia) + "] already exists.")
            if host == alia:
                raise DuplicateObjectError(
                    "Host should not be equel to a alias name [" + str(alia) +
                    "].")
    #fin anomalie9
    hostobj = models.Host(name=host)

    if not alias:
        alias = []

    logmac = ""
    if mac:
        logmac = " (mac: " + str(mac) + ")"
    LOGGER.info("Create new host \"" + str(host) + logmac + "\".")

    if category:
        hostobj.category = category
    if serial:
        hostobj.serial = serial
    if inventory:
        hostobj.inventory = inventory
    if nodns:
        hostobj.nodns = True
    hostobj.save()

    for alia in alias:
        if models.Alias.objects.filter(name=alia):
            LOGGER.warn("Alias " + str(alia) +
                        " already exists and refers to " +
                        models.Alias.objects.get(name=alia).host.name)
        else:
            aliasobj = models.Alias(name=alia, host=hostobj)
            aliasobj.save()

    if pool or category or address:
        addrobj = allocate_address(pool, hostobj, address, random, category)
        pool = addrobj.pool
        addrres = str(addrobj)
        if duration:
            addrobj.duration = (addrobj.date +
                                datetime.timedelta(days=duration))
            addrobj.save()

        if mac:
            if addrobj:
                addrobj.macaddr = mac
                addrobj.save()
            elif hostobj.address_set.all():
                first_addr = hostobj.address_set.all()[0]
                first_addr.macaddr = mac
                first_addr.save()
        LOGGER.info("Assigned address " + addrres + " to " + str(host) +
                    " from pool " + pool.name)
        return str(hostobj), addrres
    else:
        if mac:
            addrobj = models.Address(macaddr=mac, host=hostobj)
            addrobj.save()
        return str(hostobj), None