Path: blob/develop/tests/unit/customizations/s3/syncstrategy/test_caseconflict.py
2634 views
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02import pytest34from awscli.customizations.s3.filegenerator import FileStat5from awscli.customizations.s3.syncstrategy.caseconflict import (6CaseConflictException,7CaseConflictSync,8)9from awscli.testutils import skip_if_case_sensitive101112@pytest.fixture13def lower_compare_key():14return 'a.txt'151617@pytest.fixture18def lower_temp_filepath(lower_compare_key, tmp_path):19p = tmp_path / lower_compare_key20return p.resolve()212223@pytest.fixture24def lower_file_stat(lower_compare_key, lower_temp_filepath):25return FileStat(26src=f'bucket/{lower_compare_key}',27dest=lower_temp_filepath,28compare_key=lower_compare_key,29)303132@pytest.fixture33def upper_compare_key():34return 'A.txt'353637@pytest.fixture38def upper_temp_filepath(upper_compare_key, tmp_path):39p = tmp_path / upper_compare_key40p.write_text('foobar')41return p.resolve()424344@pytest.fixture45def upper_file_stat(upper_compare_key, upper_temp_filepath):46return FileStat(47src=f'bucket/{upper_compare_key}',48dest=upper_temp_filepath,49compare_key=upper_compare_key,50)515253@pytest.fixture54def upper_key(upper_file_stat):55return upper_file_stat.compare_key.lower()565758@pytest.fixture59def no_conflict_file_stat(tmp_path):60return FileStat(61src='bucket/foo.txt',62# Note that this file was never written to.63dest=f'{tmp_path}/foo.txt',64compare_key='foo.txt',65)666768class TestCaseConflictSync:69def test_error_with_no_conflict_syncs(70self, no_conflict_file_stat, lower_file_stat, capsys71):72case_conflict = CaseConflictSync(on_case_conflict='error')73should_sync = case_conflict.determine_should_sync(74no_conflict_file_stat, lower_file_stat75)76captured = capsys.readouterr()77assert should_sync is True78assert not captured.err7980@skip_if_case_sensitive()81def test_error_with_existing_file(self, lower_file_stat, upper_file_stat):82case_conflict_sync = CaseConflictSync(on_case_conflict='error')83with pytest.raises(CaseConflictException) as exc:84case_conflict_sync.determine_should_sync(85lower_file_stat,86upper_file_stat,87)88assert 'Failed to download bucket/a.txt' in str(exc.value)8990def test_error_with_case_conflicts_in_s3(91self, lower_file_stat, upper_file_stat, upper_key92):93case_conflict_sync = CaseConflictSync(94on_case_conflict='error', submitted={upper_key}95)96with pytest.raises(CaseConflictException) as exc:97case_conflict_sync.determine_should_sync(98lower_file_stat,99upper_file_stat,100)101assert 'Failed to download bucket/a.txt' in str(exc.value)102103def test_warn_with_no_conflict_syncs(104self, no_conflict_file_stat, lower_file_stat, capsys105):106case_conflict = CaseConflictSync(on_case_conflict='warn')107should_sync = case_conflict.determine_should_sync(108no_conflict_file_stat, lower_file_stat109)110captured = capsys.readouterr()111assert should_sync is True112assert not captured.err113114@skip_if_case_sensitive()115def test_warn_with_existing_file(116self, lower_file_stat, upper_file_stat, capsys117):118case_conflict_sync = CaseConflictSync(on_case_conflict='warn')119should_sync = case_conflict_sync.determine_should_sync(120lower_file_stat, upper_file_stat121)122captured = capsys.readouterr()123assert should_sync is True124assert 'warning: ' in captured.err125126def test_warn_with_case_conflicts_in_s3(127self, lower_file_stat, upper_file_stat, upper_key, capsys128):129case_conflict_sync = CaseConflictSync(130on_case_conflict='warn', submitted={upper_key}131)132should_sync = case_conflict_sync.determine_should_sync(133lower_file_stat,134upper_file_stat,135)136captured = capsys.readouterr()137assert should_sync is True138assert 'warning: ' in captured.err139140def test_skip_with_no_conflict_syncs(141self, no_conflict_file_stat, lower_file_stat, capsys142):143case_conflict = CaseConflictSync(on_case_conflict='skip')144should_sync = case_conflict.determine_should_sync(145no_conflict_file_stat, lower_file_stat146)147captured = capsys.readouterr()148assert should_sync is True149assert not captured.err150151@skip_if_case_sensitive()152def test_skip_with_existing_file(153self, lower_file_stat, upper_file_stat, capsys154):155case_conflict_sync = CaseConflictSync(on_case_conflict='skip')156should_sync = case_conflict_sync.determine_should_sync(157lower_file_stat, upper_file_stat158)159captured = capsys.readouterr()160assert should_sync is False161assert 'warning: Skipping' in captured.err162163def test_skip_with_case_conflicts_in_s3(164self, lower_file_stat, upper_file_stat, upper_key, capsys165):166case_conflict_sync = CaseConflictSync(167on_case_conflict='skip', submitted={upper_key}168)169should_sync = case_conflict_sync.determine_should_sync(170lower_file_stat,171upper_file_stat,172)173captured = capsys.readouterr()174assert should_sync is False175assert 'warning: Skipping' in captured.err176177178