# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import

# Import Salt Libs
import integration

# Import Salt Testing Libs
from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')


class BatchTest(integration.ShellCase):
    '''
    Integration tests for the salt.cli.batch module
    '''

    def test_batch_run(self):
        '''
        Tests executing a simple batch command to help catch regressions
        '''
        ret = ['',
               "Executing run on ['sub_minion']",
               '',
               'sub_minion:',
               'retcode:',
               '    0',
               '    batch testing',
               '',
               "Executing run on ['minion']",
               '',
               'minion:',
               'retcode:',
               '    0',
               '    batch testing']
        ret = sorted(ret)
        cmd = sorted(self.run_salt('\'*\' test.echo \'batch testing\' -b 50%'))
        self.assertListEqual(cmd, ret)

    def test_batch_run_number(self):
        '''
        Tests executing a simple batch command using a number division instead of
        a percentage with full batch CLI call.
        '''
        ret = ['',
               "Executing run on ['sub_minion', 'minion']",
               '',
               'retcode:',
               '    0',
               'sub_minion:',
               '    True',
               'minion:',
               '    True',
               'retcode:',
               '    0']
        cmd = sorted(self.run_salt('\'*\' test.ping --batch-size 2'))
        self.assertListEqual(cmd, sorted(ret))

    def test_batch_run_grains_targeting(self):
        '''
        Tests executing a batch command using a percentage divisor as well as grains
        targeting.
        '''
        os_grain = ''
        ret = ['',
               "Executing run on ['sub_minion']",
               '',
               'retcode:',
               '    0',
               'sub_minion:',
               '    True',
               '',
               "Executing run on ['minion']",
               '',
               'minion:',
               '    True',
               'retcode:',
               '    0']

        for item in self.run_salt('minion grains.get os'):
            if item != 'minion':
                os_grain = item

        os_grain = os_grain.strip()
        cmd = sorted(self.run_salt('-G \'os:{0}\' -b 25% test.ping'.format(os_grain)))
        self.assertListEqual(cmd, sorted(ret))


if __name__ == '__main__':
    from integration import run_tests
    run_tests(BatchTest)

@@ -153,47 +153,8 @@
 rt%0A%0A
-# Import Salt Libs%0Aimport integration%0A%0A
 # Im
@@ -172,24 +172,24 @@
 esting Libs%0A
+
 from salttes
@@ -256,16 +256,82 @@
 /../')%0A%0A
+# Import Salt Libs%0Aimport integration%0Aimport salt.ext.six as six%0A%0A
 %0Aclass B
@@ -965,34 +965,8 @@
 g'%5D%0A
-        ret = sorted(ret)%0A
     
@@ -967,39 +967,32 @@
 %5D%0A        cmd = 
-sorted(
 self.run_salt('%5C
@@ -1032,18 +1032,99 @@
 -b 50%25')
-)
 %0A
+        if six.PY3:%0A            self.assertCountEqual(cmd, ret)%0A        else:%0A    
         
@@ -1144,25 +1144,41 @@
 stEqual(
+sorted(
 cmd
+)
 , 
+sorted(
 ret)
+)
 %0A%0A    de
@@ -1677,39 +1677,32 @@
 %5D%0A        cmd = 
-sorted(
 self.run_salt('%5C
@@ -1732,18 +1732,99 @@
 size 2')
-)
 %0A
+        if six.PY3:%0A            self.assertCountEqual(cmd, ret)%0A        else:%0A    
         
@@ -1836,35 +1836,43 @@
 assertListEqual(
+sorted(
 cmd
+)
 , sorted(ret))%0A%0A
@@ -2628,23 +2628,16 @@
   cmd = 
-sorted(
 self.run
@@ -2692,18 +2692,99 @@
 _grain))
-)
 %0A
+        if six.PY3:%0A            self.assertCountEqual(cmd, ret)%0A        else:%0A    
         
@@ -2804,19 +2804,27 @@
 stEqual(
+sorted(
 cmd
+)
 , sorted

Update batch integration tests to not be flaky in Python3