def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        jobID = self.ns.submitJob( 'TEST', 'bla', 'a7' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario305' )
        ns_client.set_client_identification( 'node', 'session' )
        ns_client.on_warning = self.report_warning
        changeAffinity( ns_client, [ 'a0', 'a1', 'a2' ], [ 'a3', 'a4', 'a5' ] )

        output = execAny( ns_client,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        values = parse_qs( output, True, True )
        receivedJobID = values[ 'job_key' ][ 0 ]
        passport = values[ 'auth_token' ][ 0 ]

        if jobID != receivedJobID:
            raise Exception( "Received job ID does not match. Expected: " +
                             jobID + " Received: " + receivedJobID )

        execAny( ns_client, 'RETURN2 ' + jobID + ' ' + passport )
        output = execAny( ns_client,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        if output != "":
            raise Exception( "Expect no job (it's in the blacklist), "
                             "but received one: " + output )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch( 4 )

        # Client #2 plays a passive role of holding an affinity (a2)
        ns_client2 = self.getNetScheduleService( 'TEST', 'scenario401' )
        ns_client2.set_client_identification( 'node2', 'session2' )
        changeAffinity( ns_client2, [ 'a2' ], [] )
        self.ns.submitJob( 'TEST', 'bla', 'a2' )


        ns_client1 = self.getNetScheduleService( 'TEST', 'scenario400' )
        ns_client1.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client1, [ 'a1' ], [] )

        # Socket to receive notifications
        notifSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        notifSocket.bind( ( "", 0 ) )
        notifPort = notifSocket.getsockname()[ 1 ]

        # Second client tries to get the pending job - should get nothing
        output = execAny( ns_client1,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1 port=' + str( notifPort ) + ' timeout=15' )
        if output != "":
            notifSocket.close()
            raise Exception( "Expect no jobs, received: " + output )

        # 10 seconds till the job becomes outdated
        time.sleep( 12 )

        data = notifSocket.recv( 8192, socket.MSG_DONTWAIT )
        notifSocket.close()
        if "queue=TEST" not in data:
            raise Exception( "Expected notification, received garbage: " + data )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch( 4 )

        # Client #2 plays a passive role of holding an affinity (a2)
        ns_client2 = self.getNetScheduleService( 'TEST', 'scenario400' )
        ns_client2.set_client_identification( 'node2', 'session2' )
        changeAffinity( ns_client2, [ 'a2' ], [] )
        jobID = self.ns.submitJob( 'TEST', 'bla', 'a2' )


        ns_client1 = self.getNetScheduleService( 'TEST', 'scenario400' )
        ns_client1.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client1, [ 'a1' ], [] )

        output = execAny( ns_client1,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        if output != '':
            raise Exception( "Expected no job, received: '" + output + "'" )

        # 10 seconds till the job becomes obsolete
        time.sleep( 12 )

        output = execAny( ns_client1,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        if "job_key=" + jobID not in output:
            raise Exception( "Expected a job, received: '" + output + "'" )

        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        jobID = self.ns.submitJob( 'TEST', 'bla', 'a0' )

        # First client holds a0 affinity
        ns_client1 = self.getNetScheduleService( 'TEST', 'scenario308' )
        ns_client1.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client1, [ 'a0' ], [] )

        # Second client holds a100 affinity
        ns_client2 = self.getNetScheduleService( 'TEST', 'scenario308' )
        ns_client2.set_client_identification( 'node2', 'session2' )
        changeAffinity( ns_client2, [ 'a100' ], [] )

        # Socket to receive notifications
        notifSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        notifSocket.bind( ( "", 9007 ) )

        # Second client tries to get the pending job - should get nothing
        output = execAny( ns_client2,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1 port=9007 timeout=3' )
        if output != "":
            raise Exception( "Expect no jobs, received: " + output )

        time.sleep( 4 )
        try:
            # Exception is expected
            data = notifSocket.recv( 8192, socket.MSG_DONTWAIT )
            raise Exception( "Expected no notifications, received one: " + data )
        except Exception, exc:
            if "Resource temporarily unavailable" not in str( exc ):
                raise
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        # First client holds a0 affinity
        ns_client = self.getNetScheduleService( 'TEST', 'scenario311' )
        ns_client.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client, [ 'a0' ], [] )

        ns_admin = self.getNetScheduleService( 'TEST', 'scenario310' )

        affInfo = getAffinityInfo( ns_admin )
        if affInfo[ 'affinity_token' ] != 'a0':
            raise Exception( "Unexpected affinity registry content "
                             "after adding 1 preferred affinity (token)" )
        if 'clients__preferred' in affInfo:
            if affInfo[ 'clients__preferred' ] != [ 'node1' ]:
                raise Exception( "Unexpected affinity registry content "
                                "after adding 1 preferred affinity (node)" )
        if 'wn_clients__preferred' in affInfo:
            if affInfo[ 'wn_clients__preferred' ] != [ 'node1' ]:
                raise Exception( "Unexpected affinity registry content "
                                "after adding 1 preferred affinity (node)" )
        info = getClientInfo( ns_admin, 'node1' )
        if info[ 'preferred_affinities_reset' ] != False:
            raise Exception( "Expected to have preferred affinities non reset, "
                             "received: " +
                             str( info[ 'preferred_affinities_reset' ] ) )

        # Worker node timeout is 5 sec
        time.sleep( 7 )

        info = getClientInfo( ns_admin, 'node1' )
        if info[ 'preferred_affinities_reset' ] != True:
            raise Exception( "Expected to have preferred affinities reset, "
                             "received: " +
                             str( info[ 'preferred_affinities_reset' ] ) )

        affInfo = getAffinityInfo( ns_admin )
        if affInfo[ 'affinity_token' ] != 'a0':
            raise Exception( "Unexpected affinity registry content "
                             "after worker node is expired (token)" )
        if 'clients__preferred' in affInfo:
            if affInfo[ 'clients__preferred' ] != None:
                raise Exception( "Unexpected affinity registry content "
                                 "after worker node is expired (node)" )
        if 'wn_clients__preferred' in affInfo:
            if affInfo[ 'wn_clients__preferred' ] != None:
                raise Exception( "Unexpected affinity registry content "
                                 "after worker node is expired (node)" )

        try:
            output = execAny( ns_client,
                              'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        except Exception, excpt:
            if "ePrefAffExpired" in str( excpt ) or "expired" in str( excpt ):
                return True
            raise
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        jobID = self.ns.submitJob( 'TEST', 'bla', 'a0' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario307' )
        ns_client.set_client_identification( 'node', 'session' )
        changeAffinity( ns_client, [ 'a0' ], [] )

        output = execAny( ns_client,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        values = parse_qs( output, True, True )
        receivedJobID = values[ 'job_key' ][ 0 ]
        passport = values[ 'auth_token' ][ 0 ]

        if jobID != receivedJobID:
            raise Exception( "Received job ID does not match. Expected: " +
                             jobID + " Received: " + receivedJobID )

        execAny( ns_client, 'RETURN2 ' + jobID + ' ' + passport )
        # Here: pending job and it is in the client black list

        ns_client2 = self.getNetScheduleService( 'TEST', 'scenario307' )
        ns_client2.set_client_identification( 'node2', 'session2' )
        output = execAny( ns_client2,
                          'GET2 wnode_aff=1 any_aff=1 exclusive_new_aff=0' )
        values = parse_qs( output, True, True )
        receivedJobID = values[ 'job_key' ][ 0 ]
        passport = values[ 'auth_token' ][ 0 ]

        # The first client waits for a job
        process = self.ns.spawnGet2Wait( 'TEST', 3,
                                         [ 'a0' ], False, False,
                                         'node', 'session' )

        # Sometimes it takes so long to spawn grid_cli that the next
        # command is sent before GET2 is sent. So, we have a sleep here
        time.sleep( 2 )

        # Return the job
        execAny( ns_client2, 'RETURN2 ' + jobID + ' ' + passport )

        process.wait()
        if process.returncode != 0:
            raise Exception( "Error spawning GET2" )
        processStdout = process.stdout.read()
        processStderr = process.stderr.read()   # analysis:ignore

        if "NCBI_JSQ_TEST" in processStdout:
            raise Exception( "Expect no notifications but received one" )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        jobID = self.ns.submitJob( 'TEST', 'bla', 'a0' )

        # First client holds a0 affinity
        ns_client1 = self.getNetScheduleService( 'TEST', 'scenario309' )
        ns_client1.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client1, [ 'a0' ], [] )

        output = execAny( ns_client1,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1' )
        values = parse_qs( output, True, True )
        receivedJobID = values[ 'job_key' ][ 0 ]
        passport = values[ 'auth_token' ][ 0 ]      # analysis:ignore

        if jobID != receivedJobID:
            raise Exception( "Unexpected received job ID" )


        # Second client holds a100 affinity
        ns_client2 = self.getNetScheduleService( 'TEST', 'scenario309' )
        ns_client2.set_client_identification( 'node2', 'session2' )
        changeAffinity( ns_client2, [ 'a100' ], [] )

        # Socket to receive notifications
        notifSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        notifSocket.bind( ( "", 0 ) )
        notifPort = notifSocket.getsockname()[ 1 ]

        # Second client tries to get the pending job - should get nothing
        output = execAny( ns_client2,
                          'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=1 '
                          'port=' + str( notifPort ) + ' timeout=3' )
        if output != "":
            raise Exception( "Expect no jobs, received: " + output )

        time.sleep( 4 )
        try:
            # Exception is expected
            data = notifSocket.recv( 8192, socket.MSG_DONTWAIT )
            notifSocket.close()
            raise Exception( "Expected no notifications, received one: " +
                             data )
        except Exception, exc:
            if not "Resource temporarily unavailable" in str( exc ):
                notifSocket.close()
                raise
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        # First client holds a0 affinity
        ns_client = self.getNetScheduleService( 'TEST', 'scenario312' )
        ns_client.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client, [ 'a0' ], [] )

        ns_admin = self.getNetScheduleService( 'TEST', 'scenario312' )

        execAny( ns_client,
                 'GET2 wnode_aff=1 any_aff=0 exclusive_new_aff=0 '
                 'port=9007 timeout=4' )

        info = getNotificationInfo( ns_admin )
        if info[ 'client_node' ] != 'node1':
            raise Exception( "Unexpected client in the notifications list: " +
                             info[ 'client_node' ] )

        # Worker node timeout is 5 sec
        time.sleep( 7 )

        info = getClientInfo( ns_admin, 'node1' )
        if info[ 'preferred_affinities_reset' ] != True:
            raise Exception( "Expected to have preferred affinities reset, "
                             "received: " +
                             str( info[ 'preferred_affinities_reset' ] ) )

        affInfo = getAffinityInfo( ns_admin )
        if affInfo[ 'affinity_token' ] != 'a0':
            raise Exception( "Unexpected affinity registry content "
                             "after worker node is expired (token)" )
        if 'clients__preferred' in affInfo:
            if affInfo[ 'clients__preferred' ] != None:
                raise Exception( "Unexpected affinity registry content "
                                 "after worker node is expired (node)" )
        if 'wn_clients__preferred' in affInfo:
            if affInfo[ 'wn_clients__preferred' ] != None:
                raise Exception( "Unexpected affinity registry content "
                                 "after worker node is expired (node)" )


        if getNotificationInfo( ns_admin, True, 0 ) != None:
            raise Exception( "Expected no notification, got some" )

        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        # First client holds a0 affinity
        ns_client = self.getNetScheduleService( 'TEST', 'scenario310' )
        ns_client.set_client_identification( 'node1', 'session1' )
        changeAffinity( ns_client, [ 'a0' ], [] )

        # Worker node timeout is 5 sec
        time.sleep( 7 )

        ns_admin = self.getNetScheduleService( 'TEST', 'scenario310' )
        info = getClientInfo( ns_admin, 'node1' )
        if info[ 'preferred_affinities_reset' ] != True:
            raise Exception( "Expected to have preferred affinities reset, "
                             "received: " +
                             str( info[ 'preferred_affinities_reset' ] ) )
        return True
    def execute(self):
        " Should return True if the execution completed successfully "
        self.fromScratch(7)

        ns_client = self.getNetScheduleService("TEST", "scenario603")
        ns_client.set_client_identification("mynode", "mysession")
        changeAffinity(ns_client, ["a1", "a2"], [])

        client = getClientInfo(ns_client, "mynode", verbose=False)
        if client["preferred_affinities_reset"] != False:
            raise Exception("Expected non-resetted preferred affinities")

        # wait till the worker node is expired
        time.sleep(12)

        client = getClientInfo(ns_client, "mynode", verbose=False)
        if client["preferred_affinities_reset"] != True:
            raise Exception("Expected resetted preferred affinities")
        if client["number_of_preferred_affinities"] != 0:
            raise Exception("Unexpected length of preferred_affinities")

        execAny(ns_client, "SETAFF")
        client = getClientInfo(ns_client, "mynode", verbose=False)

        if client["preferred_affinities_reset"] != False:
            raise Exception("Expected non-resetted preferred affinities" " after SETAFF")
        if client["number_of_preferred_affinities"] != 0:
            raise Exception("Unexpected length of preferred_affinities")

        execAny(ns_client, "SETAFF a4,a7")
        client = getClientInfo(ns_client, "mynode", verbose=False)
        if client["preferred_affinities_reset"] != False:
            raise Exception("Expected non-resetted preferred affinities" " after SETAFF")
        if client["number_of_preferred_affinities"] != 2:
            raise Exception("Unexpected length of preferred_affinities")

        return True