3 # Copyright (C) 2019-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
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
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.
48 requires nbdsh
--version
50 sock
=$
(mktemp
-u /tmp
/nbdkit-test-sock.XXXXXX
)
52 files
="truncate4.pid $sock $data"
54 cleanup_fn
rm -f $files
56 # Create and truncate the file.
59 # Run nbdkit with file plugin and truncate filter in front.
60 start_nbdkit
-P truncate4.pid
-U $sock \
69 data = os.environ["data"]
70 sock = os.environ["sock"]
73 # Original test data, 1024 bytes of "TEST" repeated.
74 with open(data, "w") as file:
75 file.write("TEST"*256)
79 print("Connection A.", flush=True)
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)
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)
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