1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Image API Test (Images)"""
18
19
20 import hashlib
21 import os
22 import tempfile
23
24 from yakumo.smoketest import *
25 from yakumo import utils
26
27
29 m = hashlib.md5()
30 with open(file, 'rb') as f:
31 while True:
32 chunk = f.read(4096)
33 if not chunk:
34 break
35 m.update(chunk)
36 return m.hexdigest()
37
38
40
41 SOURCE_IMAGE = './images/cirros-0.3.5-x86_64-disk.img'
42
43 LOG.info("Create an image")
44
45 name = get_random_str('image')
46 with c.image.create(name=name,
47 file=SOURCE_IMAGE,
48 container_format='bare',
49 disk_format='qcow2',
50 tags=['tag1'],
51 os_type='linux') as m:
52 LOG.debug("wait for created")
53 m.wait_for_finished()
54
55 test("Image name", m.name == name)
56 test("Image size", m.size == os.stat(SOURCE_IMAGE).st_size)
57 test("Image checksum", m.checksum == get_md5(SOURCE_IMAGE))
58 test("Image is active", m.status == 'active')
59 test("Image is private", not m.is_public)
60 test("Container format is bare", m.container_format == 'bare')
61 test("Disk format is qcow2", m.disk_format == 'qcow2')
62 test("Extended attribute (os_type)", m.os_type == 'linux')
63 test("Invalid attribute returns None", m.foo is None)
64
65 LOG.info("Update image properties")
66
67 name = get_random_str('image')
68 m.update(name=name, os_type='windows')
69
70 test("Image name", m.name == name)
71 test("Extended attribute (os_type)", m.os_type == 'windows')
72
73 with tempfile.NamedTemporaryFile() as f:
74
75 LOG.info("Download image into a temporary file: %s", f.name)
76 m.download(file=f.name)
77
78 test("Downloaded image size", m.size == os.stat(f.name).st_size)
79 test("Downloaded image checksum", m.checksum == get_md5(f.name))
80
81 LOG.info("Deactivate an image")
82 m.deactivate()
83
84 test("Image is deactivated", m.status == 'deactivated')
85
86 LOG.info("Activate an image")
87 m.activate()
88
89 test("Image is active", m.status == 'active')
90
91 test("Image tags: tag1", m.tags == ['tag1'])
92
93 LOG.info("Add a tag to a image")
94 m.add_tag('tag2')
95
96 test("Image tags: tag1, tag2", set(m.tags) == set(['tag1', 'tag2']))
97
98 LOG.info("Remove a tag to a image")
99 m.remove_tag('tag1')
100
101 test("Image tags: tag2", m.tags == ['tag2'])
102
103
104 if __name__ == '__main__':
105 c = utils.get_client()
106
107 LOG.debug("list images: %s", [_.name for _ in c.image.list()])
108 main(c)
109 LOG.debug("list images: %s", [_.name for _ in c.image.list()])
110
111 show_test_summary()
112