Skip to content

Commit

Permalink
clean up s3 path handlling tests for clarity and add checks that uplo…
Browse files Browse the repository at this point in the history
…ad cleans paths
  • Loading branch information
michael-paddle committed Aug 27, 2019
1 parent be33acb commit 16aec13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
5 changes: 3 additions & 2 deletions backup_cloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import sys
import gpg # type: ignore
import re
from tempfile import TemporaryDirectory, mkdtemp, NamedTemporaryFile
from botocore.exceptions import ClientError # type: ignore
from typing import Dict, List, Generator, Tuple
Expand Down Expand Up @@ -372,8 +373,8 @@ def backup_file_to_s3(self, src_file: str, dest_bucket, dest_path: str):
We delete the initial slash and any double slashes from any
path to stop empty folder names coming through
"""
# re.sub("/{2,}", "/", dest_path)
# re.sub("/", "", dest_path)
dest_path = re.sub("/{2,}", "/", dest_path)
dest_path = re.sub("^/", "", dest_path)

eprint(
"uploading "
Expand Down
34 changes: 24 additions & 10 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,49 @@
from unittest.mock import patch


def test_should_handle_paths_with_double_slashes_so_we_end_up_with_one_slash():
def test_should_clean_up_double_slashes_from_target_url():
with patch("backup_cloud.base.boto3"):
with patch.object(BackupContext, "get_gpg_keys"):
c = BackupContext(ssm_path="/unit/test/fake")
c = BackupContext(ssm_path="/unit/test/fakessm")
with patch.object(
c, "s3_path", return_value="/unit/test/fake/s3/path/with/slash/"
c, "s3_path", return_value="/unit/test/fakes3/s3/path/with/slash/"
):
target = c.s3_target_url()
assert "//" not in target
assert target == "/unit/test/fake/s3/path/with/slash/backup"
assert target == "/unit/test/fakes3/s3/path/with/slash/backup"
with patch.object(c, "s3_bucket") as mockbucket:
list(c.download_gpg_keys())
mockbucket().objects.filter.assert_called_with(
Prefix="/unit/test/fake/s3/path/with/slash/config/public-keys/"
Prefix="/unit/test/fakes3/s3/path/with/slash/config/public-keys/"
)


def test_should_handle_paths_with_single_slashes_so_we_end_up_with_one_slash():
def test_should_maintain_a_single_slash_in_path():
with patch("backup_cloud.base.boto3"):
with patch.object(BackupContext, "get_gpg_keys"):
c = BackupContext(ssm_path="/unit/test/fake")
c = BackupContext(ssm_path="/unit/test/fakessm")
with patch.object(
c, "s3_path", return_value="/unit/test/fake/s3/path/without/slash"
c, "s3_path", return_value="/unit/test/fakes3/s3/path/without/slash"
):
target = c.s3_target_url()
assert "//" not in target
assert target == "/unit/test/fake/s3/path/without/slash/backup"
assert target == "/unit/test/fakes3/s3/path/without/slash/backup"
with patch.object(c, "s3_bucket") as mockbucket:
list(c.download_gpg_keys())
mockbucket().objects.filter.assert_called_with(
Prefix="/unit/test/fake/s3/path/without/slash/config/public-keys/"
Prefix="/unit/test/fakes3/s3/path/without/slash/config/public-keys/"
)


def test_upload_should_clean_slashes_in_paths_of_target_objects():
with patch("backup_cloud.base.boto3"):
with patch.object(BackupContext, "get_gpg_keys"):
c = BackupContext(ssm_path="/unit/test/fake")
with patch.object(
c, "s3_path", return_value="/unit/test/fake/s3/path/without/slash"
):
with patch.object(c, "s3_bucket") as mockbucket:
c.backup_file_to_s3(
"/etc/hosts", mockbucket, "/this//that/theother"
)
mockbucket.Object.assert_called_with("this/that/theother")

0 comments on commit 16aec13

Please sign in to comment.