Ejemplo n.º 1
0
    def test_reauth(self):
        c.http_connection = self.fake_http_connection(401)

        def get_auth(*args, **kwargs):
            return 'http://www.new.com', 'new'

        def swap_sleep(*args):
            self.swap_sleep_called = True
            c.get_auth = get_auth
            c.http_connection = self.fake_http_connection(200)

        c.sleep = swap_sleep
        self.swap_sleep_called = False

        conn = c.Connection(
            'http://www.test.com',
            'asdf',
            'asdf',
            preauthurl='http://www.old.com',
            preauthtoken='old',
        )

        self.assertEquals(conn.attempts, 0)
        self.assertEquals(conn.url, 'http://www.old.com')
        self.assertEquals(conn.token, 'old')

        value = conn.head_account()

        self.assertTrue(self.swap_sleep_called)
        self.assertEquals(conn.attempts, 2)
        self.assertEquals(conn.url, 'http://www.new.com')
        self.assertEquals(conn.token, 'new')
Ejemplo n.º 2
0
    def _make_swift_connection(self, auth_url, user, key):
        """
        Creates a connection using the Swift client library.
        """
        snet = self.snet
        auth_version = self.auth_version
        full_auth_url = (auth_url if not auth_url or auth_url.endswith('/')
                         else auth_url + '/')
        logger.debug(
            _("Creating Swift connection with "
              "(auth_address=%(full_auth_url)s, user=%(user)s, "
              "snet=%(snet)s, auth_version=%(auth_version)s)") % locals())
        tenant_name = None
        if self.auth_version == '2':
            tenant_user = user.split(':')
            if len(tenant_user) != 2:
                reason = (_("Badly formed tenant:user '%(tenant_user)s' in "
                            "Swift URI") % locals())
                raise exception.BadStoreUri(auth_url, reason)
            (tenant_name, user) = tenant_user

        return swift_client.Connection(authurl=full_auth_url,
                                       user=user,
                                       key=key,
                                       snet=snet,
                                       tenant_name=tenant_name,
                                       auth_version=auth_version)
Ejemplo n.º 3
0
    def test_resp_read_on_server_error(self):
        c.http_connection = self.fake_http_connection(500)
        conn = c.Connection('http://www.test.com', 'asdf', 'asdf', retries=0)

        def get_auth(*args, **kwargs):
            return 'http://www.new.com', 'new'

        conn.get_auth = get_auth
        self.url, self.token = conn.get_auth()

        method_signatures = (
            (conn.head_account, []),
            (conn.get_account, []),
            (conn.head_container, ('asdf', )),
            (conn.get_container, ('asdf', )),
            (conn.put_container, ('asdf', )),
            (conn.delete_container, ('asdf', )),
            (conn.head_object, ('asdf', 'asdf')),
            (conn.get_object, ('asdf', 'asdf')),
            (conn.put_object, ('asdf', 'asdf', 'asdf')),
            (conn.post_object, ('asdf', 'asdf', {})),
            (conn.delete_object, ('asdf', 'asdf')),
        )

        for method, args in method_signatures:
            self.assertRaises(c.ClientException, method, *args)
            try:
                self.assertTrue(conn.http_conn[1].has_been_read)
            except AssertionError:
                msg = '%s did not read resp on server error' % method.__name__
                self.fail(msg)
            except Exception, e:
                raise e.__class__("%s - %s" % (method.__name__, e))
Ejemplo n.º 4
0
def login(request):
    """登入页面"""
    if request.user.is_authenticated():
        return HttpResponseRedirect('/already-logged')
    if request.method == 'POST':
        form = Login_Form(request.POST)
        if not form.is_valid():
            return render_to_response('login.html', {'form': form})
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        con = client.Connection(utils.URL, username, password)
        try:
            auth_url, auth_token = con.get_auth()
            utils.auth_url = auth_url
            utils.auth_token = auth_token
        except Exception, e:
            if e[0] == 111:
                form = Login_Form()
                return render_to_response('login.html', {
                    'form': form,
                    'swift_no_start': True
                })
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth.login(request, user)
            return HttpResponseRedirect('/control-panel')
        else:
            return render_to_response('login.html', {
                'form': Login_Form(),
                'incorrect': True
            })
Ejemplo n.º 5
0
    def test_retry(self):
        c.http_connection = self.fake_http_connection(500)

        def quick_sleep(*args):
            pass

        c.sleep = quick_sleep
        conn = c.Connection('http://www.test.com', 'asdf', 'asdf')
        self.assertRaises(c.ClientException, conn.head_account)
        self.assertEquals(conn.attempts, conn.retries + 1)
Ejemplo n.º 6
0
 def _make_swift_connection(self, auth_url, user, key):
     """
     Creates a connection using the Swift client library.
     """
     snet = self.snet
     logger.debug(_("Creating Swift connection with "
                  "(auth_address=%(auth_url)s, user=%(user)s, "
                  "snet=%(snet)s)") % locals())
     return swift_client.Connection(
         authurl=auth_url, user=user, key=key, snet=snet)
Ejemplo n.º 7
0
    def test_large_objects(self):
        """
        We test the large object manifest code path in the Swift driver.
        In the case where an image file is bigger than the config variable
        swift_store_large_object_size, then we chunk the image into
        Swift, and add a manifest put_object at the end.

        We test that the delete of the large object cleans up all the
        chunks in Swift, in addition to the manifest file (LP Bug# 833285)
        """
        self.cleanup()

        self.swift_store_large_object_size = 2  # In MB
        self.swift_store_large_object_chunk_size = 1  # In MB
        self.start_servers(**self.__dict__.copy())

        api_port = self.api_port
        registry_port = self.registry_port

        # GET /images
        # Verify no public images
        path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port)
        http = httplib2.Http()
        response, content = http.request(path, 'GET')
        self.assertEqual(response.status, 200)
        self.assertEqual(content, '{"images": []}')

        # POST /images with public image named Image1
        # attribute and no custom properties. Verify a 200 OK is returned
        image_data = "*" * FIVE_MB
        headers = {
            'Content-Type': 'application/octet-stream',
            'X-Image-Meta-Name': 'Image1',
            'X-Image-Meta-Is-Public': 'True'
        }
        path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port)
        http = httplib2.Http()
        response, content = http.request(path,
                                         'POST',
                                         headers=headers,
                                         body=image_data)
        self.assertEqual(response.status, 201, content)
        data = json.loads(content)
        self.assertEqual(data['image']['checksum'],
                         hashlib.md5(image_data).hexdigest())
        self.assertEqual(data['image']['size'], FIVE_MB)
        self.assertEqual(data['image']['name'], "Image1")
        self.assertEqual(data['image']['is_public'], True)

        image_id = data['image']['id']

        # HEAD image
        # Verify image found now
        path = "http://%s:%d/v1/images/%s" % ("0.0.0.0", self.api_port,
                                              image_id)
        http = httplib2.Http()
        response, content = http.request(path, 'HEAD')
        self.assertEqual(response.status, 200)
        self.assertEqual(response['x-image-meta-name'], "Image1")

        # GET image
        # Verify all information on image we just added is correct
        path = "http://%s:%d/v1/images/%s" % ("0.0.0.0", self.api_port,
                                              image_id)
        http = httplib2.Http()
        response, content = http.request(path, 'GET')
        self.assertEqual(response.status, 200)

        expected_image_headers = {
            'x-image-meta-id': image_id,
            'x-image-meta-name': 'Image1',
            'x-image-meta-is_public': 'True',
            'x-image-meta-status': 'active',
            'x-image-meta-disk_format': '',
            'x-image-meta-container_format': '',
            'x-image-meta-size': str(FIVE_MB)
        }

        expected_std_headers = {
            'content-length': str(FIVE_MB),
            'content-type': 'application/octet-stream'
        }

        for expected_key, expected_value in expected_image_headers.items():
            self.assertEqual(
                response[expected_key], expected_value,
                "For key '%s' expected header value '%s'. Got '%s'" %
                (expected_key, expected_value, response[expected_key]))

        for expected_key, expected_value in expected_std_headers.items():
            self.assertEqual(
                response[expected_key], expected_value,
                "For key '%s' expected header value '%s'. Got '%s'" %
                (expected_key, expected_value, response[expected_key]))

        self.assertEqual(content, "*" * FIVE_MB)
        self.assertEqual(
            hashlib.md5(content).hexdigest(),
            hashlib.md5("*" * FIVE_MB).hexdigest())

        # We test that the delete of the large object cleans up all the
        # chunks in Swift, in addition to the manifest file (LP Bug# 833285)

        # Grab the actual Swift location and query the object manifest for
        # the chunks/segments. We will check that the segments don't exist
        # after we delete the object through Glance...
        path = "http://%s:%d/images/%s" % ("0.0.0.0", self.registry_port,
                                           image_id)
        http = httplib2.Http()
        response, content = http.request(path, 'GET')
        self.assertEqual(response.status, 200)
        data = json.loads(content)
        image_loc = data['image']['location']
        if hasattr(self, 'metadata_encryption_key'):
            key = self.metadata_encryption_key
        else:
            key = self.api_server.metadata_encryption_key
        image_loc = crypt.urlsafe_decrypt(key, image_loc)
        image_loc = get_location_from_uri(image_loc)
        swift_loc = image_loc.store_location

        from swift.common import client as swift_client
        swift_conn = swift_client.Connection(authurl=swift_loc.swift_auth_url,
                                             user=swift_loc.user,
                                             key=swift_loc.key)

        # Verify the object manifest exists
        headers = swift_conn.head_object(swift_loc.container, swift_loc.obj)
        manifest = headers.get('x-object-manifest')
        self.assertTrue(manifest is not None, "Manifest could not be found!")

        # Grab the segment identifiers
        obj_container, obj_prefix = manifest.split('/', 1)
        segments = [
            segment['name']
            for segment in swift_conn.get_container(obj_container,
                                                    prefix=obj_prefix)[1]
        ]

        # Verify the segments exist
        for segment in segments:
            headers = swift_conn.head_object(obj_container, segment)
            self.assertTrue(headers.get('content-length') is not None, headers)

        # DELETE image
        # Verify image and all chunks are gone...
        path = "http://%s:%d/v1/images/%s" % ("0.0.0.0", self.api_port,
                                              image_id)
        http = httplib2.Http()
        response, content = http.request(path, 'DELETE')
        self.assertEqual(response.status, 200)

        # Verify the segments no longer exist
        for segment in segments:
            self.assertRaises(swift_client.ClientException,
                              swift_conn.head_object, obj_container, segment)

        self.stop_servers()
Ejemplo n.º 8
0
        try:
            swift_host = self.swift_store_auth_address
            if not swift_host.startswith('http'):
                swift_host = 'https://' + swift_host
            user = self.swift_store_user
            key = self.swift_store_key
            container_name = self.swift_store_container
        except AttributeError, e:
            self.disabled_message = ("Failed to find required configuration "
                                     "options for Swift store. "
                                     "Got error: %s" % e)
            self.inited = True
            return

        self.swift_conn = swift_conn = swift_client.Connection(
            authurl=swift_host, user=user, key=key, snet=False, retries=1)

        try:
            _resp_headers, containers = swift_conn.get_account()
        except Exception, e:
            self.disabled_message = ("Failed to get_account from Swift "
                                     "Got error: %s" % e)
            self.inited = True
            return

        try:
            for container in containers:
                if container == container_name:
                    swift_conn.delete_container(container)
        except swift_client.ClientException, e:
            self.disabled_message = ("Failed to delete container from Swift "
Ejemplo n.º 9
0
    def test_reset_stream(self):
        class LocalContents(object):
            def __init__(self, tell_value=0):
                self.already_read = False
                self.seeks = []
                self.tell_value = tell_value

            def tell(self):
                return self.tell_value

            def seek(self, position):
                self.seeks.append(position)
                self.already_read = False

            def read(self, size=-1):
                if self.already_read:
                    return ''
                else:
                    self.already_read = True
                    return 'abcdef'

        class LocalConnection(object):
            def putrequest(self, *args, **kwargs):
                return

            def putheader(self, *args, **kwargs):
                return

            def endheaders(self, *args, **kwargs):
                return

            def send(self, *args, **kwargs):
                raise socket.error('oops')

            def request(self, *args, **kwargs):
                return

            def getresponse(self, *args, **kwargs):
                self.status = 200
                return self

            def getheader(self, *args, **kwargs):
                return ''

            def read(self, *args, **kwargs):
                return ''

        def local_http_connection(url, proxy=None):
            parsed = urlparse(url)
            return parsed, LocalConnection()

        orig_conn = c.http_connection
        try:
            c.http_connection = local_http_connection
            conn = c.Connection('http://www.example.com',
                                'asdf',
                                'asdf',
                                retries=1,
                                starting_backoff=.0001)

            contents = LocalContents()
            exc = None
            try:
                conn.put_object('c', 'o', contents)
            except socket.error, err:
                exc = err
            self.assertEquals(contents.seeks, [0])
            self.assertEquals(str(exc), 'oops')

            contents = LocalContents(tell_value=123)
            exc = None
            try:
                conn.put_object('c', 'o', contents)
            except socket.error, err:
                exc = err
Ejemplo n.º 10
0
 def test_instance(self):
     conn = c.Connection('http://www.test.com', 'asdf', 'asdf')
     self.assertEquals(conn.retries, 5)
Ejemplo n.º 11
0
 def __init__(self, authurl, user, key):
     ''' Connect to swift server and create root directory '''
     self.con = scc.Connection(authurl, user, key)
     self.dirs = {}
     #		try :
     self.mkdir('/')