def test_provider_create(self, mock_header, mock_request_info, mock_delay): mock_request_info.return_value = self.User, self.Customer serializer = AdminSourcesSerializer(context=self.request_context) source = { "source_id": 10, "name": "ProviderAWS", "source_type": "AWS", "authentication": { "resource_name": "arn:aws:iam::111111111111:role/CostManagement" }, "billing_source": { "bucket": "first-bucket" }, "auth_header": Config.SOURCES_FAKE_HEADER, "account_id": "acct10001", "offset": 10, } with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): instance = serializer.create(source) self.assertEqual(instance.billing_source.get("bucket"), "first-bucket") serializer = SourcesSerializer(context=self.request_context) validated = {"billing_source": {"bucket": "second-bucket"}} with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): instance2 = serializer.update(instance, validated) self.assertEqual(instance2.billing_source.get("bucket"), "second-bucket")
def test_gcp_admin_add_table_not_ready(self, mock_header, mock_request_info): """Test a GCP Admin Source add where the billing table is not ready.""" mock_request_info.return_value = self.User, self.Customer serializer = AdminSourcesSerializer(context=self.request_context) with self.assertRaises(ValidationError): with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): with patch.object(ProviderBuilder, "create_provider_from_source", side_effect=SkipStatusPush): serializer.create({ "source_id": 10, "name": "ProviderGCP", "source_type": "GCP", "authentication": { "credentials": { "project_id": "test-project" } }, "billing_source": { "data_source": { "dataset": "first-dataset" } }, "auth_header": Config.SOURCES_FAKE_HEADER, "account_id": "acct10001", "offset": 10, })
def test_provider_create(self, mock_header, mock_request_info, _): mock_request_info.return_value = self.User, self.Customer serializer = AdminSourcesSerializer(context=self.request_context) source = { "source_id": 10, "name": "ProviderAWS", "source_type": "AWS", "authentication": { "credentials": { "role_arn": "arn:aws:iam::111111111111:role/CostManagement" } }, "billing_source": { "data_source": { "bucket": "first-bucket" } }, "auth_header": Config.SOURCES_FAKE_HEADER, "account_id": "acct10001", "offset": 10, } with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): instance = serializer.create(source) self.assertEqual( instance.billing_source.get("data_source", {}).get("bucket"), "first-bucket") serializer = SourcesSerializer(context=self.request_context) validated = { "billing_source": { "data_source": { "bucket": "second-bucket" } } } with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): with patch("sources.api.serializers.ServerProxy") as mock_client: mock_sources_client = MockSourcesClient( "http://mock-soures-client") mock_client.return_value.__enter__.return_value = mock_sources_client instance2 = serializer.update(instance, validated) self.assertEqual( instance2.billing_source.get("data_source", {}).get("bucket"), "second-bucket")
def test_create_via_admin_serializer_bad_source_type(self, _): """Raise error for bad source type on create.""" source_data = { "name": "test", "source_type": "BAD", "authentication": {"resource_name": "arn:aws::foo:bar"}, "billing_source": {"bucket": "/tmp/s3bucket"}, } mock_request = Mock(headers={HEADER_X_RH_IDENTITY: Config.SOURCES_FAKE_HEADER}) context = {"request": mock_request} serializer = AdminSourcesSerializer(data=source_data, context=context) with self.assertRaises(ValidationError): if serializer.is_valid(raise_exception=True): serializer.save()
def test_create_via_admin_serializer(self, _): """Test create source with admin serializer.""" source_data = { "name": "test1", "source_type": "AWS", "authentication": {"resource_name": "arn:aws::foo:bar"}, "billing_source": {"bucket": "/tmp/s3bucket"}, } mock_request = Mock(headers={HEADER_X_RH_IDENTITY: Config.SOURCES_FAKE_HEADER}) context = {"request": mock_request} serializer = AdminSourcesSerializer(data=source_data, context=context) with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): if serializer.is_valid(raise_exception=True): instance = serializer.save() provider = Provider.objects.get(uuid=instance.koku_uuid) self.assertIsNotNone(provider) self.assertEqual(provider.name, instance.name) self.assertEqual(instance.source_uuid, instance.koku_uuid) else: self.fail("test_create_via_admin_serializer failed") source_data["name"] = "test2" source_data["authentication"]["resource_name"] = "arn:aws::foo:bar2" source_data["billing_source"]["bucket"] = "/tmp/mybucket" serializer = AdminSourcesSerializer(data=source_data, context=context) with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True): if serializer.is_valid(raise_exception=True): instance = serializer.save() provider = Provider.objects.get(uuid=instance.koku_uuid) self.assertIsNotNone(provider) self.assertEqual(provider.name, instance.name) self.assertEqual(instance.source_uuid, instance.koku_uuid) else: self.fail("test_create_via_admin_serializer failed")