2014-07-11 19:54:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Nicole Thomas <nicole@saltstack.com>
|
2014-07-11 19:54:35 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-07-11 19:54:35 +00:00
|
|
|
# Import Salt Libs
|
|
|
|
from salt.cli.batch import Batch
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
|
2014-07-11 19:54:35 +00:00
|
|
|
|
|
|
|
|
2014-08-07 22:59:06 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2014-07-11 19:54:35 +00:00
|
|
|
class BatchTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Unit Tests for the salt.cli.batch module
|
|
|
|
'''
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-03-24 17:22:57 +00:00
|
|
|
opts = {'batch': '',
|
|
|
|
'conf_file': {},
|
|
|
|
'tgt': '',
|
|
|
|
'transport': '',
|
|
|
|
'timeout': 5,
|
|
|
|
'gather_job_timeout': 5}
|
|
|
|
|
2014-07-11 21:38:34 +00:00
|
|
|
mock_client = MagicMock()
|
|
|
|
with patch('salt.client.get_local_client', MagicMock(return_value=mock_client)):
|
|
|
|
with patch('salt.client.LocalClient.cmd_iter', MagicMock(return_value=[])):
|
|
|
|
self.batch = Batch(opts, quiet='quiet')
|
2014-07-11 19:54:35 +00:00
|
|
|
|
|
|
|
# get_bnum tests
|
|
|
|
|
|
|
|
def test_get_bnum(self):
|
|
|
|
'''
|
|
|
|
Tests passing batch value as a number
|
|
|
|
'''
|
2015-04-06 21:04:22 +00:00
|
|
|
self.batch.opts = {'batch': '2', 'timeout': 5}
|
2014-07-11 19:54:35 +00:00
|
|
|
self.batch.minions = ['foo', 'bar']
|
|
|
|
self.assertEqual(Batch.get_bnum(self.batch), 2)
|
|
|
|
|
|
|
|
def test_get_bnum_percentage(self):
|
|
|
|
'''
|
|
|
|
Tests passing batch value as percentage
|
|
|
|
'''
|
2015-04-06 21:04:22 +00:00
|
|
|
self.batch.opts = {'batch': '50%', 'timeout': 5}
|
2014-07-11 19:54:35 +00:00
|
|
|
self.batch.minions = ['foo']
|
|
|
|
self.assertEqual(Batch.get_bnum(self.batch), 1)
|
|
|
|
|
|
|
|
def test_get_bnum_high_percentage(self):
|
|
|
|
'''
|
|
|
|
Tests passing batch value as percentage over 100%
|
|
|
|
'''
|
2015-04-06 21:04:22 +00:00
|
|
|
self.batch.opts = {'batch': '160%', 'timeout': 5}
|
2014-07-11 19:54:35 +00:00
|
|
|
self.batch.minions = ['foo', 'bar', 'baz']
|
|
|
|
self.assertEqual(Batch.get_bnum(self.batch), 4)
|
|
|
|
|
|
|
|
def test_get_bnum_invalid_batch_data(self):
|
|
|
|
'''
|
|
|
|
Tests when an invalid batch value is passed
|
|
|
|
'''
|
|
|
|
ret = Batch.get_bnum(self.batch)
|
|
|
|
self.assertEqual(ret, None)
|