def setUp(self):
     """
     set up
     """
     self.confile = os.path.join(os.path.dirname(__file__), '../scheduler.conf')
     self.host = socket.gethostname()
     self.port = 3144
     self.server = server
     self.server.master_table[self.host] = {'status': 'available', 
                                            'last_req_time': 0, 
                                            'idles':0,
                                            'jobs': 0, 
                                            'history':{},
                                            'waiting_list': [], 
                                            'env': 'GCC3', 
                                            'running_list': []}
     
     self.client_sock = socket.socket()
     self.client_sock.connect((sche_host, sche_port))
     sock = server.socket.accept()[0]
     self.handler = ScheHandler(sock)
     self.handler.server = self.server
class SchedulerTest(unittest.TestCase):
    """
    Test scheduler
    """
    def setUp(self):
        """
        set up
        """
        self.confile = os.path.join(os.path.dirname(__file__), '../scheduler.conf')
        self.host = socket.gethostname()
        self.port = 3144
        self.server = server
        self.server.master_table[self.host] = {'status': 'available', 
                                               'last_req_time': 0, 
                                               'idles':0,
                                               'jobs': 0, 
                                               'history':{},
                                               'waiting_list': [], 
                                               'env': 'GCC3', 
                                               'running_list': []}
        
        self.client_sock = socket.socket()
        self.client_sock.connect((sche_host, sche_port))
        sock = server.socket.accept()[0]
        self.handler = ScheHandler(sock)
        self.handler.server = self.server

    def tearDown(self):  
        """
        tear down
        """
        self.client_sock.close()
        
    def test_recv(self):
        """
        test recv
        """
        self.client_sock.send("hello\n")
        recv_msg = self.handler.recv()
        self.assertEquals(recv_msg, "hello")
        
    def test_handle_bad_request_msg_bad_env(self):
        """
        test handle_request_msg for bad ENV
        """
        request={'ENV': 'BADGCC',
                 'PORT':1234, 
                 'IP':socket.gethostbyname(socket.gethostname()), 
                 'STAMP':"mystamp"}
         
        self.client_sock.send("REQ%s\n" % json.dumps(request))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        recv_msg = self.client_sock.recv(1024)
        self.assertTrue(recv_msg.startswith("FAL"))
 
    def test_handle_bad_request_msg_no_env(self):
        """
        test handle_request_msg for no ENV
        """
        request={'PORT':1234, 
                 'IP':socket.gethostbyname(socket.gethostname()), 
                 'STAMP':"mystamp"}
         
        self.client_sock.send("REQ%s\n" % json.dumps(request))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("FAL"))
 
    def test_handle_request_msg_zero(self):
        """
        test handle_request_msg - 0
        """
        request={'ENV': 'GCC3',
                  'PORT':1234, 
                 'IP':socket.gethostbyname(socket.gethostname()), 
                 'STAMP':"mystamp0"}
        self.client_sock.send("REQ%s\n" % json.dumps(request))
        
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        
        self.assertTrue(feedback.startswith("SUC"))
        self.assertEquals(len(self.server.job_table.get(request['ENV'])), 1)
         
    def test_handle_request_msg_one(self):
        """
        test handle_request_msg - 1
        """
        self.server.master_table[self.host]['idle'] = 1
     
        request={'ENV': 'GCC3',
                  'PORT':1234, 
                 'IP':socket.gethostbyname(socket.gethostname()), 
                 'STAMP':"mystamp0"}
        self.client_sock.send("REQ%s\n" % json.dumps(request))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("SUC"))
        self.assertEquals(len(self.server.master_table[self.host]['waiting_list']), 1)
 
        request={'ENV': 'GCC3',
                  'PORT':1234, 
                 'IP':socket.gethostbyname(socket.gethostname()), 
                 'STAMP':"mystamp0"}
        self.client_sock.send("REQ%s\n" % json.dumps(request))
 
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("SUC"))
        self.assertEquals(len(self.server.master_table[self.host]['waiting_list']), 1)
        self.assertEquals(len(self.server.job_table.get(request['ENV'])), 1)
 
    def test_handle_bad_fetch_msg(self):
        """
        test handle_fetch_msg for bad msg
        """
        self.client_sock.send("GRQBADGCC\n")
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("FAL"))
 
    def test_handle_bad_fetch_msg_unknown_master(self):
        """
        test handle_fetch_msg for unknown master
        """
        del self.server.master_table[self.host]
 
        req_dict = {'ENV': 'GCC3', 'JOBS':2, 'IDLE':2}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("FAL"))
 
    def test_handle_bad_fetch_msg_bad_env(self):
        """
        test handle_fetch_msg for bad env
        """
        req_dict={'ENV': 'BADENV', 'JOBS':2, 'IDLE':20}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("FAL"))
 
    def test_handle_fetch_msg_ful(self):
        """
        test handle_fetch_msg for full msg
        """
        req_dict={'ENV':'GCC3', 'IDLE':0, 'JOBS':3}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("FUL"))
 
    def test_handle_fetch_msg_empty(self):
        """
        test handle_fetch_msg for empty msg
        """
        req_dict={'ENV':'GCC3', 'IDLE':2, 'JOBS':3}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(1024)
        self.assertTrue(feedback.startswith("EPT"))

    def put_job_in_waiting_list(self):
        """
        put job in waiting list
        """
        self.server.master_table[self.host]['idle'] = 0
        
        request={'ENV':'GCC3',
                 'PORT':1234, 
                 'IP': socket.gethostbyname(socket.gethostname()), 
                 'STAMP': "mystamp1"}
        self.client_sock.send ("REQ%s\n" % json.dumps(request))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        self.client_sock.recv(1024)
        
        
    def test_handle_fetch_msg_succ_from_waiting_queue(self):
        """
        test handle_fetch_msg
        """
        self.put_job_in_waiting_list()
        running_list_len = len(self.server.master_table[self.host]['running_list'])
        req_dict={'ENV': 'GCC3', 'JOBS': 2, 'IDLE': 2}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("SUC"))
        self.assertEquals(len(self.server.master_table[self.host]['running_list']), 
                          running_list_len + 1)
 
    def test_handle_fetch_msg_succ_from_waiting_queue_one(self):
        """
        test handle_fetch_msg
        """
        self.put_job_in_waiting_list()
         
        running_list_len = len(self.server.master_table[self.host]['running_list'])
         
        req_dict={'ENV': 'GCC3', 'JOBS': 2, 'IDLE': 2}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
         
        self.assertEquals(len(self.server.master_table[self.host]['running_list']),
                           running_list_len + 1)
        self.assertEquals(len(self.server.master_table[self.host]['waiting_list']), 0)
        self.assertEquals(len(self.server.job_table.get(req_dict.get('ENV'))),  0)
        self.assertTrue(feedback.startswith("SUC"))
        self.assertEquals(len(self.server.master_table[self.host]['idle']), 1)
        
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("EPT"))
 
    def test_handle_fecth_msg_succ(self):
        """
        test handle_fecth_msg success
        """
        self.put_job_in_waiting_list()
        self.put_job_in_waiting_list()
 
        req_dict={'ENV': 'GCC3', 'JOBS': 2, 'IDLE': 2}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertEquals(len(self.server.master_table[self.host]['running_list']), 1)
        self.assertEquals(len(self.server.master_table[self.host]['waiting_list']), 1)
        self.assertEquals(len(self.server.job_table.get(req_dict.get('ENV'))), 0)
        self.assertTrue(feedback.startswith("SUC"))
         
    def fetch_job(self):
        """
        fetch job
        """
        req_dict = {'ENV': 'GCC3', 'JOBS': 2, 'IDLE': 2}
        self.client_sock.send("GRQ%s\n" % json.dumps(req_dict))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        return feedback

    def test_handle_status_msg_bad_msg(self):
        """
        test handle_status_msg
        """
        self.client_sock.send("STABADMSG\n")
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("FAL"))
         
    def test_handle_status_msg_bad_env(self):
        """
        test handle_status_msg
        """
        status={'ENV': 'BADENV', 
                'PORT': 1234, 
                'IP': socket.gethostbyname(socket.gethostname()),
                'STAMP': "mystamp0"}
         
        self.client_sock.send("STA%s\n" % json.dumps(status))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("FAL"))
 
    def test_handle_status_msg_in_waiting_queue(self):
        """
        test handle_status_msg
        """
        self.put_job_in_waiting_list()
        status={'ENV': 'BADENV', 
                'PORT': 1234, 
                'IP': socket.gethostbyname(socket.gethostname()),
                'STAMP': "mystamp1"}
        self.client_sock.send("STA%s\n" % json.dumps(status))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("SUCJob is still in waiting queue"))
 
    def test_handle_status_msg_in_waiting_queue_one(self):
        """
        test handle_status_msg
        """
        self.put_job_in_waiting_list()
        status={'ENV': 'BADENV', 
                'PORT': 1234, 
                'IP': socket.gethostbyname(socket.gethostname()),
                'STAMP': "mystamp1"}
        self.client_sock.send("STA%s\n" % json.dumps(status))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        self.assertTrue(feedback.startswith("SUCJob will be executed on"))
 
    def test_handle_status_msg_in_running_queue(self):
        """
        test handle_status_msg
        """
        self.put_job_in_waiting_list()
        self.fetch_job()
        status={'ENV': 'BADENV', 
                'PORT': 1234, 
                'IP': socket.gethostbyname(socket.gethostname()),
                'STAMP': "mystamp1"}
        self.client_sock.send("STA%s\n" % json.dumps(status))
        recv_msg = self.handler.recv()
        self.handler.handle_msg(recv_msg)
        feedback = self.client_sock.recv(4096)
        assert feedback.startswith("SUCJob has been dispatched to")
 
    def test_reallocate_jobs_to_waiting_queue(self):
        """
        test_reallocate_jobs 
        """
        self.put_job_in_waiting_list()
        self.put_job_in_waiting_list()
 
        self.server.reallocate_jobs(self.host, self.server.master_table[self.host]['waiting_list'])
 
        self.assertEquals(len(self.server.master_table[self.host]['waiting_list']), 0)
        self.assertEquals(len(self.server.job_table['GCC3']), 2)
 
    def test_find_preferred_master(self):
        """
        test find_preferred_master
        """
        job_dict={'MODULE':'A',
                  'ENV': 'GCC3',
                  'PORT': 1234, 
                  'IP': socket.gethostbyname(socket.gethostname()), 
                  'STAMP': "mystamp0"}
         
        self.server.master_table['new_master0'] = {'status': 'available', 
                                                  'last_req_time': 0, 
                                                  'jobs': 5, 
                                                  'running_list': [], 
                                                  'idle': 2, 
                                                  'env': 'GCC3',
                                                  'history': {'B': time.time()},
                                                  'waiting_list': []}
        self.server.master_table['new_master1'] = {'status': 'available', 
                                                  'last_req_time': 0, 
                                                  'jobs': 5, 
                                                  'running_list': [], 
                                                  'idle': 2, 
                                                  'env': 'GCC3',
                                                  'history': {'A': time.time()},
                                                  'waiting_list': []}
         
        master = self.server.find_preferred_master(job_dict)
        self.assertEqual(master, 'new_master1')
         
    def test_valid_master(self):
        """
        test valid_master
        """
        master_info = {'status': 'available',
                       'idle': 1,
                       'running_list': [],
                       'env': 'GCC3', 
                       'waiting_list': []}
        self.assertTrue(self.server.valid_master(master_info, 'GCC3'))
 
    def test_pickup_waiting_job(self):
        """
        test pickup_waiting_job
        """
        self.put_job_in_waiting_list()
        self.put_job_in_waiting_list()
 
        master_info = self.server.master_table[self.host]
        saved_job_dict = self.server.job_table['GCC3'][0]
        job_dict = self.server.pickup_waiting_job(None, master_info)
        self.assertEquals(len(self.server.job_table['GCC3']), 0)
        self.assertEquals(saved_job_dict, job_dict)
         
    def test_clean_timeout_job(self):
        """
        test clean_timeout_job
        """
        self.put_job_in_waiting_list()
        self.fetch_job()
        self.assertEquals(len(self.server.master_table[self.host]['running_list']), 1)
        self.server.clean_timeout_job(time.time() + 4 * 3600 + 1)
        self.assertEquals(len(self.server.master_table[self.host]['running_list']), 0)
 
    def test_check_master_status_available(self):
        """
        test check_master_status available
        """
        self.fetch_job()
        self.server.check_silent_master(time.time() + 10)
        self.assertEquals(self.server.master_table[self.host]['status'], "available")
 
    def test_check_master_status_unavailable(self):
        """
        test check_master_status unavailable
        """
        self.fetch_job()
        self.server.check_master_status(time.time() + 65)
        self.assertEquals(self.server.master_table[self.host]['status'], "unavailable")
 
    def test_init_master_info(self):
        """
        test init_master_info
        """
        master_info = init_master_info()
        self.assertEqual(master_info['waiting_list'], [])
        self.assertEqual(master_info['running_list'], [])

    def test_process_args(self):
        """
        test process_args
        """
        sys.argv = ['scheduler.py', '-h']
        try:
            process_args()
        except SystemExit as e:
            e="%s" % e
        sys.argv=['scheduler.py', '-p 9527']
        process_args()
        self.assertEqual(scheduler.PORT , 9527)