|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package fake provides a buildrunner.BuildRunner whose outcome is driven by the |
| 16 | +// triggered changes. With no marker every build immediately succeeds, behaving |
| 17 | +// as a best-case stub for wiring and baselines. A failure can be injected |
| 18 | +// end-to-end (e.g. from an e2e land request) by embedding a marker token in a |
| 19 | +// head change URI of the form "sq-fake=<token>": |
| 20 | +// |
| 21 | +// sq-fake=build-fail -> Status reports BuildStatusFailed |
| 22 | +// sq-fake=build-error -> Status returns a non-nil error |
| 23 | +// |
| 24 | +// Because the contract reports a build's result via Status (which receives only |
| 25 | +// a BuildID), the runner records the desired outcome per build ID at Trigger |
| 26 | +// time and looks it up at Status time. This lets a single running stack exercise |
| 27 | +// negative paths purely by varying request payloads. It is intended for examples |
| 28 | +// and tests only, never production. |
| 29 | +package fake |
| 30 | + |
| 31 | +import ( |
| 32 | + "context" |
| 33 | + "fmt" |
| 34 | + "strings" |
| 35 | + "sync" |
| 36 | + "sync/atomic" |
| 37 | + |
| 38 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 39 | + "github.com/uber/submitqueue/submitqueue/extension/buildrunner" |
| 40 | +) |
| 41 | + |
| 42 | +// Recognized marker tokens. See the package doc for the convention. |
| 43 | +const ( |
| 44 | + tokenFail = "build-fail" |
| 45 | + tokenError = "build-error" |
| 46 | +) |
| 47 | + |
| 48 | +// outcome is the result a build will report at Status time, decided from the |
| 49 | +// head changes at Trigger time. |
| 50 | +type outcome int |
| 51 | + |
| 52 | +const ( |
| 53 | + outcomeSucceeded outcome = iota |
| 54 | + outcomeFailed |
| 55 | + outcomeError |
| 56 | +) |
| 57 | + |
| 58 | +// runner is a buildrunner.BuildRunner that reports every build as succeeded |
| 59 | +// unless a marker token in a head change URI requests otherwise. The desired |
| 60 | +// outcome is recorded per build ID at Trigger and read back at Status. The |
| 61 | +// atomic counter hands out unique build IDs; the mutex guards the outcome map. |
| 62 | +type runner struct { |
| 63 | + counter atomic.Uint64 |
| 64 | + mu sync.Mutex |
| 65 | + outcomes map[string]outcome |
| 66 | +} |
| 67 | + |
| 68 | +// New returns a buildrunner.BuildRunner that defaults to succeeding and honors |
| 69 | +// marker tokens embedded in head change URIs. |
| 70 | +func New() buildrunner.BuildRunner { |
| 71 | + return &runner{outcomes: make(map[string]outcome)} |
| 72 | +} |
| 73 | + |
| 74 | +// Trigger records the desired outcome for a new build (decided from a marker |
| 75 | +// token in the head changes) and returns its unique build ID. The base changes |
| 76 | +// and metadata are ignored. |
| 77 | +func (r *runner) Trigger(_ context.Context, _ []entity.Change, head []entity.Change, _ entity.BuildMetadata) (entity.BuildID, error) { |
| 78 | + o := outcomeSucceeded |
| 79 | + switch markerToken(head) { |
| 80 | + case tokenFail: |
| 81 | + o = outcomeFailed |
| 82 | + case tokenError: |
| 83 | + o = outcomeError |
| 84 | + } |
| 85 | + |
| 86 | + id := fmt.Sprintf("fake-%d", r.counter.Add(1)) |
| 87 | + r.mu.Lock() |
| 88 | + r.outcomes[id] = o |
| 89 | + r.mu.Unlock() |
| 90 | + return entity.BuildID{ID: id}, nil |
| 91 | +} |
| 92 | + |
| 93 | +// Status returns the outcome recorded for the build at Trigger time. Unknown |
| 94 | +// build IDs default to succeeded, keeping the runner best-case for builds it did |
| 95 | +// not create. |
| 96 | +func (r *runner) Status(_ context.Context, buildID entity.BuildID) (entity.BuildStatus, entity.BuildMetadata, error) { |
| 97 | + r.mu.Lock() |
| 98 | + o := r.outcomes[buildID.ID] |
| 99 | + r.mu.Unlock() |
| 100 | + |
| 101 | + switch o { |
| 102 | + case outcomeFailed: |
| 103 | + return entity.BuildStatusFailed, nil, nil |
| 104 | + case outcomeError: |
| 105 | + return entity.BuildStatusUnknown, nil, fmt.Errorf("fake: marked build error") |
| 106 | + default: |
| 107 | + return entity.BuildStatusSucceeded, nil, nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Cancel is a no-op and always succeeds. |
| 112 | +func (r *runner) Cancel(_ context.Context, _ entity.BuildID) error { |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// markerToken returns the first marker token found across all changes' URIs, |
| 117 | +// or "" if none carry one. |
| 118 | +func markerToken(changes []entity.Change) string { |
| 119 | + for _, change := range changes { |
| 120 | + if tok := fakeToken(change.URIs); tok != "" { |
| 121 | + return tok |
| 122 | + } |
| 123 | + } |
| 124 | + return "" |
| 125 | +} |
| 126 | + |
| 127 | +// markerPrefix introduces a marker token in a change URI: "sq-fake=<token>". |
| 128 | +const markerPrefix = "sq-fake=" |
| 129 | + |
| 130 | +// fakeToken returns the marker token embedded in the first URI that carries |
| 131 | +// one, or "" if none do. The token ends at the first "&" or "#" delimiter. |
| 132 | +func fakeToken(uris []string) string { |
| 133 | + for _, u := range uris { |
| 134 | + if i := strings.Index(u, markerPrefix); i >= 0 { |
| 135 | + rest := u[i+len(markerPrefix):] |
| 136 | + if j := strings.IndexAny(rest, "&#"); j >= 0 { |
| 137 | + rest = rest[:j] |
| 138 | + } |
| 139 | + return rest |
| 140 | + } |
| 141 | + } |
| 142 | + return "" |
| 143 | +} |
0 commit comments