ci: Update .gitlab-ci.yml
[nbdkit.git] / tests / test-memory-allocator-malloc-mlock.sh
blob7544a4be95ba31f74d308d48c8faa3dd4d8230eb
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018-2020 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 # Test the memory plugin with the malloc allocator and mlock. Usually
34 # mlock limits are very low, so this test only tries to allocate a
35 # very tiny disk, in the hope that this way we at least end up with
36 # some test coverage of the feature.
38 source ./functions.sh
39 set -e
41 requires_nbdsh_uri
43 if ! nbdkit memory --dump-plugin | grep -sq mlock=yes; then
44 echo "$0: mlock not enabled in this build of nbdkit"
45 exit 77
48 # ulimit -l is measured in kilobytes and so for this test must be at
49 # least 10 (kilobytes) and we actually check it's a bit larger to
50 # allow room for error. On Linux the default is usually 64.
51 if test "$(ulimit -l)" != "unlimited" && test "$(ulimit -l)" -le 16; then
52 echo "$0: limit for mlock memory (ulimit -l) too low for test"
53 exit 77
56 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
57 files="memory-allocator-malloc-mlock.pid $sock"
58 rm -f $files
59 cleanup_fn rm -f $files
61 # Run nbdkit with memory plugin.
62 start_nbdkit -P memory-allocator-malloc-mlock.pid -U $sock \
63 memory 10240 allocator=malloc,mlock=true
65 nbdsh --connect "nbd+unix://?socket=$sock" \
66 -c '
67 # Write some stuff.
68 buf1 = b"1" * 512
69 h.pwrite(buf1, 0)
70 buf2 = b"2" * 512
71 h.pwrite(buf2, 1024)
72 buf3 = b"3" * 512
73 h.pwrite(buf3, 1536)
74 buf4 = b"4" * 512
75 h.pwrite(buf4, 4096)
76 buf5 = b"5" * 1024
77 h.pwrite(buf5, 10240-len(buf5))
79 # Read it back.
80 buf11 = h.pread(len(buf1), 0)
81 assert buf1 == buf11
82 buf22 = h.pread(len(buf2), 1024)
83 assert buf2 == buf22
84 buf33 = h.pread(len(buf3), 1536)
85 assert buf3 == buf33
86 buf44 = h.pread(len(buf4), 4096)
87 assert buf4 == buf44
88 buf55 = h.pread(len(buf5), 10240-len(buf5))
89 assert buf5 == buf55