Strip xseg stuff
[archipelago] / tools / qa / basictest.py
1 # Copyright 2013 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 from archipelago.common import loadrc, DEVICE_PREFIX, Error, construct_peers
35 import archipelago.vlmc as vlmc
36 import archipelago.archipelago as archipelago
37
38 import os, errno
39 import tempfile
40 from subprocess import check_call
41
42 def gettempname(prefix='myvolume-'):
43     t = tempfile.mktemp(prefix=prefix)
44     return os.path.basename(t)
45
46 def getrandomdata(size):
47     return os.urandom(size)
48
49 def getrandomfile(size):
50     randomfile = gettempname(prefix='random-') 
51     randomdata = getrandomdata(size)
52     return (randomfile, randomdata)
53
54 def is_mounted(device):
55     lines = open("/proc/mounts").read().split("\n")
56     mounts = [l.split(" ")[0] for l in lines]
57     for m in mounts:
58         if m == device:
59             return True
60     return False
61
62 def mount(device, directory):
63     cmd = ['mount', device, directory]
64     try:
65         check_call(cmd, shell=False)
66     except:
67         raise Error("Cannot mount %s to %s" % (device, directory))
68
69 def umount(device):
70     if not is_mounted(device):
71         return
72     cmd = ['umount', device]
73     try:
74         check_call(cmd, shell=False)
75     except:
76         raise Error("Cannot umount %s" % device)
77
78
79 def test_create(volume, volumesize=None, snap=None, cont_addr=False):
80     vlmc.create(name=volume, size=volumesize, snap=snap, cont_addr=cont_addr)
81     #should we catch Error here and do a vlmc.remove(name=volume)
82 #    d_id = vlmc.map_volume(name=volume)
83 #    device = DEVICE_PREFIX + str(d_id)
84 #    vlmc.unmap_volume(name=device)
85
86
87 def write(volume, volumesize):
88     d_id=vlmc.map_volume(name=volume)
89     device = DEVICE_PREFIX + str(d_id)
90     try:
91         fd = os.open(device, os.O_WRONLY)
92         os.lseek(fd, (volumesize*1024*1024)+1, os.SEEK_SET)
93         os.write(fd, "This should not succeed")
94         print ("wtf")
95     except OSError, (err, reason):
96         if err != errno.EINVAL:
97             raise Error("Cannot write to device %s : %s" % (device,reason))
98     finally:
99         if fd:
100             os.close(fd)
101         vlmc.unmap_volume(name=device)
102
103
104 def mkfs_and_mount(volume, mountdir):
105     d_id=vlmc.map_volume(name=volume)
106     device = DEVICE_PREFIX + str(d_id)
107
108     cmd = ['mkfs.ext3', device]
109     check_call(cmd, shell=False)
110     try:
111         mount(device, mountdir)
112         umount(device)
113     finally:
114         vlmc.unmap_volume(name=device)
115
116
117 def write_data(volume, mountdir, randomfiles):
118     d_id=vlmc.map_volume(name=volume)
119     device = DEVICE_PREFIX + str(d_id)
120     try:
121         mount(device, mountdir)
122         for rf in randomfiles:
123             print "writing " + rf[0]
124             f = open(os.path.join(mountdir, rf[0]), 'w')
125             f.write(rf[1])
126             f.close()
127     finally:
128         umount(device)
129         vlmc.unmap_volume(name=device)
130
131 def read_data(volume, mountdir, randomfiles):
132     d_id=vlmc.map_volume(name=volume)
133     device = DEVICE_PREFIX + str(d_id)
134     try:
135         mount(device, mountdir)
136         for rf in randomfiles:
137             print "reading " + rf[0]
138             f = open(os.path.join(mountdir, rf[0]), 'r')
139             data = f.read()
140             f.close()
141             if data != rf[1]:
142                 raise Error("Data mismatch %s" % rf[0])
143     finally:
144         umount(device)
145         vlmc.unmap_volume(name=device)
146
147 def snapshot(volume):
148     vlmc.snapshot(name=volume)
149
150 if __name__ == '__main__':
151     loadrc(None)
152     peers = construct_peers()
153     tmpvolume=gettempname()
154     mntdir = '/mnt/mountpoint'
155     RANDOMSIZE=20*1024*1024
156
157     test_create(tmpvolume, volumesize=10240)
158     try:
159         write(tmpvolume, 10240)
160         mkfs_and_mount(tmpvolume, mntdir)
161
162         rf = [getrandomfile(RANDOMSIZE)]
163         write_data(tmpvolume, mntdir, rf)
164         archipelago.restart(user=True)
165         read_data(tmpvolume, mntdir, rf)
166
167         #snapshot
168         snapname=gettempname(prefix="snapshot")
169         print "Snapshot ", tmpvolume, "to", snapname
170         snap = vlmc.snapshot(name=tmpvolume, snap_name=snapname)
171         clonedvolume=gettempname()
172         print "Cloning ", snapname, "to", clonedvolume
173         test_create(clonedvolume, snap=snapname)
174     except Exception as e:
175         print e
176         vlmc.remove(name=tmpvolume)
177         raise e
178
179     #create clone volume from snapshot
180     try:
181         rf1 = [getrandomfile(RANDOMSIZE)]
182         #write new data to old volume
183         write_data(tmpvolume, mntdir, rf1)
184         #read new data from old volume
185         read_data(tmpvolume, mntdir, rf1)
186         #read old data from new volume
187         read_data(clonedvolume, mntdir, rf)
188
189         rf2 = [getrandomfile(RANDOMSIZE)]
190         #write new data2 to snapshot
191         write_data(clonedvolume, mntdir, rf2)
192         #read new data2 from clonedvolume
193         read_data(clonedvolume, mntdir, rf2)
194         #read new data from old volume
195         read_data(tmpvolume, mntdir, rf1)
196     except Exception as e:
197         print e
198     finally:
199         vlmc.remove(name=tmpvolume)
200         vlmc.remove(name=clonedvolume)
201
202
203