summaryrefslogtreecommitdiff
path: root/test/python/dockerpy/tests/test_images.py
blob: 5eae61c2f518771355a210f81cb7feb7bedf631d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import stat
import unittest
from os import remove
from stat import ST_SIZE

import docker
import requests

from . import common, constant

client = common.get_client()


class TestImages(unittest.TestCase):
    def setUp(self):
        super().setUp()
        common.restore_image_from_cache(self)

    def tearDown(self):
        common.remove_all_images()
        return super().tearDown()

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        common.enable_sock(cls)

    @classmethod
    def tearDownClass(cls):
        common.terminate_connection(cls)
        common.flush_image_cache(cls)
        return super().tearDownClass()

# Inspect Image

    def test_inspect_image(self):
        # Check for error with wrong image name
        with self.assertRaises(requests.HTTPError):
            client.inspect_image("dummy")
        alpine_image = client.inspect_image(constant.ALPINE)
        self.assertIn(constant.ALPINE, alpine_image["RepoTags"])

# Tag Image

# Validates if invalid image name is given a bad response is encountered.

    def test_tag_invalid_image(self):
        with self.assertRaises(requests.HTTPError):
            client.tag("dummy", "demo")

    #  Validates if the image is tagged successfully.
    def test_tag_valid_image(self):
        client.tag(constant.ALPINE, "demo", constant.ALPINE_SHORTNAME)
        alpine_image = client.inspect_image(constant.ALPINE)
        for x in alpine_image["RepoTags"]:
            if ("demo:alpine" in x):
                self.assertTrue
        self.assertFalse

    # Validates if name updates when the image is retagged.
    @unittest.skip("dosent work now")
    def test_retag_valid_image(self):
        client.tag(constant.ALPINE_SHORTNAME, "demo", "rename")
        alpine_image = client.inspect_image(constant.ALPINE)
        self.assertNotIn("demo:test", alpine_image["RepoTags"])

# List Image
# List All Images

    def test_list_images(self):
        allImages = client.images()
        self.assertEqual(len(allImages), 1)
        # Add more images
        client.pull(constant.BB)
        allImages = client.images()
        self.assertEqual(len(allImages), 2)

        # List images with filter
        filters = {'reference': 'alpine'}
        allImages = client.images(filters=filters)
        self.assertEqual(len(allImages), 1)

# Search Image

    def test_search_image(self):
        response = client.search("alpine")
        for i in response:
            # Alpine found
            if "docker.io/library/alpine" in i["Name"]:
                self.assertTrue
        self.assertFalse

# Image Exist (No docker-py support yet)

# Remove Image

    def test_remove_image(self):
        # Check for error with wrong image name
        with self.assertRaises(requests.HTTPError):
            client.remove_image("dummy")
        allImages = client.images()
        self.assertEqual(len(allImages), 1)
        alpine_image = client.inspect_image(constant.ALPINE)
        client.remove_image(alpine_image)
        allImages = client.images()
        self.assertEqual(len(allImages), 0)

# Image History

    def test_image_history(self):
        # Check for error with wrong image name
        with self.assertRaises(requests.HTTPError):
            client.history("dummy")

        imageHistory = client.history(constant.ALPINE)
        alpine_image = client.inspect_image(constant.ALPINE)
        for h in imageHistory:
            if h["Id"] in alpine_image["Id"]:
                self.assertTrue
        self.assertFalse

# Prune Image (No docker-py support yet)

    def test_get_image_dummy(self):
        # FIXME: seems to be an error in the library
        self.skipTest("Documentation and library do not match")
        # Check for error with wrong image name
        with self.assertRaises(docker.errors.ImageNotFound):
            client.get_image("dummy")

# Export Image

    def test_export_image(self):
        client.pull(constant.BB)
        if not os.path.exists(constant.ImageCacheDir):
            os.makedirs(constant.ImageCacheDir)

        image = client.get_image(constant.BB)

        file = os.path.join(constant.ImageCacheDir, "busybox.tar")
        tarball = open(file, mode="wb")
        for frame in image:
            tarball.write(frame)
        tarball.close()
        sz = os.path.getsize(file)
        self.assertGreater(sz, 0)


# Import|Load Image

    def test_import_image(self):
        allImages = client.images()
        self.assertEqual(len(allImages), 1)
        file = os.path.join(constant.ImageCacheDir, "alpine.tar")
        client.import_image_from_file(filename=file)
        allImages = client.images()
        self.assertEqual(len(allImages), 2)

if __name__ == '__main__':
    # Setup temporary space
    unittest.main()