예제 #1
0
def create_BITS_session(record):
    conn = bits.open_connection(get_proto(record), record['ip'], record['port'])
    bits_sid = bits.create_session(conn, record['url_path'] + ".vhd", record['username'], record['password'])
    return conn, bits_sid
예제 #2
0
    def _doTest(self, src, frag_size, sleep_time, delta, randomise_delta=False):
        """Tests how we handle the client droping/losing packets
        when they are POSTing a BITS request"""

        physize = os.stat(src).st_size                
        vdi_mb = vhd.get_virtual_size(src)
        
        #Expose a new VDI
        record, hostname = create_and_expose(vdi_mb)
        url_path = record['url_path'] + ".vhd"

        #Create BITS Session
        conn, session = create_BITS_session(record)
        
        logging.debug("Frag_Size = %d" % frag_size)
        
        #Adjust frag size for the case we have small disks
        if physize < frag_size:
            frag_size = physize

        #Initialise Upload variables
        range_start = 0
        range_end = frag_size
        file_offset = 0
        fh = open(src, 'r')
        count = 0
        while file_offset < physize:
            if randomise_delta:
                #Generate a random number within the request range
                delta = random.randint(0, range_end - range_start)
                logging.debug("Random Delta = %d" % delta)

            if range_end == physize:
                delta = 0

            logging.debug("Uploading %d-%d/%d" % (range_start, range_end, physize))
            data_size = (range_end - range_start - delta)

            if count == 1:
                data_size = 10
            logging.debug("Actually uploading %d bytes" % data_size)
            data = fh.read(data_size)
            logging.debug("Data Read")
            try:
                bits.fragment(conn, session, url_path, data, range_start, range_end, physize)
            except:
                logging.debug("Fragment Sent partially %d-%d/%d" % (range_start, range_end, physize))

            file_offset += (range_end - range_start - delta)
            range_start = file_offset + 1

            count += 1

            if count > 1:
                sys.exit(1)
    
            #Condition for last frag upload
            range_end = file_offset + frag_size
            if physize < range_end:
                range_end = physize

            #Sleep before creating new connection 
            logging.debug("Sleeping for %d seconds" % sleep_time)
            time.sleep(sleep_time)

            #Garbage Collect to make sure we don't run out of memory
            data = ''
            gc.collect()
            
            #Create new connection
            logging.debug("Get a new connection...")
            conn = bits.open_connection(get_proto(record), record['ip'], record['port'])

        #Close file handle
        fh.close()
        
        #Unexpose the transfervm
        logging.debug("Unexposing the Transfer VM for vdi %s" % record['vdi_uuid'])
        transferclient.unexpose(hostname, vdi_uuid=record['vdi_uuid'])

        #Retrieve the blockmap to expose the VDI as a vhd
        bitmap = transferclient.get_vdi_bitmap(hostname, record['vdi_uuid'])
        logging.debug("Got bitmap %s" % bitmap)

        #Expose disk with vhd_block_map
        args = {'transfer_mode': 'bits',
                'vdi_uuid': record['vdi_uuid'],
                'network_uuid': 'management',
                'vhd_blocks': bitmap,
                'vhd_uuid': record['vdi_uuid']}

        transferclient.expose(hostname, **args)

        record = transferclient.get_record(hostname, vdi_uuid=record['vdi_uuid'])
        logging.debug("Got new record: %s" % record)

        dst = 'test-download.vhd'
        logging.debug("Destination file will be %s" % dst)
        request_size = 200*M
        vhd_tests.bits_download_vhd(record, dst, request_size)
        
        #Compare the two VHDs to check they are identical
        rc = vhd.diff(src, dst)
        logging.debug("Return Code %s" % rc)
        
        #Unexpose the transfervm
        logging.debug("Unexposing the Transfer VM for vdi %s" % record['vdi_uuid'])
        transferclient.unexpose(hostname, vdi_uuid=record['vdi_uuid'])
        
        #Cleanup Disks
        transferclient.remove_vdi(hostname, record['vdi_uuid'])
        
        #Remove Downloaded File
        os.unlink(dst)