@@ -28,6 +28,7 @@ import (
2828 "net/http"
2929 "os"
3030 "regexp"
31+ "strconv"
3132 "strings"
3233 "testing"
3334 "time"
8081 }
8182)
8283
84+ func TestMain (m * testing.M ) {
85+ if healthcareProject != "" && healthcareRegion != "" && healthcareDataset != "" {
86+ ctx := context .Background ()
87+ if service , err := newHealthcareService (ctx ); err == nil {
88+ cleanupOrphanedStores (ctx , service )
89+ }
90+ }
91+ os .Exit (m .Run ())
92+ }
93+
94+ func cleanupOrphanedStores (ctx context.Context , service * healthcare.Service ) {
95+ now := time .Now ().Unix ()
96+ datasetName := fmt .Sprintf ("projects/%s/locations/%s/datasets/%s" , healthcareProject , healthcareRegion , healthcareDataset )
97+
98+ // Cleanup FHIR stores over 2 hours old
99+ _ = service .Projects .Locations .Datasets .FhirStores .List (datasetName ).Pages (ctx , func (page * healthcare.ListFhirStoresResponse ) error {
100+ for _ , store := range page .FhirStores {
101+ if ! strings .Contains (store .Name , "/fhirStores/fhir-store-" ) {
102+ continue
103+ }
104+ createdAtStr , ok := store .Labels ["created_at" ]
105+ createdAt , err := strconv .ParseInt (createdAtStr , 10 , 64 )
106+ if ! ok || err != nil || now - createdAt > 2 * 3600 {
107+ fmt .Printf ("Cleaning up orphaned FHIR store: %s\n " , store .Name )
108+ _ , _ = service .Projects .Locations .Datasets .FhirStores .Delete (store .Name ).Context (ctx ).Do ()
109+ }
110+ }
111+ return nil
112+ })
113+
114+ // Cleanup DICOM stores over 2 hours old
115+ _ = service .Projects .Locations .Datasets .DicomStores .List (datasetName ).Pages (ctx , func (page * healthcare.ListDicomStoresResponse ) error {
116+ for _ , store := range page .DicomStores {
117+ if ! strings .Contains (store .Name , "/dicomStores/dicom-store-" ) {
118+ continue
119+ }
120+ createdAtStr , ok := store .Labels ["created_at" ]
121+ createdAt , err := strconv .ParseInt (createdAtStr , 10 , 64 )
122+ if ! ok || err != nil || now - createdAt > 2 * 3600 {
123+ fmt .Printf ("Cleaning up orphaned DICOM store: %s\n " , store .Name )
124+ _ , _ = service .Projects .Locations .Datasets .DicomStores .Delete (store .Name ).Context (ctx ).Do ()
125+ }
126+ }
127+ return nil
128+ })
129+ }
130+
83131func getHealthcareVars (t * testing.T ) map [string ]any {
84132 switch "" {
85133 case healthcareProject :
@@ -257,7 +305,10 @@ func setupHealthcareResources(t *testing.T, service *healthcare.Service, dataset
257305 var err error
258306
259307 // Create FHIR store
260- fhirStore := & healthcare.FhirStore {Version : "R4" }
308+ fhirStore := & healthcare.FhirStore {
309+ Version : "R4" ,
310+ Labels : map [string ]string {"created_at" : strconv .FormatInt (time .Now ().Unix (), 10 )},
311+ }
261312 if fhirStore , err = service .Projects .Locations .Datasets .FhirStores .Create (datasetName , fhirStore ).FhirStoreId (fhirStoreID ).Do (); err != nil {
262313 t .Fatalf ("failed to create fhir store: %v" , err )
263314 }
@@ -269,7 +320,9 @@ func setupHealthcareResources(t *testing.T, service *healthcare.Service, dataset
269320 })
270321
271322 // Create DICOM store
272- dicomStore := & healthcare.DicomStore {}
323+ dicomStore := & healthcare.DicomStore {
324+ Labels : map [string ]string {"created_at" : strconv .FormatInt (time .Now ().Unix (), 10 )},
325+ }
273326 if dicomStore , err = service .Projects .Locations .Datasets .DicomStores .Create (datasetName , dicomStore ).DicomStoreId (dicomStoreID ).Do (); err != nil {
274327 t .Fatalf ("failed to create dicom store: %v" , err )
275328 }
0 commit comments