def test_gen_thin_python_exist_or_not(self): """ Test thin.gen_thin function if the opposite python binary does not exist """ with TstSuiteLoggingHandler() as handler: thin.gen_thin("") salt.utils.thin.subprocess.Popen.assert_not_called() if salt.ext.six.PY2: self.assertIn( "DEBUG:python3 binary does not exist. Will not attempt to generate " "tops for Python 3", handler.messages, ) if salt.ext.six.PY3: self.assertIn( "DEBUG:python2 binary does not exist. Will not " "detect Python 2 version", handler.messages, ) self.assertIn( "DEBUG:python2 binary does not exist. Will not attempt to generate " "tops for Python 2", handler.messages, )
def test_gen_thin_ext_alternative_content_files_written_py3(self): """ Test thin.gen_thin function if external alternative content files are written. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: """ ext_conf = { "namespace": { "py-version": [2, 7], "path": "/opt/2015.8/salt", "dependencies": { "certifi": "/opt/certifi", "whatever": "/opt/whatever", }, } } thin.gen_thin("", extended_cfg=ext_conf) files = [] for py in ("pyall", "pyall", "py2"): for i in range(1, 4): files.append(os.path.join("namespace", py, "root", "r{0}".format(i))) for i in range(4, 7): files.append(os.path.join("namespace", py, "root2", "r{0}".format(i))) for idx, cl in enumerate(thin.tarfile.open().method_calls[12:-6]): arcname = cl[2].get("arcname") self.assertIn(arcname, files) files.pop(files.index(arcname)) self.assertFalse(files)
def test_gen_thin_fails_ancient_python_version(self): ''' Test thin.gen_thin function raises an exception if Python major/minor version is lower than 2.6 :return: ''' with pytest.raises(salt.exceptions.SaltSystemExit) as err: thin.sys.exc_clear = lambda: None thin.gen_thin('') assert 'The minimum required python version to run salt-ssh is "2.6"' in str(err)
def test_gen_thin_compression_fallback_py3(self): ''' Test thin.gen_thin function if fallbacks to the gzip compression, once setup wrong. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: ''' thin.gen_thin('', compress='arj') thin.log.warning.assert_called() pt, msg = thin.log.warning.mock_calls[0][1] assert pt % msg == 'Unknown compression type: "arj". Falling back to "gzip" compression.' thin.zipfile.ZipFile.assert_not_called() thin.tarfile.open.assert_called()
def test_gen_thin_fails_ancient_python_version(self): """ Test thin.gen_thin function raises an exception if Python major/minor version is lower than 2.6 :return: """ with pytest.raises(salt.exceptions.SaltSystemExit) as err: thin.sys.exc_clear = lambda: None thin.gen_thin("") self.assertIn( "The minimum required python version to run salt-ssh is " '"2.6"', str(err.value), )
def test_gen_thin_control_files_written_py3(self): ''' Test thin.gen_thin function if control files are written (version, salt-call etc). NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: ''' thin.gen_thin('') arc_name, arc_mode = thin.tarfile.method_calls[0][1] assert arc_name == 'thin/thin.tgz' assert arc_mode == 'w:gz' for idx, fname in enumerate(['version', '.thin-gen-py-version', 'salt-call', 'supported-versions']): assert thin.tarfile.open().method_calls[idx + 4][1][0] == fname thin.tarfile.open().close.assert_called()
def test_gen_thin_control_files_written_py3(self): """ Test thin.gen_thin function if control files are written (version, salt-call etc). :return: """ thin.gen_thin("") arc_name, arc_mode = thin.tarfile.method_calls[0][1] self.assertEqual(arc_name, ".temporary") self.assertEqual(arc_mode, "w:gz") for idx, fname in enumerate( ["version", ".thin-gen-py-version", "salt-call", "supported-versions"] ): name = thin.tarfile.open().method_calls[idx + 2][1][0] self.assertEqual(name, fname) thin.tarfile.open().close.assert_called()
def test_gen_thin_main_content_files_written_py3(self): """ Test thin.gen_thin function if main content files are written. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: """ thin.gen_thin("") files = [] for py in ("py2", "py2", "py3", "pyall"): for i in range(1, 4): files.append(os.path.join(py, "root", "r{0}".format(i))) for i in range(4, 7): files.append(os.path.join(py, "root2", "r{0}".format(i))) for cl in thin.tarfile.open().method_calls[:-6]: arcname = cl[2].get("arcname") self.assertIn(arcname, files) files.pop(files.index(arcname)) self.assertFalse(files)
def test_gen_thin_main_content_files_written_py3(self): ''' Test thin.gen_thin function if main content files are written. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: ''' thin.gen_thin('') files = [ 'py2/root/r1', 'py2/root/r2', 'py2/root/r3', 'py2/root2/r4', 'py2/root2/r5', 'py2/root2/r6', 'py2/root/r1', 'py2/root/r2', 'py2/root/r3', 'py2/root2/r4', 'py2/root2/r5', 'py2/root2/r6', 'py3/root/r1', 'py3/root/r2', 'py3/root/r3', 'py3/root2/r4', 'py3/root2/r5', 'py3/root2/r6', 'pyall/root/r1', 'pyall/root/r2', 'pyall/root/r3', 'pyall/root2/r4', 'pyall/root2/r5', 'pyall/root2/r6' ] for cl in thin.tarfile.open().method_calls[:-6]: arcname = cl[2].get('arcname') assert arcname in files files.pop(files.index(arcname)) assert not bool(files)
def test_gen_thin_ext_alternative_content_files_written_py3(self): ''' Test thin.gen_thin function if external alternative content files are written. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: ''' thin.gen_thin('') files = ['namespace/pyall/root/r1', 'namespace/pyall/root/r2', 'namespace/pyall/root/r3', 'namespace/pyall/root2/r4', 'namespace/pyall/root2/r5', 'namespace/pyall/root2/r6', 'namespace/pyall/root/r1', 'namespace/pyall/root/r2', 'namespace/pyall/root/r3', 'namespace/pyall/root2/r4', 'namespace/pyall/root2/r5', 'namespace/pyall/root2/r6', 'namespace/py2/root/r1', 'namespace/py2/root/r2', 'namespace/py2/root/r3', 'namespace/py2/root2/r4', 'namespace/py2/root2/r5', 'namespace/py2/root2/r6' ] for idx, cl in enumerate(thin.tarfile.open().method_calls[12:-6]): arcname = cl[2].get('arcname') assert arcname in files files.pop(files.index(arcname)) assert not bool(files)
def test_gen_thin_python_exist_or_not(self): ''' Test thin.gen_thin function if the opposite python binary does not exist ''' with TstSuiteLoggingHandler() as handler: thin.gen_thin('') salt.utils.thin.subprocess.Popen.assert_not_called() if salt.ext.six.PY2: self.assertIn('DEBUG:python3 binary does not exist. Will not attempt to generate ' 'tops for Python 3', handler.messages) if salt.ext.six.PY3: self.assertIn('DEBUG:python2 binary does not exist. Will not ' 'detect Python 2 version', handler.messages) self.assertIn('DEBUG:python2 binary does not exist. Will not attempt to generate ' 'tops for Python 2', handler.messages)
def test_gen_thin_control_files_written_access_denied_cwd(self): """ Test thin.gen_thin function if control files are written (version, salt-call etc) when the current working directory is inaccessible, eg. Salt is configured to run as a non-root user but the command is executed in a directory that the user does not have permissions to. Issue #54317. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: """ thin.gen_thin("") arc_name, arc_mode = thin.tarfile.method_calls[0][1] self.assertEqual(arc_name, ".temporary") self.assertEqual(arc_mode, "w:gz") for idx, fname in enumerate( ["version", ".thin-gen-py-version", "salt-call", "supported-versions"] ): name = thin.tarfile.open().method_calls[idx + 2][1][0] self.assertEqual(name, fname) thin.tarfile.open().close.assert_called()
def test_gen_thin_ext_alternative_content_files_written_py3(self): ''' Test thin.gen_thin function if external alternative content files are written. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions. :return: ''' thin.gen_thin('') files = [] for py in ('pyall', 'pyall', 'py2'): for i in range(1, 4): files.append( os.path.join('namespace', py, 'root', 'r{0}'.format(i))) for i in range(4, 7): files.append( os.path.join('namespace', py, 'root2', 'r{0}'.format(i))) for idx, cl in enumerate(thin.tarfile.open().method_calls[12:-6]): arcname = cl[2].get('arcname') self.assertIn(arcname, files) files.pop(files.index(arcname)) self.assertFalse(files)