def test_set_present(self): ''' Test to verify the chain is exist. ''' name = 'salt' set_type = {'bitmap': '192.168.0.3'} ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} mock = MagicMock(side_effect=[True, False, False, True, False, '']) with patch.dict(ipset.__salt__, {'ipset.check_set': mock, 'ipset.new_set': mock}): comt = ('ipset set {0} already exist for ipv4'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(ipset.set_present(name, set_type), ret) with patch.dict(ipset.__opts__, {'test': True}): comt = ('ipset set {0} needs to added for ipv4'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(ipset.set_present(name, set_type), ret) with patch.dict(ipset.__opts__, {'test': False}): comt = ('ipset set {0} created successfully for ipv4' .format(name)) ret.update({'comment': comt, 'result': True, 'changes': {'locale': 'salt'}}) self.assertDictEqual(ipset.set_present(name, set_type), ret) comt = ('Failed to create salt set: for ipv4') ret.update({'comment': comt, 'result': False, 'changes': {}}) self.assertDictEqual(ipset.set_present(name, set_type), ret)
def _runner( self, expected_ret, test=False, check_set=False, new_set=None, new_set_assertion=True, ): mock_check_set = MagicMock(return_value=check_set) mock_new_set = (MagicMock() if new_set is None else MagicMock( return_value=new_set)) with patch.dict( ipset.__salt__, { "ipset.check_set": mock_check_set, "ipset.new_set": mock_new_set }, ): with patch.dict(ipset.__opts__, {"test": test}): actual_ret = ipset.set_present(self.fake_name, self.fake_set_type) mock_check_set.assert_called_once_with(self.fake_name) if new_set_assertion: mock_new_set.assert_called_once_with(self.fake_name, self.fake_set_type, "ipv4") else: self.assertTrue(mock_new_set.call_count == 0) self.assertDictEqual(actual_ret, expected_ret)
def _runner(self, expected_ret, test=False, check_set=False, new_set=None, new_set_assertion=True): mock_check_set = MagicMock(return_value=check_set) mock_new_set = MagicMock() if new_set is None else MagicMock(return_value=new_set) with patch.dict(ipset.__salt__, {'ipset.check_set': mock_check_set, 'ipset.new_set': mock_new_set}): with patch.dict(ipset.__opts__, {'test': test}): actual_ret = ipset.set_present(self.fake_name, self.fake_set_type) mock_check_set.assert_called_once_with(self.fake_name) if new_set_assertion: mock_new_set.assert_called_once_with(self.fake_name, self.fake_set_type, 'ipv4') else: mock_new_set.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)