summaryrefslogtreecommitdiff
path: root/tests/unit/models_images_test.py
blob: 392c58d79ff98962810328520dd9b06dc1b516d2 (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
from docker.models.images import Image
import unittest

from .fake_api import FAKE_IMAGE_ID
from .fake_api_client import make_fake_client


class ImageCollectionTest(unittest.TestCase):
    def test_build(self):
        client = make_fake_client()
        image = client.images.build()
        client.api.build.assert_called_with()
        client.api.inspect_image.assert_called_with(FAKE_IMAGE_ID)
        assert isinstance(image, Image)
        assert image.id == FAKE_IMAGE_ID

    def test_get(self):
        client = make_fake_client()
        image = client.images.get(FAKE_IMAGE_ID)
        client.api.inspect_image.assert_called_with(FAKE_IMAGE_ID)
        assert isinstance(image, Image)
        assert image.id == FAKE_IMAGE_ID

    def test_list(self):
        client = make_fake_client()
        images = client.images.list(all=True)
        client.api.images.assert_called_with(all=True, name=None, filters=None)
        assert len(images) == 1
        assert isinstance(images[0], Image)
        assert images[0].id == FAKE_IMAGE_ID

    def test_load(self):
        client = make_fake_client()
        client.images.load('byte stream')
        client.api.load_image.assert_called_with('byte stream')

    def test_pull(self):
        client = make_fake_client()
        image = client.images.pull('test_image')
        client.api.pull.assert_called_with('test_image')
        client.api.inspect_image.assert_called_with('test_image')
        assert isinstance(image, Image)
        assert image.id == FAKE_IMAGE_ID

    def test_push(self):
        client = make_fake_client()
        client.images.push('foobar', insecure_registry=True)
        client.api.push.assert_called_with(
            'foobar',
            tag=None,
            insecure_registry=True
        )

    def test_remove(self):
        client = make_fake_client()
        client.images.remove('test_image')
        client.api.remove_image.assert_called_with('test_image')

    def test_search(self):
        client = make_fake_client()
        client.images.search('test')
        client.api.search.assert_called_with('test')


class ImageTest(unittest.TestCase):
    def test_short_id(self):
        image = Image(attrs={'Id': 'sha256:b6846070672ce4e8f1f91564ea6782bd675'
                                   'f69d65a6f73ef6262057ad0a15dcd'})
        assert image.short_id == 'sha256:b684607067'

        image = Image(attrs={'Id': 'b6846070672ce4e8f1f91564ea6782bd675'
                                   'f69d65a6f73ef6262057ad0a15dcd'})
        assert image.short_id == 'b684607067'

    def test_tags(self):
        image = Image(attrs={
            'RepoTags': ['test_image:latest']
        })
        assert image.tags == ['test_image:latest']

        image = Image(attrs={
            'RepoTags': ['<none>:<none>']
        })
        assert image.tags == []

    def test_history(self):
        client = make_fake_client()
        image = client.images.get(FAKE_IMAGE_ID)
        image.history()
        client.api.history.assert_called_with(FAKE_IMAGE_ID)

    def test_save(self):
        client = make_fake_client()
        image = client.images.get(FAKE_IMAGE_ID)
        image.save()
        client.api.get_image.assert_called_with(FAKE_IMAGE_ID)

    def test_tag(self):
        client = make_fake_client()
        image = client.images.get(FAKE_IMAGE_ID)
        image.tag('foo')
        client.api.tag.assert_called_with(FAKE_IMAGE_ID, 'foo', tag=None)