summaryrefslogtreecommitdiffstats
path: root/tests/docker_source.py
blob: 60404e699b000e338442d231412d11a985e41847 (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
# -*- coding: utf-8 -*-
# Authors: Radostin Stoyanov <rstoyanov1@gmail.com>
#
# Copyright (C) 2017 Radostin Stoyanov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Tests which aim is to exercise creation of root file system with DockerSource.
"""

import unittest

from . import mock
from . import sources


# pylint: disable=invalid-name
class TestDockerSource(unittest.TestCase):
    """
    Unit tests for DockerSource
    """
    ###################################
    # Tests for: retrieve_layers_info()
    ###################################
    def _mock_retrieve_layers_info(self, manifest, kwargs):
        """
        This method is gather common test pattern used in the following
        two test cases which aim to return an instance of the class
        DockerSource with some util functions being mocked.
        """
        with mock.patch.multiple('virtBootstrap.utils',
                                 get_image_details=mock.DEFAULT,
                                 get_image_dir=mock.DEFAULT) as m_utils:

            m_utils['get_image_details'].return_value = manifest
            m_utils['get_image_dir'].return_value = '/images_path'

            patch_method = 'virtBootstrap.sources.DockerSource.gen_valid_uri'
            with mock.patch(patch_method) as m_uri:
                src_instance = sources.DockerSource(**kwargs)
        return (src_instance, m_uri, m_utils)

    def test_retrieve_layers_info_pass_arguments_to_get_image_details(self):
        """
        Ensures that retrieve_layers_info() calls get_image_details()
        with all passed arguments.
        """
        src_kwargs = {
            'uri': '',
            'progress': mock.Mock()
        }

        manifest = {'schemaVersion': 2, 'layers': []}
        (src_instance,
         m_uri, m_utils) = self._mock_retrieve_layers_info(manifest,
                                                           src_kwargs)

        kwargs = {
            'insecure': src_instance.insecure,
            'username': src_instance.username,
            'password': src_instance.password,
            'raw': True
        }
        m_utils['get_image_details'].assert_called_once_with(m_uri(), **kwargs)

    def test_retrieve_layers_info_schema_version_1(self):
        """
        Ensures that retrieve_layers_info() extracts the layers' information
        from manifest with schema version 1 a list with format:
            ["digest", "sum_type", "file_path", "size"].
        """
        kwargs = {
            'uri': '',
            'progress': mock.Mock()
        }

        manifest = {
            'schemaVersion': 1,
            'fsLayers': [
                {'blobSum': 'sha256:75c416ea'},
                {'blobSum': 'sha256:c6ff40b6'},
                {'blobSum': 'sha256:a7050fc1'}
            ]
        }

        expected_result = [
            ['sha256', 'a7050fc1', '/images_path/a7050fc1.tar', None],
            ['sha256', 'c6ff40b6', '/images_path/c6ff40b6.tar', None],
            ['sha256', '75c416ea', '/images_path/75c416ea.tar', None]
        ]

        with mock.patch('os.path.getsize') as m_getsize:
            m_getsize.return_value = None
            src_instance = self._mock_retrieve_layers_info(manifest, kwargs)[0]
        self.assertEqual(src_instance.layers, expected_result)

    def test_retrieve_layers_info_schema_version_2(self):
        """
        Ensures that retrieve_layers_info() extracts the layers' information
        from manifest with schema version 2 a list with format:
            ["digest", "sum_type", "file_path", "size"].
        """
        kwargs = {
            'uri': '',
            'progress': mock.Mock()
        }

        manifest = {
            'schemaVersion': 2,
            "layers": [
                {"size": 47103294, "digest": "sha256:75c416ea"},
                {"size": 814, "digest": "sha256:c6ff40b6"},
                {"size": 513, "digest": "sha256:a7050fc1"}
            ]
        }

        expected_result = [
            ['sha256', '75c416ea', '/images_path/75c416ea.tar', 47103294],
            ['sha256', 'c6ff40b6', '/images_path/c6ff40b6.tar', 814],
            ['sha256', 'a7050fc1', '/images_path/a7050fc1.tar', 513]
        ]

        src_instance = self._mock_retrieve_layers_info(manifest, kwargs)[0]
        self.assertEqual(src_instance.layers, expected_result)

    def test_retrieve_layers_info_raise_error_on_invalid_schema_version(self):
        """
        Ensures that retrieve_layers_info() calls get_image_details()
        with all passed arguments.
        """
        kwargs = {
            'uri': '',
            'progress': mock.Mock()
        }

        manifest = {'schemaVersion': 3}
        with self.assertRaises(ValueError):
            self._mock_retrieve_layers_info(manifest, kwargs)