def test_publish_functions_package_consumption(self, _1):
        function_app_name = 'cloud-custodian-test-consumption%s' % self.subscription_id[
            -12:]
        parameters = FunctionAppUtilities.FunctionAppInfrastructureParameters(
            app_insights={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': 'cloud-custodian-test'
            },
            storage_account={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': self.storage_name
            },
            service_plan={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': 'cloud-custodian-test',
                'sku_tier': 'dynamic'
            },
            function_app_resource_group_name=CONST_GROUP_NAME,
            function_app_name=function_app_name)

        package = FunctionPackage("TestPolicy")
        package.pkg = AzurePythonPackageArchive()
        package.close()

        FunctionAppUtilities.publish_functions_package(parameters, package)

        # verify app setting updated
        wc = self.session.client('azure.mgmt.web.WebSiteManagementClient')
        app_settings = wc.web_apps.list_application_settings(
            CONST_GROUP_NAME, function_app_name)
        self.assertIsNotNone(
            app_settings.properties['WEBSITE_RUN_FROM_PACKAGE'])
    def test_event_package_files(self, session_mock):
        p = self.load_policy({
            'name': 'test-azure-package',
            'resource': 'azure.resourcegroup',
            'mode': {
                'type': FUNCTION_EVENT_TRIGGER_MODE,
                'events': ['VmWrite']
            },
        })

        packer = FunctionPackage(p.data['name'])
        packer.pkg = AzurePythonPackageArchive()

        packer._add_functions_required_files(p.data, 'test-queue')
        files = packer.pkg._zip_file.filelist

        self.assertTrue(
            FunctionPackageTest._file_exists(files,
                                             'test-azure-package/function.py'))
        self.assertTrue(
            FunctionPackageTest._file_exists(files,
                                             'test-azure-package/__init__.py'))
        self.assertTrue(
            FunctionPackageTest._file_exists(
                files, 'test-azure-package/function.json'))
        self.assertTrue(
            FunctionPackageTest._file_exists(files,
                                             'test-azure-package/config.json'))
        self.assertTrue(FunctionPackageTest._file_exists(files, 'host.json'))
    def test_auth_file_user_assigned_identity(self):
        p = self.load_policy({
            'name': 'test-azure-public-ip',
            'resource': 'azure.publicip',
            'mode': {
                'type': FUNCTION_EVENT_TRIGGER_MODE,
                'provision-options': {
                    'identity': {
                        'type': 'SystemAssigned'
                    }
                },
                'events': ['PublicIpWrite']
            }
        })
        packer = FunctionPackage(p.data['name'])
        packer.pkg = AzurePythonPackageArchive()
        packer._add_functions_required_files(p.data, 'c7n-azure==1.0',
                                             'test-queue')

        packer.pkg.close()
        with zipfile.ZipFile(packer.pkg.path) as zf:
            content = json.loads(zf.read('test-azure-public-ip/auth.json'))
            self.assertEqual(content, {
                'subscription_id': None,
                'use_msi': True
            })
    def test_publish_functions_package_dedicated(self, _1):
        parameters = FunctionAppUtilities.FunctionAppInfrastructureParameters(
            app_insights={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': 'cloud-custodian-test'
            },
            storage_account={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': self.storage_name
            },
            service_plan={
                'id': '',
                'resource_group_name': CONST_GROUP_NAME,
                'name': 'cloud-custodian-test',
                'sku_tier': 'Basic'
            },
            function_app={
                'resource_group_name': CONST_GROUP_NAME,
                'name': self.dedicated_function_name})

        package = FunctionPackage("TestPolicy")
        package.pkg = AzurePythonPackageArchive()
        package.close()

        FunctionAppUtilities.publish_functions_package(parameters, package)
    def test_no_policy_add_required_files(self, session_mock):
        """ Tools such as mailer will package with no policy """

        packer = FunctionPackage('name')
        packer.pkg = AzurePythonPackageArchive()

        packer._add_functions_required_files(None)
        files = packer.pkg._zip_file.filelist

        self.assertTrue(FunctionPackageTest._file_exists(files, 'host.json'))
    def test_add_host_config(self):
        packer = FunctionPackage('test')
        packer.pkg = AzurePythonPackageArchive()
        with patch('c7n_azure.function_package.AzurePythonPackageArchive.add_contents') as mock:
            packer._add_host_config(FUNCTION_EVENT_TRIGGER_MODE)
            mock.assert_called_once()
            self.assertEqual(mock.call_args[1]['dest'], 'host.json')
            self.assertTrue('extensionBundle' in json.loads(mock.call_args[1]['contents']))

        with patch('c7n_azure.function_package.AzurePythonPackageArchive.add_contents') as mock:
            packer._add_host_config(FUNCTION_TIME_TRIGGER_MODE)
            mock.assert_called_once()
            self.assertEqual(mock.call_args[1]['dest'], 'host.json')
            self.assertFalse('extensionBundle' in json.loads(mock.call_args[1]['contents']))
Beispiel #7
0
    def test_add_function_config_eventhub(self):
        p = self.load_policy({
            'name': 'test-azure-public-ip',
            'resource': 'azure.publicip',
            'mode': {
                'type': 'azure-stream'
            }
        })

        packer = FunctionPackage(p.data)
        packer.pkg = mock.MagicMock()

        packer._add_function_config()

        binding = json.loads(packer.pkg.add_contents.call_args[1]['contents'])

        self.assertEqual(binding['bindings'][0]['type'], 'eventHubTrigger')
    def test_publish(self, post_mock):
        status_mock = MagicMock()
        post_mock.return_value = status_mock
        packer = FunctionPackage('test')
        packer.pkg = AzurePythonPackageArchive()
        creds = User(publishing_user_name='user',
                     publishing_password='******',
                     scm_uri='https://uri')

        packer.publish(creds)

        post_mock.assert_called_once()
        status_mock.raise_for_status.assert_called_once()

        self.assertEqual(post_mock.call_args[0][0],
                         'https://uri/api/zipdeploy?isAsync=true&synctriggers=true')
        self.assertEqual(post_mock.call_args[1]['headers']['content-type'],
                         'application/octet-stream')
Beispiel #9
0
    def test_add_function_config_periodic(self):
        p = self.load_policy({
            'name': 'test-azure-public-ip',
            'resource': 'azure.publicip',
            'mode': {
                'type': 'azure-periodic',
                'schedule': '0 1 0 0 0'
            }
        })

        packer = FunctionPackage(p.data)
        packer.pkg = mock.MagicMock()

        packer._add_function_config()

        binding = json.loads(packer.pkg.add_contents.call_args[1]['contents'])

        self.assertEqual(binding['bindings'][0]['type'], 'timerTrigger')
        self.assertEqual(binding['bindings'][0]['name'], 'input')
        self.assertEqual(binding['bindings'][0]['schedule'], '0 1 0 0 0')
Beispiel #10
0
    def test_add_policy(self):
        p = self.load_policy({
            'name': 'test-azure-public-ip',
            'resource': 'azure.publicip',
            'mode': {
                'type': 'azure-stream'
            }
        })

        packer = FunctionPackage(p.data)
        packer.pkg = mock.MagicMock()

        packer._add_policy()

        policy = json.loads(packer.pkg.add_contents.call_args[1]['contents'])

        self.assertEqual(
            policy, {
                u'resource': u'azure.publicip',
                u'name': u'test-azure-public-ip',
                u'mode': {
                    u'type': u'azure-stream'
                }
            })