Merge "Add validation to package import"
This commit is contained in:
commit
8e0aba70b5
@ -17,6 +17,9 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import json
|
import json
|
||||||
|
from muranopkgcheck import manager as check_manager
|
||||||
|
from muranopkgcheck import pkg_loader as check_pkg_loader
|
||||||
|
from muranopkgcheck import validators as check_validators
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
@ -307,7 +310,11 @@ class Package(FileWrapperMixin):
|
|||||||
def from_file(file_obj):
|
def from_file(file_obj):
|
||||||
if not isinstance(file_obj, File):
|
if not isinstance(file_obj, File):
|
||||||
file_obj = File(file_obj)
|
file_obj = File(file_obj)
|
||||||
return Package(file_obj)
|
pkg = Package(file_obj)
|
||||||
|
errs = pkg.validate()
|
||||||
|
if errs:
|
||||||
|
raise exceptions.HTTPBadRequest(details=errs)
|
||||||
|
return pkg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromFile(file_obj):
|
def fromFile(file_obj):
|
||||||
@ -342,6 +349,16 @@ class Package(FileWrapperMixin):
|
|||||||
extension='.zip')
|
extension='.zip')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def validate(self):
|
||||||
|
m = check_manager.Manager(self._file,
|
||||||
|
loader=check_pkg_loader.ZipLoader)
|
||||||
|
errors = m.validate(
|
||||||
|
validators=[check_validators.manifest.ManifestValidator],
|
||||||
|
only_errors=True)
|
||||||
|
if errors:
|
||||||
|
fmt = check_manager.PlainTextFormatter().format
|
||||||
|
return 'Invalid Murano package\n{}\n'.format(fmt(errors))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def contents(self):
|
def contents(self):
|
||||||
"""Contents of a package."""
|
"""Contents of a package."""
|
||||||
|
@ -70,13 +70,14 @@ def make_pkg(manifest_override, image_dicts=None):
|
|||||||
'Classes': {'foo': 'foo.yaml'},
|
'Classes': {'foo': 'foo.yaml'},
|
||||||
'Description': '',
|
'Description': '',
|
||||||
'Format': 1.0,
|
'Format': 1.0,
|
||||||
'FullName': '',
|
'FullName': 'org.foo',
|
||||||
'Name': 'Apache HTTP Server',
|
'Name': 'Apache HTTP Server',
|
||||||
'Type': 'Application'}
|
'Type': 'Application'}
|
||||||
manifest.update(manifest_override)
|
manifest.update(manifest_override)
|
||||||
file_obj = six.BytesIO()
|
file_obj = six.BytesIO()
|
||||||
zfile = zipfile.ZipFile(file_obj, "a")
|
zfile = zipfile.ZipFile(file_obj, "a")
|
||||||
zfile.writestr('manifest.yaml', yaml.dump(manifest))
|
zfile.writestr('manifest.yaml', yaml.dump(manifest))
|
||||||
|
zfile.writestr('Classes/foo.yaml', yaml.dump({}))
|
||||||
if image_dicts:
|
if image_dicts:
|
||||||
images_list = []
|
images_list = []
|
||||||
default_image_spec = {
|
default_image_spec = {
|
||||||
@ -414,10 +415,12 @@ class PackageTest(testtools.TestCase):
|
|||||||
self.assertRaises(ValueError, utils.Package.from_location,
|
self.assertRaises(ValueError, utils.Package.from_location,
|
||||||
name='foo.bar.baz', base_url='')
|
name='foo.bar.baz', base_url='')
|
||||||
|
|
||||||
def test_file_object_repo(self):
|
@mock.patch.object(utils.Package, 'validate')
|
||||||
|
def test_file_object_repo(self, m_validate):
|
||||||
resp = requests.Response()
|
resp = requests.Response()
|
||||||
resp.raw = six.BytesIO(six.b("123"))
|
resp.raw = six.BytesIO(six.b("123"))
|
||||||
resp.status_code = 200
|
resp.status_code = 200
|
||||||
|
m_validate.return_value = None
|
||||||
with mock.patch(
|
with mock.patch(
|
||||||
'requests.get',
|
'requests.get',
|
||||||
mock.Mock(side_effect=lambda k, *args, **kwargs: resp)):
|
mock.Mock(side_effect=lambda k, *args, **kwargs: resp)):
|
||||||
|
@ -19,6 +19,8 @@ import yaml
|
|||||||
|
|
||||||
from muranoclient.common import base
|
from muranoclient.common import base
|
||||||
from muranoclient.common import exceptions
|
from muranoclient.common import exceptions
|
||||||
|
from muranoclient.common import utils
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PAGE_SIZE = 20
|
DEFAULT_PAGE_SIZE = 20
|
||||||
|
|
||||||
@ -51,6 +53,10 @@ class PackageManager(base.Manager):
|
|||||||
response_key='categories', obj_class=Category)
|
response_key='categories', obj_class=Category)
|
||||||
|
|
||||||
def create(self, data, files):
|
def create(self, data, files):
|
||||||
|
for pkg_file in files.values():
|
||||||
|
utils.Package.from_file(pkg_file)
|
||||||
|
pkg_file.seek(0)
|
||||||
|
|
||||||
response = self.api.request(
|
response = self.api.request(
|
||||||
'/v1/catalog/packages',
|
'/v1/catalog/packages',
|
||||||
'POST',
|
'POST',
|
||||||
|
@ -13,6 +13,7 @@ requests>=2.10.0 # Apache-2.0
|
|||||||
PyYAML>=3.10.0 # MIT
|
PyYAML>=3.10.0 # MIT
|
||||||
yaql>=1.1.0 # Apache 2.0 License
|
yaql>=1.1.0 # Apache 2.0 License
|
||||||
osc-lib>=1.2.0 # Apache-2.0
|
osc-lib>=1.2.0 # Apache-2.0
|
||||||
|
murano-pkg-check>=0.2.0 # Apache-2.0
|
||||||
|
|
||||||
oslo.serialization>=1.10.0 # Apache-2.0
|
oslo.serialization>=1.10.0 # Apache-2.0
|
||||||
oslo.utils>=3.18.0 # Apache-2.0
|
oslo.utils>=3.18.0 # Apache-2.0
|
||||||
|
Loading…
Reference in New Issue
Block a user