def test_pyOpenSSL_version(self): ''' Test extension logic with different pyOpenSSL versions ''' pillarval = {'csr': {'extendedKeyUsage': 'serverAuth'}} mock_pgt = MagicMock(return_value=pillarval) with patch.dict(tls.__dict__, { 'OpenSSL_version': LooseVersion('0.1.1'), 'X509_EXT_ENABLED': False }): self.assertEqual( tls.__virtual__(), (False, 'PyOpenSSL version 0.10 or later must be installed ' 'before this module can be used.')) with patch.dict(tls.__salt__, {'pillar.get': mock_pgt}): self.assertRaises(AssertionError, tls.get_extensions, 'server') self.assertRaises(AssertionError, tls.get_extensions, 'client') with patch.dict(tls.__dict__, { 'OpenSSL_version': LooseVersion('0.14.1'), 'X509_EXT_ENABLED': True }): self.assertTrue(tls.__virtual__()) with patch.dict(tls.__salt__, {'pillar.get': mock_pgt}): self.assertEqual(tls.get_extensions('server'), pillarval) self.assertEqual(tls.get_extensions('client'), pillarval) with patch.dict(tls.__dict__, { 'OpenSSL_version': LooseVersion('0.15.1'), 'X509_EXT_ENABLED': True }): self.assertTrue(tls.__virtual__()) with patch.dict(tls.__salt__, {'pillar.get': mock_pgt}): self.assertEqual(tls.get_extensions('server'), pillarval) self.assertEqual(tls.get_extensions('client'), pillarval)
def test_npm_install_url_referenced_package(self): """ Determine if URL-referenced NPM module can be successfully installed. """ npm_version = self.run_function("cmd.run", ["npm -v"]) if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION): user = os.environ.get("SUDO_USER", "root") npm_dir = os.path.join(RUNTIME_VARS.TMP, "git-install-npm") self.run_state("file.directory", name=npm_dir, user=user, dir_mode="755") else: user = None npm_dir = None ret = self.run_state( "npm.installed", name="request/request#v2.81.1", runas=user, dir=npm_dir, registry="http://registry.npmjs.org/", ) self.assertSaltTrueReturn(ret) ret = self.run_state( "npm.removed", name="git://github.com/request/request", runas=user, dir=npm_dir, ) self.assertSaltTrueReturn(ret) if npm_dir is not None: self.run_state("file.absent", name=npm_dir)
def list_packages(self, filters, latest): """ List available packages. :returns: list """ pkglist = self._get_solvable_packages() pkglist.sort(key=cmp_to_key(self._sort_packages)) self.num_packages = len(pkglist) pkglist = self._apply_filters(pkglist, filters) if latest: latest_pkgs = {} new_pkgs = [] for pkg in pkglist: ident = '{}.{}'.format(pkg.name, pkg.arch) if ident not in latest_pkgs.keys() or LooseVersion(str( pkg.evr)) > LooseVersion(str(latest_pkgs[ident].evr)): latest_pkgs[ident] = pkg pkglist = list(latest_pkgs.values()) to_return = [] for pack in pkglist: new_pack = ContentPackage() epoch, version, release = RawSolvablePackage._parse_solvable_evr( pack.evr) new_pack.setNVREA(pack.name, version, release, epoch, pack.arch) new_pack.unique_id = RawSolvablePackage(pack) checksum = pack.lookup_checksum(solv.SOLVABLE_CHECKSUM) new_pack.checksum_type = checksum.typestr() new_pack.checksum = checksum.hex() to_return.append(new_pack) return to_return
def test_npm_install_url_referenced_package(self): ''' Determine if URL-referenced NPM module can be successfully installed. ''' if LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION): user = os.environ.get('SUDO_USER', 'root') npm_dir = os.path.join(RUNTIME_VARS.TMP, 'git-install-npm') self.run_state('file.directory', name=npm_dir, user=user, dir_mode='755') else: user = None npm_dir = None ret = self.run_state('npm.installed', name='request/request#v2.81.1', runas=user, dir=npm_dir, registry="http://registry.npmjs.org/") self.assertSaltTrueReturn(ret) ret = self.run_state('npm.removed', name='git://github.com/request/request', runas=user, dir=npm_dir) self.assertSaltTrueReturn(ret) if npm_dir is not None: self.run_state('file.absent', name=npm_dir)
def test_npm_cache_clean(self): ''' Basic test to determine if NPM successfully cleans its cached packages. ''' npm_version = self.run_function('cmd.run', ['npm -v']) if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION): self.skipTest('Skip with npm >= 5.0.0 until #41770 is fixed') ret = self.run_state('npm.cache_cleaned', name='unused', force=True) self.assertSaltTrueReturn(ret)
def test_import(self): """ Test that the module picks up installed deps """ with patch('salt.config.check_driver_dependencies', return_value=True) as p: get_deps = dimensiondata.get_dependencies() self.assertEqual(get_deps, True) if LooseVersion(mock_version) >= LooseVersion('2.0.0'): self.assertTrue(p.call_count >= 1)
def test_npm_cache_clean(self): """ Basic test to determine if NPM successfully cleans its cached packages. """ npm_version = self.run_function("cmd.run", ["npm -v"]) if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION): self.skipTest("Skip with npm >= 5.0.0 until #41770 is fixed") ret = self.run_state("npm.cache_cleaned", name="unused", force=True) self.assertSaltTrueReturn(ret)
class NpmStateTest(ModuleCase, SaltReturnAssertsMixin): @requires_network() @destructiveTest def test_npm_installed_removed(self): ''' Basic test to determine if NPM module was successfully installed and removed. ''' ret = self.run_state('npm.installed', name='[email protected]', registry="http://registry.npmjs.org/") self.assertSaltTrueReturn(ret) ret = self.run_state('npm.removed', name='pm2') self.assertSaltTrueReturn(ret) @requires_network() @destructiveTest def test_npm_install_url_referenced_package(self): ''' Determine if URL-referenced NPM module can be successfully installed. ''' if LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION): user = os.environ.get('SUDO_USER', 'root') npm_dir = os.path.join(RUNTIME_VARS.TMP, 'git-install-npm') self.run_state('file.directory', name=npm_dir, user=user, dir_mode='755') else: user = None npm_dir = None ret = self.run_state('npm.installed', name='request/request#v2.81.1', runas=user, dir=npm_dir, registry="http://registry.npmjs.org/") self.assertSaltTrueReturn(ret) ret = self.run_state('npm.removed', name='git://github.com/request/request', runas=user, dir=npm_dir) self.assertSaltTrueReturn(ret) if npm_dir is not None: self.run_state('file.absent', name=npm_dir) @requires_network() @destructiveTest def test_npm_installed_pkgs(self): ''' Basic test to determine if NPM module successfully installs multiple packages. ''' ret = self.run_state('npm.installed', name='unused', pkgs=['[email protected]', '[email protected]'], registry="http://registry.npmjs.org/") self.assertSaltTrueReturn(ret) @skipIf(salt.utils.path.which('npm') and LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION), 'Skip with npm >= 5.0.0 until #41770 is fixed') @destructiveTest def test_npm_cache_clean(self): ''' Basic test to determine if NPM successfully cleans its cached packages. ''' ret = self.run_state('npm.cache_cleaned', name='unused', force=True) self.assertSaltTrueReturn(ret)
def test_import(self): """ Test that the module picks up installed deps """ with patch('salt.config.check_driver_dependencies', return_value=True) as p: get_deps = gce.get_dependencies() self.assertEqual(get_deps, True) if LooseVersion(mock_version) >= LooseVersion('2.0.0'): p.assert_called_once()
def test_import(): """ Test that the module picks up installed deps """ with patch("salt.config.check_driver_dependencies", return_value=True) as p: get_deps = gce.get_dependencies() assert get_deps is True if LooseVersion(mock_version) >= LooseVersion("2.0.0"): p.assert_called_once()
def _list_or_not(ret): ''' Return a list instead of a string when newer than 2017.7.5 and not 2018.3, or from a git checkout Caused by this PR #47196 ''' version = __grains__['saltversion'] if (LooseVersion(version) >= LooseVersion('2017.7.6') and version != '2018.3.0') or 'n/a' in version: return [ret] return ret
def _has_required_boto(): """ Returns True/False boolean depending on if Boto is installed and correct version. """ if not HAS_BOTO: return False elif LooseVersion(boto.__version__) < LooseVersion(required_boto_version): return False else: return True
def __has_required_azure(): """ Returns True/False if the required version of the Azure SDK is installed. """ if HAS_AZURE: if hasattr(azure, "__version__"): version = LooseVersion(azure.__version__) else: version = LooseVersion(azure.common.__version__) if LooseVersion(REQUIRED_AZURE) <= version: return True return False
def __virtual__(): ''' Returns True/False boolean depending on if Boto3 is installed and correct version. ''' if not HAS_BOTO3: return False if LooseVersion(boto3.__version__) < LooseVersion(REQUIRED_BOTO3_VERSION): return False, ( 'The boto3 module must be greater or equal to version {}' ''.format(REQUIRED_BOTO3_VERSION)) return True
def _has_required_moto(): ''' Returns True/False boolean depending on if Moto is installed and correct version. ''' if not HAS_MOTO: return False else: import pkg_resources if LooseVersion(pkg_resources.get_distribution('moto').version) < LooseVersion('0.3.7'): return False return True
def _has_required_boto(): ''' Returns True/False boolean depending on if Boto is installed and correct version. ''' if not HAS_BOTO: return False elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version): return False elif LooseVersion(found_botocore_version) < LooseVersion(required_botocore_version): return False else: return True
def __has_required_azure(): ''' Returns True/False if the required version of the Azure SDK is installed. ''' if HAS_AZURE: if hasattr(azure, '__version__'): version = LooseVersion(azure.__version__) else: version = LooseVersion(azure.common.__version__) if REQUIRED_AZURE <= version: return True return False
class NpmStateTest(ModuleCase, SaltReturnAssertsMixin): @requires_network() @destructiveTest def test_npm_installed_removed(self): ''' Basic test to determine if NPM module was successfully installed and removed. ''' ret = self.run_state('npm.installed', name='pm2') self.assertSaltTrueReturn(ret) ret = self.run_state('npm.removed', name='pm2') self.assertSaltTrueReturn(ret) @requires_network() @destructiveTest def test_npm_install_url_referenced_package(self): ''' Determine if URL-referenced NPM module can be successfully installed. ''' ret = self.run_state('npm.installed', name='request/request#v2.81.1') self.assertSaltTrueReturn(ret) ret = self.run_state('npm.removed', name='git://github.com/request/request') self.assertSaltTrueReturn(ret) @requires_network() @destructiveTest def test_npm_installed_pkgs(self): ''' Basic test to determine if NPM module successfully installs multiple packages. ''' ret = self.run_state('npm.installed', name='unused', pkgs=['pm2', 'grunt']) self.assertSaltTrueReturn(ret) @skipIf( salt.utils.path.which('npm') and LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION), 'Skip with npm >= 5.0.0 until #41770 is fixed') @destructiveTest def test_npm_cache_clean(self): ''' Basic test to determine if NPM successfully cleans its cached packages. ''' ret = self.run_state('npm.cache_cleaned', name='unused', force=True) self.assertSaltTrueReturn(ret)
def _has_required_boto3(): ''' Returns True/False boolean depending on if Boto is installed and correct version. ''' try: if not HAS_BOTO3: return False elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version): return False else: return True except AttributeError as exc: if "has no attribute '__version__'" not in six.text_type(exc): raise return False
def _worktrees_supported(): ''' Check if the git version is 2.5.0 or later ''' try: return _git_version() >= LooseVersion('2.5.0') except AttributeError: return False
def test_pyOpenSSL_version(self): """ Test extension logic with different pyOpenSSL versions """ pillarval = {"csr": {"extendedKeyUsage": "serverAuth"}} mock_pgt = MagicMock(return_value=pillarval) with patch.dict( tls.__dict__, { "OpenSSL_version": LooseVersion("0.1.1"), "X509_EXT_ENABLED": False }, ): self.assertEqual( tls.__virtual__(), ( False, "PyOpenSSL version 0.10 or later must be installed " "before this module can be used.", ), ) with patch.dict(tls.__salt__, {"pillar.get": mock_pgt}): self.assertRaises(AssertionError, tls.get_extensions, "server") self.assertRaises(AssertionError, tls.get_extensions, "client") with patch.dict( tls.__dict__, { "OpenSSL_version": LooseVersion("0.14.1"), "X509_EXT_ENABLED": True }, ): self.assertTrue(tls.__virtual__()) with patch.dict(tls.__salt__, {"pillar.get": mock_pgt}): self.assertEqual(tls.get_extensions("server"), pillarval) self.assertEqual(tls.get_extensions("client"), pillarval) with patch.dict( tls.__dict__, { "OpenSSL_version": LooseVersion("0.15.1"), "X509_EXT_ENABLED": True }, ): self.assertTrue(tls.__virtual__()) with patch.dict(tls.__salt__, {"pillar.get": mock_pgt}): self.assertEqual(tls.get_extensions("server"), pillarval) self.assertEqual(tls.get_extensions("client"), pillarval)
def _worktrees_supported(): """ Check if the git version is 2.5.0 or later """ try: return _git_version() >= LooseVersion("2.5.0") except AttributeError: return False
def version_is_3_or_newer(): ''' Determine if the Tentacle executable version is 3.0 or newer. :return: A boolean indicating if the executable version is 3.0 or newer. :rtype: bool CLI Example: .. code-block:: bash salt '*' octopus_tentacle.version_is_3_or_newer ''' version = str(_get_version()) if LooseVersion(version) >= LooseVersion('3.0'): return True return False
def version_at_least(version): """ Check that the system contains a version of .NET that is at least the passed version. Args: version (str): The version to check for Returns: bool: ``True`` if the system contains a version of .NET that is at least the passed version, otherwise ``False`` """ for dotnet_version in versions_list(): if LooseVersion(dotnet_version) >= LooseVersion(str(version)): return True return False
def test_worktree_add_rm(self): """ This tests git.worktree_add, git.is_worktree, git.worktree_rm, and git.worktree_prune. Tests for 'git worktree list' are covered in tests.unit.modules.git_test. """ # We don't need to enclose this comparison in a try/except, since the # decorator would skip this test if git is not installed and we'd never # get here in the first place. if _git_version() >= LooseVersion("2.6.0"): worktree_add_prefix = "Preparing " else: worktree_add_prefix = "Enter " worktree_path = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) worktree_basename = os.path.basename(worktree_path) worktree_path2 = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) worktree_basename2 = os.path.basename(worktree_path2) # Even though this is Windows, git commands return a unix style path if salt.utils.platform.is_windows(): worktree_path = worktree_path.replace("\\", "/") worktree_path2 = worktree_path2.replace("\\", "/") # Add the worktrees ret = self.run_function( "git.worktree_add", [self.repo, worktree_path], ) self.assertTrue(worktree_add_prefix in ret) self.assertTrue(worktree_basename in ret) ret = self.run_function("git.worktree_add", [self.repo, worktree_path2]) self.assertTrue(worktree_add_prefix in ret) self.assertTrue(worktree_basename2 in ret) # Check if this new path is a worktree self.assertTrue(self.run_function("git.is_worktree", [worktree_path])) # Check if the main repo is a worktree self.assertFalse(self.run_function("git.is_worktree", [self.repo])) # Check if a non-repo directory is a worktree empty_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) self.assertFalse(self.run_function("git.is_worktree", [empty_dir])) shutil.rmtree(empty_dir) # Remove the first worktree self.assertTrue(self.run_function("git.worktree_rm", [worktree_path])) # Prune the worktrees prune_message = ( "Removing worktrees/{}: gitdir file points to non-existent location".format( worktree_basename ) ) # Test dry run output. It should match the same output we get when we # actually prune the worktrees. result = self.run_function("git.worktree_prune", [self.repo], dry_run=True) self.assertEqual(result, prune_message) # Test pruning for real, and make sure the output is the same self.assertEqual( self.run_function("git.worktree_prune", [self.repo]), prune_message )
def test_cmp(self): versions = ( ('1.5.1', '1.5.2b2', -1), ('161', '3.10a', 1), ('8.02', '8.02', 0), ('3.4j', '1996.07.12', -1), ('3.2.pl0', '3.1.1.6', 1), ('2g6', '11g', -1), ('0.960923', '2.2beta29', -1), ('1.13++', '5.5.kw', -1), # Added by us ('3.10.0-514.el7', '3.10.0-514.6.1.el7', 1), ('2.2.2', '2.12.1', -1)) for v1, v2, wanted in versions: res = getattr(LooseVersion(v1), cmp_method)(LooseVersion(v2)) self.assertEqual( res, wanted, 'cmp(%s, %s) should be %s, got %s' % (v1, v2, wanted, res))
def test_worktree_add_rm(self): ''' This tests git.worktree_add, git.is_worktree, git.worktree_rm, and git.worktree_prune. Tests for 'git worktree list' are covered in tests.unit.modules.git_test. ''' # We don't need to enclose this comparison in a try/except, since the # decorator would skip this test if git is not installed and we'd never # get here in the first place. if _git_version() >= LooseVersion('2.6.0'): worktree_add_prefix = 'Preparing ' else: worktree_add_prefix = 'Enter ' worktree_path = tempfile.mkdtemp(dir=integration.TMP) worktree_basename = os.path.basename(worktree_path) worktree_path2 = tempfile.mkdtemp(dir=integration.TMP) # Even though this is Windows, git commands return a unix style path if salt.utils.is_windows(): worktree_path = worktree_path.replace('\\', '/') worktree_path2 = worktree_path2.replace('\\', '/') # Add the worktrees ret = self.run_function( 'git.worktree_add', [self.repo, worktree_path], ) self.assertTrue(worktree_add_prefix + worktree_path in ret) ret = self.run_function('git.worktree_add', [self.repo, worktree_path2]) self.assertTrue(worktree_add_prefix + worktree_path2 in ret) # Check if this new path is a worktree self.assertTrue(self.run_function('git.is_worktree', [worktree_path])) # Check if the main repo is a worktree self.assertFalse(self.run_function('git.is_worktree', [self.repo])) # Check if a non-repo directory is a worktree empty_dir = tempfile.mkdtemp(dir=integration.TMP) self.assertFalse(self.run_function('git.is_worktree', [empty_dir])) shutil.rmtree(empty_dir) # Remove the first worktree self.assertTrue(self.run_function('git.worktree_rm', [worktree_path])) # Prune the worktrees prune_message = ( 'Removing worktrees/{0}: gitdir file points to non-existent ' 'location'.format(worktree_basename)) # Test dry run output. It should match the same output we get when we # actually prune the worktrees. self.assertEqual( self.run_function('git.worktree_prune', [self.repo], dry_run=True), prune_message) # Test pruning for real, and make sure the output is the same self.assertEqual(self.run_function('git.worktree_prune', [self.repo]), prune_message)
def test_cmp(self): versions = ( ("1.5.1", "1.5.2b2", -1), ("161", "3.10a", 1), ("8.02", "8.02", 0), ("3.4j", "1996.07.12", -1), ("3.2.pl0", "3.1.1.6", 1), ("2g6", "11g", -1), ("0.960923", "2.2beta29", -1), ("1.13++", "5.5.kw", -1), # Added by us ("3.10.0-514.el7", "3.10.0-514.6.1.el7", 1), ("2.2.2", "2.12.1", -1), ) for v1, v2, wanted in versions: res = getattr(LooseVersion(v1), cmp_method)(LooseVersion(v2)) self.assertEqual( res, wanted, "cmp(%s, %s) should be %s, got %s" % (v1, v2, wanted, res))
def _git_version(): git_version = subprocess.Popen(['git', '--version'], shell=False, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] if not git_version: log.error('Git not installed') return False log.debug('Detected git version ' + git_version) return LooseVersion(git_version.split()[-1])
def _git_version(): git_version = subprocess.Popen( ["git", "--version"], shell=False, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ).communicate()[0] if not git_version: log.error("Git not installed") return False log.debug('Detected git version %s', git_version) return LooseVersion(git_version.split()[-1])