File: index_parser_tests.ml

package info (click to toggle)
guestfs-tools 1.54.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 69,464 kB
  • sloc: ml: 15,635; ansic: 15,575; sh: 8,238; xml: 5,478; makefile: 3,547; perl: 1,537; lex: 135; yacc: 128; python: 80
file content (127 lines) | stat: -rw-r--r-- 3,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(* builder
 * Copyright (C) 2017 SUSE Inc.
 * Copyright (C) 2025 Red Hat
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

(* This file tests the Index_parser module. *)

open Printf

open Std_utils
open Unix_utils
open Tools_utils

let tmpdir =
  let tmpdir = Mkdtemp.temp_dir "guestfs-tests." in
  On_exit.rm_rf tmpdir;
  tmpdir

let dummy_sigchecker = Sigchecker.create ~gpg:"gpg"
                                         ~check_signature:false
                                         ~gpgkey:Utils.No_Key
                                         ~tmpdir

let dummy_downloader = Downloader.create ~curl:"do-not-use-curl"
                                         ~cache:None ~tmpdir

(* Utils. *)
let write_entries file entries =
  let chan = open_out (tmpdir // file) in
  List.iter (Index_parser.write_entry chan) entries;
  close_out chan

let read_file file =
  read_whole_file (tmpdir // file)

let parse_file file =
  let source = { Sources.name = "input";
                 uri = tmpdir // file;
                 gpgkey = Utils.No_Key;
                 proxy = Curl.SystemProxy;
                 format = Sources.FormatNative } in
  let entries = Index_parser.get_index ~downloader:dummy_downloader
                                       ~sigchecker:dummy_sigchecker
                                       source in
  List.map (
    fun (id, e) -> (id, { e with Index.file_uri =
                                   Filename.basename e.Index.file_uri })
  ) entries

let format_entries entries =
  let format_entry entry =
    write_entries "out" [entry];
    read_file "out" in
  List.map format_entry entries

let assert_equal ~printer a b =
  if a <> b then
    failwithf "FAIL: %s <> %s" (printer a) (printer b)

let assert_equal_string = assert_equal ~printer:(fun x -> sprintf "\"%s\"" x)
let assert_equal_list formatter =
  let printer = (
    fun x -> "(" ^ (String.escaped (String.concat "," (formatter x))) ^ ")"
  ) in
  assert_equal ~printer

let () =
  let entry =
    ("test-id", { Index.printable_name = Some "test_name";
           osinfo = Some "osinfo_data";
           file_uri = "image_path";
           arch = Index.Arch "test_arch";
           signature_uri = None;
           checksums = Some [Checksums.SHA512 "512checksum"];
           revision = Utils.Rev_int 42;
           format = Some "qcow2";
           size = Int64.of_int 123456;
           compressed_size = Some (Int64.of_int 12345);
           expand = Some "/dev/sda1";
           lvexpand = Some "/some/lv";
           notes = [ ("",
                      "Notes split\non several lines\n\n with starting space ")
                   ];
           hidden = false;
           aliases = Some ["alias1"; "alias2"];
           sigchecker = dummy_sigchecker;
           proxy = Curl.SystemProxy }) in

  write_entries "out" [entry];
  let actual = read_file "out" in
  let expected = "[test-id]
name=test_name
osinfo=osinfo_data
file=image_path
arch=test_arch
checksum[sha512]=512checksum
revision=42
format=qcow2
size=123456
compressed_size=12345
expand=/dev/sda1
lvexpand=/some/lv
notes=Notes split
 on several lines
 
  with starting space 
aliases=alias1 alias2

" in
  assert_equal_string expected actual;

  let parsed_entries = parse_file "out" in
  assert_equal_list format_entries [entry] parsed_entries