From 00b8c3dc70269f8058f4661ff5f8ad8ac7497c09 Mon Sep 17 00:00:00 2001 From: rallytime Date: Thu, 27 Feb 2014 17:03:08 -0700 Subject: [PATCH] Started writing brew unit tests --- salt/modules/brew.py | 2 +- tests/unit/modules/brew_test.py | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/unit/modules/brew_test.py diff --git a/salt/modules/brew.py b/salt/modules/brew.py index 84255344ab..9e7c58d1b5 100644 --- a/salt/modules/brew.py +++ b/salt/modules/brew.py @@ -29,7 +29,7 @@ def __virtual__(): def _list_taps(): ''' - List currently + List currently installed brew taps ''' cmd = 'brew tap' return __salt__['cmd.run'](cmd, output_loglevel='debug').splitlines() diff --git a/tests/unit/modules/brew_test.py b/tests/unit/modules/brew_test.py new file mode 100644 index 0000000000..bdd3c9a599 --- /dev/null +++ b/tests/unit/modules/brew_test.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +''' + :codeauthor: :email:`Nicole Thomas ` +''' + +# Import Salt Testing Libs +from salttesting import TestCase, skipIf +from salttesting.mock import MagicMock, patch +from salttesting.helpers import ensure_in_syspath + +ensure_in_syspath('../../') + +# Import Salt Libs +from salt.modules import brew + +# Global Variables +brew.__salt__ = {} + +taps_string = 'homebrew/dupes\nhomebrew/science\nhomebrew/x11' +taps_list = ['homebrew/dupes', 'homebrew/science', 'homebrew/x11'] + + +class BrewTestCase(TestCase): + ''' + TestCase for salt.modules.brew module + ''' + + def test_list_taps(self): + ''' + Tests the return of the list of taps + ''' + mock_taps = MagicMock(return_value=taps_string) + with patch.dict(brew.__salt__, {'cmd.run': mock_taps}): + self.assertEqual(brew._list_taps(), taps_list) + + @patch('salt.modules.brew._list_taps', MagicMock(return_value=taps_list)) + def test_tap_installed(self): + ''' + Tests if tap argument is already installed or not + ''' + self.assertTrue(brew._tap('homebrew/science')) + + @patch('salt.modules.brew._list_taps', MagicMock(return_value={})) + def test_tap_failure(self): + ''' + Tests if the tap installation failed + ''' + mock_failure = MagicMock(return_value=1) + with patch.dict(brew.__salt__, {'cmd.retcode': mock_failure}): + self.assertFalse(brew._tap('homebrew/test')) + + @patch('salt.modules.brew._list_taps', MagicMock(return_value=taps_list)) + def test_tap(self): + ''' + Tests adding unofficial Github repos to the list of brew taps + ''' + mock_success = MagicMock(return_value=0) + with patch.dict(brew.__salt__, {'cmd.retcode': mock_success}): + self.assertTrue(brew._tap('homebrew/test')) + + def test_homebrew_bin(self): + ''' + Tests the path to the homebrew binary + ''' + mock_path = MagicMock(return_value='/usr/local') + with patch.dict(brew.__salt__, {'cmd.run': mock_path}): + self.assertEqual(brew._homebrew_bin(), '/usr/local/bin/brew') + + +if __name__ == '__main__': + from integration import run_tests + run_tests(BrewTestCase, needs_daemon=False)