Skip to content

Commit d7af21b

Browse files
tests(cloudhealthcare): use t.Cleanup() instead of defer (googleapis#2332)
## Description Use t.Cleanup() to register cleanup of FHIR and DICOM stores immediately after creation. This fixes the uncleaned FHIR/DICOM stores that remain in the project(In the earlier implementation, teardown does not get triggered if the test failed). 🛠️ Fixes googleapis#1986 --------- Co-authored-by: Yuan Teoh <yuanteoh@google.com> Co-authored-by: Yuan Teoh <45984206+Yuan325@users.noreply.github.com>
1 parent adc9589 commit d7af21b

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

.ci/integration.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ steps:
293293
.ci/test_with_coverage.sh \
294294
"Cloud Healthcare API" \
295295
cloudhealthcare \
296-
cloudhealthcare || echo "Integration tests failed."
296+
cloudhealthcare
297297
298298
- id: "postgres"
299299
name: golang:1

tests/cloudhealthcare/cloud_healthcare_integration_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ func TestHealthcareToolEndpoints(t *testing.T) {
112112
fhirStoreID := "fhir-store-" + uuid.New().String()
113113
dicomStoreID := "dicom-store-" + uuid.New().String()
114114

115-
patient1ID, patient2ID, teardown := setupHealthcareResources(t, healthcareService, healthcareDataset, fhirStoreID, dicomStoreID)
116-
defer teardown(t)
115+
patient1ID, patient2ID := setupHealthcareResources(t, healthcareService, healthcareDataset, fhirStoreID, dicomStoreID)
117116

118117
toolsFile := getToolsConfig(sourceConfig)
119118
toolsFile = addClientAuthSourceConfig(t, toolsFile)
@@ -173,10 +172,8 @@ func TestHealthcareToolWithStoreRestriction(t *testing.T) {
173172
disallowedFHIRStoreID := "fhir-store-disallowed-" + uuid.New().String()
174173
disallowedDICOMStoreID := "dicom-store-disallowed-" + uuid.New().String()
175174

176-
_, _, teardownAllowedStores := setupHealthcareResources(t, healthcareService, healthcareDataset, allowedFHIRStoreID, allowedDICOMStoreID)
177-
defer teardownAllowedStores(t)
178-
_, _, teardownDisallowedStores := setupHealthcareResources(t, healthcareService, healthcareDataset, disallowedFHIRStoreID, disallowedDICOMStoreID)
179-
defer teardownDisallowedStores(t)
175+
setupHealthcareResources(t, healthcareService, healthcareDataset, allowedFHIRStoreID, allowedDICOMStoreID)
176+
setupHealthcareResources(t, healthcareService, healthcareDataset, disallowedFHIRStoreID, disallowedDICOMStoreID)
180177

181178
// Configure source with dataset restriction.
182179
sourceConfig["allowedFhirStores"] = []string{allowedFHIRStoreID}
@@ -257,7 +254,7 @@ func newHealthcareService(ctx context.Context) (*healthcare.Service, error) {
257254
return healthcareService, nil
258255
}
259256

260-
func setupHealthcareResources(t *testing.T, service *healthcare.Service, datasetID, fhirStoreID, dicomStoreID string) (string, string, func(*testing.T)) {
257+
func setupHealthcareResources(t *testing.T, service *healthcare.Service, datasetID, fhirStoreID, dicomStoreID string) (string, string) {
261258
datasetName := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", healthcareProject, healthcareRegion, datasetID)
262259
var err error
263260

@@ -266,12 +263,24 @@ func setupHealthcareResources(t *testing.T, service *healthcare.Service, dataset
266263
if fhirStore, err = service.Projects.Locations.Datasets.FhirStores.Create(datasetName, fhirStore).FhirStoreId(fhirStoreID).Do(); err != nil {
267264
t.Fatalf("failed to create fhir store: %v", err)
268265
}
266+
// Register cleanup
267+
t.Cleanup(func() {
268+
if _, err := service.Projects.Locations.Datasets.FhirStores.Delete(fhirStore.Name).Do(); err != nil {
269+
t.Logf("failed to delete fhir store: %v", err)
270+
}
271+
})
269272

270273
// Create DICOM store
271274
dicomStore := &healthcare.DicomStore{}
272275
if dicomStore, err = service.Projects.Locations.Datasets.DicomStores.Create(datasetName, dicomStore).DicomStoreId(dicomStoreID).Do(); err != nil {
273276
t.Fatalf("failed to create dicom store: %v", err)
274277
}
278+
// Register cleanup
279+
t.Cleanup(func() {
280+
if _, err := service.Projects.Locations.Datasets.DicomStores.Delete(dicomStore.Name).Do(); err != nil {
281+
t.Logf("failed to delete dicom store: %v", err)
282+
}
283+
})
275284

276285
// Create Patient 1
277286
patient1Body := bytes.NewBuffer([]byte(`{
@@ -317,15 +326,7 @@ func setupHealthcareResources(t *testing.T, service *healthcare.Service, dataset
317326
createFHIRResource(t, service, fhirStore.Name, "Observation", observation2Body)
318327
}
319328

320-
teardown := func(t *testing.T) {
321-
if _, err := service.Projects.Locations.Datasets.FhirStores.Delete(fhirStore.Name).Do(); err != nil {
322-
t.Logf("failed to delete fhir store: %v", err)
323-
}
324-
if _, err := service.Projects.Locations.Datasets.DicomStores.Delete(dicomStore.Name).Do(); err != nil {
325-
t.Logf("failed to delete dicom store: %v", err)
326-
}
327-
}
328-
return patient1ID, patient2ID, teardown
329+
return patient1ID, patient2ID
329330
}
330331

331332
func getToolsConfig(sourceConfig map[string]any) map[string]any {

0 commit comments

Comments
 (0)