<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.
 *)

open Storage_interface
open Storage_client

(* Principles:
   1. we don't delete or manipulate VDIs we didn't create
   2. we create VDIs with non-clashing names
   3. we always clean up (as best we can) after every test.
*)

(* We assume that no-one else has made VDIs with this name prefix: *)
let safe_prefix = Printf.sprintf "storage_test.%d" (Unix.getpid ())
let dbg = safe_prefix

let _vdi_create = "VDI_CREATE"
let _vdi_delete = "VDI_DELETE"
let _vdi_attach = "VDI_ATTACH"
let _vdi_detach = "VDI_DETACH"
let _vdi_activate = "VDI_ACTIVATE"
let _vdi_deactivate = "VDI_DEACTIVATE"
let _vdi_clone = "VDI_CLONE"

open OUnit

(* Names which are likely to cause problems *)
let names = [
  "simple"; (* start with an easy one *)
  "";
  ".";
  "..";
  "/";
  "!";
  String.make 128 '0';
]

(* For each VDI we check that:
   1. it shows up in a SR.scan
   2. attach RO, activate, deactivate, detach works
   3. attach RW, activate, deactivate, detach works
*)

let vdi_exists sr vdi =
  let all = Client.SR.scan ~dbg ~sr in
  List.fold_left (fun acc vdi_info -> acc || (vdi_info.vdi = vdi)) false all

let create sr name_label =
  let vdi_info = {
    default_vdi_info with
      name_label = safe_prefix ^ "." ^ name_label;
      virtual_size = 1000000000L;
  } in
  let vdi = Client.VDI.create ~dbg ~sr ~vdi_info in
  assert(vdi_exists sr vdi.vdi);
  (* Check the disk has size >= the amount we requested *)
  assert(vdi.virtual_size >= vdi_info.virtual_size);
  vdi

let destroy sr vdi =
  Client.VDI.destroy ~dbg ~sr ~vdi:vdi.vdi;
  assert(not (vdi_exists sr vdi.vdi))

let test_create_destroy sr n () = destroy sr (create sr n)

let attach_detach sr vdi read_write =
  let _ = Client.VDI.attach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi ~read_write in
  Client.VDI.detach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi

let test_attach_detach sr n () =
  let vdi = create sr n in
  List.iter (attach_detach sr vdi) [ true; false ];
  destroy sr vdi

let attach_activate_deactivate_detach sr vdi read_write =
  let _ = Client.VDI.attach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi ~read_write in
  Client.VDI.activate ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi;
  Client.VDI.deactivate ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi;
  Client.VDI.detach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi

let test_activate_deactivate sr n () =
  let vdi = create sr n in
  List.iter (attach_activate_deactivate_detach sr vdi) [ true; false ];
  destroy sr vdi

let test_clone sr n () =
  let vdi = create sr n in
  List.iter
    (fun read_write ->
      let x = Client.VDI.clone ~dbg ~sr ~vdi_info:vdi in
      Client.VDI.destroy ~dbg ~sr ~vdi:x.vdi
    ) [ true; false ];
  destroy sr vdi

let test_clone_attach sr n () =
  let vdi = create sr n in
  List.iter
    (fun read_write ->
      let x = Client.VDI.clone ~dbg ~sr ~vdi_info:vdi in
      attach_activate_deactivate_detach sr x read_write;
      Client.VDI.destroy ~dbg ~sr ~vdi:x.vdi
    ) [ true; false ];
  destroy sr vdi
<commit_msg>storage test: check that VDI.clone creates exactly 1 new VDI<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.
 *)

open Storage_interface
open Storage_client

(* Principles:
   1. we don't delete or manipulate VDIs we didn't create
   2. we create VDIs with non-clashing names
   3. we always clean up (as best we can) after every test.
*)

(* We assume that no-one else has made VDIs with this name prefix: *)
let safe_prefix = Printf.sprintf "storage_test.%d" (Unix.getpid ())
let dbg = safe_prefix

let _vdi_create = "VDI_CREATE"
let _vdi_delete = "VDI_DELETE"
let _vdi_attach = "VDI_ATTACH"
let _vdi_detach = "VDI_DETACH"
let _vdi_activate = "VDI_ACTIVATE"
let _vdi_deactivate = "VDI_DEACTIVATE"
let _vdi_clone = "VDI_CLONE"

open OUnit

(* Names which are likely to cause problems *)
let names = [
  "simple"; (* start with an easy one *)
  "";
  ".";
  "..";
  "/";
  "!";
  String.make 128 '0';
]

(* For each VDI we check that:
   1. it shows up in a SR.scan
   2. attach RO, activate, deactivate, detach works
   3. attach RW, activate, deactivate, detach works
*)

let vdi_exists sr vdi =
  let all = Client.SR.scan ~dbg ~sr in
  List.fold_left (fun acc vdi_info -> acc || (vdi_info.vdi = vdi)) false all

let create sr name_label =
  let vdi_info = {
    default_vdi_info with
      name_label = safe_prefix ^ "." ^ name_label;
      virtual_size = 1000000000L;
  } in
  let vdi = Client.VDI.create ~dbg ~sr ~vdi_info in
  assert(vdi_exists sr vdi.vdi);
  (* Check the disk has size >= the amount we requested *)
  assert(vdi.virtual_size >= vdi_info.virtual_size);
  vdi

let destroy sr vdi =
  Client.VDI.destroy ~dbg ~sr ~vdi:vdi.vdi;
  assert(not (vdi_exists sr vdi.vdi))

let test_create_destroy sr n () = destroy sr (create sr n)

let attach_detach sr vdi read_write =
  let _ = Client.VDI.attach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi ~read_write in
  Client.VDI.detach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi

let test_attach_detach sr n () =
  let vdi = create sr n in
  List.iter (attach_detach sr vdi) [ true; false ];
  destroy sr vdi

let attach_activate_deactivate_detach sr vdi read_write =
  let _ = Client.VDI.attach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi ~read_write in
  Client.VDI.activate ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi;
  Client.VDI.deactivate ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi;
  Client.VDI.detach ~dbg ~dp:dbg ~sr ~vdi:vdi.vdi

let test_activate_deactivate sr n () =
  let vdi = create sr n in
  List.iter (attach_activate_deactivate_detach sr vdi) [ true; false ];
  destroy sr vdi

let test_clone sr n () =
  let vdi = create sr n in
  List.iter
    (fun read_write ->
      let x = Client.VDI.clone ~dbg ~sr ~vdi_info:vdi in
      Client.VDI.destroy ~dbg ~sr ~vdi:x.vdi
    ) [ true; false ];
  destroy sr vdi

let test_clone_attach sr n () =
  let vdi = create sr n in
  List.iter
    (fun read_write ->
      let vdis = Client.SR.scan ~dbg ~sr in
      let x = Client.VDI.clone ~dbg ~sr ~vdi_info:vdi in
      let vdis' = Client.SR.scan ~dbg ~sr in
      attach_activate_deactivate_detach sr x read_write;
      Client.VDI.destroy ~dbg ~sr ~vdi:x.vdi;
      assert ((List.length vdis + 1) = (List.length vdis'))
    ) [ true; false ];
  destroy sr vdi

{'fraction_non_alphanumeric': 0.10032002226241825, 'fraction_numerical': 0.006400445248365104, 'mean_word_length': 3.4397776405188387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4899385', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2562, 'n_words': 889}