// Called just before TestMain calls m.Run. // Returns a cleanup function to be called after the tests finish. func initSinks(ctx context.Context) func() { // Create a unique GCS bucket so concurrent tests don't interfere with each other. testBucketPrefix := testProjectID + "-log-sink" testBucket := ltesting.UniqueID(testBucketPrefix) testSinkDestination = "storage.googleapis.com/" + testBucket var storageClient *storage.Client if integrationTest { // Create a unique bucket as a sink destination, and give the cloud logging account // owner right. ts := testutil.TokenSource(ctx, storage.ScopeFullControl) var err error storageClient, err = storage.NewClient(ctx, option.WithTokenSource(ts)) if err != nil { log.Fatalf("new storage client: %v", err) } bucket := storageClient.Bucket(testBucket) if err := bucket.Create(ctx, testProjectID, nil); err != nil { log.Fatalf("creating storage bucket %q: %v", testBucket, err) } if err := bucket.ACL().Set(ctx, "*****@*****.**", storage.RoleOwner); err != nil { log.Fatalf("setting owner role: %v", err) } } // Clean up from aborted tests. for _, sID := range ltesting.ExpiredUniqueIDs(sinkIDs(ctx), testSinkIDPrefix) { client.DeleteSink(ctx, sID) // ignore error } if integrationTest { for _, bn := range ltesting.ExpiredUniqueIDs(bucketNames(ctx, storageClient), testBucketPrefix) { storageClient.Bucket(bn).Delete(ctx) // ignore error } return func() { if err := storageClient.Bucket(testBucket).Delete(ctx); err != nil { log.Printf("deleting %q: %v", testBucket, err) } storageClient.Close() } } return func() {} }
// Initializes the tests before they run. func initMetrics(ctx context.Context) { // Clean up from aborted tests. var IDs []string it := client.Metrics(ctx) loop: for { m, err := it.Next() switch err { case nil: IDs = append(IDs, m.ID) case iterator.Done: break loop default: log.Printf("cleanupMetrics: %v", err) return } } for _, mID := range ltesting.ExpiredUniqueIDs(IDs, testMetricIDPrefix) { client.DeleteMetric(ctx, mID) } }