[Kimchi-devel] [PATCH v5 7/7] Storage volume upload: Change mockmodel and test

Crístian Viana vianac at linux.vnet.ibm.com
Fri Sep 5 16:44:56 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

Change mockmodel and tests to support storagevolume upload. Also, add
the package "python-requests" as a dependency

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
Signed-off-by: Crístian Viana <vianac at linux.vnet.ibm.com>
---
 contrib/DEBIAN/control.in     |  3 ++-
 contrib/kimchi.spec.fedora.in |  1 +
 contrib/kimchi.spec.suse.in   |  1 +
 docs/README.md                |  7 ++++---
 src/kimchi/mockmodel.py       | 21 +++++++++++++++++++++
 tests/test_rest.py            | 38 +++++++++++++++++++++++++++++++++++++-
 tests/utils.py                |  2 +-
 7 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in
index 3754fa2..99546fa 100644
--- a/contrib/DEBIAN/control.in
+++ b/contrib/DEBIAN/control.in
@@ -27,6 +27,7 @@ Depends: python-cherrypy3 (>= 3.2.0),
          python-guestfs,
          libguestfs-tools
 Build-Depends: libxslt,
-               python-libxml2
+               python-libxml2,
+               python-requests
 Maintainer: Aline Manera <alinefm at br.ibm.com>
 Description: Kimchi web server
diff --git a/contrib/kimchi.spec.fedora.in b/contrib/kimchi.spec.fedora.in
index 5766784..bd1f04d 100644
--- a/contrib/kimchi.spec.fedora.in
+++ b/contrib/kimchi.spec.fedora.in
@@ -31,6 +31,7 @@ Requires:	python-libguestfs
 Requires:	libguestfs-tools
 BuildRequires:	libxslt
 BuildRequires:	libxml2-python
+BuildRequires:	python-requests
 
 %if 0%{?fedora} >= 15 || 0%{?rhel} >= 7
 %global with_systemd 1
diff --git a/contrib/kimchi.spec.suse.in b/contrib/kimchi.spec.suse.in
index 1f193d0..d0bf317 100644
--- a/contrib/kimchi.spec.suse.in
+++ b/contrib/kimchi.spec.suse.in
@@ -30,6 +30,7 @@ Requires:	python-libguestfs
 Requires:	guestfs-tools
 BuildRequires:	libxslt-tools
 BuildRequires:	python-libxml2
+BuildRequires:	python-requests
 
 %if 0%{?sles_version} == 11
 Requires:       python-ordereddict
diff --git a/docs/README.md b/docs/README.md
index 24537e1..3bdc914 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -55,7 +55,7 @@ Install Dependencies
                         python-ipaddr python-lxml nfs-utils \
                         iscsi-initiator-utils libxslt pyparted nginx \
                         policycoreutils-python python-libguestfs \
-                        libguestfs-tools
+                        libguestfs-tools python-requests
      # If using RHEL6, install the following additional packages:
      $ sudo yum install python-unittest2 python-ordereddict
      # Restart libvirt to allow configuration changes to take effect
@@ -78,7 +78,8 @@ for more information on how to configure your system to access this repository.
                            qemu-kvm libtool python-psutil python-ethtool \
                            sosreport python-ipaddr python-lxml nfs-common \
                            open-iscsi lvm2 xsltproc python-parted nginx \
-                           firewalld python-guestfs libguestfs-tools
+                           firewalld python-guestfs libguestfs-tools \
+                           python-requests
 
     Packages version requirement:
         python-jsonschema >= 1.3.0
@@ -93,7 +94,7 @@ for more information on how to configure your system to access this repository.
                           rpm-build kvm python-psutil python-ethtool \
                           python-ipaddr python-lxml nfs-client open-iscsi \
                           libxslt-tools python-xml python-parted \
-                          python-libguestfs guestfs-tools
+                          python-libguestfs guestfs-tools python-requests
 
     Packages version requirement:
         python-psutil >= 0.6.0
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 41e001c..15a75af 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -512,6 +512,27 @@ class MockModel(object):
         taskid = self.add_task(targeturi, create_func, params)
         return self.task_lookup(taskid)
 
+    def _create_volume_with_file(self, cb, params):
+        upload_file = params['file']
+        params['name'] = params['name']
+        params['format'] = 'raw'
+        params['capacity'] = upload_file.fp.length
+        size = 0
+        try:
+            while True:
+                data = upload_file.file.read(8192)
+                if not data:
+                        break
+                size += len(data)
+                cb('%s/%s' % (size, params['capacity']), True)
+        except Exception as e:
+            raise OperationFailed('KCHVOL0007E',
+                                  {'name': params['name'],
+                                   'pool': params['pool'],
+                                   'err': e.message})
+        self._create_volume_with_capacity(cb, params)
+        cb('%s/%s' % (size, params['capacity']), True)
+
     def _create_volume_with_capacity(self, cb, params):
         pool_name = params.pop('pool')
         pool = self._get_storagepool(pool_name)
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 567a954..ae1c971 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -22,6 +22,7 @@ import base64
 import json
 import os
 import random
+import requests
 import shutil
 import time
 import unittest
@@ -1058,7 +1059,10 @@ class RestTests(unittest.TestCase):
         resp = self.request('/storagepools/pool-2/storagevolumes/',
                             req, 'POST')
         self.assertEquals(202, resp.status)
-        time.sleep(1)
+        task_id = json.loads(resp.read())['id']
+        self._wait_task(task_id)
+        status = json.loads(self.request('/tasks/%s' % task_id).read())
+        self.assertEquals('finished', status['status'])
 
         # Verify the storage volume
         resp = self.request('/storagepools/pool-2/storagevolumes/test-volume')
@@ -1892,6 +1896,38 @@ class RestTests(unittest.TestCase):
         resp = self.request('%s/fedora-fake' % base_uri, '{}', 'DELETE')
         self.assertEquals(204, resp.status)
 
+    def test_upload(self):
+        # If we use self.request, we may encode multipart formdata by ourselves
+        # requests lib take care of encode part, so use this lib instead
+        def fake_auth_header():
+            headers = {'Accept': 'application/json'}
+            user, pw = kimchi.mockmodel.fake_user.items()[0]
+            hdr = "Basic " + base64.b64encode("%s:%s" % (user, pw))
+            headers['AUTHORIZATION'] = hdr
+            return headers
+
+        with RollbackContext() as rollback:
+            upload_path = '/tmp/vol'
+            f = open(upload_path, 'w')
+            f.write('abcd')
+            f.close()
+            rollback.prependDefer(os.remove, upload_path)
+
+            vol = open(upload_path, 'rb')
+            url = "https://%s:%s/storagepools/default/storagevolumes" %\
+                (host, ssl_port)
+
+            r = requests.post(url,
+                              files={'file': vol},
+                              data={'name': 'new_vol2'},
+                              verify=False,
+                              headers=fake_auth_header())
+            self.assertEquals(r.status_code, 202)
+            time.sleep(1)
+            vol_list = json.loads(
+                self.request('/storagepools/default/storagevolumes').read())
+            self.assertEquals('new_vol2', vol_list[0]['name'])
+
 
 class HttpsRestTests(RestTests):
     """
diff --git a/tests/utils.py b/tests/utils.py
index 7fccae1..140bb1d 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -104,7 +104,7 @@ def run_server(host, port, ssl_port, test_mode, cherrypy_port=None,
 
     args = type('_', (object,),
                 {'host': host, 'port': port, 'ssl_port': ssl_port,
-                 'cherrypy_port': cherrypy_port,
+                 'cherrypy_port': cherrypy_port, 'max_body_size': '4*1024',
                  'ssl_cert': '', 'ssl_key': '',
                  'test': test_mode, 'access_log': '/dev/null',
                  'error_log': '/dev/null', 'environment': environment,
-- 
1.9.3




More information about the Kimchi-devel mailing list