|
2 | 2 | # Copyright (c) Microsoft Corporation. All rights reserved. |
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | | - |
6 | | -from azure.cli.testsdk import (ScenarioTest) |
7 | | -from azure.cli.testsdk.reverse_dependency import ( |
8 | | - get_dummy_cli, |
9 | | -) |
10 | | -from .custom_preparers import (SpringPreparer, SpringResourceGroupPreparer, SpringAppNamePreparer, |
11 | | - SpringSubResourceWrapper) |
12 | | -from .custom_dev_setting_constant import SpringTestEnvironmentEnum |
13 | | - |
14 | | - |
15 | | -# pylint: disable=line-too-long |
16 | | -# pylint: disable=too-many-lines |
17 | | - |
18 | | -class TearDown(SpringSubResourceWrapper): |
19 | | - def __init__(self, |
20 | | - resource_group_parameter_name='resource_group', |
21 | | - spring_parameter_name='spring'): |
22 | | - super(TearDown, self).__init__() |
23 | | - self.cli_ctx = get_dummy_cli() |
24 | | - self.resource_group_parameter_name = resource_group_parameter_name |
25 | | - self.spring_parameter_name = spring_parameter_name |
26 | | - |
27 | | - def create_resource(self, *_, **kwargs): |
28 | | - self.resource_group = self._get_resource_group(**kwargs) |
29 | | - self.spring = self._get_spring(**kwargs) |
30 | | - |
31 | | - def remove_resource(self, *_, **__): |
32 | | - self.live_only_execute(self.cli_ctx, |
33 | | - 'spring application-configuration-service delete -g {} -s {} --yes'.format( |
34 | | - self.resource_group, self.spring)) |
35 | | - self.live_only_execute(self.cli_ctx, 'spring application-configuration-service create -g {} -s {}'.format( |
36 | | - self.resource_group, self.spring)) |
37 | | - |
38 | | - |
39 | | -class ApplicationConfigurationServiceTest(ScenarioTest): |
40 | | - |
41 | | - @SpringResourceGroupPreparer( |
42 | | - dev_setting_name=SpringTestEnvironmentEnum.ENTERPRISE_WITH_TANZU['resource_group_name']) |
43 | | - @SpringPreparer(**SpringTestEnvironmentEnum.ENTERPRISE_WITH_TANZU['spring']) |
44 | | - @SpringAppNamePreparer() |
45 | | - @TearDown() |
46 | | - def test_application_configuration_service(self, resource_group, spring, app): |
47 | | - self.kwargs.update({ |
48 | | - 'serviceName': spring, |
49 | | - 'rg': resource_group, |
50 | | - 'repo': "repo1", |
51 | | - "label": "main", |
52 | | - "patterns": "api-gateway,customers-service", |
53 | | - "uri": "https://github.com/spring-petclinic/spring-petclinic-microservices-config", |
54 | | - "app": app |
55 | | - }) |
56 | | - |
57 | | - self.cmd('spring app create -g {rg} -s {serviceName} -n {app}') |
58 | | - |
59 | | - self.cmd('spring application-configuration-service show -g {rg} -s {serviceName}', checks=[ |
60 | | - self.check('properties.provisioningState', "Succeeded") |
61 | | - ]) |
62 | | - |
63 | | - self.cmd('spring application-configuration-service git repo add -g {rg} -s {serviceName} ' |
64 | | - '-n {repo} --label {label} --patterns {patterns} --uri {uri}', |
65 | | - checks=[self.check('properties.provisioningState', "Succeeded")]) |
66 | | - |
67 | | - self.cmd('spring application-configuration-service git repo update -g {rg} -s {serviceName} ' |
68 | | - '-n {repo} --label {label}', |
69 | | - checks=[self.check('properties.provisioningState', "Succeeded")]) |
70 | | - |
71 | | - result = self.cmd( |
72 | | - 'spring application-configuration-service git repo list -g {rg} -s {serviceName}').get_output_in_json() |
73 | | - self.assertTrue(len(result) > 0) |
74 | | - |
75 | | - self.cmd('spring application-configuration-service git repo remove --name {repo} -g {rg} -s {serviceName}') |
76 | | - result = self.cmd( |
77 | | - 'spring application-configuration-service git repo list -g {rg} -s {serviceName}').get_output_in_json() |
78 | | - self.assertTrue(len(result) == 0) |
79 | | - |
80 | | - self.cmd('spring application-configuration-service bind --app {app} -g {rg} -s {serviceName}', checks=[ |
81 | | - self.check('properties.addonConfigs.applicationConfigurationService.resourceId', |
82 | | - "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.AppPlatform/Spring/{}/configurationServices/default".format( |
83 | | - self.get_subscription_id(), resource_group, spring)) |
84 | | - ]) |
85 | | - |
86 | | - self.cmd('spring app show -n {app} -g {rg} -s {serviceName}') |
87 | | - |
88 | | - self.cmd('spring application-configuration-service unbind --app {app} -g {rg} -s {serviceName}') |
89 | | - |
90 | | - self.cmd('spring application-configuration-service clear -g {rg} -s {serviceName}', checks=[ |
91 | | - self.check('properties.provisioningState', "Succeeded") |
92 | | - ]) |
93 | | - |
94 | | - self.cmd('spring application-configuration-service update -g {rg} -s {serviceName} --generation Gen2', |
95 | | - checks=[self.check('properties.provisioningState', "Succeeded")]) |
| 5 | +from ...application_configuration_service import (application_configuration_service_create, |
| 6 | + application_configuration_service_update) |
| 7 | + |
| 8 | +import unittest |
| 9 | +from argparse import Namespace |
| 10 | +from azure.cli.core.azclierror import InvalidArgumentValueError, ArgumentUsageError |
| 11 | +from ..._validators_enterprise import validate_refresh_interval |
| 12 | +try: |
| 13 | + import unittest.mock as mock |
| 14 | +except ImportError: |
| 15 | + from unittest import mock |
| 16 | + |
| 17 | +from azure.cli.core.mock import DummyCli |
| 18 | +from azure.cli.core import AzCommandsLoader |
| 19 | +from azure.cli.core.commands import AzCliCommand |
| 20 | + |
| 21 | +from knack.log import get_logger |
| 22 | + |
| 23 | +logger = get_logger(__name__) |
| 24 | +free_mock_client = mock.MagicMock() |
| 25 | + |
| 26 | + |
| 27 | +def _get_test_cmd(): |
| 28 | + cli_ctx = DummyCli() |
| 29 | + cli_ctx.data['subscription_id'] = '00000000-0000-0000-0000-000000000000' |
| 30 | + loader = AzCommandsLoader(cli_ctx, resource_type='Microsoft.AppPlatform') |
| 31 | + cmd = AzCliCommand(loader, 'test', None) |
| 32 | + cmd.command_kwargs = {'resource_type': 'Microsoft.AppPlatform'} |
| 33 | + cmd.cli_ctx = cli_ctx |
| 34 | + return cmd |
| 35 | + |
| 36 | + |
| 37 | +def _cf_resource_group(cli_ctx, subscription_id=None): |
| 38 | + client = mock.MagicMock() |
| 39 | + rg = mock.MagicMock() |
| 40 | + rg.location = 'east us' |
| 41 | + client.resource_groups.get.return_value = rg |
| 42 | + return client |
| 43 | + |
| 44 | + |
| 45 | +def _get_basic_mock_client(*_): |
| 46 | + return mock.MagicMock() |
| 47 | + |
| 48 | + |
| 49 | +class BasicTest(unittest.TestCase): |
| 50 | + def __init__(self, methodName: str = ...): |
| 51 | + super().__init__(methodName=methodName) |
| 52 | + self.created_resource = None |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + resp = super().setUp() |
| 56 | + free_mock_client.reset_mock() |
| 57 | + return resp |
| 58 | + |
| 59 | + @mock.patch('azext_spring._utils.cf_resource_groups', _cf_resource_group) |
| 60 | + def _execute(self, resource_group, generation, refresh_interval, **kwargs): |
| 61 | + client = kwargs.pop('client', None) or _get_basic_mock_client() |
| 62 | + application_configuration_service_create(_get_test_cmd(), client, 'myasa', |
| 63 | + resource_group, generation, refresh_interval) |
| 64 | + call_args = client.configuration_services.begin_create_or_update.call_args_list |
| 65 | + self.assertEqual(1, len(call_args)) |
| 66 | + self.assertEqual(4, len(call_args[0][0])) |
| 67 | + self.assertEqual((resource_group, generation), |
| 68 | + (call_args[0][0][0], call_args[0][0][3].properties.generation)) |
| 69 | + self.created_resource = call_args[0][0][3] |
| 70 | + |
| 71 | + |
| 72 | +class TestApplicationConfigurationService(BasicTest): |
| 73 | + def test_acs_create(self): |
| 74 | + self._execute('rg', 'Gen1', 120) |
| 75 | + resource = self.created_resource |
| 76 | + self.assertIsNotNone(resource.properties) |
| 77 | + self.assertEqual(120, resource.properties.settings.refresh_interval_in_seconds) |
| 78 | + |
| 79 | + |
| 80 | +class TestApplicationConfigurationServiceValidator(unittest.TestCase): |
| 81 | + def test_validate_refresh_interval_parameter(self): |
| 82 | + ns = Namespace(refresh_interval="a") |
| 83 | + with self.assertRaises(InvalidArgumentValueError) as context: |
| 84 | + validate_refresh_interval(ns) |
| 85 | + self.assertEqual("--refresh-interval should be a number.", str(context.exception)) |
| 86 | + |
| 87 | + ns = Namespace(refresh_interval=-1) |
| 88 | + with self.assertRaises(ArgumentUsageError) as context: |
| 89 | + validate_refresh_interval(ns) |
| 90 | + self.assertEqual("--refresh-interval must be greater than or equal to 0.", str(context.exception)) |
0 commit comments