From 93d380b27eba5622518169e70a2e530bdc3aff98 Mon Sep 17 00:00:00 2001 From: tjololo <1145298+tjololo@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:41:26 +0100 Subject: [PATCH] add more tests for backend_controller --- .../internal/controller/backend_controller.go | 9 + .../controller/backend_controller_test.go | 443 +++++++++++++++--- .../test/utils/azure_fake.go | 78 +++ 3 files changed, 456 insertions(+), 74 deletions(-) create mode 100644 services/dis-apim-operator/test/utils/azure_fake.go diff --git a/services/dis-apim-operator/internal/controller/backend_controller.go b/services/dis-apim-operator/internal/controller/backend_controller.go index 2a28ac2f..c2a926d8 100644 --- a/services/dis-apim-operator/internal/controller/backend_controller.go +++ b/services/dis-apim-operator/internal/controller/backend_controller.go @@ -109,6 +109,15 @@ func (r *BackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct logger.Info("Backend updated") return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil } + logger.Info("Backend matches actual state") + if backend.Status.ProvisioningState != apimv1alpha1.BackendProvisioningStateSucceeded || backend.Status.BackendID != *azBackend.ID { + backend.Status.ProvisioningState = apimv1alpha1.BackendProvisioningStateSucceeded + backend.Status.BackendID = *azBackend.ID + if err := r.Status().Update(ctx, &backend); err != nil { + logger.Error(err, "Failed to update status") + return ctrl.Result{}, err + } + } return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil } diff --git a/services/dis-apim-operator/internal/controller/backend_controller_test.go b/services/dis-apim-operator/internal/controller/backend_controller_test.go index eaf682f8..4be74383 100644 --- a/services/dis-apim-operator/internal/controller/backend_controller_test.go +++ b/services/dis-apim-operator/internal/controller/backend_controller_test.go @@ -21,9 +21,9 @@ import ( "github.com/Altinn/altinn-platform/services/dis-apim-operator/internal/azure" "github.com/Altinn/altinn-platform/services/dis-apim-operator/internal/config" "github.com/Altinn/altinn-platform/services/dis-apim-operator/internal/utils" + testutils "github.com/Altinn/altinn-platform/services/dis-apim-operator/test/utils" "github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" - azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake" apim "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v2" apimfake "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v2/fake" "net/http" @@ -33,6 +33,7 @@ import ( . "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" + runctimeclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -75,86 +76,201 @@ var _ = Describe("Backend Controller", func() { // TODO(user): Cleanup logic after each test, like removing the resource instance. resource := &apimv1alpha1.Backend{} err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(runctimeclient.IgnoreNotFound(err)).NotTo(HaveOccurred()) + + if err == nil { + By("Removing finalizer") + resource.SetFinalizers([]string{}) + Expect(k8sClient.Update(ctx, resource)).To(Succeed()) + if resource.DeletionTimestamp == nil { + By("Cleanup the specific resource instance Backend") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + } + } + }) + It("should set success status when azure resource created", func() { + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{}, + utils.ToPointer(http.StatusNotFound), + apim.BackendClientCreateOrUpdateResponse{ + BackendContract: apim.BackendContract{ + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), + Name: utils.ToPointer("fake-apim-backend"), + Type: utils.ToPointer("Microsoft.ApiManagement/service/backends"), + }, + }, + nil, + apim.BackendClientDeleteResponse{}, + utils.ToPointer(http.StatusOK), + ) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, + }, + } + + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{RequeueAfter: 1 * time.Minute})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) Expect(err).NotTo(HaveOccurred()) + Expect(updatedBackend.Status.ProvisioningState).To(Equal(apimv1alpha1.BackendProvisioningStateSucceeded)) + Expect(updatedBackend.Status.BackendID).To(Equal("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend")) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + It("should set failed status when azure resource failed to create and requeue", func() { + fakeServer := testutils.GetFakeBackendServer(apim.BackendClientGetResponse{}, utils.ToPointer(http.StatusNotFound), apim.BackendClientCreateOrUpdateResponse{}, utils.ToPointer(http.StatusInternalServerError), apim.BackendClientDeleteResponse{}, utils.ToPointer(http.StatusOK)) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, + }, + } - By("Cleanup the specific resource instance Backend") - Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).To(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) + Expect(err).NotTo(HaveOccurred()) + Expect(updatedBackend.Status.ProvisioningState).To(Equal(apimv1alpha1.BackendProvisioningStateFailed)) + Expect(updatedBackend.Status.LastProvisioningError).NotTo(BeEmpty()) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. }) - It("should successfully reconcile the resource", func() { - fakeServer := &apimfake.BackendServer{ - CreateOrUpdate: func( - ctx context.Context, - resourceGroupName string, - serviceName string, - backendID string, - parameters apim.BackendContract, - options *apim.BackendClientCreateOrUpdateOptions, - ) (resp azfake.Responder[apim.BackendClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) { - - response := apim.BackendClientCreateOrUpdateResponse{ - BackendContract: apim.BackendContract{ - Properties: parameters.Properties, - ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), - Name: utils.ToPointer("fake-apim-backend"), - Type: utils.ToPointer("Microsoft.ApiManagement/service/backends"), - }, - } - - responder := azfake.Responder[apim.BackendClientCreateOrUpdateResponse]{} - - responder.SetResponse(http.StatusOK, response, nil) - - return responder, azfake.ErrorResponder{} - }, - Delete: func( - ctx context.Context, - resourceGroupName string, - serviceName string, - backendID string, - ifMatch string, - options *apim.BackendClientDeleteOptions, - ) (resp azfake.Responder[apim.BackendClientDeleteResponse], errResp azfake.ErrorResponder) { - response := apim.BackendClientDeleteResponse{} - responder := azfake.Responder[apim.BackendClientDeleteResponse]{} - - responder.SetResponse(http.StatusOK, response, nil) - - return responder, azfake.ErrorResponder{} - }, - Get: func( - ctx context.Context, - resourceGroupName string, - serviceName string, - backendID string, - options *apim.BackendClientGetOptions, - ) (resp azfake.Responder[apim.BackendClientGetResponse], errResp azfake.ErrorResponder) { - response := apim.BackendClientGetResponse{ - BackendContract: apim.BackendContract{ - Properties: &apim.BackendContractProperties{ - Protocol: utils.ToPointer(apim.BackendProtocolHTTP), - URL: utils.ToPointer("https://test.example.com"), - Description: utils.ToPointer("Test backend for the operator"), - TLS: &apim.BackendTLSProperties{ - ValidateCertificateChain: utils.ToPointer(true), - ValidateCertificateName: utils.ToPointer(true), - }, - Title: utils.ToPointer("test-backend"), + It("Should set status to succeeded if get returns backend", func() { + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{ + BackendContract: apim.BackendContract{ + Properties: &apim.BackendContractProperties{ + Protocol: utils.ToPointer(apim.BackendProtocolHTTP), + URL: utils.ToPointer("https://test.example.com"), + Description: utils.ToPointer("Test backend for the operator"), + TLS: &apim.BackendTLSProperties{ + ValidateCertificateChain: utils.ToPointer(true), + ValidateCertificateName: utils.ToPointer(true), }, + Title: utils.ToPointer("test-backend"), }, - ETag: utils.ToPointer("33a64df551425fcc55e4d42a148795d9f25f89d5"), - } - responder := azfake.Responder[apim.BackendClientGetResponse]{} - - responder.SetResponse(http.StatusOK, response, nil) - errResponder := azfake.ErrorResponder{} - errResponder.SetResponseError(http.StatusNotFound, "Not Found") - return responder, errResponder + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), + }, + }, + nil, + apim.BackendClientCreateOrUpdateResponse{}, + utils.ToPointer(http.StatusInternalServerError), + apim.BackendClientDeleteResponse{}, + utils.ToPointer(http.StatusOK), + ) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, }, - GetEntityTag: nil, - NewListByServicePager: nil, - Reconnect: nil, - Update: nil, } + + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{RequeueAfter: 1 * time.Minute})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) + Expect(err).NotTo(HaveOccurred()) + Expect(updatedBackend.Status.ProvisioningState).To(Equal(apimv1alpha1.BackendProvisioningStateSucceeded)) + Expect(updatedBackend.Status.LastProvisioningError).To(BeEmpty()) + Expect(updatedBackend.Status.BackendID).To(Equal("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend")) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + It("Should update azure resource if actual state does not match desired", func() { + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{ + BackendContract: apim.BackendContract{ + Properties: &apim.BackendContractProperties{ + Protocol: utils.ToPointer(apim.BackendProtocolHTTP), + URL: utils.ToPointer("https://example.com"), + Description: utils.ToPointer("Test backend for the operator"), + TLS: &apim.BackendTLSProperties{ + ValidateCertificateChain: utils.ToPointer(true), + ValidateCertificateName: utils.ToPointer(true), + }, + Title: utils.ToPointer("test-backend"), + }, + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), + }, + }, + nil, + apim.BackendClientCreateOrUpdateResponse{ + BackendContract: apim.BackendContract{ + Properties: &apim.BackendContractProperties{ + Protocol: utils.ToPointer(apim.BackendProtocolHTTP), + URL: utils.ToPointer("https://example.com"), + Description: utils.ToPointer("Test backend for the operator"), + TLS: &apim.BackendTLSProperties{ + ValidateCertificateChain: utils.ToPointer(true), + ValidateCertificateName: utils.ToPointer(true), + }, + Title: utils.ToPointer("test-backend"), + }, + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend-updated"), + }, + }, + nil, + apim.BackendClientDeleteResponse{}, + nil, + ) transport := apimfake.NewBackendServerTransport(fakeServer) factoryClientOptions := &arm.ClientOptions{ ClientOptions: azcore.ClientOptions{ @@ -186,6 +302,185 @@ var _ = Describe("Backend Controller", func() { err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) Expect(err).NotTo(HaveOccurred()) Expect(updatedBackend.Status.ProvisioningState).To(Equal(apimv1alpha1.BackendProvisioningStateSucceeded)) + Expect(updatedBackend.Status.LastProvisioningError).To(BeEmpty()) + Expect(updatedBackend.Status.BackendID).To(Equal("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend-updated")) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + It("Should delete azure resources and remove finalizer on deletion", func() { + beforeTest := &apimv1alpha1.Backend{} + err := k8sClient.Get(ctx, typeNamespacedName, beforeTest) + Expect(err).NotTo(HaveOccurred()) + beforeTest.SetFinalizers([]string{BACKEND_FINALIZER}) + Expect(k8sClient.Update(ctx, beforeTest)).To(Succeed()) + Expect(k8sClient.Delete(ctx, beforeTest)).To(Succeed()) + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{ + ETag: utils.ToPointer("fake-etag"), + BackendContract: apim.BackendContract{ + Properties: &apim.BackendContractProperties{ + Protocol: utils.ToPointer(apim.BackendProtocolHTTP), + URL: utils.ToPointer("https://test.example.com"), + Description: utils.ToPointer("Test backend for the operator"), + TLS: &apim.BackendTLSProperties{ + ValidateCertificateChain: utils.ToPointer(true), + ValidateCertificateName: utils.ToPointer(true), + }, + Title: utils.ToPointer("test-backend"), + }, + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), + }, + }, + nil, + apim.BackendClientCreateOrUpdateResponse{}, + utils.ToPointer(http.StatusInternalServerError), + apim.BackendClientDeleteResponse{}, + nil, + ) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, + }, + } + + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) + Expect(err).To(HaveOccurred()) + Expect(errors.IsNotFound(err)).To(BeTrue()) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + It("Should requeue deletion if delete of backend in azure failed", func() { + beforeTest := &apimv1alpha1.Backend{} + err := k8sClient.Get(ctx, typeNamespacedName, beforeTest) + Expect(err).NotTo(HaveOccurred()) + beforeTest.SetFinalizers([]string{BACKEND_FINALIZER}) + Expect(k8sClient.Update(ctx, beforeTest)).To(Succeed()) + Expect(k8sClient.Delete(ctx, beforeTest)).To(Succeed()) + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{ + ETag: utils.ToPointer("fake-etag"), + BackendContract: apim.BackendContract{ + Properties: &apim.BackendContractProperties{ + Protocol: utils.ToPointer(apim.BackendProtocolHTTP), + URL: utils.ToPointer("https://test.example.com"), + Description: utils.ToPointer("Test backend for the operator"), + TLS: &apim.BackendTLSProperties{ + ValidateCertificateChain: utils.ToPointer(true), + ValidateCertificateName: utils.ToPointer(true), + }, + Title: utils.ToPointer("test-backend"), + }, + ID: utils.ToPointer("/subscriptions/fake-subscription/resourceGroups/fake-resource-group/providers/APIM/Backend/fake-apim-backend"), + }, + }, + nil, + apim.BackendClientCreateOrUpdateResponse{}, + utils.ToPointer(http.StatusInternalServerError), + apim.BackendClientDeleteResponse{}, + utils.ToPointer(http.StatusInternalServerError), + ) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, + }, + } + + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).To(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) + Expect(err).ToNot(HaveOccurred()) + Expect(updatedBackend.Finalizers).To(HaveExactElements([]string{BACKEND_FINALIZER})) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + It("Should remove finalizer on deletion when azure backend not found", func() { + beforeTest := &apimv1alpha1.Backend{} + err := k8sClient.Get(ctx, typeNamespacedName, beforeTest) + Expect(err).NotTo(HaveOccurred()) + beforeTest.SetFinalizers([]string{BACKEND_FINALIZER}) + Expect(k8sClient.Update(ctx, beforeTest)).To(Succeed()) + Expect(k8sClient.Delete(ctx, beforeTest)).To(Succeed()) + fakeServer := testutils.GetFakeBackendServer( + apim.BackendClientGetResponse{}, + utils.ToPointer(http.StatusNotFound), + apim.BackendClientCreateOrUpdateResponse{}, + utils.ToPointer(http.StatusInternalServerError), + apim.BackendClientDeleteResponse{}, + utils.ToPointer(http.StatusInternalServerError), + ) + transport := apimfake.NewBackendServerTransport(fakeServer) + factoryClientOptions := &arm.ClientOptions{ + ClientOptions: azcore.ClientOptions{ + Transport: transport, + }, + } + By("Reconciling the created resource") + controllerReconciler := &BackendReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + NewClient: azure.NewAPIMClient, + ApimClientConfig: &azure.ApimClientConfig{ + AzureConfig: config.AzureConfig{ + SubscriptionId: "fake-subscription-id", + ResourceGroup: "fake-resource-group", + ApimServiceName: "fake-apim-service", + }, + FactoryOptions: factoryClientOptions, + }, + } + + rsp, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(rsp).To(Equal(reconcile.Result{})) + // Fetch the updated Backend resource + updatedBackend := &apimv1alpha1.Backend{} + err = k8sClient.Get(ctx, typeNamespacedName, updatedBackend) + Expect(err).To(HaveOccurred()) + Expect(errors.IsNotFound(err)).To(BeTrue()) // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. // Example: If you expect a certain status condition after reconciliation, verify it here. }) diff --git a/services/dis-apim-operator/test/utils/azure_fake.go b/services/dis-apim-operator/test/utils/azure_fake.go new file mode 100644 index 00000000..450d7bd3 --- /dev/null +++ b/services/dis-apim-operator/test/utils/azure_fake.go @@ -0,0 +1,78 @@ +package utils + +import ( + "context" + azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake" + apim "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v2" + apimfake "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v2/fake" + "net/http" +) + +func GetFakeBackendServer(getResponse apim.BackendClientGetResponse, getErrorCode *int, createOrUpdateResponse apim.BackendClientCreateOrUpdateResponse, createOrUpdateErrorCode *int, deleteResponse apim.BackendClientDeleteResponse, deleteErrorCode *int) *apimfake.BackendServer { + fakeServer := &apimfake.BackendServer{ + CreateOrUpdate: func( + ctx context.Context, + resourceGroupName string, + serviceName string, + backendID string, + parameters apim.BackendContract, + options *apim.BackendClientCreateOrUpdateOptions, + ) (resp azfake.Responder[apim.BackendClientCreateOrUpdateResponse], errResp azfake.ErrorResponder) { + + response := createOrUpdateResponse + + responder := azfake.Responder[apim.BackendClientCreateOrUpdateResponse]{} + + errResponder := azfake.ErrorResponder{} + if createOrUpdateErrorCode != nil { + errResponder.SetResponseError(*createOrUpdateErrorCode, "Some fake error occurred") + } else { + response.Properties = parameters.Properties + } + responder.SetResponse(http.StatusOK, response, nil) + + return responder, errResponder + }, + Delete: func( + ctx context.Context, + resourceGroupName string, + serviceName string, + backendID string, + ifMatch string, + options *apim.BackendClientDeleteOptions, + ) (resp azfake.Responder[apim.BackendClientDeleteResponse], errResp azfake.ErrorResponder) { + response := deleteResponse + responder := azfake.Responder[apim.BackendClientDeleteResponse]{} + + responder.SetResponse(http.StatusOK, response, nil) + + errorResponder := azfake.ErrorResponder{} + if deleteErrorCode != nil { + errorResponder.SetResponseError(*deleteErrorCode, "Some fake error occurred") + } + return responder, errorResponder + }, + Get: func( + ctx context.Context, + resourceGroupName string, + serviceName string, + backendID string, + options *apim.BackendClientGetOptions, + ) (resp azfake.Responder[apim.BackendClientGetResponse], errResp azfake.ErrorResponder) { + response := getResponse + responder := azfake.Responder[apim.BackendClientGetResponse]{} + + responder.SetResponse(http.StatusOK, response, nil) + errResponder := azfake.ErrorResponder{} + if getErrorCode != nil { + errResponder.SetResponseError(*getErrorCode, "Some fake error occurred") + } + return responder, errResponder + }, + GetEntityTag: nil, + NewListByServicePager: nil, + Reconnect: nil, + Update: nil, + } + return fakeServer +}