Exemplo n.º 1
0
 def test_error_value_error(self, mock_trace):
     mock_trace.return_value = "test trace"
     fault_wrapper = fault.FaultWrapper(None)
     exception = exceptions.PackageClassLoadError("test")
     exception.message = "Unable to load class 'test' from package"
     result = fault_wrapper._error(exception)
     self.assertEqual(result['code'], 400)
     self.assertEqual(result['error']['message'],
                      "Unable to load class 'test' from package")
Exemplo n.º 2
0
 def test_package_class_load_error(self):
     class_name = 'test class name'
     message = 'test message'
     error = exceptions.PackageClassLoadError(class_name=class_name,
                                              message=message)
     expected = 'Unable to load class "{0}" from package: {1}'\
                .format(class_name, message)
     if six.PY2:
         self.assertEqual(expected, error.message)
     elif six.PY34:
         self.assertEqual(expected, error.args[0])
Exemplo n.º 3
0
    def _translate_class(self):
        csar_file = os.path.join(self._source_directory, 'csar.zip')
        shutil.copy(csar_file, self.get_resource(self.full_name))

        if not os.path.isfile(csar_file):
            raise exceptions.PackageClassLoadError(
                self.full_name, 'File with class definition not found')

        csar_obj = csar.CSAR(csar_file)
        try:
            csar_obj.validate()
        except csar_exception.ValidationError as ve:
            raise exceptions.PackageFormatError('Not a CSAR archive: ' +
                                                str(ve))

        translated = {
            'Name': self.full_name,
            'Extends': 'io.murano.Application'
        }

        csar_envs_path = os.path.join(self._source_directory,
                                      CSAR_RESOURCES_DIR_NAME,
                                      CSAR_ENV_DIR_NAME)

        validate_csar_parameters = (not os.path.isdir(csar_envs_path)
                                    or not os.listdir(csar_envs_path))

        tosca = csar_obj.get_main_template_yaml()
        parameters = CSARPackage._build_properties(tosca,
                                                   validate_csar_parameters)
        parameters.update(CSARPackage._translate_outputs(tosca))
        translated['Properties'] = parameters
        hot = yaml.load(
            self._translate('tosca', csar_obj.csar, parameters, True))
        files = CSARPackage._translate_files(self._source_directory)

        template_file = os.path.join(self._source_directory,
                                     CSAR_RESOURCES_DIR_NAME, 'template.yaml')
        with open(template_file, 'w') as outfile:
            outfile.write(yaml.safe_dump(hot))
        translated.update(CSARPackage._generate_workflow(hot, files))
        self._translated_class = yaml.dump(translated,
                                           Dumper=Dumper,
                                           default_style='"')
Exemplo n.º 4
0
 def test_fault_call(self, mock_trace):
     mock_trace.return_value = "test trace"
     fault_wrapper = fault.FaultWrapper(None)
     exception = exceptions.PackageClassLoadError("test")
     exception.message = "Unable to load class 'test' from package"
     test_fault = fault.Fault(fault_wrapper._error(exception))
     environ = {
         'SERVER_NAME': 'server.test',
         'SERVER_PORT': '8082',
         'SERVER_PROTOCOL': 'http',
         'SCRIPT_NAME': '/',
         'PATH_INFO': '/',
         'wsgi.url_scheme': 'http',
         'QUERY_STRING': '',
         'CONTENT_TYPE': 'application/json',
         'REQUEST_METHOD': 'HEAD'
     }
     req = wsgi.Request(environ)
     response = jsonutils.loads(test_fault(req).body)
     self.assertEqual(response['code'], 400)
Exemplo n.º 5
0
    def _translate_class(self):
        template_file = path.secure_join(self._source_directory,
                                         'template.yaml')

        if not os.path.isfile(template_file):
            raise exceptions.PackageClassLoadError(
                self.full_name, 'File with class definition not found')

        shutil.copy(template_file, self.get_resource(self.full_name))
        with open(template_file) as stream:
            hot = yaml.safe_load(stream)
            if 'resources' not in hot:
                raise exceptions.PackageFormatError('Not a HOT template')
        translated = {
            'Name': self.full_name,
            'Extends': 'io.murano.Application'
        }

        hot_envs_path = path.secure_join(self._source_directory,
                                         RESOURCES_DIR_NAME, HOT_ENV_DIR_NAME)

        # if using hot environments, doing parameter validation with contracts
        # will overwrite the parameters in the hot environment.
        # don't validate parameters if hot environments exist.
        validate_hot_parameters = (not os.path.isdir(hot_envs_path)
                                   or not os.listdir(hot_envs_path))

        parameters = HotPackage._build_properties(hot, validate_hot_parameters)
        parameters.update(HotPackage._translate_outputs(hot))
        translated['Properties'] = parameters

        files = HotPackage._translate_files(self._source_directory)
        translated.update(HotPackage._generate_workflow(hot, files))

        # use default_style with double quote mark because by default PyYAML
        # doesn't put any quote marks ans as a result strings with e.g. dashes
        # may be interpreted as YAQL expressions upon load
        self._translated_class = yaml.dump(translated,
                                           Dumper=Dumper,
                                           default_style='"')
Exemplo n.º 6
0
    def _translate_ui(self):
        template_file = os.path.join(self._source_directory, 'template.yaml')

        if not os.path.isfile(template_file):
            raise exceptions.PackageClassLoadError(
                self.full_name, 'File with class definition not found')
        with open(template_file) as stream:
            hot = yaml.safe_load(stream)

        groups = HotPackage._translate_ui_parameters(hot, self.description)
        forms = []
        for i, record in enumerate(groups):
            forms.append({'group{0}'.format(i): {'fields': record[0]}})

        translated = {
            'Version': 2,
            'Application': HotPackage._generate_application_ui(
                groups, self.full_name, self.full_name, str(self.version)),
            'Forms': forms
        }

        # see comment above about default_style
        return yaml.dump(translated, Dumper=Dumper, default_style='"')