1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Container API Test (Container Metadata)"""
18
19
20 import hashlib
21 import os
22 import sys
23 import tempfile
24 import time
25
26 from yakumo.smoketest import *
27 from yakumo import utils
28
29
31 m = hashlib.md5()
32 with open(file, 'rb') as f:
33 while True:
34 chunk = f.read(4096)
35 if not chunk:
36 break
37 m.update(chunk)
38 return m.hexdigest()
39
40
41 CONTENT_TYPE = "application/octet-stream"
42 CONTENT_TYPE2 = "application/qcow2"
43 TRANS_ID_EXTRA = "extra"
44 FILE = "images/cirros-0.3.5-x86_64-disk.img"
45 MD5 = get_md5(FILE)
46 SIZE = os.stat(FILE).st_size
47
48
50 if not c._session.has_endpoint('object-store'):
51 return
52
53 LOG.info("Create an container")
54
55 name = get_random_str('container')
56 metadata = {'foo': 'bar', 'foo2': 'bar2'}
57 with c.container.create(name=name) as co:
58
59 name = get_random_str('object')
60 with co.object.create(name=name,
61 content_type=CONTENT_TYPE,
62 file=FILE,
63 metadata=metadata) as o:
64 LOG.debug("list objects: %s",
65 [_.name for _ in co.object.list()])
66
67 test("Object #1: name is " + name, o.name == name)
68 test("Object #1: content type is %s" % CONTENT_TYPE,
69 o.content_type == CONTENT_TYPE)
70 test("Object #1: MD5 checksum is %s" % MD5,
71 o.etag == MD5)
72 test("Object #1: size is %s" % SIZE,
73 o.size == SIZE)
74
75 LOG.info("Update content type")
76 o.update(content_type=CONTENT_TYPE2)
77 time.sleep(5)
78 test("Object #1: content type is %s" % CONTENT_TYPE2,
79 o.content_type == CONTENT_TYPE2)
80
81 LOG.info("Update metadata")
82 LOG.debug("Initial metadata: %s", o.metadata)
83 test("Metadata has %s" % metadata, o.metadata == metadata)
84
85 m = {'foo2': 'bar4', 'foo3': 'bar3'}
86 LOG.debug("Set metadata : %s", m)
87 metadata.update(m)
88 o.set_metadata(**m)
89 LOG.debug("Updated metadata: %s", o.metadata)
90 test("Metadata has %s" % metadata, o.metadata == metadata)
91
92 m = ['foo', 'foo2']
93 LOG.debug("Unset metadata : %s", m)
94 for key in m:
95 metadata.pop(key)
96 o.unset_metadata(*m)
97 LOG.debug("Updated metadata: %s", o.metadata)
98 test("Metadata has %s" % metadata, o.metadata == metadata)
99
100 LOG.info("Copy Object #1 as Object #2")
101 name = get_random_str('object')
102 with o.copy(container=co, name=name) as o2:
103 LOG.debug("list objects: %s",
104 [_.name for _ in co.object.list()])
105
106 test("Object #2: name is " + name, o2.name == name)
107 test("Object #2: content type is %s" % CONTENT_TYPE2,
108 o.content_type == CONTENT_TYPE2)
109 test("Object #2: MD5 checksum is %s" % MD5,
110 o2.etag == MD5)
111 test("Object #2: size is %s" % SIZE,
112 o2.size == SIZE)
113 test("Metadata has %s" % metadata, o2.metadata == metadata)
114
115 LOG.debug("list objects: %s",
116 [_.name for _ in co.object.list()])
117 test("Object #2 is gone", o2 not in co.object.list())
118
119 LOG.info("Replace Object #1")
120 o.replace(content_type=CONTENT_TYPE, file=FILE)
121 time.sleep(5)
122
123
124 LOG.debug("content type: %s", o.content_type)
125 test("Object #1: content type is %s" % CONTENT_TYPE2,
126 o.content_type == CONTENT_TYPE2)
127 test("Object #1: MD5 checksum is %s" % MD5,
128 o.etag == MD5)
129 test("Object #1: size is %s" % SIZE,
130 o.size == SIZE)
131
132 with tempfile.NamedTemporaryFile() as f:
133
134 LOG.info("Download an object into a temporary file: %s",
135 f.name)
136 o.download(file=f.name)
137
138 test("Downloaded image size",
139 os.stat(f.name).st_size == SIZE)
140 test("Downloaded image checksum",
141 get_md5(f.name) == MD5)
142
143 LOG.debug("list objects: %s",
144 [_.name for _ in co.object.list()])
145 test("Object #1 is gone", o not in co.object.list())
146
147 test("Container #1 is gone", co not in c.container.list())
148
149 if __name__ == '__main__':
150 c = utils.get_client()
151 if not c._session.has_endpoint('object-store'):
152 sys.exit(0)
153
154 LOG.debug("list containers: %s", [_.name for _ in c.container.list()])
155 main(c)
156 LOG.debug("list containers: %s", [_.name for _ in c.container.list()])
157
158 show_test_summary()
159