|
| 1 | +package database |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// Licensed under the Apache License 2.0. |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/Azure/ARO-RP/pkg/api" |
| 13 | + "github.com/Azure/ARO-RP/pkg/database/cosmosdb" |
| 14 | + "github.com/Azure/ARO-RP/pkg/util/uuid" |
| 15 | +) |
| 16 | + |
| 17 | +// mockNilReturningClient is a minimal mock that returns (nil, nil) from Create and Replace |
| 18 | +// to test the regression where CosmosDB could return (nil, nil) and we didn't handle it |
| 19 | +type mockNilReturningClient struct { |
| 20 | + cosmosdb.OpenShiftClusterDocumentClient |
| 21 | + returnNilNil bool |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockNilReturningClient) Create(ctx context.Context, partitionKey string, doc *api.OpenShiftClusterDocument, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) { |
| 25 | + if m.returnNilNil { |
| 26 | + return nil, nil |
| 27 | + } |
| 28 | + return doc, nil |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockNilReturningClient) Replace(ctx context.Context, partitionKey string, doc *api.OpenShiftClusterDocument, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) { |
| 32 | + if m.returnNilNil { |
| 33 | + return nil, nil |
| 34 | + } |
| 35 | + return doc, nil |
| 36 | +} |
| 37 | + |
| 38 | +// TestCreateReturnsErrorWhenCosmosDBReturnsNilNil verifies that when the CosmosDB client |
| 39 | +// returns (nil, nil) from Create, the wrapper returns a proper error instead of (nil, nil). |
| 40 | +// This is a regression test for a bug where CosmosDB could return (nil, nil) and we would |
| 41 | +// propagate it to callers, leading to nil pointer dereferences. |
| 42 | +func TestCreateReturnsErrorWhenCosmosDBReturnsNilNil(t *testing.T) { |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + mockClient := &mockNilReturningClient{returnNilNil: true} |
| 46 | + db := NewOpenShiftClustersWithProvidedClient(mockClient, nil, "test-uuid", uuid.DefaultGenerator) |
| 47 | + |
| 48 | + doc := &api.OpenShiftClusterDocument{ |
| 49 | + Key: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourcegroup/providers/microsoft.redhatopenshift/openshiftclusters/cluster", |
| 50 | + PartitionKey: "00000000-0000-0000-0000-000000000000", |
| 51 | + OpenShiftCluster: &api.OpenShiftCluster{ |
| 52 | + ID: "test-id", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + result, err := db.Create(ctx, doc) |
| 57 | + |
| 58 | + // Verify we get an error instead of (nil, nil) |
| 59 | + if err == nil { |
| 60 | + t.Fatal("expected error when CosmosDB returns (nil, nil), got nil") |
| 61 | + } |
| 62 | + |
| 63 | + if result != nil { |
| 64 | + t.Fatalf("expected nil result when error occurs, got %v", result) |
| 65 | + } |
| 66 | + |
| 67 | + cosmosErr, ok := err.(*cosmosdb.Error) |
| 68 | + if !ok { |
| 69 | + t.Fatalf("expected *cosmosdb.Error, got %T", err) |
| 70 | + } |
| 71 | + |
| 72 | + if cosmosErr.StatusCode != http.StatusInternalServerError { |
| 73 | + t.Errorf("expected status code 500, got %d", cosmosErr.StatusCode) |
| 74 | + } |
| 75 | + |
| 76 | + // Verify the error message contains descriptive text |
| 77 | + expectedMsg := fmt.Sprintf("creating OpenShift cluster: CosmosDB returned nil document with no error for key %q and partition key %q", doc.Key, doc.PartitionKey) |
| 78 | + if cosmosErr.Message != expectedMsg { |
| 79 | + t.Errorf("expected error message %q, got %q", expectedMsg, cosmosErr.Message) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestUpdateReturnsErrorWhenCosmosDBReturnsNilNil verifies that when the CosmosDB client |
| 84 | +// returns (nil, nil) from Replace, the wrapper returns a proper error instead of (nil, nil). |
| 85 | +// This is a regression test for a bug where CosmosDB could return (nil, nil) and we would |
| 86 | +// propagate it to callers, leading to nil pointer dereferences. |
| 87 | +func TestUpdateReturnsErrorWhenCosmosDBReturnsNilNil(t *testing.T) { |
| 88 | + ctx := context.Background() |
| 89 | + |
| 90 | + mockClient := &mockNilReturningClient{returnNilNil: true} |
| 91 | + db := NewOpenShiftClustersWithProvidedClient(mockClient, nil, "test-uuid", uuid.DefaultGenerator) |
| 92 | + |
| 93 | + doc := &api.OpenShiftClusterDocument{ |
| 94 | + Key: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourcegroup/providers/microsoft.redhatopenshift/openshiftclusters/cluster", |
| 95 | + PartitionKey: "00000000-0000-0000-0000-000000000000", |
| 96 | + OpenShiftCluster: &api.OpenShiftCluster{ |
| 97 | + ID: "test-id", |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + result, err := db.Update(ctx, doc) |
| 102 | + |
| 103 | + // Verify we get an error instead of (nil, nil) |
| 104 | + if err == nil { |
| 105 | + t.Fatal("expected error when CosmosDB returns (nil, nil), got nil") |
| 106 | + } |
| 107 | + |
| 108 | + if result != nil { |
| 109 | + t.Fatalf("expected nil result when error occurs, got %v", result) |
| 110 | + } |
| 111 | + |
| 112 | + cosmosErr, ok := err.(*cosmosdb.Error) |
| 113 | + if !ok { |
| 114 | + t.Fatalf("expected *cosmosdb.Error, got %T", err) |
| 115 | + } |
| 116 | + |
| 117 | + if cosmosErr.StatusCode != http.StatusInternalServerError { |
| 118 | + t.Errorf("expected status code 500, got %d", cosmosErr.StatusCode) |
| 119 | + } |
| 120 | + |
| 121 | + // Verify the error message contains descriptive text |
| 122 | + expectedMsg := fmt.Sprintf("OpenShiftClusters Replace returned nil document with nil error for key %q and partition key %q", doc.Key, doc.PartitionKey) |
| 123 | + if cosmosErr.Message != expectedMsg { |
| 124 | + t.Errorf("expected error message %q, got %q", expectedMsg, cosmosErr.Message) |
| 125 | + } |
| 126 | +} |
0 commit comments