Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-truncate4.sh
blobfc8adab36e18bffad3e5e5e41332b8ea1b30c528
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright Red Hat
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 # Regression test when next->get_size changes between connections.
35 # For now, NBD does not support dynamic resize; but the file plugin
36 # reads size from the file system for each new connection, at which
37 # point the client remembers that size for the life of the connection.
39 # We are testing that connection A can still see the tail of a file,
40 # even when connection B is opened while the file was temporarily
41 # shorter. If the actions of connection B affect the size visible
42 # through connection A, we didn't isolate per-connection state.
44 source ./functions.sh
45 set -e
46 set -x
48 requires nbdsh --version
50 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
51 data=truncate4.data
52 files="truncate4.pid $sock $data"
53 rm -f $files
54 cleanup_fn rm -f $files
56 # Create and truncate the file.
57 : > $data
59 # Run nbdkit with file plugin and truncate filter in front.
60 start_nbdkit -P truncate4.pid -U $sock \
61 --filter=truncate \
62 file $data \
63 round-up=1024
65 export data sock
66 nbdsh -c '
67 import os
69 data = os.environ["data"]
70 sock = os.environ["sock"]
72 def restore_file():
73 # Original test data, 1024 bytes of "TEST" repeated.
74 with open(data, "w") as file:
75 file.write("TEST"*256)
77 restore_file()
79 print("Connection A.", flush=True)
80 connA = nbd.NBD()
81 connA.set_handle_name("A")
82 connA.connect_unix(sock)
83 print("Check the size.", flush=True)
84 assert connA.get_size() == 1024
86 print("Truncate %s to 512 bytes." % data, flush=True)
87 os.truncate(data, 512)
89 print("Connection B.", flush=True)
90 connB = nbd.NBD()
91 connB.set_handle_name("B")
92 connB.connect_unix(sock)
93 print("Check the size.", flush=True)
94 assert connB.get_size() == 1024 # because of the round-up parameter
95 print("Read data from connection B.", flush=True)
96 buf = connB.pread(1024, 0)
97 assert buf == b"TEST"*128 + b"\0"*512
99 print("Restore the file size and original data.", flush=True)
100 restore_file()
102 print("Read data from connection A.", flush=True)
103 buf = connA.pread(1024, 0)
104 assert 1024 == len(buf)
105 assert buf == b"TEST"*256