|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gorilla/mux" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "go.uber.org/mock/gomock" |
| 14 | + |
| 15 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api" |
| 16 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api/openapi" |
| 17 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/errors" |
| 18 | + "github.com/openshift-hyperfleet/hyperfleet-api/pkg/services" |
| 19 | +) |
| 20 | + |
| 21 | +func TestClusterHandler_Patch(t *testing.T) { |
| 22 | + RegisterTestingT(t) |
| 23 | + |
| 24 | + now := time.Now() |
| 25 | + clusterID := testClusterID |
| 26 | + validBody := `{"spec":{"region":"us-east1"}}` |
| 27 | + |
| 28 | + tests := []struct { |
| 29 | + setupMocks func(ctrl *gomock.Controller) (*services.MockClusterService, *services.MockGenericService) |
| 30 | + name string |
| 31 | + clusterID string |
| 32 | + expectedStatusCode int |
| 33 | + expectedError bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Success - Patch active cluster", |
| 37 | + clusterID: clusterID, |
| 38 | + setupMocks: func(ctrl *gomock.Controller) (*services.MockClusterService, *services.MockGenericService) { |
| 39 | + mockClusterSvc := services.NewMockClusterService(ctrl) |
| 40 | + mockGenericSvc := services.NewMockGenericService(ctrl) |
| 41 | + |
| 42 | + mockClusterSvc.EXPECT().Get(gomock.Any(), clusterID).Return(&api.Cluster{ |
| 43 | + Meta: api.Meta{ID: clusterID, CreatedTime: now, UpdatedTime: now}, |
| 44 | + Name: "test-cluster", |
| 45 | + Spec: []byte("{}"), |
| 46 | + Labels: []byte("{}"), |
| 47 | + StatusConditions: []byte("[]"), |
| 48 | + CreatedBy: testSystemUser, |
| 49 | + UpdatedBy: testSystemUser, |
| 50 | + }, nil) |
| 51 | + |
| 52 | + mockClusterSvc.EXPECT().Replace(gomock.Any(), gomock.Any()).Return(&api.Cluster{ |
| 53 | + Meta: api.Meta{ID: clusterID, CreatedTime: now, UpdatedTime: now}, |
| 54 | + Name: "test-cluster", |
| 55 | + Spec: []byte(`{"region":"us-east1"}`), |
| 56 | + Labels: []byte("{}"), |
| 57 | + StatusConditions: []byte("[]"), |
| 58 | + CreatedBy: testSystemUser, |
| 59 | + UpdatedBy: testSystemUser, |
| 60 | + }, nil) |
| 61 | + |
| 62 | + return mockClusterSvc, mockGenericSvc |
| 63 | + }, |
| 64 | + expectedStatusCode: http.StatusOK, |
| 65 | + expectedError: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Error 409 - Cluster is soft-deleted", |
| 69 | + clusterID: clusterID, |
| 70 | + setupMocks: func(ctrl *gomock.Controller) (*services.MockClusterService, *services.MockGenericService) { |
| 71 | + mockClusterSvc := services.NewMockClusterService(ctrl) |
| 72 | + mockGenericSvc := services.NewMockGenericService(ctrl) |
| 73 | + |
| 74 | + deletedTime := now |
| 75 | + mockClusterSvc.EXPECT().Get(gomock.Any(), clusterID).Return(&api.Cluster{ |
| 76 | + Meta: api.Meta{ID: clusterID, CreatedTime: now, UpdatedTime: now}, |
| 77 | + Name: "test-cluster", |
| 78 | + DeletedTime: &deletedTime, |
| 79 | + }, nil) |
| 80 | + |
| 81 | + return mockClusterSvc, mockGenericSvc |
| 82 | + }, |
| 83 | + expectedStatusCode: http.StatusConflict, |
| 84 | + expectedError: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "Error 404 - Cluster not found", |
| 88 | + clusterID: "non-existent", |
| 89 | + setupMocks: func(ctrl *gomock.Controller) (*services.MockClusterService, *services.MockGenericService) { |
| 90 | + mockClusterSvc := services.NewMockClusterService(ctrl) |
| 91 | + mockGenericSvc := services.NewMockGenericService(ctrl) |
| 92 | + |
| 93 | + mockClusterSvc.EXPECT().Get(gomock.Any(), "non-existent").Return(nil, errors.NotFound("Cluster not found")) |
| 94 | + |
| 95 | + return mockClusterSvc, mockGenericSvc |
| 96 | + }, |
| 97 | + expectedStatusCode: http.StatusNotFound, |
| 98 | + expectedError: true, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + RegisterTestingT(t) |
| 105 | + |
| 106 | + ctrl := gomock.NewController(t) |
| 107 | + defer ctrl.Finish() |
| 108 | + |
| 109 | + mockClusterSvc, mockGenericSvc := tt.setupMocks(ctrl) |
| 110 | + handler := NewClusterHandler(mockClusterSvc, mockGenericSvc) |
| 111 | + |
| 112 | + reqURL := "/api/hyperfleet/v1/clusters/" + tt.clusterID |
| 113 | + req := httptest.NewRequest(http.MethodPatch, reqURL, strings.NewReader(validBody)) |
| 114 | + req.Header.Set("Content-Type", "application/json") |
| 115 | + req = mux.SetURLVars(req, map[string]string{ |
| 116 | + "id": tt.clusterID, |
| 117 | + }) |
| 118 | + |
| 119 | + rr := httptest.NewRecorder() |
| 120 | + handler.Patch(rr, req) |
| 121 | + |
| 122 | + Expect(rr.Code).To(Equal(tt.expectedStatusCode)) |
| 123 | + |
| 124 | + if !tt.expectedError { |
| 125 | + var response openapi.Cluster |
| 126 | + err := json.Unmarshal(rr.Body.Bytes(), &response) |
| 127 | + Expect(err).NotTo(HaveOccurred()) |
| 128 | + Expect(*response.Id).To(Equal(clusterID)) |
| 129 | + } |
| 130 | + |
| 131 | + if tt.expectedStatusCode == http.StatusConflict { |
| 132 | + var errResp openapi.Error |
| 133 | + err := json.Unmarshal(rr.Body.Bytes(), &errResp) |
| 134 | + Expect(err).NotTo(HaveOccurred()) |
| 135 | + Expect(errResp.Status).To(Equal(http.StatusConflict)) |
| 136 | + Expect(*errResp.Detail).To(ContainSubstring("marked for deletion")) |
| 137 | + Expect(*errResp.Code).To(Equal("HYPERFLEET-CNF-003")) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments