summaryrefslogtreecommitdiffstats
path: root/aria/multivim-plugin/glance_plugin/tests/test.py
blob: 4a88cba4e7409ba341ea750b6148096ce3d9e949 (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
#########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

import mock
import os
import tempfile
import unittest

import glance_plugin
from glance_plugin import image

from cloudify.mocks import MockCloudifyContext
from cloudify.test_utils import workflow_test
from cloudify.exceptions import NonRecoverableError


def ctx_mock(image_dict):
    return MockCloudifyContext(
        node_id='d',
        properties=image_dict)


class TestCheckImage(unittest.TestCase):

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image': {}}))
    def test_check_image_no_file_no_url(self):
        # Test if it throws exception no file & no url
        self.assertRaises(NonRecoverableError,
                          image._validate_image)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image_url': 'test-url', 'image': {'data': '.'}}))
    def test_check_image_and_url(self):
        # Test if it throws exception file & url
        self.assertRaises(NonRecoverableError,
                          image._validate_image)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image_url': 'test-url', 'image': {}}))
    def test_check_image_url(self):
        # test if it passes no file & url
        http_connection_mock = mock.MagicMock()
        http_connection_mock.return_value.getresponse.return_value.status = 200
        with mock.patch('httplib.HTTPConnection', http_connection_mock):
            glance_plugin.image._validate_image()

    def test_check_image_file(self):
        # test if it passes file & no url
        image_file_path = tempfile.mkstemp()[1]
        with mock.patch('glance_plugin.image.ctx',
                        ctx_mock({'image': {'data': image_file_path}})):
            glance_plugin.image._validate_image()

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image': {'data': '/test/path'}}))
    # test when open file throws IO error
    def test_check_image_bad_file(self):
        open_name = '%s.open' % __name__
        with mock.patch(open_name, create=True) as mock_open:
            mock_open.side_effect = [mock_open(read_data='Data').return_value]
            self.assertRaises(NonRecoverableError,
                              glance_plugin.image._validate_image)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image_url': '?', 'image': {}}))
    # test when bad url
    def test_check_image_bad_url(self):
        http_connection_mock = mock.MagicMock()
        http_connection_mock.return_value.getresponse.return_value.status = 400
        with mock.patch('httplib.HTTPConnection', http_connection_mock):
            self.assertRaises(NonRecoverableError,
                              glance_plugin.image._validate_image)


class TestValidateProperties(unittest.TestCase):

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image': {'container_format': 'bare'}}))
    def test_check_image_container_format_no_disk_format(self):
        # Test if it throws exception no file & no url
        self.assertRaises(NonRecoverableError,
                          image._validate_image_dictionary)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image': {'disk_format': 'qcow2'}}))
    def test_check_image_no_container_format_disk_format(self):
        # Test if it throws exception no container_format & disk_format
        self.assertRaises(NonRecoverableError,
                          image._validate_image_dictionary)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock({'image': {}}))
    def test_check_image_no_container_format_no_disk_format(self):
        # Test if it throws exception no container_format & no disk_format
        self.assertRaises(NonRecoverableError,
                          image._validate_image_dictionary)

    @mock.patch('glance_plugin.image.ctx',
                ctx_mock(
                    {'image':
                        {'container_format': 'bare',
                         'disk_format': 'qcow2'}}))
    def test_check_image_container_format_disk_format(self):
        # Test if it do not throw exception container_format & disk_format
        image._validate_image_dictionary()


class TestStartImage(unittest.TestCase):
    blueprint_path = os.path.join('resources',
                                  'test-image-start.yaml')

    @mock.patch('glance_plugin.image.create')
    @workflow_test(blueprint_path, copy_plugin_yaml=True)
    def test_image_lifecycle_start(self, cfy_local, *_):
        test_vars = {
            'counter': 0,
            'image': mock.MagicMock()
        }

        def _mock_get_image_by_ctx(*_):
            i = test_vars['image']
            if test_vars['counter'] == 0:
                i.status = 'different image status'
            else:
                i.status = glance_plugin.image.IMAGE_STATUS_ACTIVE
            test_vars['counter'] += 1
            return i

        with mock.patch('openstack_plugin_common.GlanceClient'):
            with mock.patch('glance_plugin.image._get_image_by_ctx',
                            side_effect=_mock_get_image_by_ctx):
                cfy_local.execute('install', task_retries=3)

        self.assertEqual(2, test_vars['counter'])
        self.assertEqual(0, test_vars['image'].start.call_count)