Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Use a dataclass for pre_migration input parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Kartik Ganesh <[email protected]>
  • Loading branch information
kartg committed Aug 15, 2023
1 parent 677b01f commit 98f1b68
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
6 changes: 4 additions & 2 deletions FetchMigration/python/pre_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Constants
from endpoint_info import EndpointInfo
from pre_migration_params import PreMigrationParams

SUPPORTED_ENDPOINTS = ["opensearch", "elasticsearch"]
SOURCE_KEY = "source"
Expand Down Expand Up @@ -157,7 +158,7 @@ def compute_endpoint_and_fetch_indices(config: dict, key: str) -> tuple[Endpoint
return endpoint_info, indices


def run(args: argparse.Namespace) -> None:
def run(args: PreMigrationParams) -> None:
# Sanity check
if not args.report and len(args.output_file) == 0:
raise ValueError("No output file specified")
Expand Down Expand Up @@ -223,4 +224,5 @@ def run(args: argparse.Namespace) -> None:
help="Print a report of the index differences")
arg_parser.add_argument("--dryrun", action="store_true",
help="Skips the actual creation of indices on the target cluster")
run(arg_parser.parse_args())
namespace = arg_parser.parse_args()
run(PreMigrationParams(namespace.config_file_path, namespace.output_file, namespace.report, namespace.dryrun))
9 changes: 9 additions & 0 deletions FetchMigration/python/pre_migration_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass


@dataclass
class PreMigrationParams:
config_file_path: str
output_file: str = ""
report: bool = False
dryrun: bool = False
24 changes: 4 additions & 20 deletions FetchMigration/python/tests/test_pre_migration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import copy
import pickle
import random
Expand All @@ -7,6 +6,7 @@
from unittest.mock import patch, MagicMock, ANY

import pre_migration
from pre_migration_params import PreMigrationParams
from tests import test_constants

# Constants
Expand Down Expand Up @@ -251,13 +251,7 @@ def test_run_report(self, mock_fetch_indices: MagicMock, mock_create_indices: Ma
index_settings[test_constants.INDEX_KEY][test_constants.NUM_REPLICAS_SETTING] += 1
# Fetch indices is called first for source, then for target
mock_fetch_indices.side_effect = [test_constants.BASE_INDICES_DATA, target_indices_data]
# Set up test input
test_input = argparse.Namespace()
test_input.config_file_path = test_constants.PIPELINE_CONFIG_RAW_FILE_PATH
# Default value for missing output file
test_input.output_file = ""
test_input.report = True
test_input.dryrun = False
test_input = PreMigrationParams(test_constants.PIPELINE_CONFIG_RAW_FILE_PATH, report=True)
pre_migration.run(test_input)
mock_create_indices.assert_called_once_with(expected_create_payload, ANY)
mock_doc_count.assert_called()
Expand All @@ -280,12 +274,7 @@ def test_run_dryrun(self, mock_fetch_indices: MagicMock, mock_write_output: Magi
del target_indices_data[index_to_create]
# Fetch indices is called first for source, then for target
mock_fetch_indices.side_effect = [test_constants.BASE_INDICES_DATA, target_indices_data]
# Set up test input
test_input = argparse.Namespace()
test_input.config_file_path = test_constants.PIPELINE_CONFIG_RAW_FILE_PATH
test_input.output_file = expected_output_path
test_input.dryrun = True
test_input.report = False
test_input = PreMigrationParams(test_constants.PIPELINE_CONFIG_RAW_FILE_PATH, expected_output_path, dryrun=True)
pre_migration.run(test_input)
mock_write_output.assert_called_once_with(self.loaded_pipeline_config, {index_to_create}, expected_output_path)
mock_doc_count.assert_called()
Expand Down Expand Up @@ -321,12 +310,7 @@ def test_write_output(self, mock_dump: MagicMock):
mock_dump.assert_called_once_with(expected_output_data, ANY)

def test_missing_output_file_non_report(self):
# Set up test input
test_input = argparse.Namespace()
test_input.config_file_path = test_constants.PIPELINE_CONFIG_RAW_FILE_PATH
# Default value for missing output file
test_input.output_file = ""
test_input.report = False
test_input = PreMigrationParams(test_constants.PIPELINE_CONFIG_RAW_FILE_PATH)
self.assertRaises(ValueError, pre_migration.run, test_input)


Expand Down

0 comments on commit 98f1b68

Please sign in to comment.