1#!/bin/bash
2
3set -e
4
5if [ -x ./xl ] ; then
6    export LD_LIBRARY_PATH=.:../libxc:../xenstore
7    XL=./xl
8else
9    XL=xl
10fi
11
12fprefix=tmp.check-xl-disk-parse
13
14expected () {
15    cat >$fprefix.expected
16}
17
18failures=0
19
20one () {
21    expected_rc=$1; shift
22    printf "test case %s...\n" "$*"
23    set +e
24    ${XL} -N block-attach 0 "$@" </dev/null >$fprefix.actual 2>/dev/null
25    actual_rc=$?
26    diff -u $fprefix.expected $fprefix.actual
27    diff_rc=$?
28    set -e
29    if [ $actual_rc != $expected_rc ] || [ $diff_rc != 0 ]; then
30        echo >&2 "test case \`$*' failed ($actual_rc $diff_rc)"
31        failures=$(( $failures + 1 ))
32    fi
33}
34
35complete () {
36    if [ "$failures" = 0 ]; then
37        echo all ok.; exit 0
38    else
39        echo "$failures tests failed."; exit 1
40    fi
41}
42
43e=1
44
45
46#---------- test data ----------
47#
48# culled from docs/misc/xl-disk-configuration.txt
49
50expected </dev/null
51one $e foo
52
53expected <<END
54disk: {
55    "pdev_path": "/dev/vg/guest-volume",
56    "vdev": "hda",
57    "format": "raw",
58    "readwrite": 1
59}
60
61END
62one 0 /dev/vg/guest-volume,,hda
63one 0 /dev/vg/guest-volume,raw,hda,rw
64one 0 "format=raw, vdev=hda, access=rw, target=/dev/vg/guest-volume"
65one 0  format=raw  vdev=hda  access=rw  target=/dev/vg/guest-volume
66one 0 raw:/dev/vg/guest-volume,hda,w
67
68expected <<END
69disk: {
70    "pdev_path": "/root/image.iso",
71    "vdev": "hdc",
72    "format": "raw",
73    "removable": 1,
74    "is_cdrom": 1
75}
76
77END
78one 0 /root/image.iso,,hdc,cdrom
79one 0 /root/image.iso,,hdc,,cdrom
80one 0 /root/image.iso,raw,hdc,devtype=cdrom
81one 0 "format=raw, vdev=hdc, access=ro, devtype=cdrom, target=/root/image.iso"
82one 0  format=raw  vdev=hdc  access=ro  devtype=cdrom  target=/root/image.iso
83one 0 raw:/root/image.iso,hdc:cdrom,ro
84
85expected <<EOF
86disk: {
87    "pdev_path": "/dev/vg/guest-volume",
88    "vdev": "xvdb",
89    "backend": "phy",
90    "format": "raw",
91    "readwrite": 1
92}
93
94EOF
95one 0 backendtype=phy,vdev=xvdb,access=w,target=/dev/vg/guest-volume
96
97expected <<EOF
98disk: {
99    "pdev_path": "",
100    "vdev": "hdc",
101    "format": "empty",
102    "removable": 1,
103    "is_cdrom": 1
104}
105
106EOF
107one 0 devtype=cdrom,,,hdc
108one 0 ,,hdc:cdrom,r
109one 0 ,hdc:cdrom,r
110one 0 vdev=hdc,access=r,devtype=cdrom,target=
111one 0 ,empty,hdc:cdrom,r
112
113expected <<EOF
114disk: {
115    "vdev": "hdc",
116    "format": "empty",
117    "removable": 1,
118    "is_cdrom": 1
119}
120
121EOF
122one 0 vdev=hdc,access=r,devtype=cdrom,format=empty
123one 0 vdev=hdc,access=r,devtype=cdrom
124
125expected <<EOF
126disk: {
127    "pdev_path": "iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost",
128    "vdev": "xvda",
129    "format": "raw",
130    "script": "block-iscsi",
131    "readwrite": 1
132}
133
134EOF
135
136# http://backdrift.org/xen-block-iscsi-script-with-multipath-support
137one 0 iscsi:iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost,xvda,w
138one 0 vdev=xvda,access=w,script=block-iscsi,target=iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost
139
140expected <<EOF
141disk: {
142    "pdev_path": "app01",
143    "vdev": "hda",
144    "format": "raw",
145    "script": "block-drbd",
146    "readwrite": 1
147}
148
149EOF
150
151# http://lists.linbit.com/pipermail/drbd-user/2008-September/010221.html
152# http://www.drbd.org/users-guide-emb/s-xen-configure-domu.html
153one 0 drbd:app01,hda,w
154
155expected <<END
156disk: {
157    "pdev_path": "/some/disk/image.raw",
158    "vdev": "hda",
159    "format": "raw",
160    "readwrite": 1,
161    "discard_enable": "True"
162}
163
164END
165one 0  discard vdev=hda target=/some/disk/image.raw
166one 0  discard vdev=hda target=/some/disk/image.raw
167
168expected <<END
169disk: {
170    "pdev_path": "/some/disk/image.iso",
171    "vdev": "hda",
172    "format": "raw",
173    "removable": 1,
174    "is_cdrom": 1,
175    "discard_enable": "False"
176}
177
178END
179one 0  cdrom no-discard vdev=hda target=/some/disk/image.iso
180
181complete
182