Permalink
Browse files

Merge pull request #1194 from mnemonikk/add-tests-to-rvm-module

Add tests to rvm and gem module and state
  • Loading branch information...
2 parents 3fef845 + d02f0be commit 6bcc64da7b472c90471e3601af852b01555d5d48 @thatch45 thatch45 committed May 4, 2012
View
2 salt/modules/rvm.py
@@ -45,7 +45,7 @@ def install():
installer = "https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
return 0 == __salt__["cmd.retcode"](
# the RVM installer only does a multi-user install when it is invoked under sudo
- "curl -s {installer} | sudo bash -s stable").format(installer=installer)
+ "curl -s {installer} | sudo bash -s stable".format(installer=installer))
def install_ruby(ruby, runas=None):
View
6 salt/states/rvm.py
@@ -154,9 +154,9 @@ def _check_ruby(ret, ruby, runas=None):
match_version = True
match_micro_version = False
micro_version_regex = re.compile('-([0-9]{4}\.[0-9]{2}|p[0-9]+)$')
- if micro_version_regex.match(ruby):
+ if micro_version_regex.search(ruby):
match_micro_version = True
- if re.match('^[a-z]+$', ruby):
+ if re.search('^[a-z]+$', ruby):
match_version = False
ruby = re.sub('^ruby-', '', ruby)
@@ -166,7 +166,7 @@ def _check_ruby(ret, ruby, runas=None):
if not match_micro_version:
version = micro_version_regex.sub('', version)
if not match_version:
- version = re.sub('-.*', '')
+ version = re.sub('-.*', '', version)
if version == ruby:
ret['result'] = True
ret['comment'] = 'Requested ruby exists.'
View
2 tests/runtests.py 100644 → 100755
@@ -65,7 +65,7 @@ def run_unit_tests(opts=None):
if not opts.get('unit', True):
return
loader = saltunittest.TestLoader()
- tests = loader.discover(os.path.join(TEST_DIR, 'unit', 'templates'), '*.py')
+ tests = loader.discover(os.path.join(TEST_DIR, 'unit'), '*_test.py')
print('~' * PNUM)
print('Starting Unit Tests')
print('~' * PNUM)
View
0 tests/unit/modules/__init__.py
No changes.
View
68 tests/unit/modules/gem_test.py
@@ -0,0 +1,68 @@
+import sys
+import os
+sys.path.insert(
+ 0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
+
+from saltunittest import TestCase, TestLoader, TextTestRunner
+from mock import MagicMock, patch
+
+import salt.modules.gem as gem
+gem.__salt__ = {}
+
+
+class TestGemModule(TestCase):
+
+ def test__gem(self):
+ mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
+ with patch.dict(gem.__salt__,
+ {'rvm.is_installed': MagicMock(return_value=False),
+ 'cmd.run_all': mock}):
+ gem._gem("install rails")
+ mock.assert_called_once_with("gem install rails", runas=None)
+
+ mock = MagicMock(return_value=None)
+ with patch.dict(gem.__salt__,
+ {'rvm.is_installed': MagicMock(return_value=True),
+ 'rvm.do': mock}):
+ gem._gem("install rails", ruby="1.9.3")
+ mock.assert_called_once_with("1.9.3", "gem install rails", runas=None)
+
+ def test_list(self):
+ output = '''
+actionmailer (2.3.14)
+actionpack (2.3.14)
+activerecord (2.3.14)
+activeresource (2.3.14)
+activesupport (3.0.5, 2.3.14)
+rake (0.9.2, 0.8.7)
+responds_to_parent (1.0.20091013)
+sass (3.1.15, 3.1.7)
+'''
+ mock = MagicMock(return_value=output)
+ with patch.object(gem, '_gem', new=mock):
+ self.assertEqual(
+ {'actionmailer': ['2.3.14'],
+ 'actionpack': ['2.3.14'],
+ 'activerecord': ['2.3.14'],
+ 'activeresource': ['2.3.14'],
+ 'activesupport': ['3.0.5', '2.3.14'],
+ 'rake': ['0.9.2', '0.8.7'],
+ 'responds_to_parent': ['1.0.20091013'],
+ 'sass': ['3.1.15', '3.1.7']},
+ gem.list())
+
+ def test_sources_list(self):
+ output = '''*** CURRENT SOURCES ***
+
+http://rubygems.org/
+'''
+ mock = MagicMock(return_value=output)
+ with patch.object(gem, '_gem', new=mock):
+ self.assertEqual(
+ ['http://rubygems.org/'], gem.sources_list())
+
+
+if __name__ == "__main__":
+ loader = TestLoader()
+ tests = loader.loadTestsFromTestCase(TestGemModule)
+ TextTestRunner(verbosity=1).run(tests)
View
111 tests/unit/modules/rvm_test.py
@@ -0,0 +1,111 @@
+import sys
+import os
+sys.path.insert(
+ 0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
+
+from saltunittest import TestCase, TestLoader, TextTestRunner
+from mock import MagicMock, patch
+
+import salt.modules.rvm as rvm
+rvm.__salt__ = {
+ 'cmd.has_exec': MagicMock(return_value=True)}
+
+
+class TestRvmModule(TestCase):
+
+ def test__rvm(self):
+ mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
+ with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
+ rvm._rvm("install", "1.9.3")
+ mock.assert_called_once_with("/usr/local/rvm/bin/rvm install 1.9.3", runas=None)
+
+ def test__rvm_do(self):
+ mock = MagicMock(return_value=None)
+ with patch.object(rvm, '_rvm', new=mock):
+ rvm._rvm_do("1.9.3", "gemset list")
+ mock.assert_called_once_with("1.9.3 do gemset list", runas=None)
+
+ def test_install(self):
+ mock = MagicMock(return_value=0)
+ with patch.dict(rvm.__salt__, {'cmd.retcode': mock}):
+ rvm.install()
+
+ def test_list(self):
+ list_output = '''
+rvm rubies
+
+ jruby-1.6.5.1 [ amd64 ]
+ ree-1.8.7-2011.03 [ x86_64 ]
+ ree-1.8.7-2011.12 [ x86_64 ]
+=* ree-1.8.7-2012.02 [ x86_64 ]
+ ruby-1.9.2-p180 [ x86_64 ]
+ ruby-1.9.3-p125 [ x86_64 ]
+ ruby-head [ x86_64 ]
+
+# => - current
+# =* - current && default
+# * - default
+
+'''
+ with patch.object(rvm, '_rvm', return_value=list_output):
+ self.assertEqual(
+ [['jruby', '1.6.5.1', False],
+ ['ree', '1.8.7-2011.03', False],
+ ['ree', '1.8.7-2011.12', False],
+ ['ree', '1.8.7-2012.02', True],
+ ['ruby', '1.9.2-p180', False],
+ ['ruby', '1.9.3-p125', False],
+ ['ruby', 'head', False]],
+ rvm.list())
+
+ def test_gemset_list(self):
+ output = '''
+gemsets for ree-1.8.7-2012.02 (found in /usr/local/rvm/gems/ree-1.8.7-2012.02)
+ global
+ bar
+ foo
+
+'''
+ with patch.object(rvm, '_rvm_do', return_value=output):
+ self.assertEqual(
+ ['global', 'bar', 'foo'],
+ rvm.gemset_list())
+
+ def test_gemset_list_all(self):
+ output = '''
+
+gemsets for ruby-1.9.3-p125 (found in /usr/local/rvm/gems/ruby-1.9.3-p125)
+ 9bar
+ 9foo
+ global
+
+
+gemsets for ruby-head (found in /usr/local/rvm/gems/ruby-head)
+ global
+ headbar
+ headfoo
+
+
+gemsets for jruby-1.6.5.1 (found in /usr/local/rvm/gems/jruby-1.6.5.1)
+ global
+ jbar
+ jfoo
+
+
+gemsets for ruby-1.9.2-p180 (found in /usr/local/rvm/gems/ruby-1.9.2-p180)
+ global
+
+
+'''
+ with patch.object(rvm, '_rvm_do', return_value=output):
+ self.assertEqual(
+ {'jruby-1.6.5.1': ['global', 'jbar', 'jfoo'],
+ 'ruby-1.9.2-p180': ['global'],
+ 'ruby-1.9.3-p125': ['9bar', '9foo', 'global'],
+ 'ruby-head': ['global', 'headbar', 'headfoo']},
+ rvm.gemset_list_all())
+
+if __name__ == "__main__":
+ loader = TestLoader()
+ tests = loader.loadTestsFromTestCase(TestRvmModule)
+ TextTestRunner(verbosity=1).run(tests)
View
0 tests/unit/states/__init__.py
No changes.
View
64 tests/unit/states/gem_test.py
@@ -0,0 +1,64 @@
+import sys
+import os
+sys.path.insert(
+ 0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
+
+from saltunittest import TestCase, TestLoader, TextTestRunner
+from mock import MagicMock, patch
+
+import salt.states.gem as gem
+gem.__salt__ = {}
+gem.__opts__ = {'test': False}
+
+
+class TestGemState(TestCase):
+
+ def test_installed(self):
+ gems = ['foo', 'bar']
+ gem_list = MagicMock(return_value=gems)
+ gem_install_succeeds = MagicMock(return_value=True)
+ gem_install_fails = MagicMock(return_value=False)
+
+ with patch.dict(gem.__salt__, {'gem.list': gem_list}):
+ with patch.dict(gem.__salt__,
+ {'gem.install': gem_install_succeeds}):
+ ret = gem.installed('foo')
+ self.assertEqual(True, ret['result'])
+ ret = gem.installed('quux')
+ self.assertEqual(True, ret['result'])
+ gem_install_succeeds.assert_called_once_with(
+ 'quux', None, runas=None)
+
+ with patch.dict(gem.__salt__,
+ {'gem.install': gem_install_fails}):
+ ret = gem.installed('quux')
+ self.assertEqual(False, ret['result'])
+ gem_install_fails.assert_called_once_with(
+ 'quux', None, runas=None)
+
+ def test_removed(self):
+ gems = ['foo', 'bar']
+ gem_list = MagicMock(return_value=gems)
+ gem_uninstall_succeeds = MagicMock(return_value=True)
+ gem_uninstall_fails = MagicMock(return_value=False)
+ with patch.dict(gem.__salt__, {'gem.list': gem_list}):
+ with patch.dict(gem.__salt__,
+ {'gem.uninstall': gem_uninstall_succeeds}):
+ ret = gem.removed('quux')
+ self.assertEqual(True, ret['result'])
+ ret = gem.removed('foo')
+ self.assertEqual(True, ret['result'])
+ gem_uninstall_succeeds.assert_called_once_with(
+ 'foo', None, runas=None)
+
+ with patch.dict(gem.__salt__,
+ {'gem.uninstall': gem_uninstall_fails}):
+ ret = gem.removed('bar')
+ self.assertEqual(False, ret['result'])
+ gem_uninstall_fails.assert_called_once_with(
+ 'bar', None, runas=None)
+
+if __name__ == "__main__":
+ loader = TestLoader()
+ tests = loader.loadTestsFromTestCase(TestGemState)
+ TextTestRunner(verbosity=1).run(tests)
View
88 tests/unit/states/rvm_test.py
@@ -0,0 +1,88 @@
+import sys
+import os
+sys.path.insert(
+ 0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
+
+from saltunittest import TestCase, TestLoader, TextTestRunner
+from mock import MagicMock, patch
+
+import salt.states.rvm as rvm
+rvm.__salt__ = {}
+rvm.__opts__ = {'test': False}
+
+
+class TestRvmState(TestCase):
+
+ def test__check_rvm(self):
+ mock = MagicMock(return_value=True)
+ with patch.dict(
+ rvm.__salt__,
+ {'rvm.is_installed': MagicMock(return_value=False),
+ 'rvm.install': mock}):
+ rvm._check_rvm({'changes': {}})
+ mock.assert_called_once_with()
+
+ def test__check_and_install_ruby(self):
+ mock_check_rvm = MagicMock(
+ return_value={'changes': {}, 'result': True})
+ mock_check_ruby = MagicMock(
+ return_value={'changes': {}, 'result': False})
+ mock_install_ruby = MagicMock(return_value="")
+ with patch.object(rvm, '_check_rvm', new=mock_check_rvm):
+ with patch.object(rvm, '_check_ruby', new=mock_check_ruby):
+ with patch.dict(rvm.__salt__,
+ {'rvm.install_ruby': mock_install_ruby}):
+ rvm._check_and_install_ruby({'changes': {}}, '1.9.3')
+ mock_install_ruby.assert_called_once_with('1.9.3', runas=None)
+
+ def test__check_ruby(self):
+ mock = MagicMock(return_value=[['ruby', '1.9.3-p125', False],
+ ['jruby', '1.6.5.1', True]])
+ with patch.dict(rvm.__salt__, {'rvm.list': mock}):
+ for ruby, result in {'1.9.3': True,
+ 'ruby-1.9.3': True,
+ 'ruby-1.9.3-p125': True,
+ '1.9.3-p125': True,
+ '1.9.3-p126': False,
+ 'rbx': False,
+ 'jruby': True,
+ 'jruby-1.6.5.1': True,
+ 'jruby-1.6': False,
+ 'jruby-1.9.3': False,
+ 'jruby-1.9.3-p125': False}.iteritems():
+ ret = rvm._check_ruby({'changes': {}, 'result': False}, ruby)
+ self.assertEqual(result, ret['result'])
+
+ def test_gemset_present(self):
+ with patch.object(rvm, '_check_rvm',
+ return_value={'result': True, 'changes': {}}):
+ gems = ['global', 'foo', 'bar']
+ gemset_list = MagicMock(return_value=gems)
+ gemset_create = MagicMock(return_value=True)
+ check_ruby = MagicMock(
+ return_value={'result': False, 'changes': {}})
+ with patch.object(rvm, '_check_ruby', new=check_ruby):
+ with patch.dict(rvm.__salt__,
+ {'rvm.gemset_list': gemset_list,
+ 'rvm.gemset_create': gemset_create}):
+ ret = rvm.gemset_present('foo')
+ self.assertEqual(True, ret['result'])
+
+ ret = rvm.gemset_present('quux')
+ self.assertEqual(True, ret['result'])
+ gemset_create.assert_called_once_with(
+ 'default', 'quux', runas=None)
+
+ def test_installed(self):
+ mock = MagicMock()
+ with patch.object(rvm, '_check_rvm', return_value={'result': True}):
+ with patch.object(rvm, '_check_and_install_ruby', new=mock):
+ rvm.installed("1.9.3", default=True)
+ mock.assert_called_once_with(
+ {'result': True}, '1.9.3', True, runas=None)
+
+
+if __name__ == "__main__":
+ loader = TestLoader()
+ tests = loader.loadTestsFromTestCase(TestRvmState)
+ TextTestRunner(verbosity=1).run(tests)
View
0 tests/unit/templates/jinja.py → tests/unit/templates/jinja_test.py
File renamed without changes.

0 comments on commit 6bcc64d

Please sign in to comment.