From 93129d8758c60b03fe1057836a1b0b8693755028 Mon Sep 17 00:00:00 2001 From: Mike Place Date: Thu, 12 Dec 2013 17:04:40 -0700 Subject: [PATCH] Laying the groundwork for overstate unit testing. --- tests/unit/overstate_test.py | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/unit/overstate_test.py diff --git a/tests/unit/overstate_test.py b/tests/unit/overstate_test.py new file mode 100644 index 0000000000..df71f11477 --- /dev/null +++ b/tests/unit/overstate_test.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +''' + :codauthor: :email:`Mike Place ` +''' + +# Import Salt Testing libs +from salttesting import skipIf, TestCase +from salttesting.helpers import ensure_in_syspath +from salttesting.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch, mock_open, call +ensure_in_syspath('../') + +# Import salt libs +import salt.overstate + +opts = {'file_roots': {}, + 'conf_file': ''} + +overstate_sls = { + 'mysql': { + 'match': 'db*', + 'sls': { + 'mysql.server', 'drbd' + } + }, + 'webservers': { + 'match': 'web*', + 'require': ['mysql'] + }, + 'all': { + 'match': '*', + 'require': { + 'mysql', 'webservers' + } + } + +} + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class OverstateTestCase(TestCase): + @patch('salt.client.LocalClient.cmd') + def test__stage_list(self, local_client_mock): + overstate = salt.overstate.OverState(opts) + overstate._stage_list(['test1', 'test2']) + local_client_mock.assert_called_with('test1 or test2', 'test.ping', expr_form='compound') + + # @skipIf(True, "Do not run this test! It exposes a memory leak inside pyyaml: https://bitbucket.org/xi/pyyaml/issue/24") + # def test__names(self): + # y = 'a' + # mopen = mock_open(read_data=y) + # with patch('salt.utils.fopen', mopen, create=True): + # overstate = salt.overstate.OverState(opts, overstate='a') + + def test__names(self): + overstate = salt.overstate.OverState(opts) + overstate.over = overstate._OverState__sort_stages(overstate_sls) + self.assertEqual({'webservers', 'all', 'mysql'}, overstate._names()) + + def test_get_stage(self): + overstate = salt.overstate.OverState(opts) + overstate.over = overstate._OverState__sort_stages(overstate_sls) + ret = overstate.get_stage('mysql') + self.assertDictEqual({'mysql': {'match': 'db*', 'sls': {'drbd', 'mysql.server'}}}, ret) + + @patch('salt.overstate.OverState.call_stage') + def test_stages(self, call_stage_mock): + ''' + This is a very basic test and needs expansion, since call_stage is mocked! + ''' + overstate = salt.overstate.OverState(opts) + overstate.over = overstate._OverState__sort_stages(overstate_sls) + overstate.stages() + expected_calls = [call('all', {'require': {'webservers', 'mysql'}, 'match': '*'}), + call('mysql', {'match': 'db*', 'sls': {'drbd', 'mysql.server'}}), + call('webservers', {'require': ['mysql'], 'match': 'web*'})] + call_stage_mock.assert_has_calls(expected_calls, any_order=False) + + # @patch('salt.overstate.OverState.call_stage') + # def test_call_stage(self, call_stage_mock): + # overstate = salt.overstate.OverState(opts) + # overstate.over = overstate._OverState__sort_stages(overstate_sls) + # overstate.call_stage('all', {'require': {'webservers', 'mysql'}, 'match': '*'}) + # overstate.call_stage('mysql', {'match': 'db*', 'sls': {'drbd', 'mysql.server'}}) + # overstate.call_stage({'require': ['mysql'], 'match': 'web*'})