def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()
        jobID1 = self.ns.submitJob(  'TEST', 'bla', '', '222', '', '' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1701' )
        ns_client.set_client_identification( 'node', 'session' )
        ns_client.on_warning = self.report_warning

        j1 = self.ns.getJob( 'TEST' )
        self.ns.putJob( 'TEST', j1[ 0 ], j1[ 1 ], 0 )

        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1 group=111,333' )
        if output != "no_more_jobs=true":
            raise Exception( "Expected no job, received some: " + output )

        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1 group=111,222' )
        if output == "":
            raise Exception( "Expected a job, got nothing" )

        values = parse_qs( output, True, True )
        receivedJobID = values[ 'job_key' ][ 0 ]
        if receivedJobID != jobID1:
            raise Exception( "Expected: " + jobID1 + ", got: " + output )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario301' )

        output = execAny( ns_client, 'SST2 ' + jobID )
        values = parse_qs( output, True, True )
        if values[ 'job_status' ] != ['Pending'] or \
           values.has_key( 'job_exptime' ) == False:
            raise Exception( "Unexpected SST2 output" )

        time1 = int( values[ 'job_exptime' ][ 0 ] )
        time.sleep( 5 )

        output = execAny( ns_client, 'SST2 ' + jobID )
        values = parse_qs( output, True, True )
        time2 = int( values[ 'job_exptime' ][ 0 ] )

        if time2 - time1 < 3:
            raise Exception( "SST2 does not change the job "
                             "expiration while it must" )

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1703' )
        ns_client.set_client_identification( 'node', 'session' )

        notifSocket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        notifSocket.bind( ( "", 0 ) )
        notifPort = notifSocket.getsockname()[ 1 ]

        execAny( ns_client,
                 'READ2 reader_aff=0 any_aff=1 port=' +
                 str( notifPort ) + ' timeout=10 group=111,333' )
        # Submit a job
        self.ns.submitJob(  'TEST', 'bla', '', '222', '', '' )
        j1 = self.ns.getJob( 'TEST' )
        self.ns.putJob( 'TEST', j1[ 0 ], j1[ 1 ], 0 )
        time.sleep( 1 )
        result = self.getNotif( notifSocket )
        if result != 0:
            raise Exception( "Expected no notifications, received some" )

        self.ns.submitJob(  'TEST', 'bla', '', '333', '', '' )
        j2 = self.ns.getJob( 'TEST' )
        self.ns.putJob( 'TEST', j2[ 0 ], j2[ 1 ], 0 )
        time.sleep( 1 )
        result = self.getNotif( notifSocket )
        notifSocket.close()
        if result != 1:
            raise Exception( "Expect notifications but received none" )
        return True
    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', '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' )

        # Start listening
        port = self.ns.getPort() + 2 
        sock = socket.socket( socket.AF_INET, # Internet
                              socket.SOCK_DGRAM ) # UDP
        sock.bind( ( "" , port ) )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario502' )
        ns_client.set_client_identification( 'mynode', 'mysession' )
        execAny( ns_client, 'LISTEN ' + jobID + " " + str( port ) + " 30")

        # Main job loop
        jobID, authToken, attrs, jobInput = self.ns.getJob( "TEST", -1, '', '',
                                                           "node1", "session1" )
        self.ns.returnJob( 'TEST', jobID, authToken )
        jobID, authToken, attrs, jobInput = self.ns.getJob( "TEST", -1, '', '',
                                                           "node2", "session2" )
        self.ns.putJob( 'TEST', jobID, authToken, 0, out = "my output" )
        jobID, state, passport = self.ns.getJobsForReading2( 'TEST', -1, "",
                                                           'node3', 'session3' )
        time.sleep( 15 )

        # Receive from the socket
        time.sleep( 3 )
        notifications = ""
        while True:
            try:
                data = sock.recv( 16384, socket.MSG_DONTWAIT )
                if not data:
                    break
                if len( notifications ) != 0:
                    notifications += "\n"
                notifications += data
            except:
                break

        if "job_status=Running&last_event_index=1" not in notifications:
            raise Exception( "Did not receive job_status=Running&last_event_index=1" )
        if "job_status=Pending&last_event_index=2" not in notifications:
            raise Exception( "Did not receive job_status=Pending&last_event_index=2" )
        if "job_status=Running&last_event_index=3" not in notifications:
            raise Exception( "Did not receive job_status=Running&last_event_index=3" )
        if "job_status=Done&last_event_index=4" not in notifications:
            raise Exception( "Did not receive job_status=Done&last_event_index=4" )
        if "job_status=Reading&last_event_index=5" not in notifications:
            raise Exception( "Did not receive job_status=Reading&last_event_index=5" )
        if "job_status=Done&last_event_index=6" not in notifications:
            raise Exception( "Did not receive job_status=Done&last_event_index=6" )

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario303' )
        ns_client.set_client_identification( 'node', 'session' )

        try:
            execAny( ns_client,
                     'GET2 wnode_aff=1 any_aff=1 exclusive_new_aff=1' )
        except Exception, exc:
            if "forbidden" in str( exc ):
                return True
            raise
    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( 3 )

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1903' )
        ns_client.set_client_identification( 'node', 'session' )
        ns_client.on_warning = self.report_warning

        try:
            output = execAny( ns_client, 'REDO ' + jobID )
            raise Exception( "Expected exception, got none (case 1)" )
        except Exception as exc:
            if "Cannot redo job" not in str( exc ):
                raise exc

        j = self.ns.getJob( 'TEST' )
        try:
            output = execAny( ns_client, 'REDO ' + jobID )
            raise Exception( "Expected exception, got none (case 2)" )
        except Exception as exc:
            if "Cannot redo job" not in str( exc ):
                raise exc

        execAny( ns_client, "FPUT2 " + j[ 0 ] + " " + j[ 1 ] + " ErrMsg Output 2" )
        execAny( ns_client, 'REDO ' + jobID )

        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Pending":
            raise Exception( "Unexpected job state: " + status )

        j = self.ns.getJob( 'TEST' )
        if j[ 0 ] != jobID:
            raise Exception( "Could not get the job which was REDO" )

        execAny( ns_client, 'CANCEL ' + jobID )
        execAny( ns_client, 'REDO ' + jobID )

        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Pending":
            raise Exception( "Unexpected job state: " + status )

        j = self.ns.getJob( 'TEST' )
        if j[ 0 ] != jobID:
            raise Exception( "Could not get the job which was REDO" )

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

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

        self.ns.submitJob( 'TEST', 'bla', affinity='a2' )
        self.ns.submitJob( 'TEST', 'bla', affinity='a1' )

        j = self.ns.getJob( 'TEST' )
        self.ns.putJob( 'TEST', j[ 0 ], j[ 1 ], 0 )
        j = self.ns.getJob( 'TEST' )
        self.ns.putJob( 'TEST', j[ 0 ], j[ 1 ], 0 )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1606' )
        ns_client.set_client_identification( 'node', 'session' )

        ex = False
        try:
            execAny( ns_client, 'READ2 reader_aff=1 any_aff=0 '
                                'aff=a1,a2 prioritized_aff=1' )
        #except Exception, exc:
        except Exception:
            ex = True
            #print str( exc )

        if ex == False:
            raise Exception( "Expected exception, got none (case 1)" )

        # 4.27.0 and up should provide a job
        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1 '
                                     'aff=a3,a4 prioritized_aff=1' )
        if 'job_key=' not in output:
            raise Exception( "Expected a job with affinity a2, received no job: '" + output + "'" )

        ex = False
        try:
            execAny( ns_client, 'READ2 reader_aff=0 any_aff=0 exclusive_new_aff=1 '
                                'aff=a1,a2 prioritized_aff=1' )
        #except Exception, exc:
        except Exception:
            ex = True
            #print str( exc )

        if ex == False:
            raise Exception( "Expected exception, got none (case 3)" )

        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', '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", "blah")

        ns_client = self.getNetScheduleService("", "scenario702")
        ns_client.set_client_identification("node1", "session1")

        output = execAny(ns_client, "SST " + jobID)
        if output != "0":
            raise Exception("Unexpected job state")

        # Try to submit a job without the queue
        try:
            output = execAny(ns_client, "SUBMIT blah")
        except Exception, exc:
            if "Job queue is required" in str(exc):
                return True
            raise
    def execute(self):
        " Should return True if the execution completed successfully "
        self.fromScratch(2)

        jobID = self.ns.submitJob("TEST2", "blah")
        if "TEST2" not in jobID:
            raise Exception("Job key does not have first queue in it")

        ns_client = self.getNetScheduleService("TEST3", "scenario703")
        ns_client.set_client_identification("node1", "session1")

        output = execAny(ns_client, "SST " + jobID)
        if output != "0":
            raise Exception("Unexpected job state")

        # Try to submit a job to another queue
        jobID2 = execAny(ns_client, "SUBMIT blah")
        if "TEST3" not in jobID2:
            raise Exception("Job key does not have second queue in it")

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

        # Client1 for a job
        ns_client1 = self.getNetScheduleService("TEST", "scenario602")
        ns_client1.set_client_identification("node1", "session1")
        # Socket to receive notifications
        notifSocket1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        notifSocket1.bind(("", 9007))

        # Client2 for a job
        ns_client2 = self.getNetScheduleService("TEST", "scenario602")
        ns_client2.set_client_identification("node2", "session2")
        # Socket to receive notifications
        notifSocket2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        notifSocket2.bind(("", 9008))

        execAny(ns_client1, "GET2 wnode_aff=0 any_aff=1 exclusive_new_aff=0 port=9007 timeout=15")
        execAny(ns_client2, "GET2 wnode_aff=0 any_aff=1 exclusive_new_aff=0 port=9008 timeout=15")

        # Submit a job and wait for notifications
        jobID = self.ns.submitJob("TEST", "bla", "a0")

        # First notification must come immediately and the second later on
        time.sleep(0.1)
        result1 = self.getNotif(notifSocket1, notifSocket2)
        if result1 == 0:
            raise Exception("Expected one notification, received nothing")

        time.sleep(10)
        result2 = self.getNotif(notifSocket1, notifSocket2)
        if result2 == 0:
            raise Exception("Expected another notification, received nothing")

        if result1 + result2 != 3:
            raise Exception("Expected notifications to both worker nodes, " "received in the same")

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

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

        # Start listening
        port = self.ns.getPort() + 2 
        sock = socket.socket( socket.AF_INET, # Internet
                              socket.SOCK_DGRAM ) # UDP
        sock.bind( ( "" , port ) )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario503' )
        ns_client.set_client_identification( 'mynode', 'mysession' )
        execAny( ns_client, 'LISTEN ' + jobID + " " + str( port ) + " 30")

        # Main job loop
        # Wait till the job is deleted by GC
        time.sleep( 35 )

        # Receive from the socket
        time.sleep( 3 )
        notifications = ""
        while True:
            try:
                data = sock.recv( 16384, socket.MSG_DONTWAIT )
                if not data:
                    break
                if len( notifications ) != 0:
                    notifications += "\n"
                notifications += data
            except:
                break

        if "job_status=Deleted&last_event_index=0" not in notifications:
            raise Exception( "Did not receive job_status=Deleted&last_event_index=0" )

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

        self.ns.submitJob( 'TEST', 'bla', '', '', '', '' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1812' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'DUMP', isMultiline =True )
        if output != []:
            raise Exception( "Expected no jobs, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=' )
        output = execAny( ns_client, 'DUMP', isMultiline =True )
        if 'status: Pending' not in "\n".join( output ):
            raise Exception( "Expected a job, got nothing: " + str(output) )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        self.ns.submitJob( 'TEST', 'bla', 'a1', '', '', '' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1806' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'STAT AFFINITIES verbose=1', isMultiline =True )
        if output != [''] and output != []:
            raise Exception( "Expected no affinities, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=' )
        output = execAny( ns_client, 'STAT AFFINITIES verbose=1', isMultiline =True )
        if "AFFINITY: 'a1'" not in "\n".join( output ):
            raise Exception( "Expected an affinity, got nothing: " + str(output) )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        self.ns.submitJob( 'TEST', 'bla', '', '', '', '' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1800' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'GET2 wnode_aff=0 any_aff=1' )
        if output != "":
            raise Exception( "Expected no job, received some: " + output )

        execAny( ns_client, 'SETSCOPE scope=' )
        output = execAny( ns_client, 'GET2 wnode_aff=0 any_aff=1' )
        if output == "":
            raise Exception( "Expected a job, got nothing" )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        j = self.ns.submitJob( 'TEST', 'bla', '', '', '', '' )

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1804' )
        ns_client.set_client_identification( 'node', 'session' )
        ns_client.on_warning = self.report_warning

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'CANCEL ' + j )
        if output != "0" or "eJobNotFound" not in self.warning:
            raise Exception( "Expected no job to cancel, canceled some: " + str( output ) )

        self.warning = None
        execAny( ns_client, 'SETSCOPE scope=' )
        output = execAny( ns_client, 'CANCEL ' + j )
        if output != "1" or self.warning is not None:
            raise Exception( "Expected to cancel a job, could not do it" )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1809' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        execAny( ns_client, 'SUBMIT blah group=g1' )
        execAny( ns_client, 'SETSCOPE scope=no-scope-only' )

        output = execAny( ns_client, 'STAT GROUPS verbose=1', isMultiline =True )
        if output != []:
            raise Exception( "Expected no groups, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=MyWrongScope' )
        output = execAny( ns_client, 'STAT GROUPS verbose=1', isMultiline =True )
        if output != []:
            raise Exception( "Expected no groups, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'STAT GROUPS verbose=1', isMultiline =True )
        if "GROUP: 'g1'" not in "\n".join( output ):
            raise Exception( "Expected a group, got nothing: " + str(output) )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch( 3 )

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1904' )
        ns_client.set_client_identification( 'node', 'session' )
        ns_client.on_warning = self.report_warning

        try:
            output = execAny( ns_client, 'REREAD ' + jobID )
            raise Exception( "Expected exception, got none (case 1)" )
        except Exception as exc:
            if "Cannot reread job" not in str( exc ):
                raise exc

        j = self.ns.getJob( 'TEST' )
        try:
            output = execAny( ns_client, 'REREAD ' + jobID )
            raise Exception( "Expected exception, got none (case 2)" )
        except Exception as exc:
            if "Cannot reread job" not in str( exc ):
                raise exc

        execAny( ns_client, "FPUT2 " + j[ 0 ] + " " + j[ 1 ] + " ErrMsg Output 2" )

        self.warning = None
        execAny( ns_client, 'REREAD ' + jobID )
        if "eJobNotRead" not in self.warning:
            raise Exception( "Expected warning, got none" )

        # reading
        execAny( ns_client, "READ" )
        try:
            output = execAny( ns_client, 'REREAD ' + jobID )
            raise Exception( "Expected exception, got none (case 3)" )
        except Exception as exc:
            if "Cannot reread job" not in str( exc ):
                raise exc

        # confirmed
        execAny( ns_client, "CFRM " + j[0] + " " + j[1] )
        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Confirmed":
            raise Exception( "Unexpected job state 1: " + status )

        execAny( ns_client, 'REREAD ' + jobID )
        # print "warning: " + str( self.warning )
        # print "\n".join( execAny( ns_client, 'DUMP ' + jobID, isMultiline = True ) )

        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Failed":
            raise Exception( "Unexpected job state 1: " + status )

        execAny( ns_client, 'CANCEL ' + jobID )
        self.warning = None
        execAny( ns_client, 'REREAD ' + jobID )
        if "eJobNotRead" not in self.warning:
            raise Exception( "Expected warning, got none" )

        execAny( ns_client, "READ" )
        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Reading":
            raise Exception( "Unexpected job state 2: " + status )

        execAny( ns_client, "CFRM " + j[0] + " " + j[1] )
        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Confirmed":
            raise Exception( "Unexpected job state 3: " + status )

        execAny( ns_client, 'REREAD ' + jobID )
        status = self.ns.getFastJobStatus( 'TEST', jobID )
        if status != "Canceled":
            raise Exception( "Unexpected job state 4: " + status )

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

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1811' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        execAny( ns_client, 'SUBMIT blah' )
        execAny( ns_client, 'SETSCOPE scope=no-scope-only' )

        output = execAny( ns_client, 'STAT JOBS', isMultiline =True )
        if 'Pending: 0' not in "\n".join( output ):
            raise Exception( "Expected no jobs, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=MyWrongScope' )
        output = execAny( ns_client, 'STAT JOBS', isMultiline =True )
        if 'Pending: 0' not in "\n".join( output ):
            raise Exception( "Expected no jobs, received some: " + str(output) )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'STAT JOBS', isMultiline =True )
        if 'Pending: 1' not in "\n".join( output ):
            raise Exception( "Expected a job, got nothing: " + str(output) )
        return True
    def execute( self ):
        " Should return True if the execution completed successfully "
        self.fromScratch()

        ns_client = self.getNetScheduleService( 'TEST', 'scenario1800' )
        ns_client.set_client_identification( 'node', 'session' )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        execAny( ns_client, 'SUBMIT blah' )
        output = execAny( ns_client, 'GET2 wnode_aff=0 any_aff=1' )
        if output == "":
            raise Exception( "Expected a job, got nothing" )
        values = parse_qs( output, True, True )
        jobID = values[ 'job_key' ][ 0 ]
        execAny( ns_client, 'PUT ' + jobID + ' 0 0' )

        execAny( ns_client, 'SETSCOPE scope=' )
        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1' )
        if output != "no_more_jobs=true":
            raise Exception( "Expected no job, received some: " + output )

        execAny( ns_client, 'SETSCOPE scope=MyWrongScope' )
        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1' )
        if output != "no_more_jobs=true":
            raise Exception( "Expected no job, received some: " + output )

        execAny( ns_client, 'SETSCOPE scope=MyScope' )
        output = execAny( ns_client, 'READ2 reader_aff=0 any_aff=1' )
        if output == "" or "no_more_jobs" in output:
            raise Exception( "Expected a job, got nothing" )
        return True