File: drm_plugin_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 (399 lines) | stat: -rwxr-xr-x 11,757 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash

include './src/plugins/subsystems/drm/drm.sh'
include './src/kwlib.sh'
include './tests/utils.sh'

function setUp()
{
  # Create a temporary directory for holding different config file
  export FAKE_DRM_SYSFS="${SHUNIT_TMPDIR}/sys/class/drm"
  export original_dir="$PWD"
  export SYSFS_CLASS_DRM="$FAKE_DRM_SYSFS"

  mkdir -p "$FAKE_DRM_SYSFS"

  cp "${SAMPLES_DIR}/kworkflow_drm_plugin.config" "${SHUNIT_TMPDIR}/kworkflow.config"
  cp "${SAMPLES_DIR}/deploy_remote.config" "${SHUNIT_TMPDIR}/deploy.config"

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

  # Prepare fake sysfs
  mk_fake_sys_class_drm

  # Parser default config file for the average case
  parse_configuration "$KW_CONFIG_SAMPLE"
}

function tearDown()
{
  configurations=()
  configurations[ssh_user]=juca

  cd "$original_dir" || {
    fail "($LINENO) It was not possible to move into $original_dir"
    return
  }

  if [[ -d "$SHUNIT_TMPDIR" ]]; then
    rm -rf "$SHUNIT_TMPDIR"
  fi
}

function mk_fake_sys_class_drm()
{
  declare -a fake_dirs=(
    "card0"
    "card0-DP-1"
    "card0-DP-2"
    "card0-DP-3"
    "card0-DVI-D-1"
    "card0-HDMI-A-1"
    "card1"
    "card1-DP-4"
    "card1-DP-5"
    "card1-DP-6"
    "card1-HDMI-A-2"
    "renderD128"
    "renderD129"
    "ttm")

  for dir in "${fake_dirs[@]}"; do
    mkdir -p "${FAKE_DRM_SYSFS}/${dir}"
  done

  touch "${FAKE_DRM_SYSFS}/version"
  touch "${FAKE_DRM_SYSFS}/card0-DP-3/modes"

  cat << END >> "${FAKE_DRM_SYSFS}/card0-DP-3/modes"
1920x2160
2560x1440
1920x1080
1680x1050
1280x1024
1440x900
1280x960
1152x864
1280x720
1440x576
1024x768
1440x480
800x600
720x576
720x480
640x480
720x400
END

  cat << END >> "${FAKE_DRM_SYSFS}/card1-HDMI-A-2/modes"
2560x1440
1920x1080
1280x1024
640x480
720x400
END
}

function test_drm_parser_options()
{
  parse_drm_options --remote 'jozzi@something:3232'
  assertEquals "($LINENO)" 'something' "${remote_parameters['REMOTE_IP']}"
  assertEquals "($LINENO)" '3232' "${remote_parameters['REMOTE_PORT']}"
  assertEquals "($LINENO)" 'jozzi' "${remote_parameters['REMOTE_USER']}"
  assertEquals "($LINENO)" 3 "${options_values['TARGET']}"

  parse_drm_options --local
  assertEquals "($LINENO)" 2 "${options_values['TARGET']}"

  assertEquals "($LINENO)" '' "${options_values['GUI_ON']}"
  assertEquals "($LINENO)" '' "${options_values['GUI_OFF']}"
  assertEquals "($LINENO)" '' "${options_values['CONN_AVAILABLE']}"
  assertEquals "($LINENO)" '' "${options_values['MODES_AVAILABLE']}"

  parse_drm_options --gui-on
  assertEquals "($LINENO)" 1 "${options_values['GUI_ON']}"

  parse_drm_options --gui-off
  assertEquals "($LINENO)" 1 "${options_values['GUI_OFF']}"

  parse_drm_options --conn-available
  assertEquals "($LINENO)" 1 "${options_values['CONN_AVAILABLE']}"

  parse_drm_options --modes
  assertEquals "($LINENO)" 1 "${options_values['MODES_AVAILABLE']}"

  parse_drm_options --load-module 'amdgpu'
  assertEquals "($LINENO)" 'amdgpu' "${options_values['LOAD_MODULE']}"

  # Validate module load
  parse_drm_options --load-module 'amdgpu:dc=0'
  assertEquals "($LINENO)" 'amdgpu:dc=0' "${options_values['LOAD_MODULE']}"

  parse_drm_options --load-module 'amdgpu:dc=0,cik_support=1'
  assertEquals "($LINENO)" 'amdgpu:dc=0,cik_support=1' "${options_values['LOAD_MODULE']}"

  parse_drm_options --load-module 'amdgpu:dc=0,cik_support=1,vkms'
  assertEquals "($LINENO)" 'amdgpu:dc=0,cik_support=1,vkms' "${options_values['LOAD_MODULE']}"

  parse_drm_options --load-module 'amdgpu:dc=0,cik_support=1,vkms:enable_writeback=1'
  assertEquals "($LINENO)" 'amdgpu:dc=0,cik_support=1,vkms:enable_writeback=1' "${options_values['LOAD_MODULE']}"

  parse_drm_options --load-module 'amdgpu:dc=0,cik_support=1,vkms:enable_writeback=1,enable_overlay=0'
  assertEquals "($LINENO)" 'amdgpu:dc=0,cik_support=1,vkms:enable_writeback=1,enable_overlay=0' "${options_values['LOAD_MODULE']}"

  # Validate unload module
  parse_drm_options --unload-module 'amdgpu'
  assertEquals "($LINENO)" 'amdgpu' "${options_values['UNLOAD_MODULE']}"

  parse_drm_options --unload-module='amdgpu,drm'
  assertEquals "($LINENO)" 'amdgpu,drm' "${options_values['UNLOAD_MODULE']}"
}

function test_gui_control_remote()
{
  local gui_on_cmd='systemctl isolate graphical.target'
  local gui_off_cmd='systemctl isolate multi-user.target'
  local bind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 1 > $i; done; sleep 0.5'
  local unbind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 0 > $i; done; sleep 0.5'
  local output

  # Remote
  remote_parameters['REMOTE_IP']='127.0.0.1'
  remote_parameters['REMOTE_PORT']='8888'
  remote_parameters['REMOTE_USER']='juca'

  tearDown # We want to test the default cases first
  # REMOTE = 3
  ssh_part="ssh -p 8888 juca@127.0.0.1"
  full_turn_on_gui_cmd="${ssh_part} sudo \"${gui_on_cmd}\""
  full_bind_cmd="${ssh_part} 'sudo bash -c '\''${bind_cmd}'\'"

  declare -a expected_cmd_seq=(
    "$full_turn_on_gui_cmd"
    "$full_bind_cmd"
  )

  output=$(gui_control 'ON' '3' '127.0.0.1:8888' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"

  full_turn_off_gui_cmd="${ssh_part} sudo \"${gui_off_cmd}\""
  full_unbind_cmd="$ssh_part 'sudo bash -c '\''${unbind_cmd}'\'"

  declare -a expected_cmd_seq=(
    "$full_turn_off_gui_cmd"
    "$full_unbind_cmd"
  )

  output=$(gui_control 'OFF' '3' '127.0.0.1:8888' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"

  # Test with config file
  parse_configuration "$KW_CONFIG_SAMPLE"

  # Remote
  remote_parameters['REMOTE_PORT']='3333'

  gui_off_cmd='turn off'
  ssh_part="ssh -p 3333 juca@127.0.0.1"
  full_turn_off_gui_cmd="${ssh_part} sudo \"${gui_off_cmd}\""
  full_unbind_cmd="${ssh_part} 'sudo bash -c '\''${unbind_cmd}'\'"

  declare -a expected_cmd_seq=(
    "$full_turn_off_gui_cmd"
    "$full_unbind_cmd"
  )

  output=$(gui_control 'OFF' '3' '' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"

  gui_on_cmd='turn on'
  full_turn_on_gui_cmd="${ssh_part} sudo \"${gui_on_cmd}\""
  full_bind_cmd="${ssh_part} 'sudo bash -c '\''${bind_cmd}'\'"

  declare -a expected_cmd_seq=(
    "$full_turn_on_gui_cmd"
    "$full_bind_cmd"
  )

  output=$(gui_control 'ON' '3' '' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"
}

function test_gui_control_local()
{
  local gui_on_cmd='systemctl isolate graphical.target'
  local gui_off_cmd='systemctl isolate multi-user.target'
  local bind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 1 > $i; done; sleep 0.5'
  local unbind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 0 > $i; done; sleep 0.5'
  local output

  configurations[gui_on]="$gui_on_cmd"
  configurations[gui_off]="$gui_off_cmd"

  declare -a expected_cmd_seq=(
    "sudo ${gui_on_cmd}"
    "sudo ${bind_cmd}"
  )

  output=$(gui_control 'ON' '2' '' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"

  declare -a expected_cmd_seq=(
    "sudo ${gui_off_cmd}"
    "sudo ${unbind_cmd}"
  )

  output=$(gui_control 'OFF' '2' '' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output"
}

function test_get_available_connectors_local()
{
  local output

  declare -a expected_output=(
    '[local] Card1 supports:'
    'DP'
    'DP'
    'DP'
    'HDMI'
    '[local] Card0 supports:'
    'DP'
    'DP'
    'DP'
    'DVI'
    'HDMI'
  )

  # Local
  output=$(get_available_connectors '2' '' 'TEST_MODE')
  compare_command_sequence '' "$LINENO" 'expected_output' "$output"
}

function test_get_available_connectors_remote()
{
  local output

  # Remote
  output=$(
    function cmd_remotely()
    {
      printf '/sys/class/drm/card0-%s-1\n' 'DP'
      printf '/sys/class/drm/card0-%s-1\n' 'eDP'
    }
    get_available_connectors '3' '' 'TEST_MODE'
  )

  declare -a expected_output=(
    '[remote] Card0 supports:'
    'DP'
    'eDP'
  )
  compare_command_sequence '' "$LINENO" 'expected_output' "$output"
}

function test_get_supported_mode_per_connector()
{
  declare -a expected_output=(
    "Modes per card"
    "${SHUNIT_TMPDIR}/card0-DP-3:"
    "1920x2160"
    "2560x1440"
    "1920x1080"
    "1680x1050"
    "1280x1024"
    "1440x900"
    "1280x960"
    "1152x864"
    "1280x720"
    "1440x576"
    "1024x768"
    "1440x480"
    "800x600"
    "720x576"
    "720x480"
    "640x480"
    "720x400"
    ""
    "${SHUNIT_TMPDIR}/card1-HDMI-A-2:"
    "2560x1440"
    "1920x1080"
    "1280x1024"
    "640x480"
    "720x400"
  )

  export SYSFS_CLASS_DRM="$FAKE_DRM_SYSFS"
  output=$(get_supported_mode_per_connector 2)
  compare_command_sequence '' "$LINENO" 'expected_output' "$output"
}

function test_module_control()
{
  local default_ssh='ssh -p 3333 juca@127.0.0.1 sudo'

  expected="sudo bash -c \"modprobe  amdgpu\""
  output=$(module_control "LOAD" "2" "" "amdgpu" "TEST_MODE")
  assertEquals "($LINENO): Simple module load" "$expected" "$output"

  expected="sudo bash -c \"modprobe  amdgpu && modprobe  vkms\""
  output=$(module_control "LOAD" "2" "" "amdgpu;vkms" "TEST_MODE")
  assertEquals "($LINENO): Load two different modules" "$expected" "$output"

  expected="sudo bash -c \"modprobe  amdgpu tmz=1 dc=1  && modprobe  vkms enable_cursor=1 \""
  output=$(module_control "LOAD" "2" "" "amdgpu:tmz=1,dc=1;vkms:enable_cursor=1" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"

  expected="sudo bash -c \"modprobe  amdgpu tmz=1 dc=1  && modprobe  vkms enable_cursor=1 \""
  output=$(module_control "LOAD" "2" "" "amdgpu:tmz=1,dc=1;vkms:enable_cursor=1" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"

  expected="sudo bash -c \"modprobe -r amdgpu\""
  output=$(module_control "UNLOAD" "2" "" "amdgpu" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"

  expected="sudo bash -c \"modprobe -r amdgpu && modprobe -r vkms\""
  output=$(module_control "UNLOAD" "2" "" "amdgpu;vkms" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"

  output=$(module_control "UNLOAD" "2" "" "" "TEST_MODE")
  assertEquals "($LINENO): It is required the driver name" "22" "$?"

  output=$(module_control "LOAD" "2" "" "" "TEST_MODE")
  assertEquals "($LINENO): It is required the driver name" "22" "$?"

  expected="$default_ssh \"modprobe  amdgpu && modprobe  vkms\""
  output=$(module_control "LOAD" "3" "" "amdgpu;vkms" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"

  expected="$default_ssh \"modprobe -r amdgpu && modprobe -r vkms\""
  output=$(module_control "UNLOAD" "3" "" "amdgpu;vkms" "TEST_MODE")
  assertEquals "($LINENO): Load modules with parameters" "$expected" "$output"
}

function test_convert_module_info()
{
  output=$(convert_module_info "LOAD" "amdgpu;vkms")
  expected="modprobe  amdgpu && modprobe  vkms"
  assertEquals "$LINENO" "$expected" "$output"

  output=$(convert_module_info "LOAD" "amdgpu;vkms;lala;xpto")
  expected="modprobe  amdgpu && modprobe  vkms && modprobe  lala && modprobe  xpto"
  assertEquals "$LINENO" "$expected" "$output"

  output=$(convert_module_info "LOAD" "amdgpu:dc=0,emu_mode=1,vm_debug=0;vkms enable_cursor=1")
  expected="modprobe  amdgpu dc=0 emu_mode=1 vm_debug=0  && modprobe  vkms enable_cursor=1"
  assertEquals "$LINENO" "$expected" "$output"

  output=$(convert_module_info "UNLOAD" "amdgpu;vkms;xpto")
  expected="modprobe -r amdgpu && modprobe -r vkms && modprobe -r xpto"
  assertEquals "$LINENO" "$expected" "$output"

  output=$(convert_module_info "LOAD" "")
  assertEquals "$LINENO" "$?" "22"
}

invoke_shunit