Skip to content

Commit

Permalink
Update snapshot tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <akurait@amazon.com>
  • Loading branch information
AndreKurait committed Jun 25, 2024
1 parent 656d36e commit af22643
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
logger = logging.getLogger(__name__)


def get_snapshot(config: Dict, source_cluster: Cluster, target_cluster: Cluster):
def get_snapshot(config: Dict, source_cluster: Cluster):
if 'fs' in config:
return FileSystemSnapshot(config, source_cluster, target_cluster)
return S3Snapshot(config, source_cluster, target_cluster)
return FileSystemSnapshot(config, source_cluster)
return S3Snapshot(config, source_cluster)


SCHEMA = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ class Snapshot(ABC):
"""
Interface for creating and managing snapshots.
"""
def __init__(self, config: Dict, source_cluster: Cluster, target_cluster: Optional[Cluster] = None) -> None:
def __init__(self, config: Dict, source_cluster: Cluster) -> None:
self.config = config
self.source_cluster = source_cluster
self.target_cluster = target_cluster
v = Validator(SNAPSHOT_SCHEMA)
if not v.validate({'snapshot': config}):
raise ValueError("Invalid config file for snapshot", v.errors)
Expand All @@ -77,19 +76,17 @@ def status(self, *args, **kwargs) -> CommandResult:


class S3Snapshot(Snapshot):
def __init__(self, config: Dict, source_cluster: Cluster, target_cluster: Cluster) -> None:
super().__init__(config, source_cluster, target_cluster)
def __init__(self, config: Dict, source_cluster: Cluster) -> None:
super().__init__(config, source_cluster)
self.snapshot_name = config['snapshot_name']
self.s3_repo_uri = config['s3']['repo_uri']
self.s3_region = config['s3']['aws_region']

def create(self, *args, **kwargs) -> CommandResult:
assert isinstance(self.target_cluster, Cluster)
assert isinstance(self.source_cluster, Cluster)
if self.source_cluster.auth_type != AuthMethod.NO_AUTH:
raise NotImplementedError("Source cluster authentication is not supported for creating snapshots")

if self.target_cluster.auth_type != AuthMethod.NO_AUTH:
raise NotImplementedError("Target cluster authentication is not supported for creating snapshots")
wait = kwargs.get('wait', False)
max_snapshot_rate_mb_per_node = kwargs.get('max_snapshot_rate_mb_per_node')
command = [
Expand All @@ -102,8 +99,6 @@ def create(self, *args, **kwargs) -> CommandResult:

if self.source_cluster.allow_insecure:
command.append("--source-insecure")
if self.target_cluster.allow_insecure:
command.append("--target-insecure")
if not wait:
command.append("--no-wait")
if max_snapshot_rate_mb_per_node is not None:
Expand All @@ -125,20 +120,17 @@ def status(self, *args, **kwargs) -> CommandResult:


class FileSystemSnapshot(Snapshot):
def __init__(self, config: Dict, source_cluster: Cluster, target_cluster: Cluster) -> None:
super().__init__(config, source_cluster, target_cluster)
def __init__(self, config: Dict, source_cluster: Cluster) -> None:
super().__init__(config, source_cluster)
self.snapshot_name = config['snapshot_name']
self.repo_path = config['fs']['repo_path']

def create(self, *args, **kwargs) -> CommandResult:
assert isinstance(self.target_cluster, Cluster)
assert isinstance(self.source_cluster, Cluster)

if self.source_cluster.auth_type != AuthMethod.NO_AUTH:
raise NotImplementedError("Source cluster authentication is not supported for creating snapshots")

if self.target_cluster.auth_type != AuthMethod.NO_AUTH:
raise NotImplementedError("Target cluster authentication is not supported for creating snapshots")

command = [
"/root/createSnapshot/bin/CreateSnapshot",
"--snapshot-name", self.snapshot_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def s3_snapshot():
"aws_region": "us-east-1"
}
}
return S3Snapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH),
create_valid_cluster(auth_type=AuthMethod.NO_AUTH))
return S3Snapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH))


@pytest.fixture()
Expand All @@ -26,8 +25,7 @@ def fs_snapshot():
"repo_path": "/path/for/repo"
}
}
return FileSystemSnapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH),
create_valid_cluster(auth_type=AuthMethod.NO_AUTH))
return FileSystemSnapshot(snapshot_config, create_valid_cluster(auth_type=AuthMethod.NO_AUTH))


def test_metadata_init_with_fully_specified_config_succeeds():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_s3_snapshot_init_succeeds():
},
}
}
snapshot = S3Snapshot(config['snapshot'], create_valid_cluster(), create_valid_cluster())
snapshot = S3Snapshot(config['snapshot'], create_valid_cluster())
assert isinstance(snapshot, Snapshot)


Expand Down Expand Up @@ -113,7 +113,7 @@ def test_fs_snapshot_create_calls_subprocess_run_with_correct_args(mocker):
"--snapshot-name", config["snapshot"]["snapshot_name"],
"--file-system-repo-path", config["snapshot"]["fs"]["repo_path"],
"--source-host", source.endpoint,
"--source-insecure", "--target-insecure"],
"--source-insecure"],
stdout=None, stderr=None, text=True, check=True)


Expand All @@ -128,7 +128,7 @@ def test_s3_snapshot_create_calls_subprocess_run_with_correct_args(mocker):
}
}
source = create_valid_cluster(auth_type=AuthMethod.NO_AUTH)
snapshot = S3Snapshot(config["snapshot"], source, create_valid_cluster(auth_type=AuthMethod.NO_AUTH))
snapshot = S3Snapshot(config["snapshot"], source)

mock = mocker.patch("subprocess.run")
snapshot.create()
Expand All @@ -138,13 +138,12 @@ def test_s3_snapshot_create_calls_subprocess_run_with_correct_args(mocker):
"--s3-repo-uri", config["snapshot"]["s3"]["repo_uri"],
"--s3-region", config["snapshot"]["s3"]["aws_region"],
"--source-host", source.endpoint,
"--source-insecure", "--target-insecure"],
"--source-insecure", "--no-wait"],
stdout=None, stderr=None, text=True, check=True)


@pytest.mark.parametrize("source_auth,target_auth", [(AuthMethod.NO_AUTH, AuthMethod.BASIC_AUTH),
(AuthMethod.BASIC_AUTH, AuthMethod.NO_AUTH)])
def test_s3_snapshot_create_fails_for_clusters_with_auth(source_auth, target_auth):
@pytest.mark.parametrize("source_auth", [(AuthMethod.BASIC_AUTH)])
def test_s3_snapshot_create_fails_for_clusters_with_auth(source_auth):
config = {
"snapshot": {
"snapshot_name": "reindex_from_snapshot",
Expand All @@ -154,16 +153,14 @@ def test_s3_snapshot_create_fails_for_clusters_with_auth(source_auth, target_aut
},
}
}
snapshot = S3Snapshot(config["snapshot"], create_valid_cluster(auth_type=source_auth),
create_valid_cluster(auth_type=target_auth))
snapshot = S3Snapshot(config["snapshot"], create_valid_cluster(auth_type=source_auth))
with pytest.raises(NotImplementedError) as excinfo:
snapshot.create()
assert "authentication is not supported" in str(excinfo.value.args[0])


@pytest.mark.parametrize("source_auth,target_auth", [(AuthMethod.NO_AUTH, AuthMethod.BASIC_AUTH),
(AuthMethod.BASIC_AUTH, AuthMethod.NO_AUTH)])
def test_fs_snapshot_create_fails_for_clusters_with_auth(source_auth, target_auth):
@pytest.mark.parametrize("source_auth", [(AuthMethod.BASIC_AUTH)])
def test_fs_snapshot_create_fails_for_clusters_with_auth(source_auth):
config = {
"snapshot": {
"snapshot_name": "reindex_from_snapshot",
Expand All @@ -173,7 +170,6 @@ def test_fs_snapshot_create_fails_for_clusters_with_auth(source_auth, target_aut
}
}
with pytest.raises(NotImplementedError) as excinfo:
snapshot = FileSystemSnapshot(config["snapshot"], create_valid_cluster(auth_type=source_auth),
create_valid_cluster(auth_type=target_auth))
snapshot = FileSystemSnapshot(config["snapshot"], create_valid_cluster(auth_type=source_auth))
snapshot.create()
assert "authentication is not supported" in str(excinfo.value.args[0])

0 comments on commit af22643

Please sign in to comment.