예제 #1
0
    def run(self):
        time.sleep(1)  # avoid printing glitch
        grasp.tprint(
            "WARNING: you can't run this test suite more than once without restarting the Python context; it leaves GRASP data structures dirty!\n"
        )
        grasp.skip_dialogue(testing=True, selfing=True, diagnosing=True)
        time.sleep(1)  # just to avoid mixed up print output

        ####################################
        # Test code: register ASA/objective#
        ####################################

        test_obj = grasp.objective("EX1")
        test_obj.loop_count = 2
        test_obj.synch = True
        err, test_nonce = grasp.register_asa("ASA-1")
        if not err:
            err = grasp.register_obj(test_nonce, test_obj, ttl=10000)
            if not err:
                grasp.tprint("ASA-1 and EX1 Registered OK")

####################################
# Test code: discover EX1          #
####################################

        err, test_ll = grasp.discover(test_nonce, test_obj, 10000)
        if len(test_ll) > 0:
            grasp.tprint("EX1 discovery result", test_ll[0].locator)
        else:
            grasp.tprint("No EX1 discovery response")

####################################
# Test code: register and discover #
# Boot ASA/objective               #
####################################

        err, boot_nonce = grasp.register_asa("Boot")
        if err:
            #we've got a problem...
            raise RuntimeError("Can't register Boot as ASA")

        boot_obj = grasp.objective("Boot")
        boot_obj.loop_count = 2
        boot_obj.synch = True
        err = grasp.register_obj(boot_nonce, boot_obj, discoverable=True)

        if err:
            #we've got a different problem...
            raise RuntimeError("Can't register Boot objective")

        for i in range(3):
            #test discovery 3 times, including artificial Divert
            grasp.tprint("Test ASA: grasp.test_divert", grasp.test_divert)
            err, boot_ll = grasp.discover(boot_nonce, boot_obj, 5000)
            if len(boot_ll) > 0:
                grasp.tprint("Boot discovery result", boot_ll[0].locator)
                grasp.test_divert = True
            else:
                grasp.tprint("No Boot discovery response")
            time.sleep(5)
        grasp.test_divert = False

        ####################################
        # Test code: send Flood messages
        ####################################

        obj1 = grasp.objective("Money")
        obj1.synch = True
        obj1.loop_count = 2
        err = grasp.register_obj(test_nonce, obj1)
        obj1.value = [100, "NZD"]

        obj2 = grasp.objective("Bling")
        obj2.synch = True
        obj2.loop_count = 2
        err = grasp.register_obj(test_nonce, obj2)
        obj2.value = ["Diamonds", "Rubies"]
        err = grasp.register_obj(test_nonce, obj2)
        if err:
            grasp.tprint(grasp.etext[err])

        obj3 = grasp.objective("Intent.PrefixManager")
        obj3.synch = True
        obj3.loop_count = 2
        err = grasp.register_obj(test_nonce, obj3)
        #obj3.value = '{"autonomic_intent":[{"model_version": "1.0"},{"intent_type": "Network management"},{"autonomic_domain": "Customer_X_intranet"},{"intent_name": "Prefix management"},{"intent_version": 73},{"Timestamp": "20150606 00:00:00"},{"Lifetime": "Permanent"},{"signature":"XXXXXXXXXXXXXXXXXXX"},{"content":[{"role": [{"role_name": "RSG"},{"role_characteristic":[{"prefix_length":"34"}]}]},{"role": [{"role_name": "ASG"},{"role_characteristic":[{"prefix_length": "44"}]}]},{"role": [{"role_name": "CSG"},{"role_characteristic":[{"prefix_length": "56"}]}]}]}]}'

        #obj3.value = '{"autonomic_intent dummy text"}'
        #obj3.value = bytes.fromhex('48deadbeefdeadbeef') #dummy CBOR
        obj3.value = cbor.dumps(["Some", "embedded", "CBOR", [1, 2, 3]])

        grasp.flood(test_nonce, 0, grasp.tagged_objective(obj1, None),
                    grasp.tagged_objective(obj2, None),
                    grasp.tagged_objective(obj3, None))

        ###################################
        # Test code: Listen Synchronize as from Boot ASA
        ###################################

        boot_obj.value = [1, "two", 3]
        boot_obj.synch = True
        err = grasp.listen_synchronize(boot_nonce, boot_obj)
        grasp.tprint("Listen synch", grasp.etext[err])
        grasp.tprint(grasp._obj_registry[1].objective.name, "value",
                     grasp._obj_registry[1].objective.value)

        ###################################
        # Test code: call Synchronize as from EX1
        ###################################
        time.sleep(5)

        err, result = grasp.synchronize(test_nonce, obj2, None, 5000)
        if not err:
            grasp.tprint("Flooded synch obj2", result.value)
        else:
            grasp.tprint("Synch fail obj2", grasp.etext[err])

        err, result = grasp.synchronize(test_nonce, obj3, None, 5000)
        if not err:
            grasp.tprint("Flooded synch obj3", result.value)
        else:
            grasp.tprint("Synch fail obj3", grasp.etext[err])

        #this should fail as test_obj was neither flooded or listened for.
        err, result = grasp.synchronize(test_nonce, test_obj, None, 5000)
        if not err:
            grasp.tprint("Synch test_obj (should fail)", result.value)
        else:
            grasp.tprint("Synch fail test_obj (should fail)", grasp.etext[err])

        boot2_obj = grasp.objective("Boot")
        boot2_obj.synch = True

        err, result = grasp.synchronize(test_nonce, boot2_obj, None, 5000)
        if not err:
            grasp.tprint("Synch boot2_obj", result.name, result.value)
        else:
            grasp.tprint("Synch fail boot2_obj", grasp.etext[err])

###################################
# Test code: print obj_registry
# and flood cache
###################################

        grasp.tprint("Objective registry contents:")
        for x in grasp._obj_registry:
            o = x.objective
            grasp.tprint(o.name, "ASA:", x.asa_id, "Listen:", x.listening,
                         "Neg:", o.neg, "Synch:", o.synch, "Count:",
                         o.loop_count, "Value:", o.value)
        grasp.tprint("Flood cache contents:")
        for x in grasp._flood_cache:
            grasp.tprint(x.objective.name, "count:", x.objective.loop_count,
                         "value:", x.objective.value, "source:", x.source)
        time.sleep(5)

        ###################################
        # Test code: check flood cache for Tag 24 case
        ###################################

        flobj = grasp.objective("Intent.PrefixManager")
        flobj.synch = True
        err, tobs = grasp.get_flood(test_nonce, flobj)
        if not err:
            for x in tobs:
                try:
                    grasp.tprint("Flooded CBOR", x.objective.name,
                                 cbor.loads(x.objective.value))
                except:
                    grasp.tprint("Flooded raw", x.objective.name,
                                 x.objective.value)

###################################
# Test code: deregister and then
# attempt synch
###################################

        err = grasp.deregister_obj(boot_nonce, boot_obj)
        if not err:
            grasp.tprint("Deregistered Boot", )
        else:
            grasp.tprint("Deregister failure", grasp.etext[err])

        err, result = grasp.synchronize(test_nonce, boot2_obj, None, 5000)
        if not err:
            grasp.tprint("Synch boot2_obj (should fail)", result.name,
                         result.value)
        else:
            grasp.tprint("Synch fail boot2_obj (should fail)",
                         grasp.etext[err])

###################################
# Test code: start in-host negotiate test
###################################
        cheat_nonce = None
        Neg1().start()
        time.sleep(10)
        Neg2().start()

        #####################################
        # Test code:
        # test deregistering an ASA,
        # and exit the thread
        #####################################

        grasp.deregister_asa(test_nonce, "ASA-1")
        grasp.tprint("Exiting ASA test thread")
예제 #2
0
파일: pfxm3.py 프로젝트: woodlums/graspy
##    time.sleep(5)

###################################
# Negotiate obj1 as listener for ever
# unless pool is empty
###################################

main_negotiator().start()

###################################
# Get parameters (if not master)
###################################

if not master:
    grasp.tprint("Attempting to obtain prefix management parameters")
    err, param_obj = grasp.synchronize(asa_nonce, obj2, None, 3000)
    if not err:
        grasp.tprint("Results for", param_obj.name, ":", param_obj.value)
    else:
        grasp.tprint("Error obtaining parameters:", grasp.etext[err])

    need = 32  #arbitrary starter pack
    delegator().start()

###################################
# Start garbage collector
###################################

compress().start()

###################################
예제 #3
0
err = grasp.register_obj(asa_nonce, obj3)
if not err:
    grasp.tprint("Objective EX3 registered OK")
else:
    exit()

###################################
# Try synchronizes
###################################

time.sleep(5)

grasp.tprint("Synchronization tests will start; some may fail.")

err, result = grasp.synchronize(asa_nonce, obj1, None, 5000)
if not err:
    grasp.tprint("Synchronized EX1", result.value)
else:
    grasp.tprint("Synch failed EX1", grasp.etext[err])

err, result = grasp.synchronize(asa_nonce, obj2, None, 5000)
if not err:
    grasp.tprint("Synchronized EX2", result.value)
else:
    grasp.tprint("Synch failed EX2", grasp.etext[err])

#This should fail as test_obj was neither flooded or listened for.
test_obj = grasp.objective("Nonsense")
err, result = grasp.synchronize(asa_nonce, test_obj, None, 5000)
if not err: