<commit_before>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(**
 * @group Storage
 *)

open OUnit

open Listext
open Stringext

open Xmlrpc_client

let default_path = "/var/xapi/storage"
let transport = ref (Unix default_path)

let rpc call =
	XMLRPC_protocol.rpc ~transport:!transport
		~http:(xmlrpc ~version:"1.0" "/") call

open Storage_interface

let task = "sm-test"

let mib = Int64.mul 1024L 1024L

let usage_and_exit () =
	Printf.fprintf stderr "Usage:\n";
	Printf.fprintf stderr "  %s <SR>" Sys.argv.(0);
	exit 1

let find_vdi_in_scan sr vdi =
	match Client.SR.scan rpc ~task ~sr with
		| Success (Vdis results) ->
			begin
				try
					Some (List.find (fun x -> x.vdi = vdi) results)
				with Not_found ->
					None
			end
		| x ->
			failwith (Printf.sprintf "Unexpected result from SR.scan: %s\n" (string_of_result x))

let test_query sr _ = let (_: query_result) = Client.query rpc () in ()

let missing_vdi = "missing"

let test_scan_missing_vdi sr _ =
	match find_vdi_in_scan sr missing_vdi with
		| Some vdi -> failwith (Printf.sprintf "SR.scan found a VDI that was supposed to be missing: %s" (string_of_vdi_info vdi))
		| None -> ()

let test_destroy_missing_vdi sr _ =
	begin match Client.VDI.destroy rpc ~task ~sr ~vdi:missing_vdi with
		| Failure Vdi_does_not_exist -> ()
		| x -> failwith (Printf.sprintf "Unexpected result from VDI.destroy: %s\n" (string_of_result x))
	end

let vdi_info_assert_equal vdi_info vdi_info' =
	assert_equal ~msg:"name_label" ~printer:(fun x -> x) vdi_info.name_label vdi_info'.name_label;
	assert_equal ~msg:"name_description" ~printer:(fun x -> x) vdi_info.name_description vdi_info'.name_description;
	assert_equal ~msg:"ty" ~printer:(fun x -> x) (String.lowercase vdi_info.ty) (String.lowercase vdi_info'.ty);
	assert_equal ~msg:"metadata_of_pool" ~printer:(fun x -> x) vdi_info.metadata_of_pool vdi_info'.metadata_of_pool;
	assert_equal ~msg:"is_a_snapshot" ~printer:string_of_bool vdi_info.is_a_snapshot vdi_info'.is_a_snapshot;
	assert_equal ~msg:"snapshot_time" ~printer:(fun x -> x) vdi_info.snapshot_time vdi_info'.snapshot_time;
	assert_equal ~msg:"snapshot_of" ~printer:(fun x -> x) vdi_info.snapshot_of vdi_info'.snapshot_of;
	assert_equal ~msg:"read_only" ~printer:string_of_bool vdi_info.read_only vdi_info'.read_only

let example_vdi_info =
	let name_label = "test_name_label" in
	let name_description = "test_name_description" in
	let ty = "ephemeral" in
	let metadata_of_pool = "mop" in
	let is_a_snapshot = true in
	let snapshot_time = "19700101T00:00:00Z" in
	let snapshot_of = "sof" in
	let read_only = false in
	let virtual_size = Int64.mul 8L mib in
	let physical_utilisation = 0L in
	{
		vdi = "";
		name_label = name_label;
		name_description = name_description;
		ty = ty;
		metadata_of_pool = metadata_of_pool;
		is_a_snapshot = is_a_snapshot;
		snapshot_time = snapshot_time;
		snapshot_of = snapshot_of;
		read_only = read_only;
		virtual_size = virtual_size;
		physical_utilisation = physical_utilisation;
	}

let test_create_destroy sr _ =
	let vdi_info = example_vdi_info in
	let vdi_info' = begin match Client.VDI.create rpc ~task ~sr ~vdi_info ~params:[] with
		| Success (Vdi vdi_info') ->
			vdi_info_assert_equal vdi_info vdi_info';
			vdi_info'
			(* sizes will be rounded up *)
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end in
	begin match find_vdi_in_scan sr vdi_info'.vdi with
		| None -> failwith (Printf.sprintf "SR.scan failed to find vdi: %s" (string_of_vdi_info vdi_info'))
		| Some vdi_info'' -> vdi_info_assert_equal vdi_info' vdi_info''
	end;
	begin match Client.VDI.destroy rpc ~task ~sr ~vdi:vdi_info'.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end;
	begin match find_vdi_in_scan sr vdi_info'.vdi with
		| Some vdi_info''' -> failwith (Printf.sprintf "SR.scan found a VDI that was just deleted: %s" (string_of_vdi_info vdi_info'''))
		| None -> ()
	end

let test_attach_activate sr _ =
	let vdi_info = match Client.VDI.create rpc ~task ~sr ~vdi_info:example_vdi_info ~params:[] with
		| Success (Vdi x) -> x
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))	in
	let dp = "test_attach_activate" in
	let (_: string) = match Client.VDI.attach rpc ~task ~sr ~dp ~vdi:vdi_info.vdi ~read_write:true with
		| Success (Params x) -> x
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))	in
	begin match Client.VDI.activate rpc ~task ~sr ~dp ~vdi:vdi_info.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end;
	begin match Client.VDI.destroy rpc ~task ~sr ~vdi:vdi_info.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end		<commit_msg>Fix sm-cli/test<commit_after>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(**
 * @group Storage
 *)

open OUnit

open Listext
open Stringext

open Xmlrpc_client

let default_path = "/var/xapi/storage"
let transport = ref (Unix default_path)

let rpc call =
	XMLRPC_protocol.rpc ~transport:!transport
		~http:(xmlrpc ~version:"1.0" "/") call

open Storage_interface
module Client = Storage_interface.Client(struct let rpc = rpc end)

let task = "sm-test"

let mib = Int64.mul 1024L 1024L

let usage_and_exit () =
	Printf.fprintf stderr "Usage:\n";
	Printf.fprintf stderr "  %s <SR>" Sys.argv.(0);
	exit 1

let find_vdi_in_scan sr vdi =
	match Client.SR.scan ~task ~sr with
		| Success (Vdis results) ->
			begin
				try
					Some (List.find (fun x -> x.vdi = vdi) results)
				with Not_found ->
					None
			end
		| x ->
			failwith (Printf.sprintf "Unexpected result from SR.scan: %s\n" (string_of_result x))

let test_query sr _ = let (_: query_result) = Client.query () in ()

let missing_vdi = "missing"

let test_scan_missing_vdi sr _ =
	match find_vdi_in_scan sr missing_vdi with
		| Some vdi -> failwith (Printf.sprintf "SR.scan found a VDI that was supposed to be missing: %s" (string_of_vdi_info vdi))
		| None -> ()

let test_destroy_missing_vdi sr _ =
	begin match Client.VDI.destroy ~task ~sr ~vdi:missing_vdi with
		| Failure Vdi_does_not_exist -> ()
		| x -> failwith (Printf.sprintf "Unexpected result from VDI.destroy: %s\n" (string_of_result x))
	end

let vdi_info_assert_equal vdi_info vdi_info' =
	assert_equal ~msg:"name_label" ~printer:(fun x -> x) vdi_info.name_label vdi_info'.name_label;
	assert_equal ~msg:"name_description" ~printer:(fun x -> x) vdi_info.name_description vdi_info'.name_description;
	assert_equal ~msg:"ty" ~printer:(fun x -> x) (String.lowercase vdi_info.ty) (String.lowercase vdi_info'.ty);
	assert_equal ~msg:"metadata_of_pool" ~printer:(fun x -> x) vdi_info.metadata_of_pool vdi_info'.metadata_of_pool;
	assert_equal ~msg:"is_a_snapshot" ~printer:string_of_bool vdi_info.is_a_snapshot vdi_info'.is_a_snapshot;
	assert_equal ~msg:"snapshot_time" ~printer:(fun x -> x) vdi_info.snapshot_time vdi_info'.snapshot_time;
	assert_equal ~msg:"snapshot_of" ~printer:(fun x -> x) vdi_info.snapshot_of vdi_info'.snapshot_of;
	assert_equal ~msg:"read_only" ~printer:string_of_bool vdi_info.read_only vdi_info'.read_only

let example_vdi_info =
	let name_label = "test_name_label" in
	let name_description = "test_name_description" in
	let ty = "ephemeral" in
	let metadata_of_pool = "mop" in
	let is_a_snapshot = true in
	let snapshot_time = "19700101T00:00:00Z" in
	let snapshot_of = "sof" in
	let read_only = false in
	let virtual_size = Int64.mul 8L mib in
	let physical_utilisation = 0L in
	{
		vdi = "";
		name_label = name_label;
		name_description = name_description;
		ty = ty;
		metadata_of_pool = metadata_of_pool;
		is_a_snapshot = is_a_snapshot;
		snapshot_time = snapshot_time;
		snapshot_of = snapshot_of;
		read_only = read_only;
		virtual_size = virtual_size;
		physical_utilisation = physical_utilisation;
	}

let test_create_destroy sr _ =
	let vdi_info = example_vdi_info in
	let vdi_info' = begin match Client.VDI.create ~task ~sr ~vdi_info ~params:[] with
		| Success (Vdi vdi_info') ->
			vdi_info_assert_equal vdi_info vdi_info';
			vdi_info'
			(* sizes will be rounded up *)
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end in
	begin match find_vdi_in_scan sr vdi_info'.vdi with
		| None -> failwith (Printf.sprintf "SR.scan failed to find vdi: %s" (string_of_vdi_info vdi_info'))
		| Some vdi_info'' -> vdi_info_assert_equal vdi_info' vdi_info''
	end;
	begin match Client.VDI.destroy ~task ~sr ~vdi:vdi_info'.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end;
	begin match find_vdi_in_scan sr vdi_info'.vdi with
		| Some vdi_info''' -> failwith (Printf.sprintf "SR.scan found a VDI that was just deleted: %s" (string_of_vdi_info vdi_info'''))
		| None -> ()
	end

let test_attach_activate sr _ =
	let vdi_info = match Client.VDI.create ~task ~sr ~vdi_info:example_vdi_info ~params:[] with
		| Success (Vdi x) -> x
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))	in
	let dp = "test_attach_activate" in
	let (_: string) = match Client.VDI.attach ~task ~sr ~dp ~vdi:vdi_info.vdi ~read_write:true with
		| Success (Params x) -> x
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))	in
	begin match Client.VDI.activate ~task ~sr ~dp ~vdi:vdi_info.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end;
	begin match Client.VDI.destroy ~task ~sr ~vdi:vdi_info.vdi with
		| Success Unit -> ()
		| x -> failwith (Printf.sprintf "Unexpected result: %s\n" (string_of_result x))
	end		
{'fraction_non_alphanumeric': 0.10844717033732924, 'fraction_numerical': 0.00631911532385466, 'mean_word_length': 4.438100050530571, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '878429', 'n_tokens_mistral': 4063, 'n_tokens_neox': 3931, 'n_words': 1105}