Add test for device metadata file creation and fixes until test passed.
This commit is contained in:
@@ -2,6 +2,7 @@ import logging
|
||||
import sys
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
|
||||
def setup_logging():
|
||||
logger_obj = logging.getLogger('iottbLogger')
|
||||
logger_obj.setLevel(logging.DEBUG)
|
||||
|
||||
@@ -19,26 +19,36 @@ def setup_init_device_root_parser(subparsers):
|
||||
def handle_add(args):
|
||||
logger.info(f"Add device handler called with args {args}")
|
||||
|
||||
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear what to assume
|
||||
|
||||
if args.guided:
|
||||
logger.debug("Guided setup")
|
||||
logger.debug("begin guided setup")
|
||||
metadata = guided_setup(args.root_dir)
|
||||
logger.debug("guided setup complete")
|
||||
else:
|
||||
logger.debug("Setup through passed args: setup")
|
||||
device_name = args.name
|
||||
args.root_dir.mkdir(parents=True, exist_ok=True)
|
||||
metadata = DeviceMetadata(device_name, args.root_dir)
|
||||
if not args.name:
|
||||
logger.error("No device name specified with unguided setup.")
|
||||
return ReturnCodes.ERROR
|
||||
metadata = DeviceMetadata(args.name, args.root_dir)
|
||||
|
||||
file_path = args.root_dir / DEVICE_METADATA_FILE
|
||||
|
||||
response = input(f"Confirm device metadata: {metadata.to_json()} [y/N]")
|
||||
if response.lower() not in definitions.AFFIRMATIVE_USER_RESPONSE.add(""):
|
||||
configure_metadata()
|
||||
assert False, "TODO implement dynamic setup"
|
||||
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear
|
||||
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
||||
print("Directory already contains a device metadata file. Aborting operation.")
|
||||
if file_path.exists():
|
||||
print("Directory already contains a metadata file. Aborting.")
|
||||
return ReturnCodes.ABORTED
|
||||
assert Path(file_path).exists(), f"{file_path} does not exist"
|
||||
serialized_metadata = metadata.to_json()
|
||||
response = input(f"Confirm device metadata: {serialized_metadata} [y/N]")
|
||||
logger.debug(f"response: {response}")
|
||||
if response not in definitions.AFFIRMATIVE_USER_RESPONSE:
|
||||
print("Adding device aborted by user.")
|
||||
return ReturnCodes.ABORTED
|
||||
|
||||
logger.debug(f"Device metadata file {file_path}")
|
||||
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
||||
logger.error("File exists after checking, which should not happen.")
|
||||
return ReturnCodes.ABORTED
|
||||
|
||||
print("Device metadata successfully created.")
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user