Example #1
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 #2
0
def t7(h1, h2):
    pretty = '%s t7' % __file__
    print(pretty)

    # start a mocked broker to receive updates from the lister
    pipe = Pipe()
    sock, port = find_free_port()
    broker = MockBroker(port, sock, pipe)
    lister = HandsetLister(port, None, [], False)  # filter out all paths
    broker.start()
    lister.start()

    def stop():
        broker.terminate()
        lister.terminate()
        broker.join()
        lister.join()

    try:
        profile = pipe.get(timeout=3)
        print('FAIL %s: unfiltered profile seen: %s' % (pretty, profile))
        stop()
        return False
    except ConnectionTimeout:
        pass  # good

    stop()
    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 t9(h1, h2):
    pretty = '%s t9' % __file__
    print(pretty)

    p1 = h1.get_profile()['sysfs_path']
    p2 = h2.get_profile()['sysfs_path']

    # start a mocked broker to receive updates from the lister
    pipe = Pipe()
    sock, port = find_free_port()
    broker = MockBroker(port, sock, pipe)
    lister = HandsetLister(port, None, [p1], False)  # only filter one handset
    broker.start()
    lister.start()

    def stop():
        broker.terminate()
        lister.terminate()
        broker.join()
        lister.join()

    # reboot the handsets
    h1.reboot()
    h2.reboot()
    h1.wait_power_state('boot_completed', timeout=120)
    h2.wait_power_state('boot_completed', timeout=120)
    # give the lister a couple of seconds to report to the broker (the handset
    # makes a call by itself to the lister class, not waiting for the lister to
    # report state which may cause a situation where the handset's "wait power
    # state" is done but the broker has not yet been notified)
    time.sleep(3)

    # check that various power states were reported by the lister for the
    # filtered handset
    seen = []
    while True:
        try:
            profile = pipe.get(False)
            if profile['sysfs_path'] != p1:
                print('FAIL %s: wrong profile added: %s' % (pretty, profile))
                stop()
                return False
            seen.append(profile['power_state'])
        except ConnectionTimeout:
            break
    if 'offline' not in seen:
        print('FAIL %s: offline state not reported: %s' % (pretty, seen))
        stop()
        return False
    if 'boot_completed' not in seen:
        print('FAIL %s: boot_completed state not reported: %s' %
              (pretty, seen))
        stop()
        return False

    stop()
    return True
Example #5
0
def t01():
    pretty = '%s t01' % __file__
    print(pretty)

    p = Pipe()
    p.put('hello')

    try:
        msg = p.get(timeout=0)
    except Exception, e:
        print('FAIL %s: first get() failed: %s' % (pretty, e))
        return False
Example #6
0
def t5(pretty, factory):
    pipe = Pipe()
    p = P2(pipe)
    p.start()
    factory.processes.append(p)

    gc_pid = pipe.get()

    try:
        proc = psutil.Process(gc_pid)
    except Exception, e:
        print('FAIL %s: could not query grandchild: %s' % (pretty, e))
        return False
Example #7
0
def make_peers():
    sock, port = find_free_port()
    pipe = Pipe()
    ctrl = StepControl(port, 'password', sock, {'admin': None}, pipe, 0.1)
    conn = BlockingConnection(('', port))
    ctrl.initialize()
    return conn, ctrl, pipe
Example #8
0
def t8(pretty, factory):
    pipe = Pipe()
    p = P2(pipe)
    p.start()
    factory.processes.append(p)
    gc_pid = pipe.get()
    d = Dummy(gc_pid)
    factory.processes.append(d)

    try:
        alive = d.is_alive()
        print('FAIL %s: could check liveness of grandchild' % pretty)
        return False
    except Unwaitable:
        pass
    except Exception, e:
        print('FAIL %s: wrong error: %s' % (pretty, e))
        return False
Example #9
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 #10
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 #11
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 #12
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 #13
0
def t8(h1, h2):
    pretty = '%s t8' % __file__
    print(pretty)

    p1 = h1.get_profile()['sysfs_path']
    p2 = h2.get_profile()['sysfs_path']

    # start a mocked broker to receive updates from the lister
    pipe = Pipe()
    sock, port = find_free_port()
    broker = MockBroker(port, sock, pipe)
    lister = HandsetLister(port, None, None, False)  # filter in all paths
    broker.start()
    lister.start()

    def stop():
        broker.terminate()
        lister.terminate()
        broker.join()
        lister.join()

    found = False
    seen = []
    while True:
        try:
            seen.append(pipe.get(timeout=3)['sysfs_path'])
            if p1 in seen and p2 in seen:
                found = True
                break
        except ConnectionTimeout:
            break

    if not found:
        print('FAIL %s: filtered profile not seen' % pretty)
        stop()
        return False

    stop()
    return True
Example #14
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 #15
0
def t6(h1, h2):
    pretty = '%s t6' % __file__
    print(pretty)

    p1 = h1.get_profile()['sysfs_path']
    p2 = h2.get_profile()['sysfs_path']

    # start a mocked broker to receive updates from the lister
    pipe = Pipe()
    sock, port = find_free_port()
    broker = MockBroker(port, sock, pipe)
    lister = HandsetLister(port, None, [p1, p2], False)
    broker.start()
    lister.start()

    def stop():
        broker.terminate()
        lister.terminate()
        broker.join()
        lister.join()

    seen = []
    try:
        seen.append(pipe.get(timeout=3))
    except ConnectionTimeout:
        print('FAIL %s: filtered profile not seen' % pretty)
        stop()
        return False

    for s in seen:
        if s['sysfs_path'] not in [p1, p2]:
            print('FAIL %s: wrong path: %s' % (pretty, s['sysfs_path']))
            stop()
            return False

    stop()
    return True
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 decorated_fn():
     sock, port = find_free_port()
     pipe    = Pipe()
     control = self.Class(port, 'password', sock, [], pipe)
     control.start()
     remote  = RemoteControl(('', port), 'password', 5)
     result  = fn(control, remote, pipe)
     try:
         time.sleep(0.1)
         control.terminate()
         control.join()
     except OSError, e:
         if e.errno == errno.ESRCH:
             pass # the test already killed the process
         else:
             raise e
Example #18
0
def all_review():
    res = True
    res &= tests.review.t1()
    res &= tests.review.t2()
    res &= tests.review.t3()

    gerrit_listener_pipe = Pipe()
    gerrit_listener = events.GerritEventStream(pipe=gerrit_listener_pipe)
    gerrit_listener.start()

    res &= tests.review.t4(gerrit_listener)
    res &= tests.review.t5(gerrit_listener)
    res &= tests.review.t6(gerrit_listener)

    gerrit_listener.terminate()
    gerrit_listener.join()
    return res
Example #19
0
    def start(self, daemonize=False, synchronize=False):
        if synchronize:
            # pass a pipe down to the (grand) child so it can pass its pid back
            # when the child has been fully initialized. this synchronizes the
            # setup of signal handlers, which is otherwise hard to do for the
            # caller. it also lets the caller see the pid of daemon processes
            # as a property on the Process instance.
            # synchronization has a significant performance cost. disable it
            # for short lived daemons that need to be created quickly and will
            # not be interacted with by the caller. e.g. panotti shouters.
            pipe = Pipe()
            exclude = [pipe.w]
        else:
            pipe = None
            exclude = []
        if daemonize:
            if not self._daemonize():
                if synchronize:
                    self._pid = pipe.get()  # now guaranteed to be reparented
                    del pipe
                return  # parent returns to caller
            else:
                pass  # continue below, at self.redirect()
        else:
            self._pid = os.fork()
            if self._pid != 0:  # parent process
                if synchronize:
                    pipe.get()  # now guaranteed to be fully initialized
                    del pipe
                return
        self.close_fds(exclude)
        self.redirect()
        gc.collect()
        self.log('start PID=%d' % self.pid)
        self.initialize()

        if synchronize:
            pipe.put(self.pid)
            del pipe

        exit_code = 0

        try:
            self.target(*self.args)
        except Exception, e:
            self.log('Exception in target():\n%s' % traceback.format_exc())
            exit_code = 1
Example #20
0
            r2 = RemoteBroker(address, home=home)
            try:
                w, d = r2.get_resource({'type':'workspace'}, {'type':'handset'})
            except Exception, e:
                print('FAIL %s: got no handset: %s' % (pretty, str(e)))
                return
            # use the queue to synchronize with parent process. send the
            # profiles of the allocated handsets so that it has something to
            # look up in the sessions held by the broker
            q1.put([
                w.get_profile(),
                d.get_profile()
            ])
            q2.get() # block until parent signals, then die

    q1 = Pipe()
    q2 = Pipe()
    p = Client(args=(q1, q2, r.address, factory.HOME.path))
    p.start()

    profiles = [profile_factory(prof) for prof in q1.get()]
    # check that the resources really are allocated
    listing = r.list_allocations_all()
    for resource in profiles:
        if resource not in listing:
            print('FAIL %s: resource not allocated: %s' % (pretty, resource))
            return False

    q2.put('DIE DIE DIE!')
    p.join() # wait for death
Example #21
0
            exclude.append(self.args[1].w)
            Process.close_fds(self, exclude)

        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()