File: vm_test.sh

package info (click to toggle)
kworkflow 1%3A0.6.2-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,148 kB
  • sloc: sh: 22,233; perl: 2,172; ansic: 96; python: 72; sql: 28; makefile: 19
file content (243 lines) | stat: -rwxr-xr-x 6,028 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash

include './tests/utils.sh'
include './src/kw_config_loader.sh'
include './src/vm.sh'

function setUp()
{
  export PREFIX="$SHUNIT_TMPDIR/"
  mkdir -p "$SHUNIT_TMPDIR/.kw/"
  cp -f "$KW_VM_CONFIG_SAMPLE" "$SHUNIT_TMPDIR/.kw/"

  tests="$PWD/tests"
  etc="${PREFIX}etc"

  mkdir -p "${PREFIX}boot"
  mkdir -p "$etc"
}

function tearDown()
{
  rm -rf "$SHUNIT_TMPDIR"
}

function test_vm_mount()
{
  local mount_point="${SHUNIT_TMPDIR}/lala"
  local qemu_path='/any/path'
  local -r current_path="$PWD"
  local ret
  local expected_ret
  local msg

  # Message to user
  local say_msg="Mount $qemu_path in $mount_point"

  # Guestmount cmd
  local guestmount_cmd="guestmount -a $qemu_path -i $mount_point 2>&1"

  declare -a expected_cmd

  function uname()
  {
    printf '5.1'
  }

  cd "$SHUNIT_TMPDIR" || {
    fail "($LINENO) It was not possible to move to temporary directory"
    return
  }

  # Mock vmlinuz
  touch "${PREFIX}boot/vmlinuz-$(uname)"

  # Removing read permission from our mock vmlinuz
  chmod a-r "${PREFIX}boot/vmlinuz-$(uname)"

  # Suppose it's a debian system
  cp -f "$tests/samples/os/debian/etc/os-release" "$PREFIX/etc"

  expected_cmd=(
    'To mount the VM, the kernel image needs to be readable'
    "sudo dpkg-statoverride --update --add root root 0644 ${PREFIX}boot/vmlinuz-$(uname -r)"
    "$say_msg"
    "$guestmount_cmd"
  )

  output=$(printf '%s\n' 'y' | vm_mount 'TEST_MODE' "$qemu_path" "$mount_point")
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  # Suppose it's not debian
  rm -rf "${etc:?}/"*
  cp -f "$tests/samples/os/arch/etc/os-release" "$PREFIX/etc"

  expected_cmd[1]="sudo chmod +r ${PREFIX}boot/vmlinuz-$(uname -r)"
  output=$(printf '%s\n' 'y' | vm_mount 'TEST_MODE' "$qemu_path" "$mount_point")
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  # Adding back read permission
  chmod +r "${PREFIX}boot/vmlinuz-$(uname)"

  expected_cmd=(
    "$say_msg"
    "$guestmount_cmd"
  )

  output=$(
    # shellcheck disable=SC2317
    function findmnt()
    {
      printf '%s\n' 'anything'
    }
    vm_mount 'TEST_MODE'
  )
  ret="$?"
  expected_ret='125'
  assertEquals "($LINENO) - Expected 125" "$expected_ret" "$ret"

  output=$(vm_mount 'TEST_MODE' "$qemu_path" "$mount_point")
  ret="$?"
  assertTrue "($LINENO)" "$ret"

  output=$(vm_mount 'TEST_MODE' "$qemu_path" "$mount_point")
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  load_configuration "$KW_CONFIG_SAMPLE"

  say_msg="Mount ${vm_config[qemu_path_image]} in $mount_point"
  guestmount_cmd="guestmount -a ${vm_config[qemu_path_image]} -i $mount_point 2>&1"
  expected_cmd[0]="$say_msg"
  expected_cmd[1]="$guestmount_cmd"

  output=$(vm_mount 'TEST_MODE' '' "$mount_point")
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  cd "$current_path" || {
    fail "($LINENO) It was not possible to move back from temp directory"
    return
  }
}

function test_vm_umount()
{
  local mount_point="/"
  local -r current_path="$PWD"
  local ret
  local expected_ret

  # Message to user
  local say_msg="Unmount $mount_point"

  # Guestunmount cmd
  local guestmount_cmd="guestunmount $mount_point"

  declare -a expected_cmd=(
    "$say_msg"
    "$guestmount_cmd"
  )

  cd "$SHUNIT_TMPDIR" || {
    fail "($LINENO) It was not possible to move to temporary directory"
    return
  }

  load_vm_config

  output=$(vm_umount 'TEST_MODE')
  ret="$?"
  expected_ret="125"
  assertEquals "($LINENO)" "$expected_ret" "$ret"

  output=$(vm_umount 'TEST_MODE' '' "$mount_point")
  ret="$?"
  assertTrue "($LINENO)" "$ret"

  output=$(vm_umount 'TEST_MODE' '' "$mount_point")
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  cd "$current_path" || {
    fail "($LINENO) It was not possible to move back from temp directory"
    return
  }
}

function test_vm_up()
{
  local output=''
  local current_path="$PWD"
  local virtualizer='qemu-system-x86_64'
  local qemu_hw_options='-enable-kvm -daemonize -smp 2 -m 1024'
  local qemu_net_options='-nic user,hostfwd=tcp::2222-:22,smb=/home/USERKW'
  local qemu_path='/home/USERKW/p/virty.qcow2'
  local cmd_vm_up="$virtualizer $qemu_hw_options $qemu_net_options $qemu_path"

  cp "$SAMPLES_DIR/vm_x86.config" "${SHUNIT_TMPDIR}/.kw/vm.config"

  declare -a expected_cmd=(
    'Starting Qemu with:'
    "$cmd_vm_up"
    "$cmd_vm_up"
  )

  cd "$SHUNIT_TMPDIR" || {
    fail "($LINENO) It was not possible to move to temporary directory"
    return
  }

  load_vm_config
  output=$(vm_up 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  cd "$current_path" || {
    fail "($LINENO) It was not possible to move back from temp directory"
    return
  }

}

function test_vm_parse_options()
{
  unset options_values
  declare -gA options_values
  local output
  local option_output

  # test default options
  parse_vm_options
  assertEquals "($LINENO)" '' "${options_values['MOUNT']}"
  assertEquals "($LINENO)" '' "${options_values['UMOUNT']}"
  assertEquals "($LINENO)" '' "${options_values['UP']}"

  # test individual options
  unset options_values
  declare -gA options_values
  parse_vm_options --mount
  assertEquals "($LINENO)" '1' "${options_values['MOUNT']}"

  unset options_values
  declare -gA options_values
  parse_vm_options --umount
  assertEquals "($LINENO)" '1' "${options_values['UMOUNT']}"

  unset options_values
  declare -gA options_values
  parse_vm_options --up
  assertEquals "($LINENO)" '1' "${options_values['UP']}"

  unset options_values
  declare -gA options_values
  parse_vm_options --alert=v
  assertEquals "($LINENO)" '--alert=v' "${options_values['ALERT_COMPLETION']}"

  unset options_values
  declare -gA options_values
  parse_vm_options --alert=s
  assertEquals "($LINENO)" '--alert=s' "${options_values['ALERT_COMPLETION']}"

  parse_vm_options --mispelled
  assertEquals "($LINENO)" 22 "$?"
  assertEquals "($LINENO)" "kw vm: unrecognized option '--mispelled'" "${options_values['ERROR']}"
}

invoke_shunit