1*** Settings ***
2Suite Setup                     Custom Suite Setup
3Suite Teardown                  Custom Suite Teardown
4Library                         OperatingSystem
5
6*** Variables ***
7${UART}                         sysbus.uart0
8${DRIVE}                        https://dl.antmicro.com/projects/renode/empty-ext4-filesystem.img-s_33554432-1eb65a808612389cc35a69b81178fbad5708a863
9${FS}                           https://dl.antmicro.com/projects/renode/virtio-filesystem-passthrough_hp_uds-s_7318728-f7b60ce9b60c82cede19e798e81971663e1c0ad2
10${SCRIPT_BLK}                   ${CURDIR}/../../scripts/single-node/hifive_unleashed.resc
11${SCRIPT_FS}                    ${CURDIR}/../../tests/peripherals/virtio-vexriscv.resc
12${INPUT}                        Quick Brown Fox Jumps Over the Lazy Dog
13${PROMPT}                       #
14${SOCK_FILE}                    libfuse-passthrough-hp.sock
15${SOCK_PATH}                    ${TEMPDIR}${/}${SOCK_FILE}
16${SHARED_FILE}                  testfile
17${SHARED_DIR}                   shareddir
18${VIRTIOFS_TAG}                 "MySharedDir"
19${SYSTEM}=                      Evaluate         platform.system()    modules=platform
20
21*** Keywords ***
22Custom Suite Setup
23    Setup
24
25    ${TEMP_DRIVE}=              Download File    ${DRIVE}
26    ${DIR}  ${FILE_NAME}=       Split Path       ${TEMP_DRIVE}
27    Copy File                   ${TEMP_DRIVE}    ${TEMPDIR}
28    Set Suite Variable          ${DRIVE_PATH}    ${TEMPDIR}${/}${FILE_NAME}
29    ${TEMP_FS}=                 Download File    ${FS}
30    ${DIR}  ${FILE_NAME}=       Split Path       ${TEMP_FS}
31    Copy File                   ${TEMP_FS}       ${TEMPDIR}
32    Set Suite Variable          ${FS_PATH}       ${TEMPDIR}${/}${FILE_NAME}
33    Run Process                 chmod    +x      ${FS_PATH}
34
35Custom Suite Teardown
36    Remove File                 ${DRIVE_PATH}
37    Remove File                 ${FS_PATH}
38    Teardown
39
40Create Machine VirtIOBlock
41    Execute Command             $fdt=@https://dl.antmicro.com/projects/renode/virtio-hifive_unleashed.dtb-s_10640-08834542504afb748827fdca52515f156e971d5f
42    Execute Script              ${SCRIPT_BLK}
43    Execute Command             machine LoadPlatformDescriptionFromString 'virtioblk: Storage.VirtIOBlockDevice @ sysbus 0x100d0000 { IRQ -> plic@50 }'
44
45Create Machine VirtIOFS
46    Execute Command             $platform=@tests/peripherals/virtio-platform.repl
47    Execute Command             $img=@https://dl.antmicro.com/projects/renode/virtio-filesystem-image-s_8448188-414604e8f64c41ebdbffe0f9ae7525c20bb1b124
48    Execute Command             $dtb=@https://dl.antmicro.com/projects/renode/virtio-filesystem-rv32.dtb-s_1806-b2ad3ecaf517c6a6781d1cbb48eff6fca7972094
49    Execute Command             $osbi=@https://dl.antmicro.com/projects/renode/litex_vexriscv_smp--opensbi.bin-s_45360-dcfe5f7b149bd1e0232609d87fb698f95f5e33c4
50    Execute Command             $rootfs=@https://dl.antmicro.com/projects/renode/virtio-filesystem-rootfs.cpio-s_39962112-95a3591d189699f21b988b036a9843c882d8e42f
51    Execute Command             $sock_path=@${SOCK_PATH}
52    Execute Command             $virtiofs_tag=${VIRTIOFS_TAG}
53    Execute Script              ${SCRIPT_FS}
54
55Setup Machine VirtIOBlock
56    Wait For Prompt On Uart     buildroot login:
57    Write Line To Uart          root
58    Wait For Prompt On Uart     Password:
59    Write Line To Uart          root             waitForEcho=false
60    Wait For Prompt On Uart     ${PROMPT}
61    Execute Command             virtioblk LoadImage @${DRIVE_PATH} true
62    Wait For Prompt On Uart     ${PROMPT}
63    Write Line To Uart          dmesg -n 1
64    Wait For Prompt On Uart     ${PROMPT}
65    Write Line To Uart          mkdir /mnt/drive
66    Wait For Prompt On Uart     ${PROMPT}
67    Write Line To Uart          mount /dev/vda /mnt/drive
68    Wait For Prompt On Uart     ${PROMPT}
69
70Setup Machine VirtIOFS
71    Wait For Prompt On Uart     buildroot login:
72    Write Line To Uart          root
73    Wait For Line On Uart       root login on 'console'
74    Wait For Prompt On Uart     ${PROMPT}
75    Write Line To Uart          mkdir ${SHARED_DIR}
76    Wait For Prompt On Uart     ${PROMPT}
77    Write Line To Uart          mount -t virtiofs ${VIRTIOFS_TAG} ${SHARED_DIR}
78    Wait For Prompt On Uart     ${PROMPT}
79
80Setup Shared Directory
81    Create Directory            ${SHARED_DIR}
82    Create File                 ${SHARED_DIR}/${SHARED_FILE}    ${INPUT}
83
84*** Test Cases ***
85Read Shared Directory
86    [Tags]                      skip_windows    skip_osx  skip_host_arm
87    # Unix domain sockets
88
89    Setup Shared Directory
90    ${FS_PROCESS}=              Start Process    ${FS_PATH}    ${SHARED_DIR}    --socket    ${SOCK_PATH}
91
92    Create Machine VirtIOFS
93    Create Terminal Tester      ${UART}
94    Start Emulation
95
96    Setup Machine VirtIOFS
97    Write Line To Uart          ls -al ${SHARED_DIR}
98    Wait For Prompt On Uart     ${PROMPT}
99    Write Line To Uart          cat ${SHARED_DIR}/${SHARED_FILE}
100    Wait For Prompt On Uart     ${INPUT}
101    Sleep                       5s
102    Terminate Process           ${FS_PROCESS}    kill=true
103    Run Process                 rm    ${SOCK_PATH}
104
105Should Boot
106    Create Machine VirtIOBlock
107    Create Terminal Tester      ${UART}
108    Start Emulation
109
110    Setup Machine VirtIOBlock
111    Write Line To Uart          echo ${INPUT} > /mnt/drive/file
112    Wait For Prompt On Uart     ${PROMPT}
113    Write Line To Uart          cat /mnt/drive/file
114    Wait For Line On Uart       ${INPUT}
115    Wait For Prompt On Uart     ${PROMPT}
116    Write Line To Uart          umount /dev/vda
117    Wait For Prompt On Uart     ${PROMPT}
118    # We encountered data corruption when closing the emulation right after `umount`.
119    # Although `umount` should wait for all write operations on the device to finish, we noticed writes even after the prompt in bash is printed.
120    # Surprisingly even using `sync; sync` doesn't help here.
121    # As a workaround let's include sleep, but this should be fixed later.
122    Sleep                       5s
123
124Should Be Persistent
125    Create Machine VirtIOBlock
126    Create Terminal Tester      ${UART}
127    Start Emulation
128
129    Setup Machine VirtIOBlock
130    Write Line To Uart          cat /mnt/drive/file
131    Wait For Line On Uart       ${INPUT}
132    Wait For Prompt On Uart     ${PROMPT}
133    Write Line To Uart          umount /dev/vda
134    Wait For Prompt On Uart     ${PROMPT}
135