summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/tests/cli/test_services.py
blob: 7dc84bc793b1f0db4ff207b04a2d5cb53519c680 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 pytest
import mock

from aria.cli.env import _Environment
from aria.core import Core
from aria.exceptions import DependentActiveExecutionsError, DependentAvailableNodesError
from aria.modeling.exceptions import ParameterException
from aria.storage import exceptions as storage_exceptions

from .base_test import (  # pylint: disable=unused-import
    TestCliBase,
    raise_exception,
    assert_exception_raised,
    mock_storage
)
from ..mock import models as mock_models


class TestServicesList(TestCliBase):

    @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [
        ('', '', 'created_at', 'asc'),
        ('', ' --descending', 'created_at', 'desc'),
        (' --sort-by name', '', 'name', 'asc'),
        (' --sort-by name', ' --descending', 'name', 'desc')
    ])
    def test_no_specified_service_template(self, monkeypatch, mock_storage, sort_by, order,
                                           sort_by_in_output, order_in_output):

        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services list{sort_by}{order}'.format(sort_by=sort_by, order=order))
        assert 'Listing all services...' in self.logger_output_string
        assert 'Listing services for service template' not in self.logger_output_string

        mock_storage.service.list.assert_called_once_with(sort={sort_by_in_output: order_in_output},
                                                          filters={})
        assert 'Services:' in self.logger_output_string
        assert mock_models.SERVICE_TEMPLATE_NAME in self.logger_output_string
        assert mock_models.SERVICE_NAME in self.logger_output_string

    @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [
        ('', '', 'created_at', 'asc'),
        ('', ' --descending', 'created_at', 'desc'),
        (' --sort-by name', '', 'name', 'asc'),
        (' --sort-by name', ' --descending', 'name', 'desc')
    ])
    def test_specified_service_template(self, monkeypatch, mock_storage, sort_by, order,
                                        sort_by_in_output, order_in_output):

        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services list -t test_st{sort_by}{order}'.format(sort_by=sort_by, order=order))
        assert 'Listing services for service template test_st...' in self.logger_output_string
        assert 'Listing all services...' not in self.logger_output_string

        mock_storage.service.list.assert_called_once_with(sort={sort_by_in_output: order_in_output},
                                                          filters={'service_template': mock.ANY})
        assert 'Services:' in self.logger_output_string
        assert mock_models.SERVICE_TEMPLATE_NAME in self.logger_output_string
        assert mock_models.SERVICE_NAME in self.logger_output_string


class TestServicesCreate(TestCliBase):

    def test_header_string(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services create -t test_st test_s')
        assert 'Creating new service from service template test_st...' in self.logger_output_string

    def test_no_exception(self, monkeypatch, mock_storage):

        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)

        m = mock.MagicMock(return_value=mock_models.create_service_with_dependencies())
        monkeypatch.setattr(Core, 'create_service', m)
        self.invoke('services create -t test_st test_s')
        assert "Service created. The service's name is test_s" in self.logger_output_string

    def test_raises_storage_error_resulting_from_name_uniqueness(self, monkeypatch,
                                                                 mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        monkeypatch.setattr(Core,
                            'create_service',
                            raise_exception(storage_exceptions.NotFoundError,
                                            msg='UNIQUE constraint failed'))
        assert_exception_raised(
            self.invoke('services create -t test_st test_s'),
            expected_exception=storage_exceptions.NotFoundError,
            expected_msg='There already a exists a service with the same name')

        assert "Service created. The service's name is test_s" not in self.logger_output_string

    def test_raises_other_storage_error(self, monkeypatch, mock_object):
        monkeypatch.setattr(_Environment, 'model_storage', mock_object)
        monkeypatch.setattr(Core,
                            'create_service',
                            raise_exception(storage_exceptions.NotFoundError))

        assert_exception_raised(
            self.invoke('services create -t test_st test_s'),
            expected_exception=storage_exceptions.NotFoundError)

        assert "Service created. The service's name is test_s" not in self.logger_output_string

    def test_raises_inputs_exception(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        monkeypatch.setattr(Core,
                            'create_service',
                            raise_exception(ParameterException))

        assert_exception_raised(
            self.invoke('services create -t with_inputs test_s'),
            expected_exception=ParameterException)

        assert "Service created. The service's name is test_s" not in self.logger_output_string


class TestServicesDelete(TestCliBase):

    def test_header_string(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services delete test_s')
        assert 'Deleting service test_s...' in self.logger_output_string

    def test_delete_no_exception(self, monkeypatch, mock_storage, mock_object):

        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        monkeypatch.setattr(Core, 'delete_service', mock_object)
        self.invoke('services delete test_s')
        assert 'Service test_s deleted' in self.logger_output_string

    def test_delete_active_execution_error(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        mock_service_with_execution = \
            mock.MagicMock(return_value=mock_models.create_service_with_dependencies(
                include_execution=True))
        monkeypatch.setattr(mock_storage.service, 'get', mock_service_with_execution)
        assert_exception_raised(
            self.invoke('services delete test_s'),
            expected_exception=DependentActiveExecutionsError,
            expected_msg="Can't delete service `{name}` - there is an active execution "
                         "for this service. Active execution ID: 1".format(
                             name=mock_models.SERVICE_NAME))

    def test_delete_available_nodes_error(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        assert_exception_raised(
            self.invoke('services delete test_s'),
            expected_exception=DependentAvailableNodesError,
            expected_msg="Can't delete service `{name}` - there are available nodes "
                         "for this service. Available node IDs: 1".format(
                             name=mock_models.SERVICE_NAME))

    def test_delete_available_nodes_error_with_force(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services delete service_with_available_nodes --force')

        assert mock_storage.service.delete.call_count == 1
        assert 'Service service_with_available_nodes deleted' in self.logger_output_string


class TestServicesOutputs(TestCliBase):

    def test_header_string(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services outputs test_s')
        assert 'Showing outputs for service test_s...' in self.logger_output_string

    def test_outputs_no_outputs(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services outputs service_with_no_outputs')

        assert 'No outputs' in self.logger_output_string
        assert 'output1' not in self.logger_output_string
        assert 'value1' not in self.logger_output_string

    def test_outputs_one_output(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        s = mock_models.create_service_with_dependencies(include_output=True)
        monkeypatch.setattr(mock_storage.service, 'get_by_name', mock.MagicMock(return_value=s))

        self.invoke('services outputs test_s')

        assert 'output1' in self.logger_output_string
        assert 'value1' in self.logger_output_string
        assert 'No outputs' not in self.logger_output_string


class TestServicesInputs(TestCliBase):

    def test_header_string(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services inputs test_s')
        assert 'Showing inputs for service test_s...' in self.logger_output_string

    def test_inputs_no_inputs(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        self.invoke('services inputs service_with_no_inputs')

        assert 'No inputs' in self.logger_output_string
        assert 'input1' not in self.logger_output_string
        assert 'value1' not in self.logger_output_string

    def test_inputs_one_input(self, monkeypatch, mock_storage):
        monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
        s = mock_models.create_service_with_dependencies(include_input=True)
        monkeypatch.setattr(mock_storage.service, 'get_by_name', mock.MagicMock(return_value=s))

        self.invoke('services inputs test_s')

        assert 'input1' in self.logger_output_string
        assert 'value1' in self.logger_output_string
        assert 'No inputs' not in self.logger_output_string