-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: Add CheckForConflict method
Returns a "409 Conflict" error response if the provisioning state of the resource or any parent resources (in the same namespace) should block a request from proceeding. Furthermore, if a DELETE request is accepted, mark any active operation on the resource as canceled. (Cluster Service will handle the actual cancellation of operations.)
- Loading branch information
Matthew Barnes
committed
Oct 29, 2024
1 parent
e10cd0b
commit b268381
Showing
5 changed files
with
366 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/ARO-HCP/internal/api/arm" | ||
"github.com/Azure/ARO-HCP/internal/database" | ||
) | ||
|
||
func TestCheckForProvisioningStateConflict(t *testing.T) { | ||
const clusterResourceID = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/testCluster" | ||
const nodePoolResourceID = clusterResourceID + "/nodePools/testNodePool" | ||
|
||
tests := []struct { | ||
name string | ||
resourceID string | ||
operationRequest database.OperationRequest | ||
directConflicts map[arm.ProvisioningState]bool | ||
parentConflicts map[arm.ProvisioningState]bool | ||
}{ | ||
{ | ||
name: "Create cluster", | ||
resourceID: clusterResourceID, | ||
operationRequest: database.OperationRequestCreate, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: false, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
}, | ||
{ | ||
name: "Delete cluster", | ||
resourceID: clusterResourceID, | ||
operationRequest: database.OperationRequestDelete, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
}, | ||
{ | ||
name: "Update cluster", | ||
resourceID: clusterResourceID, | ||
operationRequest: database.OperationRequestUpdate, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: true, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: true, | ||
arm.ProvisioningStateUpdating: true, | ||
}, | ||
}, | ||
{ | ||
name: "Create node pool", | ||
resourceID: nodePoolResourceID, | ||
operationRequest: database.OperationRequestCreate, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: false, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
parentConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
}, | ||
{ | ||
name: "Delete node pool", | ||
resourceID: nodePoolResourceID, | ||
operationRequest: database.OperationRequestDelete, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
parentConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
}, | ||
{ | ||
name: "Update node pool", | ||
resourceID: nodePoolResourceID, | ||
operationRequest: database.OperationRequestUpdate, | ||
directConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: true, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: true, | ||
arm.ProvisioningStateUpdating: true, | ||
}, | ||
parentConflicts: map[arm.ProvisioningState]bool{ | ||
arm.ProvisioningStateSucceeded: false, | ||
arm.ProvisioningStateFailed: false, | ||
arm.ProvisioningStateCanceled: false, | ||
arm.ProvisioningStateAccepted: false, | ||
arm.ProvisioningStateDeleting: true, | ||
arm.ProvisioningStateProvisioning: false, | ||
arm.ProvisioningStateUpdating: false, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
var name string | ||
|
||
resourceID, err := arm.ParseResourceID(tt.resourceID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for directState, directConflict := range tt.directConflicts { | ||
name = fmt.Sprintf("%s (directState=%s)", tt.name, directState) | ||
t.Run(name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
frontend := &Frontend{ | ||
logger: slog.Default(), | ||
dbClient: database.NewCache(), | ||
} | ||
|
||
doc := database.NewResourceDocument(resourceID) | ||
doc.ProvisioningState = directState | ||
|
||
parentResourceID := resourceID.GetParent() | ||
if parentResourceID.ResourceType.Namespace == resourceID.ResourceType.Namespace { | ||
parentDoc := database.NewResourceDocument(parentResourceID) | ||
// Hold the provisioning state to something benign. | ||
parentDoc.ProvisioningState = arm.ProvisioningStateSucceeded | ||
_ = frontend.dbClient.CreateResourceDoc(ctx, parentDoc) | ||
} | ||
|
||
cloudError := frontend.CheckForProvisioningStateConflict(ctx, tt.operationRequest, doc) | ||
|
||
if cloudError == nil { | ||
if directConflict { | ||
t.Errorf("Expected %d %s but got no error", http.StatusConflict, http.StatusText(http.StatusConflict)) | ||
} | ||
} else { | ||
if !directConflict || cloudError.StatusCode != http.StatusConflict { | ||
t.Errorf("Got unexpected error: %d %s", cloudError.StatusCode, http.StatusText(cloudError.StatusCode)) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
for parentState, parentConflict := range tt.parentConflicts { | ||
name = fmt.Sprintf("%s (parentState=%s)", tt.name, parentState) | ||
t.Run(name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
frontend := &Frontend{ | ||
logger: slog.Default(), | ||
dbClient: database.NewCache(), | ||
} | ||
|
||
doc := database.NewResourceDocument(resourceID) | ||
// Hold the provisioning state to something benign. | ||
doc.ProvisioningState = arm.ProvisioningStateSucceeded | ||
|
||
parentResourceID := resourceID.GetParent() | ||
if parentResourceID.ResourceType.Namespace == resourceID.ResourceType.Namespace { | ||
parentDoc := database.NewResourceDocument(parentResourceID) | ||
parentDoc.ProvisioningState = parentState | ||
_ = frontend.dbClient.CreateResourceDoc(ctx, parentDoc) | ||
} else { | ||
t.Fatalf("Parent resource type namespace (%s) differs from child namespace (%s)", | ||
parentResourceID.ResourceType.Namespace, | ||
resourceID.ResourceType.Namespace) | ||
} | ||
|
||
cloudError := frontend.CheckForProvisioningStateConflict(ctx, tt.operationRequest, doc) | ||
|
||
if cloudError == nil { | ||
if parentConflict { | ||
t.Errorf("Expected %d %s but got no error", http.StatusConflict, http.StatusText(http.StatusConflict)) | ||
} | ||
} else { | ||
if !parentConflict || cloudError.StatusCode != http.StatusConflict { | ||
t.Errorf("Got unexpected error: %d %s", cloudError.StatusCode, http.StatusText(cloudError.StatusCode)) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.