|
| 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. Failures are injected by |
| 18 | +// embedding a marker token in a head change URI of the form "sq-fake=<token>": |
| 19 | +// |
| 20 | +// sq-fake=trigger-error -> Trigger returns a non-nil error |
| 21 | +// sq-fake=build-fail -> Status reports BuildStatusFailed |
| 22 | +// sq-fake=build-error -> Status returns a non-nil error |
| 23 | +// |
| 24 | +// The runner is stateless: Trigger encodes the desired terminal outcome into the |
| 25 | +// returned BuildID, and Status decides the result purely from the BuildID it is |
| 26 | +// given — no per-build bookkeeping. This means any runner instance can answer |
| 27 | +// Status for an ID minted by any other (Trigger and Status can even live in |
| 28 | +// different controllers/processes), and a single running stack can exercise the |
| 29 | +// negative paths purely by varying request payloads. It is intended for examples |
| 30 | +// and tests only, never production. |
| 31 | +package fake |
| 32 | + |
| 33 | +import ( |
| 34 | + "context" |
| 35 | + "fmt" |
| 36 | + "strings" |
| 37 | + "sync/atomic" |
| 38 | + |
| 39 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 40 | + "github.com/uber/submitqueue/submitqueue/extension/buildrunner" |
| 41 | +) |
| 42 | + |
| 43 | +// Recognized marker tokens. See the package doc for the convention. |
| 44 | +const ( |
| 45 | + tokenTriggerError = "trigger-error" |
| 46 | + tokenFail = "build-fail" |
| 47 | + tokenError = "build-error" |
| 48 | +) |
| 49 | + |
| 50 | +// outcomeOK is the BuildID outcome segment for a build that should succeed. |
| 51 | +const outcomeOK = "ok" |
| 52 | + |
| 53 | +// runner is a buildrunner.BuildRunner that reports every build as succeeded |
| 54 | +// unless a marker token in a head change URI requests otherwise. It holds no |
| 55 | +// per-build state: the outcome is encoded in the BuildID at Trigger and read |
| 56 | +// back out at Status. The atomic counter only hands out unique IDs. |
| 57 | +type runner struct { |
| 58 | + counter atomic.Uint64 |
| 59 | +} |
| 60 | + |
| 61 | +// New returns a buildrunner.BuildRunner that defaults to succeeding and honors |
| 62 | +// marker tokens embedded in head change URIs. |
| 63 | +func New() buildrunner.BuildRunner { |
| 64 | + return &runner{} |
| 65 | +} |
| 66 | + |
| 67 | +// Trigger fails when a head change URI carries the trigger-error marker; |
| 68 | +// otherwise it returns a unique BuildID that encodes the terminal outcome the |
| 69 | +// build should report at Status time (decided from the head marker). The base |
| 70 | +// changes and metadata are ignored. |
| 71 | +func (r *runner) Trigger(_ context.Context, _ []entity.Change, head []entity.Change, _ entity.BuildMetadata) (entity.BuildID, error) { |
| 72 | + outcome := outcomeOK |
| 73 | + switch markerToken(head) { |
| 74 | + case tokenTriggerError: |
| 75 | + return entity.BuildID{}, fmt.Errorf("fake: marked trigger error") |
| 76 | + case tokenFail: |
| 77 | + outcome = tokenFail |
| 78 | + case tokenError: |
| 79 | + outcome = tokenError |
| 80 | + } |
| 81 | + |
| 82 | + // Encode the outcome in the ID (e.g. "fake-build-fail-7") so Status is |
| 83 | + // stateless. The counter just keeps IDs unique. |
| 84 | + id := fmt.Sprintf("fake-%s-%d", outcome, r.counter.Add(1)) |
| 85 | + return entity.BuildID{ID: id}, nil |
| 86 | +} |
| 87 | + |
| 88 | +// Status decides the result purely from the BuildID's encoded outcome. IDs that |
| 89 | +// carry no recognized outcome (including those not minted by this fake) default |
| 90 | +// to succeeded, keeping the runner best-case. |
| 91 | +func (r *runner) Status(_ context.Context, buildID entity.BuildID) (entity.BuildStatus, entity.BuildMetadata, error) { |
| 92 | + switch { |
| 93 | + case strings.Contains(buildID.ID, tokenError): |
| 94 | + return entity.BuildStatusUnknown, nil, fmt.Errorf("fake: marked build error") |
| 95 | + case strings.Contains(buildID.ID, tokenFail): |
| 96 | + return entity.BuildStatusFailed, nil, nil |
| 97 | + default: |
| 98 | + return entity.BuildStatusSucceeded, nil, nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Cancel is a no-op and always succeeds. |
| 103 | +func (r *runner) Cancel(_ context.Context, _ entity.BuildID) error { |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// markerToken returns the first marker token found across all changes' URIs, |
| 108 | +// or "" if none carry one. |
| 109 | +func markerToken(changes []entity.Change) string { |
| 110 | + for _, change := range changes { |
| 111 | + if tok := fakeToken(change.URIs); tok != "" { |
| 112 | + return tok |
| 113 | + } |
| 114 | + } |
| 115 | + return "" |
| 116 | +} |
| 117 | + |
| 118 | +// markerPrefix introduces a marker token in a change URI: "sq-fake=<token>". |
| 119 | +const markerPrefix = "sq-fake=" |
| 120 | + |
| 121 | +// fakeToken returns the marker token embedded in the first URI that carries |
| 122 | +// one, or "" if none do. The token ends at the first "&" or "#" delimiter. |
| 123 | +func fakeToken(uris []string) string { |
| 124 | + for _, u := range uris { |
| 125 | + if i := strings.Index(u, markerPrefix); i >= 0 { |
| 126 | + rest := u[i+len(markerPrefix):] |
| 127 | + if j := strings.IndexAny(rest, "&#"); j >= 0 { |
| 128 | + rest = rest[:j] |
| 129 | + } |
| 130 | + return rest |
| 131 | + } |
| 132 | + } |
| 133 | + return "" |
| 134 | +} |
0 commit comments