def test_node_comparison(self, service_template1, service_template2): comparer = TemplateComparer() node1_1 = service_template1[0].get_node("hello-1") node2_1 = service_template2[0].get_node("hello-1") context = TemplateContext(node1_1, node2_1, service_template1[2], service_template2[2]) equal, diff = comparer._compare_node(node1_1, node2_1, context) # pylint: disable=protected-access assert equal is False assert "capabilities" in diff.changed assert "interfaces" in diff.changed assert "properties" in diff.changed assert "requirements" not in diff.changed assert "types" not in diff.changed node1_2 = service_template1[0].get_node("hello-2") node2_2 = service_template2[0].get_node("hello-2") context = TemplateContext(node1_2, node2_2, service_template1[2], service_template2[2]) equal, diff = comparer._compare_node(node1_2, node2_2, context) # pylint: disable=protected-access assert equal is False assert "capabilities" in diff.changed assert "interfaces" in diff.changed assert "properties" in diff.changed assert "requirements" in diff.changed assert "types" in diff.changed
def template_diff(self, service_template1, service_template2): comparer = TemplateComparer() context = TemplateContext(service_template1[0], service_template2[0], service_template1[2], service_template2[2]) equal, diff = comparer.compare_service_template( service_template1[0], service_template2[0], context) return diff
def test_requirement_comparison(self, service_template1, service_template2): comparer = TemplateComparer() node1 = service_template1[0].get_node("hello-6") node2 = service_template2[0].get_node("hello-6") context = TemplateContext(node1, node2, service_template1[2], service_template2[2]) equal, diff = comparer._compare_node(node1, node2, context) # pylint: disable=protected-access assert equal is False assert "requirements" in diff.changed assert "dependency" in diff.changed["requirements"].changed assert "target" in diff.changed["requirements"].changed["dependency"].changed
def test_interface_comparison(self, service_template1, service_template2): comparer = TemplateComparer() node1 = service_template1[0].get_node("hello-1") node2 = service_template2[0].get_node("hello-1") context = TemplateContext(node1, node2, service_template1[2], service_template2[2]) equal, diff = comparer._compare_node(node1, node2, context) # pylint: disable=protected-access assert equal is False assert "interfaces" in diff.changed assert "create" in diff.changed["interfaces"].changed["Standard"].changed["operations"].changed assert "artifacts" in \ diff.changed["interfaces"].changed["Standard"].changed["operations"].changed["create"].changed assert "inputs" in \ diff.changed["interfaces"].changed["Standard"].changed["operations"].changed["create"].changed
def diff_templates( service_template_old: str, workdir_old: str, inputs_old: typing.Optional[dict], service_template_new: str, workdir_new: str, inputs_new: typing.Optional[dict], template_comparer: TemplateComparer, verbose_mode: bool ): if inputs_new is None: inputs_new = {} if inputs_old is None: inputs_old = {} ast_old = tosca.load(Path(workdir_old), PurePath(service_template_old)) ast_new = tosca.load(Path(workdir_new), PurePath(service_template_new)) template_old = ast_old.get_template(inputs_old) template_new = ast_new.get_template(inputs_new) context = TemplateContext(template_old, template_new, workdir_old, workdir_new) _, diff = template_comparer.compare_service_template(template_old, template_new, context) return diff
def _parser_callback(args): if args.instance_path and not path.isdir(args.instance_path): raise argparse.ArgumentTypeError( "Directory {} is not a valid path!".format(args.instance_path)) if args.workers < 1: print("{} is not a positive number!".format(args.workers)) return 1 storage_old = Storage.create(args.instance_path) comparer = TemplateComparer() instance_comparer = InstanceComparer() if args.template: service_template_new = args.template.name else: print("Template file for update was not supplied.") return 1 try: if args.inputs: inputs_new = yaml.safe_load(args.inputs) else: inputs_new = {} except YAMLError as e: print("Invalid inputs: {}".format(e)) return 1 workdir_old = get_workdir(storage_old) try: with tempfile.TemporaryDirectory() as temp_path: storage_new = Storage.create(temp_path) storage_new.write_json(inputs_new, "inputs") storage_new.write(service_template_new, "root_file") workdir_new = get_workdir(storage_new) instance_diff = diff_instances(storage_old, workdir_old, storage_new, workdir_new, comparer, instance_comparer, args.verbose) update(storage_old, workdir_old, storage_new, workdir_new, instance_comparer, instance_diff, args.verbose, args.workers, overwrite=True) except ParseError as e: print("{}: {}".format(e.loc, e)) return 1 except DataError as e: print(str(e)) return 1 return 0
def test_service_template_comparison(self, service_template1, service_template2): comparer = TemplateComparer() context = TemplateContext(service_template1[0], service_template2[0], service_template1[2], service_template2[2]) equal, diff = comparer.compare_service_template(service_template1[0], service_template2[0], context) assert equal is False assert len(diff.changed["nodes"].added) == 1 assert len(diff.changed["nodes"].deleted) == 1 assert len(diff.changed["nodes"].changed) == 4 assert diff.changed["nodes"].added[0] == "hello-5" assert diff.changed["nodes"].deleted[0] == "hello-4" assert "hello-1" in diff.changed["nodes"].changed assert "hello-2" in diff.changed["nodes"].changed assert "hello-3" in diff.changed["nodes"].changed assert "hello-6" in diff.changed["nodes"].changed assert " my-workstation" not in diff.changed["nodes"].changed
def diff(body: dict = None): logger.debug("Entry: diff") diff_request = DiffRequest.from_dict(body) try: original_storage = Storage.create() original_service_template = original_storage.read("root_file") original_inputs = original_storage.read_json("inputs") with tempfile.TemporaryDirectory(prefix=".opera-api-diff", dir=".") as new_storage_root: new_storage = Storage.create(instance_path=new_storage_root) with tempfile.NamedTemporaryFile(prefix="diff-service-template", dir=".") as new_service_template: new_service_template.write( diff_request.new_service_template_contents) new_service_template.flush() if diff_request.template_only: diff_result = opera_diff_templates( original_service_template, ".", original_inputs, new_service_template.name, ".", diff_request.inputs, TemplateComparer(), True) else: diff_result = opera_diff_instances(original_storage, ".", new_storage, ".", TemplateComparer(), InstanceComparer(), True) result = Diff(added=diff_result.added, changed=diff_result.changed, deleted=diff_result.deleted) except Exception as e: logger.error("Error performing diff.", e) return {"message": str(e)}, 500 return result, 200
def test_instance_diff(self, service_template1, service_template2): storage_1 = service_template1[3] storage_2 = service_template2[3] comparer_template = TemplateComparer() comparer_instance = InstanceComparer() diff = diff_instances(storage_1, service_template1[2], storage_2, service_template2[2], comparer_template, comparer_instance, False) assert "hello-1" in diff.changed["nodes"].changed assert "hello-2" in diff.changed["nodes"].changed assert "hello-3" in diff.changed["nodes"].changed assert "hello-6" in diff.changed["nodes"].changed
def diff_instances(storage_old: Storage, workdir_old: Path, storage_new: Storage, workdir_new: Path, template_comparer: TemplateComparer, instance_comparer: InstanceComparer, verbose_mode: bool): template_old = get_template(storage_old, workdir_old) template_new = get_template(storage_new, workdir_new) topology_old = template_old.instantiate(storage_old) topology_new = template_new.instantiate(storage_new) context = TemplateContext(template_old, template_new, workdir_old, workdir_new) _, diff = template_comparer.compare_service_template( template_old, template_new, context) _, diff = instance_comparer.compare_topology_template( topology_old, topology_new, diff) return diff
def test_update_service_template(self, service_template, service_template_updated): _, path, storage = service_template _, path_updated, storage_updated = service_template_updated deploy_service_template(path / "service.yaml", {"marker": "test-marker"}, storage, False, 1, True) deploy_service_template(path_updated / "service.yaml", {"marker": "test-marker"}, storage_updated, False, 1, True) template_comparer = TemplateComparer() instance_comparer = InstanceComparer() diff = diff_instances(storage, path, storage_updated, path_updated, template_comparer, instance_comparer, False) update(storage, path, storage_updated, path_updated, instance_comparer, diff, False, 1, True)
def _update(service_template: str, inputs: typing.Optional[dict], num_workers: int): original_storage = Storage.create() new_storage = Storage.create(instance_path=".opera-api-update") new_storage.write_json(inputs, "inputs") new_storage.write(service_template, "root_file") instance_diff = opera_diff_instances(original_storage, ".", new_storage, ".", TemplateComparer(), InstanceComparer(), True) opera_update(original_storage, ".", new_storage, ".", InstanceComparer(), instance_diff, True, num_workers, overwrite=True)
def _parser_callback(args): if args.instance_path and not path.isdir(args.instance_path): raise argparse.ArgumentTypeError( f"Directory {args.instance_path} is not a valid path!") storage_old = Storage.create(args.instance_path) comparer = TemplateComparer() if args.template: service_template_new_path = Path(args.template.name) else: print("Template file for comparison was not supplied.") return 1 if storage_old.exists("root_file"): service_template_old_path = Path(storage_old.read("root_file")) else: print("There is no root_file in storage.") return 1 if storage_old.exists("inputs"): inputs_old = storage_old.read_json("inputs") else: inputs_old = {} try: if args.inputs: inputs_new = yaml.safe_load(args.inputs) else: inputs_new = {} except yaml.YAMLError as e: print(f"Invalid inputs: {e}") return 1 workdir_old = get_workdir(storage_old) workdir_new = Path(service_template_new_path.parent) try: if args.template_only: template_diff = diff_templates(service_template_old_path, workdir_old, inputs_old, service_template_new_path, workdir_new, inputs_new, comparer, args.verbose) else: instance_comparer = InstanceComparer() with tempfile.TemporaryDirectory() as temp_path: storage_new = Storage.create(temp_path) storage_new.write_json(inputs_new, "inputs") storage_new.write(str(service_template_new_path), "root_file") template_diff = diff_instances(storage_old, workdir_old, storage_new, workdir_new, comparer, instance_comparer, args.verbose) outputs = template_diff.outputs() if args.output: save_outputs(outputs, args.format, args.output) else: print(format_outputs(outputs, args.format)) except ParseError as e: print(f"{e.loc}: {e}") return 1 except DataError as e: print(str(e)) return 1 return 0