예제 #1
0
    def _doTestOverlapFrag(self, src, frag_size, sleep_time, randomise_delta=False):
        """Test that we handle a missing fragment appropriately - 416"""
        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)

        fh = open(src, 'r')

        #Read initial 50M
        data = fh.read(50*M)

        #Send Initial Request
        rheaders = bits.fragment(conn, session, url_path, data, 0, 50*M, physize)
        print "Initial Request:\n %s" % rheaders

        #Read 25M overlap
        fh.seek(45*M)
        data = fh.read(10*M)
        
        #Send Second Request (expect failure)

        rheaders = bits.fragment(conn, session,url_path, data, 45*M, 45*M + 10*M, physize)
        print "Second Request:\n %s" % rheaders
예제 #2
0
    def _doTestAlreadyReceivedFragSubset(self, src, frag_size, sleep_time, randomise_delta=False):
        """Test that we handle a missing fragment appropriately - 416"""
        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)

        fh = open(src, 'r')

        #Read initial 50M
        data = fh.read(50*M)

        #Send Initial Request
        rheaders = bits.fragment(conn, session, url_path, data, 0, 50*M, physize)
        print "Initial Request:\n %s" % rheaders

        #Read 25M overlap
        fh.seek(25*M)
        data = fh.read(20)
        
        #Send Second Request (expect failure)
        try:
            rheaders = bits.fragment(conn, session,url_path, data, 25*M, 25*M + 20, physize)
            print "Second Request:\n %s" % rheaders

        except bits.Http416Exception as instance:
            logging.debug(instance.msg)
            logging.debug(instance.headers)
        
            #assert that we return the following values:
            if BITS_RECEIVED_CONTENT_RANGE not in instance.headers:
                raise Exception("BITS-Received-Content-Range header should be in repsonse for an overlapping fragement!")
            else:
                assert int(instance.headers[BITS_RECEIVED_CONTENT_RANGE]) == 50*M
예제 #3
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)
예제 #4
0
def get_encoded_bitmap_from_file(fn):
    vdi_mb = vhd.get_virtual_size(fn)
    blocks = vhd.get_allocated_blocks(fn)
    bitmap = vhd.to_bitmap(blocks, vdi_mb * M)
    return base64.b64encode(zlib.compress(bitmap))
예제 #5
0
 def _testCustomVHD(self, vhd_filename):
     vdi_mb = vhd.get_virtual_size(vhd_filename)
     self._testUploadDownload(vhd_filename, vdi_mb)
예제 #6
0
def get_encoded_bitmap_from_file(fn):
    vdi_mb = vhd.get_virtual_size(fn)
    blocks = vhd.get_allocated_blocks(fn)
    bitmap = vhd.to_bitmap(blocks, vdi_mb * M)
    return base64.b64encode(zlib.compress(bitmap))
예제 #7
0
 def _testCustomVHD(self, vhd_filename):
     vdi_mb = vhd.get_virtual_size(vhd_filename)
     self._testUploadDownload(vhd_filename, vdi_mb)