Exemplo n.º 1
0
    def test_upload_dir_to_selected_upload_area_recursive(self, transfer, os, get_selected_area):
        # given
        get_selected_area.return_value = 'selected'
        path_map = self.path_map

        os.path.getsize = lambda path: path_map[path].get('getsize')
        os.path.isfile = lambda path: path_map[path].get('isfile')
        os.path.isdir = lambda path: path_map[path].get('isdir')
        os.path.abspath = lambda path: path
        os.path.join = lambda path, file: f'{path}/{file}'

        os.listdir = lambda path: path_map[path].get('listdir')

        transfer.side_effect = mock_transfer

        args = Mock()
        args.PATH = ['dir1']
        args.a = None
        args.r = True

        # when
        cmd = CmdUpload(self.aws_mock, args)
        success, msg = cmd.run()

        # then
        self.assertTrue(success)
        transfer.assert_called_once()
        uploaded_files = [f.path for f in cmd.files]
        expected_files = ['dir1/file1', 'dir1/file2', 'dir1/dir2/file3'] if DIR_SUPPORT else ['dir1/file1', 'dir1/file2']
        self.assertEqual(uploaded_files, expected_files)
Exemplo n.º 2
0
    def test_upload_file_no_upload_area_selected(self, get_selected_area):
        # given
        get_selected_area.return_value = None
        args = MagicMock()

        # when
        success, msg = CmdUpload(self.aws_mock, args).run()

        # then
        self.assertFalse(success)
        self.assertEqual(msg, 'No area selected')
Exemplo n.º 3
0
    def test_upload_inexisting_file(self):
        # given
        args = ['upload', 'inexisting-file.txt']

        # when
        with self.assertRaises(SystemExit) as error:
            parsed_args = parse_args(args)
            success, msg = CmdUpload(self.aws_mock, parsed_args)
            self.assertFalse(parsed_args)
            self.assertFalse(success)
            self.assertFalse(msg)

        self.assertEqual(error.exception.code, 2)
Exemplo n.º 4
0
    def test_upload_dir_to_selected_upload_area_no_overwrite(self, transfer_progress, os, get_selected_area):
        # given
        get_selected_area.return_value = 'selected/'
        path_map = self.path_map

        os.path.getsize = lambda path: path_map[path].get('getsize')
        os.path.isfile = lambda path: path_map[path].get('isfile')
        os.path.isdir = lambda path: path_map[path].get('isdir')
        os.path.abspath = lambda path: path
        os.path.join = lambda path, file: f'{path}/{file}'

        os.listdir = lambda path: path_map[path].get('listdir')

        def mock_file_transfer(f):
            f.successful = True
            f.complete = True

        transfer_progress.side_effect = mock_file_transfer

        args = Mock()
        args.PATH = ['dir1']
        args.r = True
        args.o = False

        existing_key_map = {
            'selected/file1': True,
            'selected/file2': False,
            'selected/file3': False
        }

        self.aws_mock.obj_exists = lambda key: existing_key_map.get(key, False)

        # when
        cmd = CmdUpload(self.aws_mock, args)
        success, msg = cmd.run()

        # then
        self.assertTrue(success)

        uploaded_files_map = {}
        for f in cmd.files:
            uploaded_files_map[f.path] = f

        expected_files = ['dir1/file1', 'dir1/file2', 'dir1/dir2/file3'] if DIR_SUPPORT else ['dir1/file1', 'dir1/file2']
        expected_count = 2 if DIR_SUPPORT else 1

        self.assertEqual(list(uploaded_files_map.keys()), expected_files)
        self.assertEqual(self.upload_file.call_count, expected_count, 'Should not overwrite files')
Exemplo n.º 5
0
    def test_upload_file_to_selected_upload_area_with_exception(self, transfer_progress, os, get_selected_area):
        # given
        get_selected_area.return_value = 'selected/'
        path_map = self.path_map

        os.path.getsize = lambda path: path_map[path].get('getsize')
        os.path.isfile = lambda path: path_map[path].get('isfile')
        os.path.isdir = lambda path: path_map[path].get('isdir')
        os.path.abspath = lambda path: path
        os.path.join = lambda path, file: f'{path}/{file}'

        os.path.basename.return_value = 'file0'

        os.listdir = lambda path: path_map[path].get('listdir')

        def mock_transfer_progress(f):
            f.successful = True
            f.complete = True

        transfer_progress.side_effect = mock_transfer_progress
        self.upload_file.side_effect = Mock(side_effect=Exception('Test'))

        args = Mock()
        args.PATH = ['file0']
        args.r = True
        args.o = False

        existing_key_map = {
            'selected/file0': False
        }

        self.aws_mock.obj_exists = lambda key: existing_key_map.get(key, False)

        # when
        cmd = CmdUpload(self.aws_mock, args)
        success, msg = cmd.run()

        # then
        self.assertFalse(success)

        uploaded_files_map = {}
        for f in cmd.files:
            uploaded_files_map[f.path] = f

        self.assertEqual(list(uploaded_files_map.keys()), [])
Exemplo n.º 6
0
    def test_upload_file_to_selected_upload_area_duplicate_path(self, transfer, os_path, get_selected_area):
        # given
        get_selected_area.return_value = 'selected'

        os_path.get_size.return_value = 'size'
        os_path.isfile.return_value = True
        os_path.isdir.return_value = False
        os_path.basename.return_value = 'filename'
        os_path.abspath = lambda path: 'abs' + path

        transfer.side_effect = mock_transfer

        args = MagicMock()
        args.PATH = ['filename', 'filename']

        # when
        cmd = CmdUpload(self.aws_mock, args)
        success, msg = cmd.run()

        # then
        self.assertTrue(success)
        transfer.assert_called_once()
        uploaded_files = [f.path for f in cmd.files]
        self.assertEqual(uploaded_files, ['absfilename'])
Exemplo n.º 7
0
    def execute(self, args):
        if args.command == 'create':
            success, msg = CmdCreate(self.aws, args).run()
            self.exit(success, msg)

        elif args.command == 'select':
            success, msg = CmdSelect(self.aws, args).run()
            self.exit(success, msg)

        elif args.command == 'list':
            success, msg = CmdList(self.aws, args).run()
            self.exit(success, msg)

        elif args.command == 'upload':
            success, msg = CmdUpload(self.aws, args).run()
            self.exit(success, msg)

        elif args.command == 'download':
            success, msg = CmdDownload(self.aws, args).run()
            self.exit(success, msg)

        elif args.command == 'delete':
            success, msg = CmdDelete(self.aws, args).run()
            self.exit(success, msg)