def test_push(self): ''' Test if push works with good posix path. ''' filename = '/saltines/test.file' with patch('salt.modules.cp.os.path', MagicMock(isfile=Mock(return_value=True), wraps=cp.os.path)), \ patch.multiple('salt.modules.cp', _auth=MagicMock(**{'return_value.gen_token.return_value': 'token'}), __opts__={'id': 'abc', 'file_buffer_size': 10}), \ patch('salt.utils.files.fopen', mock_open(read_data=b'content')) as m_open, \ patch('salt.transport.Channel.factory', MagicMock()): response = cp.push(filename) assert response, response num_opens = len(m_open.filehandles[filename]) assert num_opens == 1, num_opens fh_ = m_open.filehandles[filename][0] assert fh_.read.call_count == 2, fh_.read.call_count salt.transport.Channel.factory({}).send.assert_called_once_with( dict( loc=fh_.tell(), # pylint: disable=resource-leakage cmd='_file_recv', tok='token', path=['saltines', 'test.file'], data= b'', # data is empty here because load['data'] is overwritten id='abc'))
def test_push_success(self): ''' Test if push succeeds. ''' path = '/srv/salt/saltines' file_data = '' mock_buf_size = len(file_data) mock_id = 'You don\'t need to see his identification.' ret = True class MockChannel(object): @staticmethod def factory(__opts__): return MockChannel() def send(self, load): return 'channel info' class MockAuth(object): def gen_token(self, salt): return 'token info' def mock_auth_factory(): return MockAuth() with patch('salt.transport.Channel', MockChannel): with patch('salt.modules.cp._auth', mock_auth_factory): with patch('salt.utils.fopen', mock_open(read_data=file_data)): with patch.dict(cp.__opts__, {'file_buffer_size': mock_buf_size, 'id': mock_id}): self.assertEqual(cp.push(path), ret)
def test_push_non_absolute_path(): """ Test if push fails on a non absolute path. """ path = "../saltines" ret = False assert cp.push(path) == ret
def test_push_non_absolute_path(self): ''' Test if push fails on a non absolute path. ''' path = '../saltines' ret = False self.assertEqual(cp.push(path), ret)
def test_push_non_file(self): ''' Test if push fails on a non file. ''' path = '/srv/salt/saltines' ret = False self.assertEqual(cp.push(path), ret)
def test_push_non_absolute_path(self): """ Test if push fails on a non absolute path. """ path = "../saltines" ret = False self.assertEqual(cp.push(path), ret)
def test_push(self): ''' Test if push works with good posix path. ''' response = cp.push('/saltines/test.file') self.assertEqual(response, True) self.assertEqual(salt.utils.fopen().read.call_count, 2) salt.transport.Channel.factory({}).send.assert_called_once_with( dict( loc=salt.utils.fopen().tell(), cmd='_file_recv', tok='token', path=['saltines', 'test.file'], data= '', # data is empty here because load['data'] is overwritten id='abc'))
def test_push(self): """ Test if push works with good posix path. """ filename = "/saltines/test.file" if salt.utils.platform.is_windows(): filename = "C:\\saltines\\test.file" with patch( "salt.modules.cp.os.path", MagicMock(isfile=Mock(return_value=True), wraps=cp.os.path), ), patch( "salt.modules.cp.os.path", MagicMock(getsize=MagicMock(return_value=10), wraps=cp.os.path), ), patch.multiple( "salt.modules.cp", _auth=MagicMock( **{"return_value.gen_token.return_value": "token"}), __opts__={ "id": "abc", "file_buffer_size": 10 }, ), patch("salt.utils.files.fopen", mock_open(read_data=b"content")) as m_open, patch( "salt.channel.client.ReqChannel.factory", MagicMock()) as req_channel_factory_mock: response = cp.push(filename) assert response, response num_opens = len(m_open.filehandles[filename]) assert num_opens == 1, num_opens fh_ = m_open.filehandles[filename][0] assert fh_.read.call_count == 2, fh_.read.call_count req_channel_factory_mock().__enter__( ).send.assert_called_once_with( dict( loc=fh_.tell(), # pylint: disable=resource-leakage cmd="_file_recv", tok="token", path=["saltines", "test.file"], size=10, data= b"", # data is empty here because load['data'] is overwritten id="abc", ))