#1175 Don't default the module name to "unnamed" on a direct modulemd submission
Merged 5 years ago by mprahl. Opened 5 years ago by mprahl.

@@ -473,7 +473,12 @@ 

      yaml_file = to_text_type(handle.read())

      mmd = load_mmd(yaml_file)

      dt = datetime.utcfromtimestamp(int(time.time()))

-     def_name = str(os.path.splitext(os.path.basename(handle.filename))[0])

+     if hasattr(handle, 'filename'):

+         def_name = str(os.path.splitext(os.path.basename(handle.filename))[0])

+     elif not mmd.get_name():

+         raise ValidationError(

+             "The module's name was not present in the modulemd file. Please use the "

+             "\"module_name\" parameter")

      def_version = int(dt.strftime("%Y%m%d%H%M%S"))

      mmd.set_name(mmd.get_name() or def_name)

      mmd.set_stream(stream or mmd.get_stream() or "master")

@@ -436,10 +436,8 @@ 

      def post(self):

          if "modulemd" in self.data:

              handle = BytesIO(self.data["modulemd"].encode("utf-8"))

-             if "module_name" in self.data and self.data["module_name"]:

+             if self.data.get("module_name"):

                  handle.filename = self.data["module_name"]

-             else:

-                 handle.filename = "unnamed"

          else:

              handle = request.files["yaml"]

          return submit_module_build_from_yaml(self.username, handle, self.data)

@@ -1843,6 +1843,35 @@ 

          assert module.buildrequires[0].context == '00000000'

          assert module.buildrequires[0].stream_version == 280000

  

+     @patch('module_build_service.auth.get_user', return_value=user)

+     @patch('module_build_service.config.Config.modules_allow_scratch',

+            new_callable=PropertyMock, return_value=True)

+     @patch('module_build_service.config.Config.yaml_submit_allowed',

+            new_callable=PropertyMock, return_value=True)

+     def test_submit_scratch_build_with_mmd_no_module_name(

+             self, mocked_allow_yaml, mocked_allow_scratch, mocked_get_user):

+         base_dir = path.abspath(path.dirname(__file__))

+         mmd_path = path.join(base_dir, '..', 'staged_data', 'testmodule.yaml')

+         post_url = '/module-build-service/1/module-builds/'

+         with open(mmd_path, 'rb') as f:

+             modulemd = f.read().decode('utf-8')

+ 

+         post_data = {

+             'branch': 'master',

+             'scratch': True,

+             'modulemd': modulemd,

+         }

+         rv = self.client.post(post_url, data=json.dumps(post_data))

+         assert rv.status_code == 400

+         data = json.loads(rv.data)

+         expected_error = {

+             'error': 'Bad Request',

+             'message': ('The module\'s name was not present in the modulemd file. Please use the '

+                         '"module_name" parameter'),

+             'status': 400

+         }

+         assert data == expected_error

+ 

      @pytest.mark.parametrize('api_version', [1, 2])

      @patch('module_build_service.auth.get_user', return_value=user)

      @patch('module_build_service.config.Config.modules_allow_scratch',

The module name should either be set in the modulemd file or provided by the module_name parameter.

LGTM.

:thumbsup:

I considered enforcing this when implementing PR #1165, but decided to go with a default name instead. Thank you for merging that PR and fixing this.

I also checked... the README file describing the module_name parameter is ambiguous enough that it shouldn't need updating. :wink:

Pull-Request has been merged by mprahl

5 years ago