def test_sr_clean_test(self):
        '''
        Test to check sr_clean when hana is not disabled
        node in test mode
        '''
        name = 'pdr'

        ret = {
            'name': name,
            'changes': {
                'disabled': name
            },
            'result': None,
            'comment': '{} would be clean'.format(name)
        }

        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=True)
        mock_state = MagicMock(return_value='PRIMARY')
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state
                }):
            with patch.dict(hanamod.__opts__, {'test': True}):
                assert hanamod.sr_clean('pdr', '00', 'pass', True) == ret
    def test_sr_clean(self):
        '''
        Test to check sr_clean when hana is already disabled
        node
        '''
        name = 'pdr'

        ret = {
            'name': name,
            'changes': {},
            'result': True,
            'comment': 'HANA node already clean'
        }

        mock_installed = MagicMock(return_value=True)
        with patch.dict(hanamod.__salt__,
                        {'hana.is_installed': mock_installed}):
            mock_running = MagicMock(return_value=True)
            mock_state = MagicMock(return_value='DISABLED')

            with patch.dict(hanamod.__salt__, {
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state
            }):
                assert hanamod.sr_clean('pdr', '00', 'pass', True) == ret
    def test_sr_clean_error(self):
        '''
        Test to check sr_clean when hana is already not disabled
        node and some hana command fail
        '''
        name = 'pdr'

        ret = {
            'name': name,
            'changes': {},
            'result': False,
            'comment': 'hana command error'
        }

        mock_insatlled = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=False)
        state = MagicMock()
        mock_state = MagicMock(return_value=state)
        mock_clean = MagicMock(
            side_effect=exceptions.CommandExecutionError('hana command error'))
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_insatlled,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.sr_cleanup': mock_clean
                }):
            assert hanamod.sr_clean('pdr', '00', 'pass', True) == ret
            mock_clean.assert_called_once_with(sid='pdr',
                                               inst='00',
                                               password='******',
                                               force=True)
    def test_sr_clean_not_installed(self):
        '''
        Test to check sr_clean when hana is not installed
        '''
        name = 'pdr'

        ret = {
            'name': name,
            'changes': {},
            'result': False,
            'comment': 'HANA is not installed properly with the provided data'
        }

        mock_installed = MagicMock(return_value=False)
        with patch.dict(hanamod.__salt__,
                        {'hana.is_installed': mock_installed}):
            assert hanamod.sr_clean('pdr', '00', 'pass', True) == ret
    def test_sr_clean_basic(self, mock_disabled):
        '''
        Test to check sr_clean when hana is already set as secondary
        node with basic setup
        '''
        name = 'SITE1'

        ret = {
            'name': name,
            'changes': {
                'disabled': name
            },
            'result': True,
            'comment': 'HANA node set as {}'.format('DISABLED')
        }

        mock_disabled.DISABLED.name = 'DISABLED'
        mock_installed = MagicMock(return_value=True)
        mock_running = MagicMock(return_value=True)
        state = MagicMock()
        state_disabled = MagicMock()
        state_disabled.name = 'DISABLED'
        mock_state = MagicMock(side_effect=[state, state_disabled])
        mock_stop = MagicMock()
        mock_clean = MagicMock()
        with patch.dict(
                hanamod.__salt__, {
                    'hana.is_installed': mock_installed,
                    'hana.is_running': mock_running,
                    'hana.get_sr_state': mock_state,
                    'hana.stop': mock_stop,
                    'hana.sr_cleanup': mock_clean
                }):
            assert hanamod.sr_clean(name, True, 'pdr', '00', 'pass') == ret
            mock_stop.assert_called_once_with(sid='pdr',
                                              inst='00',
                                              password='******')
            mock_clean.assert_called_once_with(force=True,
                                               sid='pdr',
                                               inst='00',
                                               password='******')