test_azure_container_instance.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. import json
  19. import unittest
  20. from unittest.mock import patch
  21. from azure.mgmt.containerinstance.models import (
  22. Container,
  23. ContainerGroup,
  24. Logs,
  25. ResourceRequests,
  26. ResourceRequirements,
  27. )
  28. from airflow.models import Connection
  29. from airflow.providers.microsoft.azure.hooks.container_instance import AzureContainerInstanceHook
  30. from airflow.utils import db
  31. class TestAzureContainerInstanceHook(unittest.TestCase):
  32. def setUp(self):
  33. db.merge_conn(
  34. Connection(
  35. conn_id='azure_container_instance_test',
  36. conn_type='azure_container_instances',
  37. login='login',
  38. password='key',
  39. extra=json.dumps({'tenantId': 'tenant_id', 'subscriptionId': 'subscription_id'}),
  40. )
  41. )
  42. self.resources = ResourceRequirements(requests=ResourceRequests(memory_in_gb='4', cpu='1'))
  43. with patch(
  44. 'azure.common.credentials.ServicePrincipalCredentials.__init__', autospec=True, return_value=None
  45. ):
  46. with patch('azure.mgmt.containerinstance.ContainerInstanceManagementClient'):
  47. self.hook = AzureContainerInstanceHook(conn_id='azure_container_instance_test')
  48. @patch('azure.mgmt.containerinstance.models.ContainerGroup')
  49. @patch('azure.mgmt.containerinstance.operations.ContainerGroupsOperations.create_or_update')
  50. def test_create_or_update(self, create_or_update_mock, container_group_mock):
  51. self.hook.create_or_update('resource_group', 'aci-test', container_group_mock)
  52. create_or_update_mock.assert_called_once_with('resource_group', 'aci-test', container_group_mock)
  53. @patch('azure.mgmt.containerinstance.operations.ContainerGroupsOperations.get')
  54. def test_get_state(self, get_state_mock):
  55. self.hook.get_state('resource_group', 'aci-test')
  56. get_state_mock.assert_called_once_with('resource_group', 'aci-test', raw=False)
  57. @patch('azure.mgmt.containerinstance.operations.ContainerOperations.list_logs')
  58. def test_get_logs(self, list_logs_mock):
  59. expected_messages = ['log line 1\n', 'log line 2\n', 'log line 3\n']
  60. logs = Logs(content=''.join(expected_messages))
  61. list_logs_mock.return_value = logs
  62. logs = self.hook.get_logs('resource_group', 'name', 'name')
  63. assert logs == expected_messages
  64. @patch('azure.mgmt.containerinstance.operations.ContainerGroupsOperations.delete')
  65. def test_delete(self, delete_mock):
  66. self.hook.delete('resource_group', 'aci-test')
  67. delete_mock.assert_called_once_with('resource_group', 'aci-test')
  68. @patch('azure.mgmt.containerinstance.operations.ContainerGroupsOperations.list_by_resource_group')
  69. def test_exists_with_existing(self, list_mock):
  70. list_mock.return_value = [
  71. ContainerGroup(
  72. os_type='Linux',
  73. containers=[Container(name='test1', image='hello-world', resources=self.resources)],
  74. )
  75. ]
  76. assert not self.hook.exists('test', 'test1')
  77. @patch('azure.mgmt.containerinstance.operations.ContainerGroupsOperations.list_by_resource_group')
  78. def test_exists_with_not_existing(self, list_mock):
  79. list_mock.return_value = [
  80. ContainerGroup(
  81. os_type='Linux',
  82. containers=[Container(name='test1', image='hello-world', resources=self.resources)],
  83. )
  84. ]
  85. assert not self.hook.exists('test', 'not found')