Example #1
0
def t21():
    pretty = '%s t21' % __file__
    print(pretty)

    sock, port = find_free_port()
    accounts = {'admin':'key2', 'root':'key3', 'nobody':'key4'}
    c = MockControl(port, 'key1', sock, accounts)
    c.start()

    r = RemoteControl(('',port), 'key4', timeout=5)
    who = r.whoami()
    if who != 'key4':
        print('FAIL %s: wrong who: %s' % (pretty, who))
        c.terminate()
        c.join()
        return False

    r = RemoteControl(('',port), 'key4', timeout=5)
    try:
        r.whoami_preauth()
    except Exception, e:
        if str(e) != 'not authorized to make this call':
            print('FAIL %s: wrong error: %s' % (pretty, str(e)))
            c.terminate()
            c.join()
            return False
Example #2
0
def t19():
    pretty = '%s t19' % __file__
    print(pretty)

    sock, port = find_free_port()
    c = MockControl(port, 'key1', sock, {'alt2':'key2', 'alt3':'key3'})
    c.start()

    r = RemoteControl(('',port), 'no such key', timeout=5)
    who = r.whoami()
    if who != None:
        print('FAIL %s: wrong who 1: %s' % (pretty, who))
        c.terminate()
        c.join()
        return False

    r = RemoteControl(('',port), 'key3', timeout=5)
    who = r.whoami()
    if who != 'key3':
        print('FAIL %s: wrong who 2: %s' % (pretty, who))
        c.terminate()
        c.join()
        return False

    c.terminate()
    c.join()
    return True
Example #3
0
def t09():
    pretty = '%s t9' % __file__
    print(pretty)

    s,p1 = find_free_port()
    q1   = Pipe()
    ctr1 = MockControl(p1, 'password', s, [], q1)
    ctr1.start()

    s,p2 = find_free_port()
    q2   = Pipe()
    ctr2 = MockControl(p2, None, s, [], q2)
    ctr2.start()

    rem1 = RemoteControl(('',p1), 'password', timeout=5)
    rem1.connect_remote(('',p2), None)

    rem2 = RemoteControl(('',p2), None, timeout=5)
    rem2.stop(__async__=True)

    result = True
    try:
        msg = q1.get(timeout=1)
        if msg != ['',p2]:
            print('FAIL %s: wrong connection lost: %s' % (pretty, msg))
            result = False
    except Exception, e:
        print('FAIL %s: connection not lost: %s' % (pretty, str(e)))
        result = False
        ctr2.terminate()
Example #4
0
def t11():
    pretty = '%s t11' % __file__
    print(pretty)

    s,p1 = find_free_port()
    q1   = Pipe()
    ctr1 = MockControl(p1, 'password', s, [], q1)
    ctr1.start()

    s,p2 = find_free_port()
    q2   = Pipe()
    ctr2 = MockControl(p2, None, s, [], q2)
    ctr2.start()

    # ask ctr1 to create a connection to ctr2
    rem1 = RemoteControl(('',p1), 'password', timeout=5)
    msg = rem1.connect_remote(('',p2), None)
    if msg != 'pong':
        print('FAIL %s: bad connection established: %s' % (pretty, msg))
        ctr1.terminate()
        ctr2.terminate()
        ctr1.join()
        ctr2.join()
        return False

    # tell ctr1 to shut down and wait for out of band ack
    rem1.stop(__async__=True)
    try:
        msg = q1.get(1)
    except Exception, e:
        print('FAIL %s: first oob not received: %s' % (pretty, str(e)))
        ctr1.terminate()
        ctr2.terminate()
        return False
Example #5
0
def t03():
    pretty = '%s t3' % __file__
    print(pretty)

    sock, port = find_free_port()
    try:
        pipe    = Pipe()
        control = MockControl(None, None, sock, [], pipe)
        control.start()
    except Exception, e:
        print('FAIL %s: could not create control: %s' % (pretty, str(e)))
        return False
Example #6
0
def t10():
    pretty = '%s t10' % __file__
    print(pretty)

    s,p = find_free_port()
    q = Pipe()
    c = MockControl(p, None, s, [], q)
    c.start()
    r = RemoteControl(('',p), None, timeout=5)
    r.sync_ping()

    c.terminate()
    try:
        q.get(timeout=1)
    except Exception, e:
        print('FAIL %s: never got oob message: %s' % (pretty, str(e)))
        c.kill()
        return False
Example #7
0
def t17():
    pretty = '%s t17' % __file__
    print(pretty)

    sock, port = find_free_port()
    c = MockControl(port, 'key1', sock, {'alt':'key'})
    c.start()

    r = RemoteControl(('',port), 'key', timeout=5)
    who = r.whoami()

    result = True
    if who != 'key':
        print('FAIL %s: wrong who: %s' % (pretty, who))
        result = False

    c.terminate()
    c.join()
    return result
Example #8
0
def t14():
    pretty = '%s t14' % __file__
    print(pretty)

    s,p = find_free_port()
    c = MockControl(p, 'pass', s)
    c.start()
    r = RemoteControl(('',p), 'pass', timeout=5)

    # any call to a non-function will do
    result = True
    try:
        r.bla_bla_bla()
        print('FAIL %s: call did not fail' % pretty)
        result = False
    except Exception, e:
        if str(e) != 'no such RPC: bla_bla_bla':
            print('FAIL %s: wrong error message: %s' % (pretty, str(e)))
            result = False
Example #9
0
def t23():
    pretty = '%s t23' % __file__
    print(pretty)

    sock, port = find_free_port()
    c = MockControl(port, None, sock, {'admin':None, 'root':None})
    c.start()

    r = RemoteControl(('',port), 'foo bar', timeout=5)
    who = r.whoami_preauth()
    if who != None:
        print('FAIL %s: wrong who: %s' % (pretty, who))
        c.terminate()
        c.join()
        return False

    c.terminate()
    c.join()
    return True
Example #10
0
def t07():
    pretty = '%s t7' % __file__
    print(pretty)

    s,p  = find_free_port()
    ctrl = MockControl(p, 'password', s)
    ctrl.start()
    con1 = RemoteControl(('',p), 'password', 1)
    try:
        con1.raise_exit()
    except Exit:
        pass # good
    ctrl.join(2)

    con2 = BlockingConnection(('',p), 'password')
    try:
        con2.connect(timeout=1)
    except ConnectionRefused, e:
        pass # good
Example #11
0
def t18():
    pretty = '%s t18' % __file__
    print(pretty)

    sock, port = find_free_port()
    c = MockControl(port, 'key1', sock, {'alt2':'key2', 'alt3':'key3'})
    c.start()

    result = True
    for key in ['key1', 'key2', 'key3']:
        r = RemoteControl(('',port), key, timeout=5)
        who = r.whoami()
        if who != key:
            print('FAIL %s: wrong who: %s' % (pretty, who))
            result = False
            break

    c.terminate()
    c.join()
    return result
Example #12
0
def t20():
    pretty = '%s t20' % __file__
    print(pretty)

    sock, port = find_free_port()
    accounts = {'admin':'key2', 'root':'key3', 'nobody':'key4'}
    c = MockControl(port, 'key1', sock, accounts)
    c.start()

    r = RemoteControl(('',port), 'key2', timeout=5)
    who = r.whoami_preauth()
    if who != 'key2':
        print('FAIL %s: wrong who: %s' % (pretty, who))
        c.terminate()
        c.join()
        return False

    c.terminate()
    c.join()
    return True
Example #13
0
def t13():
    pretty = '%s t13' % __file__
    print(pretty)

    s,p1 = find_free_port()
    q1   = Pipe()
    ctr1 = MockControl(p1, 'pass', s, [], q1)
    ctr1.start()

    s,p2 = find_free_port()
    q2   = Pipe()
    ctr2 = MockControl(p2, 'secret', s, [], q2)
    ctr2.start()

    # ask ctr1 to create a connection to ctr2
    rem1 = RemoteControl(('',p1), 'pass', timeout=5)
    msg = rem1.connect_remote(('',p2), 'secret')
    if msg != 'pong':
        print('FAIL %s: bad connection established: %s' % (pretty, msg))
        ctr1.terminate()
        ctr2.terminate()
        ctr1.join()
        ctr2.join()
        return False

    # stop ctr1 but don't drop the connection to it (rem1 is still in scope and
    # doesn't get garbage collected or expressely closed)
    rem1.stop(__async__=True)
    ctr1.join()

    # check if ctr2 discovered the connection loss from ctr1
    result = True
    try:
        msg = q2.get(1)
        if type(msg) != list: # no way of knowing the exact content beforehand
            print('FAIL %s: wrong oob received: %s' % (pretty, msg))
            result = False
    except Exception, e:
        print('FAIL %s: oob not received: %s' % (pretty, str(e)))
        result = False
Example #14
0
def t24():
    pretty = '%s t24' % __file__
    print(pretty)

    sock, port = find_free_port()
    q = Pipe()
    c = MockControl(port, None, sock, pipe=q, interval=1)
    c.start()

    start = datetime.now()
    ok = True
    for i in range(3):
        try:
            msg = q.get(timeout=2)
        except Exception, e:
            print('FAIL %s: did not receive message: %s' % (pretty, str(e)))
            ok = False
            break
        if msg != 'idle':
            print('FAIL %s: wrong message: %s' % (pretty, str(msg)))
            ok = False
            break
Example #15
0
def t08():
    pretty = '%s t8' % __file__
    print(pretty)

    s,p1 = find_free_port()
    q    = Pipe()
    ctr1 = MockControl(p1, 'password', s, [], q)
    ctr1.start()

    s,p2 = find_free_port()
    ctr2 = MockControl(p2, None, s)
    ctr2.start()

    rem1 = RemoteControl(('',p1), 'password', timeout=5)
    rem1.connect_remote(('',p2), None)

    result = True
    ctr2.kill()
    try:
        msg = q.get(timeout=1)
    except Exception, e:
        print('FAIL %s: connection not lost: %s' % (pretty, str(e)))
        result = False
Example #16
0
def t12():
    pretty = '%s t12' % __file__
    print(pretty)

    s,p1 = find_free_port()
    ctr1 = MockControl(p1, 'password', s)
    ctr1.start()

    s,p2 = find_free_port()
    q    = Pipe()
    ctr2 = MockControl(p2, None, s, [], q)
    ctr2.start()

    # ask ctr1 to create a connection to ctr2
    rem1 = RemoteControl(('',p1), 'password', timeout=5)
    msg = rem1.connect_remote(('',p2), None)
    if msg != 'pong':
        print('FAIL %s: bad connection established: %s' % (pretty, msg))
        ctr1.terminate()
        ctr2.terminate()
        ctr1.join()
        ctr2.join()
        return False

    # kill ctr1
    ctr1.kill()

    # check if ctr2 discovered the connection loss from ctr1
    result = True
    try:
        msg = q.get(1)
        if type(msg) != list: # no way of knowing the exact content beforehand
            print('FAIL %s: wrong oob received: %s' % (pretty, msg))
            result = False
    except Exception, e:
        print('FAIL %s: oob not received: %s' % (pretty, str(e)))
        result = False
Example #17
0
        def run(self, port, pipe):
            r = RemoteControl(('',port), 'pass', timeout=5)
            try:
                r.mock_death()
            except ConnectionClosed, e:
                pass # good
            except Exception, e:
                print('FAIL %s: unexpected exception: %s' % (pretty, str(e)))
                pipe.put(False)
            pipe.put(True)

    sock,port = find_free_port()
    q1 = Pipe()
    c = MockControl(port, 'pass', sock, [], q1)
    c.start()
    q2 = Pipe()
    p = Ping(args=(port,q2))
    p.start()

    result = True
    try:
        result = q2.get(timeout=5)
    except Exception, e:
        print('FAIL %s: death was mocked: %s' % (pretty, str(e)))
        result = False
        c.terminate()

    c.join()
    return result