aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/pkg/app/env.go
blob: 191513ddd8199b03bbf9c1503bb0735bca16c5f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package app

import (
	"context"
	"fmt"
	"os"
	"testing"

	"cloud.google.com/go/spanner"
	"github.com/google/syzkaller/syz-cluster/pkg/api"
	"github.com/google/syzkaller/syz-cluster/pkg/blob"
	"github.com/google/syzkaller/syz-cluster/pkg/db"
)

type AppEnvironment struct {
	Spanner     *spanner.Client
	BlobStorage blob.Storage
	Config      *AppConfig
	URLs        *api.URLGenerator
}

func Environment(ctx context.Context) (*AppEnvironment, error) {
	spanner, err := DefaultSpanner(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to set up a Spanner client: %w", err)
	}
	storage, err := DefaultStorage(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to set up the blob storage: %w", err)
	}
	cfg, err := Config()
	if err != nil {
		return nil, fmt.Errorf("failed to query the config: %w", err)
	}
	return &AppEnvironment{
		Spanner:     spanner,
		BlobStorage: storage,
		Config:      cfg,
		URLs:        api.NewURLGenerator(cfg.URL),
	}, nil
}

func TestEnvironment(t *testing.T) (*AppEnvironment, context.Context) {
	client, ctx := db.NewTransientDB(t)
	return &AppEnvironment{
		Spanner:     client,
		BlobStorage: blob.NewLocalStorage(t.TempDir()),
		Config: &AppConfig{
			Name: "Test",
		},
		URLs: api.NewURLGenerator("http://dashboard"),
	}, ctx
}

func DefaultSpannerURI() (db.ParsedURI, error) {
	rawURI := os.Getenv("SPANNER_DATABASE_URI")
	if rawURI == "" {
		return db.ParsedURI{}, fmt.Errorf("no SPANNER_DATABASE_URI is set")
	}
	return db.ParseURI(rawURI)
}

func DefaultSpanner(ctx context.Context) (*spanner.Client, error) {
	uri, err := DefaultSpannerURI()
	if err != nil {
		// Validate the URI early on.
		return nil, err
	}
	return spanner.NewClient(ctx, uri.Full)
}

func DefaultStorage(ctx context.Context) (blob.Storage, error) {
	// BLOB_STORAGE_GCS_BUCKET is the only supported option.
	bucket := os.Getenv("BLOB_STORAGE_GCS_BUCKET")
	if bucket == "" {
		return nil, fmt.Errorf("empty BLOB0_STORAGE_GCS_BUCKET")
	}
	return blob.NewGCSClient(ctx, bucket)
}

func DefaultClient() *api.Client {
	return api.NewClient(`http://controller-service:8080`)
}

func DefaultReporterClient() *api.ReporterClient {
	return api.NewReporterClient(`http://reporter-server-service:8080`)
}