def test_create_without_template(self): # call without templates with self.assertRaises(cfy_exc.NonRecoverableError) as ex: deployment.create(ctx=self.fake_ctx, name="check", azure_config=self.azure_config) self.assertEqual(str(ex.exception), "Template is not defined.")
def test_create_with_template_file(self): self.node.properties['template_file'] = "check.json" self.fake_ctx.get_resource.return_value = '{"a":"b"}' deployment.create(ctx=self.fake_ctx, name="check", location="west", params={'c': 'd'}, azure_config=self.azure_config) self.fake_ctx.get_resource.assert_called_with("check.json") self.client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': { 'c': { 'value': 'd' } }, 'mode': DeploymentMode.incremental, 'template': { 'a': 'b' } }, verify=True) async_call = self.client.deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=900)
def test_create_with_template_string(self, rg_client, deployment_client, credentials): self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = TEST_RESOURCE_GROUP_NAME self.node.properties['name'] = resource_group self.node.properties['resource_group_name'] = resource_group err = self._not_found_cloud_error() rg_client().resource_groups.get.side_effect = err deployment_client().deployments.get.side_effect = err deployment_properties = DeploymentProperties( mode=DeploymentMode.incremental, template={}, parameters={}) with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): deployment.create( ctx=self.fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config=self.node.properties.get('azure_config') ) deployment_client()\ .deployments.create_or_update.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group, parameters=AzDeployment(properties=deployment_properties), verify=True ) async_call = \ deployment_client().deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=10)
def test_create_without_template(self): # call without templates with self.assertRaises(cfy_exc.NonRecoverableError) as ex: deployment.create( ctx=self.fake_ctx, name="check", azure_config=self.azure_config ) self.assertEqual(str(ex.exception), "Template is not defined.")
def test_create_with_external_resource(self): self.node.properties['use_external_resource'] = True deployment.create(ctx=self.fake_ctx, name="check", location="west", azure_config=self.azure_config) self.client.deployments.create_or_update.assert_not_called() self.client.deployments.get.assert_called()
def test_create_with_external_resource(self): self.node.properties['use_external_resource'] = True deployment.create( ctx=self.fake_ctx, name="check", location="west", azure_config=self.azure_config ) self.client.deployments.create_or_update.assert_not_called() self.client.deployments.get.assert_called()
def test_create_with_external_resource(self, rg_client, deployment_client, credentials): self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = 'sample_deployment' self.node.properties['name'] = resource_group self.node.properties['resource_group_name'] = resource_group self.node.properties['location'] = 'westus' self.node.properties['use_external_resource'] = True with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): deployment.create(ctx=self.fake_ctx, template="{}")
def test_create(self, rg_client, deployment_client, credentials): self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = 'sample_deployment' self.node.properties['name'] = resource_group self.node.properties['resource_group_name'] = resource_group self.node.properties['template'] = TEST_TEMPLATE self.node.properties['location'] = 'westus' resource_group_params = { 'location': self.node.properties.get('location'), } properties = self.node.properties template = deployment.get_template(self.fake_ctx, properties) deployment_params = { 'mode': DeploymentMode.incremental, 'template': template, 'parameters': deployment.format_params(properties.get( 'params', {})) } deployment_properties = DeploymentProperties( mode=deployment_params['mode'], template=deployment_params['template'], parameters=deployment_params['parameters']) err = self._not_found_cloud_error() rg_client().resource_groups.get.side_effect = err deployment_client().deployments.get.side_effect = err with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): deployment.create(ctx=self.fake_ctx) deployment_client().deployments.get.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group ) rg_client().resource_groups.create_or_update.assert_called_with( resource_group_name=resource_group, parameters=resource_group_params ) deployment_client()\ .deployments.create_or_update.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group, parameters=AzDeployment(properties=deployment_properties), verify=True ) self.assertEquals( self.fake_ctx.instance.runtime_properties.get("name"), resource_group )
def test_create_without_template(self, rg_client, deployment_client, credentials): self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = 'sample_deployment' self.node.properties['name'] = resource_group self.node.properties['resource_group_name'] = resource_group err = self._not_found_cloud_error() rg_client().resource_groups.get.side_effect = err deployment_client().deployments.get.side_effect = err with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): with self.assertRaises(cfy_exc.NonRecoverableError) as ex: deployment.create(ctx=self.fake_ctx) self.assertTrue( "Deployment template not provided" in '{0}'.format( ex.exception))
def test_init(self): deployment.create(ctx=self.fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config=self.azure_config) self.Credentials.assert_called_with( client_id='client_id', resource=constants.OAUTH2_MGMT_RESOURCE, secret='client_secret', tenant='tenant_id', verify=True) self.Client.assert_called_with(self.Credentials.return_value, "subscription_id", base_url='https://management.azure.com')
def test_create_with_template_string(self): deployment.create(ctx=self.fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config=self.azure_config) self.client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': {}, 'mode': DeploymentMode.incremental, 'template': '{}' }, verify=True) async_call = self.client.deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=10)
def test_create_with_template_file(self, rg_client, deployment_client, credentials): fock = tempfile.NamedTemporaryFile(delete=False) fock.write(json.dumps({'a': 'b'}).encode('utf-8')) fock.close() self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = TEST_RESOURCE_GROUP_NAME self.node.properties['name'] = resource_group self.node.properties['resource_group_name'] = resource_group self.node.properties['template'] = None self.node.properties['template_file'] = fock.name self.fake_ctx.get_resource.return_value = open(fock.name).read() err = self._not_found_cloud_error() rg_client().resource_groups.get.side_effect = err deployment_client().deployments.get.side_effect = err deployment_properties = DeploymentProperties( mode=DeploymentMode.incremental, template={'a': 'b'}, parameters={'c': {'value': 'd'}}) with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): deployment.create( ctx=self.fake_ctx, name="check", location="west", params={'c': 'd'}, azure_config=self.node.properties.get('azure_config') ) self.fake_ctx.get_resource.assert_called_with(fock.name) deployment_client() \ .deployments.create_or_update.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group, parameters=AzDeployment(properties=deployment_properties), verify=True ) async_call = \ deployment_client().deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=900)
def test_create_with_deployment_outputs(self): mock_outputs = { "exampleOutput": { "type": "String", "value": "exampleOutput", } } self.client.deployments.get.return_value.properties.outputs = \ mock_outputs deployment.create(ctx=self.fake_ctx, name="check", location="west", template="{}", params={'c': 'd'}, azure_config=self.azure_config) outputs = self.instance.runtime_properties['outputs'] self.assertDictEqual(outputs, mock_outputs)
def test_create_with_template_string(self): deployment.create( ctx=self.fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config=self.azure_config ) self.client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': {}, 'mode': DeploymentMode.incremental, 'template': {} }, verify=True ) async_call = self.client.deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=10)
def test_create_with_deployment_outputs(self): mock_outputs = { "exampleOutput": { "type": "String", "value": "exampleOutput", } } self.client.deployments.get.return_value.properties.outputs = \ mock_outputs deployment.create( ctx=self.fake_ctx, name="check", location="west", template="{}", params={'c': 'd'}, azure_config=self.azure_config ) outputs = self.instance.runtime_properties['outputs'] self.assertDictEqual(outputs, mock_outputs)
def test_init(self): deployment.create( ctx=self.fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config=self.azure_config ) self.Credentials.assert_called_with( client_id='client_id', resource=constants.OAUTH2_MGMT_RESOURCE, secret='client_secret', certificate='', thumbprint='', tenant='tenant_id', verify=True) self.Client.assert_called_with( self.Credentials.return_value, "subscription_id", base_url='https://management.azure.com')
def test_create_with_template_file(self): self.node.properties['template_file'] = "check.json" self.fake_ctx.get_resource.return_value = '{"a":"b"}' deployment.create( ctx=self.fake_ctx, name="check", location="west", params={'c': 'd'}, azure_config=self.azure_config ) self.fake_ctx.get_resource.assert_called_with("check.json") self.client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': {'c': {'value': 'd'}}, 'mode': DeploymentMode.incremental, 'template': {'a': 'b'} }, verify=True ) async_call = self.client.deployments.create_or_update.return_value async_call.wait.assert_called_with(timeout=None)
def test_create_already_exists(self, rg_client, deployment_client, credentials): self.node.properties['azure_config'] = self.dummy_azure_credentials resource_group = 'sample_deployment' self.node.properties['name'] = resource_group self.node.properties['use_external_resource'] = True self.node.properties['resource_group_name'] = resource_group self.node.properties['template'] = TEST_TEMPLATE self.node.properties['location'] = 'westus' properties = self.node.properties template = deployment.get_template(self.fake_ctx, properties) deployment_params = { 'mode': DeploymentMode.incremental, 'template': template, 'parameters': deployment.format_params(properties.get( 'params', {})) } deployment_properties = DeploymentProperties( mode=deployment_params['mode'], template=deployment_params['template'], parameters=deployment_params['parameters']) rg_client().resource_groups.get.return_value = mock.Mock() with mock.patch('cloudify_azure.utils.secure_logging_content', mock.Mock()): deployment.create(ctx=self.fake_ctx) deployment_client().deployments.get.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group ) deployment_client()\ .deployments.create_or_update.assert_called_with( resource_group_name=resource_group, deployment_name=resource_group, parameters=AzDeployment(properties=deployment_properties), verify=True )
def test_create(self): fake_ctx, node, _ = self._get_mock_context_for_run() current_ctx.set(fake_ctx) credentials = mock.MagicMock() async_call = mock.MagicMock() async_call.wait = mock.MagicMock() client = mock.MagicMock() client.deployments = mock.MagicMock() client.deployments.create_or_update = mock.MagicMock( return_value=async_call) with mock.patch( 'cloudify_azure.resources.deployment.ServicePrincipalCredentials', mock.MagicMock(return_value=credentials)) as credentials_call: with mock.patch( 'cloudify_azure.resources.deployment.ResourceManagementClient', mock.MagicMock(return_value=client)) as client_call: # call without templates with self.assertRaises(cfy_exc.NonRecoverableError) as ex: deployment.create(ctx=fake_ctx, name="check", azure_config={ 'client_id': "client_id", 'client_secret': "client_secret", 'tenant_id': "tenant_id", 'subscription_id': "subscription_id", }) self.assertEqual(str(ex.exception), "Template does not defined.") credentials_call.assert_called_with( client_id='client_id', resource=constants.OAUTH2_MGMT_RESOURCE, secret='client_secret', tenant='tenant_id', verify=True) client_call.assert_called_with( credentials, "subscription_id", base_url='https://management.azure.com') deployment.create(ctx=fake_ctx, name="check", template="{}", location="west", timeout=10, azure_config={ 'client_id': "client_id", 'client_secret': "client_secret", 'tenant_id': "tenant_id", 'subscription_id': "subscription_id", }) async_call.wait.assert_called_with(timeout=10) client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': {}, 'mode': DeploymentMode.incremental, 'template': {} }, verify=True) node.properties['template_file'] = "check.json" fake_ctx.get_resource = mock.MagicMock( return_value='{"a":"b"}') deployment.create(ctx=fake_ctx, name="check", location="west", params={'c': 'd'}, azure_config={ 'client_id': "client_id", 'client_secret': "client_secret", 'tenant_id': "tenant_id", 'subscription_id': "subscription_id", }) fake_ctx.get_resource.assert_called_with("check.json") async_call.wait.assert_called_with(timeout=None) client.deployments.create_or_update.assert_called_with( 'check', 'check', { 'parameters': { 'c': { 'value': 'd' } }, 'mode': DeploymentMode.incremental, 'template': { 'a': 'b' } }, verify=True)