def test_minion_event_no_id(self): me = event.MinionEvent(dict(sock_dir=SOCK_DIR), listen=False) id_hash = hashlib.sha256(to_bytes('')).hexdigest()[:10] self.assertEqual( me.puburi, '{0}'.format( os.path.join(SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash)))) self.assertEqual( me.pulluri, '{0}'.format( os.path.join(SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash))))
def test_minion_event(self): opts = dict(id='foo', sock_dir=SOCK_DIR) id_hash = hashlib.sha256(to_bytes(opts['id'])).hexdigest()[:10] me = event.MinionEvent(opts, listen=False) self.assertEqual( me.puburi, '{0}'.format( os.path.join(SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash)))) self.assertEqual( me.pulluri, '{0}'.format( os.path.join(SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash))))
def test_to_bytes(self): for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None): self.assertRaises(TypeError, utils.to_bytes, x) if six.PY3: self.assertEqual(utils.to_bytes('xyzzy'), b'xyzzy') ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29)) un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string self.assertEqual(utils.to_bytes(ut), ut) self.assertEqual(utils.to_bytes(bytearray(ut)), ut) self.assertEqual(utils.to_bytes(un, 'utf-8'), ut) else: self.assertEqual(utils.to_bytes('xyzzy'), 'xyzzy') ut = ''.join([chr(x) for x in (0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29)]) un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string self.assertEqual(utils.to_bytes(ut), ut) self.assertEqual(utils.to_bytes(bytearray(ut)), ut) self.assertEqual(utils.to_bytes(un, 'utf-8'), ut)
def test_minion_event_no_id(self): me = event.MinionEvent(dict(sock_dir=SOCK_DIR), listen=False) id_hash = hashlib.sha256(to_bytes('')).hexdigest()[:10] self.assertEqual( me.puburi, '{0}'.format( os.path.join( SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash) ) ) ) self.assertEqual( me.pulluri, '{0}'.format( os.path.join( SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash) ) ) )
def test_minion_event(self): opts = dict(id='foo', sock_dir=SOCK_DIR) id_hash = hashlib.sha256(to_bytes(opts['id'])).hexdigest()[:10] me = event.MinionEvent(opts, listen=False) self.assertEqual( me.puburi, '{0}'.format( os.path.join( SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash) ) ) ) self.assertEqual( me.pulluri, '{0}'.format( os.path.join( SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash) ) ) )
def run_chunked(self): ''' Make the salt client call in the new fasion chunked multi-call way ''' files, empty_dirs = self._list_files() dest = self.opts['dest'] gzip = self.opts['gzip'] tgt = self.opts['tgt'] timeout = self.opts['timeout'] selected_target_option = self.opts.get('selected_target_option') dest_is_dir = bool(empty_dirs) \ or len(files) > 1 \ or bool(re.search(r'[\\/]$', dest)) reader = salt.utils.gzip_util.compress_file \ if gzip \ else salt.utils.itertools.read_file minions = salt.utils.minions.CkMinions(self.opts).check_minions( tgt, tgt_type=selected_target_option or 'glob') local = salt.client.get_local_client(self.opts['conf_file']) def _get_remote_path(fn_): if fn_ in self.opts['src']: # This was a filename explicitly passed on the CLI return os.path.join(dest, os.path.basename(fn_)) \ if dest_is_dir \ else dest else: for path in self.opts['src']: relpath = os.path.relpath(fn_, path + os.sep) if relpath.startswith(parent): # File is not within this dir continue return os.path.join(dest, os.path.basename(path), relpath) else: # pylint: disable=useless-else-on-loop # Should not happen log.error('Failed to find remote path for %s', fn_) return None ret = {} parent = '..' + os.sep for fn_, mode in six.iteritems(files): remote_path = _get_remote_path(fn_) index = 1 failed = {} for chunk in reader(fn_, chunk_size=self.opts['salt_cp_chunk_size']): chunk = base64.b64encode(to_bytes(chunk)) append = index > 1 log.debug( 'Copying %s to %starget \'%s\' as %s%s', fn_, '{0} '.format(selected_target_option) if selected_target_option else '', tgt, remote_path, ' (chunk #{0})'.format(index) if append else '') args = [ tgt, 'cp.recv_chunked', [remote_path, chunk, append, gzip, mode], timeout, ] if selected_target_option is not None: args.append(selected_target_option) result = local.cmd(*args) if not result: # Publish failed msg = ('Publish failed.{0} It may be necessary to ' 'decrease salt_cp_chunk_size (current value: ' '{1})'.format( ' File partially transferred.' if index > 1 else '', self.opts['salt_cp_chunk_size'], )) for minion in minions: ret.setdefault(minion, {})[remote_path] = msg break for minion_id, minion_ret in six.iteritems(result): ret.setdefault(minion_id, {})[remote_path] = minion_ret # Catch first error message for a given minion, we will # rewrite the results after we're done iterating through # the chunks. if minion_ret is not True and minion_id not in failed: failed[minion_id] = minion_ret index += 1 for minion_id, msg in six.iteritems(failed): ret[minion_id][remote_path] = msg for dirname in empty_dirs: remote_path = _get_remote_path(dirname) log.debug( 'Creating empty dir %s on %starget \'%s\'', dirname, '{0} '.format(selected_target_option) if selected_target_option else '', tgt, ) args = [tgt, 'cp.recv_chunked', [remote_path, None], timeout] if selected_target_option is not None: args.append(selected_target_option) for minion_id, minion_ret in six.iteritems(local.cmd(*args)): ret.setdefault(minion_id, {})[remote_path] = minion_ret return ret