The tests happened to work because py2 does not default to absolute
imports, contrary to py3.
Additionally, with py3 absolute imports we must remove imported libs
from `sys.modules`(not salt loader modules)
With the merging of #39996, salt/modules/docker.py now imports
salt.utils.docker. However, our loader appends the module dir (in
this case salt/modules/) to sys.path temporarily for the length of the
loading process. So, as the docker execution module tries to import
salt.utils.docker, when salt.utils.docker attempts to do an "import
docker", and docker-py is *not* installed, this results in
salt/modules/docker.py (the docker execution module) being loaded in its
place, which results in tracebacks in the minion log.
Renaming the docker execution module keeps this import shadowing from
occurring. Note that we don't need to do this for the placeholder
salt/states/docker.py as it does not import salt.utils.docker.
If the condition is false, the 'state' var will not be defined and
referencing it in the subsequent code will produce UnboundLocalError.
Use dict.get() to initialize the 'state' variable.
If HAS_WIN32 is True that only means that the import of
salt.utils.win_functions succeeded. To ensure that required win libs are
available one has to check salt.utils.win_functions.HAS_WIN32 instead.
For instance, HAS_WIN32 is True even on POSIX:
(py2-env) salt-test@tommynaut:~> uname -a
Linux tommynaut 4.9.11-1-default #1 SMP PREEMPT Sat Feb 18 17:59:27 UTC 2017 (cf9c670) x86_64 x86_64 x86_64 GNU/Linux
(py2-env) salt-test@tommynaut:~> python
Python 2.7.13 (default, Jan 03 2017, 17:41:54) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import salt.utils
>>> salt.utils.HAS_WIN32
True
Rename HAS_WIN32 to HAS_WIN_FUNCTIONS to reflect that.
Commit f3b010ebe5 ("Fix imports") fixed an import issue introduced in
commit 96afc0b6cf ("fix: no more test against SYSTEM user") by
removing salt.utils.win_functions.HAS_WIN32 check from
salt/utils/__init__.py and setting HAS_WIN32 to True if
salt.utils.win_functions was successfully imported. However,
salt.utils.win_functions doesn't raise ImportError if required libs
aren't available but sets salt.utils.win_functions.HAS_WIN32 to False in
such a case.
So after commit f3b010ebe5 ("Fix imports") salt.utils.HAS_WIN32 is
always true:
(py2-env) salt-test@tommynaut:~> uname -a
Linux tommynaut 4.9.11-1-default #1 SMP PREEMPT Sat Feb 18 17:59:27 UTC 2017 (cf9c670) x86_64 x86_64 x86_64 GNU/Linux
(py2-env) salt-test@tommynaut:~> python
Python 2.7.13 (default, Jan 03 2017, 17:41:54) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import salt.utils
>>> salt.utils.HAS_WIN32
True
salt.utils.get_user thus calls salt.utils.win_functions.get_current_user
without ensuring that all required libs were imported.
Explicitly check salt.utils.win_functions.HAS_WIN32 in get_user(), so it
properly raises CommandExecutionError if required libs aren't available.