salt icon indicating copy to clipboard operation
salt copied to clipboard

postgres.postgres_database.present, query string encoding fix

Open lsh-0 opened this issue 3 years ago • 1 comments

What does this PR do?

Changes input to a NamedTemporaryFile.write method from strings to bytes.

What issues does this PR fix or reference?

Fixes the exception:

            Traceback (most recent call last):
                File "/usr/lib/python3/dist-packages/salt/state.py", line 2171, in call
                  ret = self.states[cdata["full"]](
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 1241, in __call__
                  return self.loader.run(run_func, *args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2274, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2289, in _run_as
                  return _func_or_method(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2322, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/states/postgres_database.py", line 160, in present
                  elif name in dbs and __salt__["postgres.db_alter"](
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 1241, in __call__
                  return self.loader.run(run_func, *args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2274, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/loader.py", line 2289, in _run_as
                  return _func_or_method(*args, **kwargs)
                File "/usr/lib/python3/dist-packages/salt/modules/postgres.py", line 664, in db_alter
                  ret = owner_to(
                File "/usr/lib/python3/dist-packages/salt/modules/postgres.py", line 2219, in owner_to
                  sqlfile.write("begin;\n")
                File "/usr/lib/python3.8/tempfile.py", line 612, in func_wrapper
                  return func(*args, **kwargs)
              TypeError: a bytes-like object is required, not 'str'

on

$ salt-call --versions
Salt Version:
          Salt: 3003.4
 
Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: 2.7.3
     docker-py: 4.1.0
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 2.10.1
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: Not Installed
  pycryptodome: 3.6.1
        pygit2: Not Installed
        Python: 3.8.10 (default, Mar 15 2022, 12:22:08)
  python-gnupg: 0.4.5
        PyYAML: 5.3.1
         PyZMQ: 18.1.1
         smmap: Not Installed
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.2
 
System Versions:
          dist: ubuntu 20.04 focal
        locale: utf-8
       machine: x86_64
       release: 5.11.0-1028-aws
        system: Linux
       version: Ubuntu 20.04 focal

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

  • [ ] Docs
  • [ ] Changelog - https://docs.saltproject.io/en/master/topics/development/changelog.html
  • [ ] Tests written/updated

Commits signed with GPG?

Yes/No

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

lsh-0 avatar Jul 22 '22 02:07 lsh-0

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey. Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar. If you have additional questions, email us at [email protected]. We’re glad you’ve joined our community and look forward to doing awesome things with you!

welcome[bot] avatar Jul 22 '22 02:07 welcome[bot]

@lsh-0 thanks for the PR. This looks good, but we need some tests on this one. Please let us know if you need any assistance in doing so. Thanks!

garethgreenaway avatar Sep 02 '22 19:09 garethgreenaway

hi @garethgreenaway , thanks for the feedback. If this code had tests already then they would be failing in Python3. NamedTemporaryFile is opened in binary write mode by default so all data using .write must be bytes: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

I don't have free time to spelunk this code and write proper unit tests for it, sorry.

lsh-0 avatar Sep 05 '22 04:09 lsh-0