|
| 1 | +// Copyright (c) Tailscale Inc & AUTHORS |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +package gobuild |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/md5" |
| 9 | + "expvar" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 18 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 19 | + "github.com/creachadair/gocache" |
| 20 | + "github.com/creachadair/gocache/cachedir" |
| 21 | + "github.com/tailscale/go-cache-plugin/lib/s3util" |
| 22 | +) |
| 23 | + |
| 24 | +func TestPutS3Metrics(t *testing.T) { |
| 25 | + ctx := context.Background() |
| 26 | + local, err := cachedir.New(t.TempDir()) |
| 27 | + if err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + // Model S3's conditional HEAD and object PUT in memory. All requests use |
| 32 | + // this transport, with no network access or credential discovery. |
| 33 | + objects := make(map[string]string) |
| 34 | + puts := make(map[string]int) |
| 35 | + heads := 0 |
| 36 | + client := s3.New(s3.Options{ |
| 37 | + Region: "us-east-1", |
| 38 | + Credentials: aws.AnonymousCredentials{}, |
| 39 | + BaseEndpoint: aws.String("https://s3.invalid"), |
| 40 | + UsePathStyle: true, |
| 41 | + RetryMaxAttempts: 1, |
| 42 | + HTTPClient: &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 43 | + status := http.StatusOK |
| 44 | + switch req.Method { |
| 45 | + case http.MethodHead: |
| 46 | + heads++ |
| 47 | + data, ok := objects[req.URL.Path] |
| 48 | + if !ok { |
| 49 | + status = http.StatusNotFound |
| 50 | + } else if req.Header.Get("If-Match") != fmt.Sprintf("%x", md5.Sum([]byte(data))) { |
| 51 | + status = http.StatusPreconditionFailed |
| 52 | + } |
| 53 | + case http.MethodPut: |
| 54 | + data, err := io.ReadAll(req.Body) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + objects[req.URL.Path] = string(data) |
| 59 | + puts[req.URL.Path]++ |
| 60 | + default: |
| 61 | + return nil, fmt.Errorf("unexpected S3 request: %s %s", req.Method, req.URL) |
| 62 | + } |
| 63 | + return &http.Response{ |
| 64 | + StatusCode: status, |
| 65 | + Header: make(http.Header), |
| 66 | + Body: io.NopCloser(strings.NewReader("")), |
| 67 | + Request: req, |
| 68 | + }, nil |
| 69 | + })}, |
| 70 | + }) |
| 71 | + cache := &S3Cache{ |
| 72 | + Local: local, |
| 73 | + S3Client: &s3util.Client{Client: client, Bucket: "test-bucket"}, |
| 74 | + UploadConcurrency: 1, |
| 75 | + } |
| 76 | + t.Cleanup(func() { cache.Close(ctx) }) |
| 77 | + metrics := new(expvar.Map).Init() |
| 78 | + cache.SetMetrics(ctx, metrics) |
| 79 | + metric := func(name string) int64 { return metrics.Get(name).(*expvar.Int).Value() } |
| 80 | + |
| 81 | + const outputID = "abcdef0123456789" |
| 82 | + const body = "cached build output" |
| 83 | + outputPath := "/test-bucket/output/ab/" + outputID |
| 84 | + for i, step := range []struct { |
| 85 | + name string |
| 86 | + actionID string |
| 87 | + object int64 |
| 88 | + found int64 |
| 89 | + }{ |
| 90 | + {"new output", "0123456789abcdef", 1, 0}, |
| 91 | + {"same output for another action", "23456789abcdef01", 0, 1}, |
| 92 | + } { |
| 93 | + t.Run(step.name, func(t *testing.T) { |
| 94 | + wantDeltas := map[string]int64{ |
| 95 | + "put_s3_object": step.object, |
| 96 | + "put_s3_found": step.found, |
| 97 | + "put_s3_action": 1, |
| 98 | + "put_s3_error": 0, |
| 99 | + } |
| 100 | + before := make(map[string]int64) |
| 101 | + for name := range wantDeltas { |
| 102 | + before[name] = metric(name) |
| 103 | + } |
| 104 | + diskPath, err := cache.Put(ctx, gocache.Object{ |
| 105 | + ActionID: step.actionID, |
| 106 | + OutputID: outputID, |
| 107 | + Size: int64(len(body)), |
| 108 | + Body: strings.NewReader(body), |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + // Waiting for the task group also checks asynchronous upload errors |
| 114 | + // and makes reads of the fake S3 state safe, without polling/sleeps. |
| 115 | + if err := cache.push.Wait(); err != nil { |
| 116 | + t.Fatalf("upload: %v", err) |
| 117 | + } |
| 118 | + for name, want := range wantDeltas { |
| 119 | + if got := metric(name) - before[name]; got != want { |
| 120 | + t.Errorf("%s increment = %d, want %d", name, got, want) |
| 121 | + } |
| 122 | + } |
| 123 | + if heads != i+1 || puts[outputPath] != 1 || objects[outputPath] != body { |
| 124 | + t.Errorf("S3 output: HEADs=%d, PUTs=%d, body=%q; want %d, 1, %q", |
| 125 | + heads, puts[outputPath], objects[outputPath], i+1, body) |
| 126 | + } |
| 127 | + actionPath := "/test-bucket/action/" + step.actionID[:2] + "/" + step.actionID |
| 128 | + if puts[actionPath] != 1 || !strings.HasPrefix(objects[actionPath], outputID+" ") { |
| 129 | + t.Errorf("S3 action: PUTs=%d, body=%q", puts[actionPath], objects[actionPath]) |
| 130 | + } |
| 131 | + // Simulate local eviction so the next Put reads and hashes the body |
| 132 | + // again instead of cachedir skipping an already-present local file. |
| 133 | + if err := os.Remove(diskPath); err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 141 | + |
| 142 | +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } |
0 commit comments