github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/pkg/app/env.go (about) 1 // Copyright 2024 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package app 5 6 import ( 7 "context" 8 "fmt" 9 "os" 10 "testing" 11 12 "cloud.google.com/go/spanner" 13 "github.com/google/syzkaller/syz-cluster/pkg/api" 14 "github.com/google/syzkaller/syz-cluster/pkg/blob" 15 "github.com/google/syzkaller/syz-cluster/pkg/db" 16 ) 17 18 type AppEnvironment struct { 19 Spanner *spanner.Client 20 BlobStorage blob.Storage 21 Config *AppConfig 22 URLs *api.URLGenerator 23 } 24 25 func Environment(ctx context.Context) (*AppEnvironment, error) { 26 spanner, err := DefaultSpanner(ctx) 27 if err != nil { 28 return nil, fmt.Errorf("failed to set up a Spanner client: %w", err) 29 } 30 storage, err := DefaultStorage(ctx) 31 if err != nil { 32 return nil, fmt.Errorf("failed to set up the blob storage: %w", err) 33 } 34 cfg, err := Config() 35 if err != nil { 36 return nil, fmt.Errorf("failed to query the config: %w", err) 37 } 38 return &AppEnvironment{ 39 Spanner: spanner, 40 BlobStorage: storage, 41 Config: cfg, 42 URLs: api.NewURLGenerator(cfg.URL), 43 }, nil 44 } 45 46 func TestEnvironment(t *testing.T) (*AppEnvironment, context.Context) { 47 client, ctx := db.NewTransientDB(t) 48 return &AppEnvironment{ 49 Spanner: client, 50 BlobStorage: blob.NewLocalStorage(t.TempDir()), 51 Config: &AppConfig{ 52 Name: "Test", 53 }, 54 URLs: api.NewURLGenerator("http://dashboard"), 55 }, ctx 56 } 57 58 func DefaultSpannerURI() (db.ParsedURI, error) { 59 rawURI := os.Getenv("SPANNER_DATABASE_URI") 60 if rawURI == "" { 61 return db.ParsedURI{}, fmt.Errorf("no SPANNER_DATABASE_URI is set") 62 } 63 return db.ParseURI(rawURI) 64 } 65 66 func DefaultSpanner(ctx context.Context) (*spanner.Client, error) { 67 uri, err := DefaultSpannerURI() 68 if err != nil { 69 // Validate the URI early on. 70 return nil, err 71 } 72 return spanner.NewClient(ctx, uri.Full) 73 } 74 75 func DefaultStorage(ctx context.Context) (blob.Storage, error) { 76 // BLOB_STORAGE_GCS_BUCKET is the only supported option. 77 bucket := os.Getenv("BLOB_STORAGE_GCS_BUCKET") 78 if bucket == "" { 79 return nil, fmt.Errorf("empty BLOB0_STORAGE_GCS_BUCKET") 80 } 81 return blob.NewGCSClient(ctx, bucket) 82 } 83 84 func DefaultClient() *api.Client { 85 return api.NewClient(`http://controller-service:8080`) 86 } 87 88 func DefaultReporterClient() *api.ReporterClient { 89 return api.NewReporterClient(`http://reporter-server-service:8080`) 90 }