relay-flow 0.1.0-alpha.0 → 0.2.1-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +160 -143
- package/cmd/relay-flow/commands_test.go +968 -0
- package/cmd/relay-flow/main.go +877 -175
- package/cmd/relay-flow/scenario_test.go +1310 -0
- package/cmd/relay-flow/serve.go +610 -0
- package/examples/default-story-workflow.yaml +88 -0
- package/go.mod +69 -2
- package/go.sum +185 -0
- package/internal/config/config.go +88 -0
- package/internal/config/machine.go +99 -48
- package/internal/config/machine_test.go +248 -0
- package/internal/config/merge_test.go +118 -0
- package/internal/config/writeatomic.go +36 -0
- package/internal/config/writeatomic_test.go +98 -0
- package/internal/execution/goworkflows/activities.go +490 -0
- package/internal/execution/goworkflows/engine.go +520 -0
- package/internal/execution/goworkflows/engine_test.go +660 -0
- package/internal/execution/goworkflows/fakes_test.go +509 -0
- package/internal/execution/goworkflows/interpreter.go +613 -0
- package/internal/execution/goworkflows/logging_test.go +154 -0
- package/internal/execution/goworkflows/mailbox_test.go +423 -0
- package/internal/execution/goworkflows/node_runtime_integration_test.go +133 -0
- package/internal/execution/goworkflows/node_runtime_test.go +510 -0
- package/internal/execution/goworkflows/projection.go +504 -0
- package/internal/execution/goworkflows/recovery_test.go +1092 -0
- package/internal/execution/goworkflows/retry_log_test.go +59 -0
- package/internal/execution/goworkflows/retry_projection_test.go +98 -0
- package/internal/harness/contract_test.go +174 -0
- package/internal/harness/factory.go +63 -0
- package/internal/harness/harness.go +41 -0
- package/internal/harness/opencode/opencode.go +166 -0
- package/internal/harness/opencode/opencode_test.go +50 -0
- package/internal/harness/plugin_selection_test.go +126 -0
- package/internal/identity/identity.go +37 -0
- package/internal/logging/logging.go +56 -0
- package/internal/logging/logging_test.go +116 -0
- package/internal/paths/paths.go +69 -0
- package/internal/recover/recover.go +115 -0
- package/internal/repo/poller.go +186 -0
- package/internal/repo/poller_test.go +327 -0
- package/internal/repo/repo.go +132 -0
- package/internal/repo/service.go +216 -0
- package/internal/repo/service_test.go +298 -0
- package/internal/retry/retry.go +118 -0
- package/internal/router/router.go +84 -0
- package/internal/router/router_test.go +231 -0
- package/internal/run/manager.go +121 -0
- package/internal/run/run.go +140 -0
- package/internal/run/run_identity_test.go +52 -0
- package/internal/run/run_manager_test.go +286 -0
- package/internal/runner/contract_test.go +259 -0
- package/internal/runner/factory.go +65 -0
- package/internal/runner/orca/orca.go +341 -172
- package/internal/runner/orca/orca_test.go +256 -143
- package/internal/runner/orca/orcacli/orcacli.go +220 -0
- package/internal/runner/orca/orcacli/orcacli_test.go +157 -0
- package/internal/runner/orca/orcacli/testdata/repo-list.json +18 -0
- package/internal/runner/orca/orcacli/testdata/strict-orca.sh +32 -0
- package/internal/runner/orca/orcacli/testdata/terminal-close.json +12 -0
- package/internal/runner/orca/orcacli/testdata/terminal-create.json +18 -0
- package/internal/runner/orca/orcacli/testdata/terminal-list.json +51 -0
- package/internal/runner/orca/orcacli/testdata/terminal-send.json +1 -0
- package/internal/runner/orca/orcacli/testdata/terminal-show.json +1 -0
- package/internal/runner/orca/orcacli/testdata/worktree-create.json +22 -0
- package/internal/runner/orca/orcacli/testdata/worktree-list.json +31 -0
- package/internal/runner/orca/orcacli/testdata/worktree-remove.json +6 -0
- package/internal/runner/runner.go +54 -64
- package/internal/server/api_test.go +300 -0
- package/internal/server/client.go +192 -74
- package/internal/server/fixture_test.go +248 -0
- package/internal/server/server.go +425 -248
- package/internal/server/shutdown_test.go +116 -0
- package/internal/task/auth_test.go +48 -0
- package/internal/task/contract_test.go +225 -0
- package/internal/task/factory.go +119 -0
- package/internal/task/jira/auth.go +183 -0
- package/internal/task/jira/auth_test.go +107 -0
- package/internal/task/jira/effects_test.go +39 -0
- package/internal/task/jira/filters_test.go +254 -0
- package/internal/task/jira/helpers_test.go +70 -0
- package/internal/task/jira/jira.go +538 -0
- package/internal/task/jira/normalize.go +119 -0
- package/internal/task/jira/rest/adf.go +128 -0
- package/internal/task/jira/rest/client.go +573 -0
- package/internal/task/jira/rest/client_test.go +381 -0
- package/internal/task/jira/testdata/jira_search_issues.json +120 -0
- package/internal/task/jira/transition_defaults_test.go +158 -0
- package/internal/task/jira/validation_test.go +94 -0
- package/internal/task/task.go +84 -0
- package/internal/workflow/report.go +85 -0
- package/internal/workflow/report_test.go +259 -0
- package/internal/workflow/service.go +142 -0
- package/internal/workflow/store.go +136 -0
- package/internal/workflow/store_test.go +282 -0
- package/internal/workflow/workflow.go +345 -0
- package/internal/workflow/workflow_test.go +412 -0
- package/package.json +7 -2
- package/internal/acli/acli.go +0 -229
- package/internal/config/demo_test.go +0 -17
- package/internal/config/schema.go +0 -193
- package/internal/config/schema_test.go +0 -162
- package/internal/daemon/daemon.go +0 -218
- package/internal/daemon/daemon_test.go +0 -204
- package/internal/discovery/discovery.go +0 -122
- package/internal/discovery/discovery_test.go +0 -62
- package/internal/opencode/opencode.go +0 -26
- package/internal/orcacli/orcacli.go +0 -264
- package/internal/runner/orca/README.md +0 -64
- package/internal/runner/runner_test.go +0 -64
- package/internal/server/server_test.go +0 -195
- package/internal/tasks/jira/README.md +0 -69
- package/internal/tasks/jira/component_test.go +0 -16
- package/internal/tasks/jira/decode.go +0 -24
- package/internal/tasks/jira/jira.go +0 -231
- package/internal/tasks/jira/jira_test.go +0 -259
- package/internal/tasks/jira/jql_test.go +0 -16
- package/internal/tasks/tasks.go +0 -90
- package/internal/tasks/tasks_test.go +0 -91
|
@@ -5,122 +5,240 @@ import (
|
|
|
5
5
|
"context"
|
|
6
6
|
"encoding/json"
|
|
7
7
|
"fmt"
|
|
8
|
+
"io"
|
|
8
9
|
"net"
|
|
9
10
|
"net/http"
|
|
10
|
-
"
|
|
11
|
+
"net/url"
|
|
11
12
|
|
|
12
|
-
"github.com/rajpopat27/relay-flow/internal/
|
|
13
|
+
"github.com/rajpopat27/relay-flow/internal/repo"
|
|
14
|
+
"github.com/rajpopat27/relay-flow/internal/run"
|
|
15
|
+
"github.com/rajpopat27/relay-flow/internal/runner"
|
|
16
|
+
"github.com/rajpopat27/relay-flow/internal/workflow"
|
|
13
17
|
)
|
|
14
18
|
|
|
15
|
-
// Client
|
|
16
|
-
//
|
|
17
|
-
//
|
|
19
|
+
// Client is the typed HTTP-over-Unix-socket client matching the routes in
|
|
20
|
+
// docs/structs-methods-interfaces.md. Commands use it; the CLI never
|
|
21
|
+
// speaks JSON itself.
|
|
18
22
|
type Client struct {
|
|
19
23
|
Socket string
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
// NewClient returns a Client
|
|
23
|
-
func NewClient()
|
|
24
|
-
p, err := discovery.SocketPath()
|
|
25
|
-
if err != nil {
|
|
26
|
-
return nil, err
|
|
27
|
-
}
|
|
28
|
-
return &Client{Socket: p}, nil
|
|
29
|
-
}
|
|
26
|
+
// NewClient returns a Client bound to the given socket path.
|
|
27
|
+
func NewClient(socket string) *Client { return &Client{Socket: socket} }
|
|
30
28
|
|
|
31
29
|
func (c *Client) httpClient() *http.Client {
|
|
32
|
-
return &http.Client{
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
return &http.Client{
|
|
31
|
+
Transport: &http.Transport{
|
|
32
|
+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
33
|
+
return (&net.Dialer{}).DialContext(ctx, "unix", c.Socket)
|
|
34
|
+
},
|
|
35
35
|
},
|
|
36
|
-
}
|
|
36
|
+
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
// call performs one HTTP request against the socket server and decodes
|
|
40
|
+
// the standard envelope. Non-2xx responses return an error carrying the
|
|
41
|
+
// server-provided code and message.
|
|
42
|
+
func (c *Client) call(ctx context.Context, method, path string, body []byte, out any) error {
|
|
43
|
+
var rdr io.Reader
|
|
41
44
|
if body != nil {
|
|
42
|
-
|
|
43
|
-
rdr = bytes.NewReader(b)
|
|
44
|
-
} else {
|
|
45
|
-
rdr = bytes.NewReader(nil)
|
|
45
|
+
rdr = bytes.NewReader(body)
|
|
46
46
|
}
|
|
47
|
-
req, err := http.
|
|
47
|
+
req, err := http.NewRequestWithContext(ctx, method, "http://unix"+path, rdr)
|
|
48
48
|
if err != nil {
|
|
49
49
|
return err
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
if body != nil {
|
|
52
|
+
// Default to JSON; raw YAML workflow submits override below.
|
|
53
|
+
ct := "application/json"
|
|
54
|
+
if len(body) > 0 && body[0] != '{' && body[0] != '[' {
|
|
55
|
+
ct = "application/x-yaml"
|
|
56
|
+
}
|
|
57
|
+
req.Header.Set("Content-Type", ct)
|
|
58
|
+
}
|
|
52
59
|
resp, err := c.httpClient().Do(req)
|
|
53
60
|
if err != nil {
|
|
54
|
-
|
|
55
|
-
return fmt.Errorf("no server at %s — is `relay-flow serve` running?", c.Socket)
|
|
56
|
-
}
|
|
57
|
-
return fmt.Errorf("server call %s %s: %w (is `relay-flow serve` running?)", method, path, err)
|
|
61
|
+
return fmt.Errorf("server call %s %s: %w", method, path, err)
|
|
58
62
|
}
|
|
59
63
|
defer resp.Body.Close()
|
|
64
|
+
raw, _ := io.ReadAll(resp.Body)
|
|
60
65
|
var env struct {
|
|
61
|
-
OK
|
|
62
|
-
|
|
66
|
+
OK bool `json:"ok"`
|
|
67
|
+
Data json.RawMessage `json:"data"`
|
|
68
|
+
Error *struct {
|
|
69
|
+
Code string `json:"code"`
|
|
70
|
+
Message string `json:"message"`
|
|
71
|
+
} `json:"error"`
|
|
72
|
+
}
|
|
73
|
+
if err := json.Unmarshal(raw, &env); err != nil {
|
|
74
|
+
return fmt.Errorf("server response not JSON envelope: %w", err)
|
|
63
75
|
}
|
|
64
|
-
if
|
|
65
|
-
|
|
76
|
+
if !env.OK {
|
|
77
|
+
if env.Error != nil {
|
|
78
|
+
return fmt.Errorf("server %s: %s", env.Error.Code, env.Error.Message)
|
|
79
|
+
}
|
|
80
|
+
return fmt.Errorf("server returned not-ok (HTTP %d)", resp.StatusCode)
|
|
66
81
|
}
|
|
67
|
-
if
|
|
68
|
-
if env.
|
|
69
|
-
return fmt.Errorf("
|
|
82
|
+
if out != nil && len(env.Data) > 0 {
|
|
83
|
+
if err := json.Unmarshal(env.Data, out); err != nil {
|
|
84
|
+
return fmt.Errorf("decode response data: %w", err)
|
|
70
85
|
}
|
|
71
|
-
return fmt.Errorf("server: status %d", resp.StatusCode)
|
|
72
86
|
}
|
|
73
87
|
return nil
|
|
74
88
|
}
|
|
75
89
|
|
|
76
|
-
//
|
|
77
|
-
func (c *Client)
|
|
78
|
-
return c.
|
|
79
|
-
"repoPath": repoPath, "yaml": string(yamlBytes),
|
|
80
|
-
})
|
|
90
|
+
// Stop asks the server to shut down gracefully.
|
|
91
|
+
func (c *Client) Stop(ctx context.Context) error {
|
|
92
|
+
return c.call(ctx, http.MethodPost, "/stop", nil, nil)
|
|
81
93
|
}
|
|
82
94
|
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
95
|
+
// SubmitWorkflow creates or replaces a workflow definition from YAML.
|
|
96
|
+
// The request body is the raw YAML document, not a JSON wrapper.
|
|
97
|
+
func (c *Client) SubmitWorkflow(ctx context.Context, yaml []byte) (*workflow.Workflow, error) {
|
|
98
|
+
var wf workflow.Workflow
|
|
99
|
+
if err := c.call(ctx, http.MethodPost, "/workflows", yaml, &wf); err != nil {
|
|
100
|
+
return nil, err
|
|
101
|
+
}
|
|
102
|
+
return &wf, nil
|
|
86
103
|
}
|
|
87
104
|
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
Detail string `json:"detail"`
|
|
105
|
+
// RemoveWorkflow deletes a workflow by name.
|
|
106
|
+
func (c *Client) RemoveWorkflow(ctx context.Context, name string) error {
|
|
107
|
+
return c.call(ctx, http.MethodDelete, "/workflows/"+url.PathEscape(name), nil, nil)
|
|
92
108
|
}
|
|
93
109
|
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
"workflow": workflow, "ticket": ticket, "node": node, "outcome": outcome, "summary": summary,
|
|
99
|
-
})
|
|
100
|
-
req, err := http.NewRequest("POST", "http://unix/report", bytes.NewReader(b))
|
|
101
|
-
if err != nil {
|
|
110
|
+
// ListWorkflows returns every registered workflow.
|
|
111
|
+
func (c *Client) ListWorkflows(ctx context.Context) ([]*workflow.Workflow, error) {
|
|
112
|
+
var out []*workflow.Workflow
|
|
113
|
+
if err := c.call(ctx, http.MethodGet, "/workflows", nil, &out); err != nil {
|
|
102
114
|
return nil, err
|
|
103
115
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
116
|
+
return out, nil
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// GetWorkflow returns one workflow by name.
|
|
120
|
+
func (c *Client) GetWorkflow(ctx context.Context, name string) (*workflow.Workflow, error) {
|
|
121
|
+
var wf workflow.Workflow
|
|
122
|
+
if err := c.call(ctx, http.MethodGet, "/workflows/"+url.PathEscape(name), nil, &wf); err != nil {
|
|
123
|
+
return nil, err
|
|
111
124
|
}
|
|
112
|
-
|
|
125
|
+
return &wf, nil
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// DiscoverRepos returns runner-visible registration candidates.
|
|
129
|
+
func (c *Client) DiscoverRepos(ctx context.Context) ([]runner.RepoCandidate, error) {
|
|
130
|
+
var out []runner.RepoCandidate
|
|
131
|
+
if err := c.call(ctx, http.MethodGet, "/repos/discover", nil, &out); err != nil {
|
|
132
|
+
return nil, err
|
|
133
|
+
}
|
|
134
|
+
return out, nil
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// RepoTaskFields returns the task factory's required repo keys, matching
|
|
138
|
+
// GET /repos/task-fields (server wraps the list as {"fields": [...]}).
|
|
139
|
+
func (c *Client) RepoTaskFields(ctx context.Context) ([]string, error) {
|
|
113
140
|
var out struct {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
141
|
+
Fields []string `json:"fields"`
|
|
142
|
+
}
|
|
143
|
+
if err := c.call(ctx, http.MethodGet, "/repos/task-fields", nil, &out); err != nil {
|
|
144
|
+
return nil, err
|
|
145
|
+
}
|
|
146
|
+
return out.Fields, nil
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// RegisterRepo registers a repo by name/path with optional task config.
|
|
150
|
+
func (c *Client) RegisterRepo(ctx context.Context, input repo.RegisterInput) (repo.Info, error) {
|
|
151
|
+
payload, _ := json.Marshal(input)
|
|
152
|
+
var out repo.Info
|
|
153
|
+
if err := c.call(ctx, http.MethodPost, "/repos", payload, &out); err != nil {
|
|
154
|
+
return repo.Info{}, err
|
|
155
|
+
}
|
|
156
|
+
return out, nil
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// RemoveRepo unregisters a repo by name.
|
|
160
|
+
func (c *Client) RemoveRepo(ctx context.Context, name string) error {
|
|
161
|
+
return c.call(ctx, http.MethodDelete, "/repos/"+url.PathEscape(name), nil, nil)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ListRepos returns registered repo infos.
|
|
165
|
+
func (c *Client) ListRepos(ctx context.Context) ([]repo.Info, error) {
|
|
166
|
+
var out []repo.Info
|
|
167
|
+
if err := c.call(ctx, http.MethodGet, "/repos", nil, &out); err != nil {
|
|
168
|
+
return nil, err
|
|
169
|
+
}
|
|
170
|
+
return out, nil
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// GetRepo returns one repo by name.
|
|
174
|
+
func (c *Client) GetRepo(ctx context.Context, name string) (repo.Info, error) {
|
|
175
|
+
var out repo.Info
|
|
176
|
+
if err := c.call(ctx, http.MethodGet, "/repos/"+url.PathEscape(name), nil, &out); err != nil {
|
|
177
|
+
return repo.Info{}, err
|
|
178
|
+
}
|
|
179
|
+
return out, nil
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// SubmitReport delivers one structured node report.
|
|
183
|
+
func (c *Client) SubmitReport(ctx context.Context, report run.ReportRequest) (run.ReportAck, error) {
|
|
184
|
+
payload, err := json.Marshal(report)
|
|
185
|
+
if err != nil {
|
|
186
|
+
return run.ReportAck{}, err
|
|
118
187
|
}
|
|
119
|
-
|
|
120
|
-
|
|
188
|
+
var ack run.ReportAck
|
|
189
|
+
if err := c.call(ctx, http.MethodPost, "/reports", payload, &ack); err != nil {
|
|
190
|
+
return run.ReportAck{}, err
|
|
121
191
|
}
|
|
122
|
-
|
|
123
|
-
|
|
192
|
+
return ack, nil
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// RegisterNodeSession binds the OpenCode session emitted for a run/node.
|
|
196
|
+
func (c *Client) RegisterNodeSession(ctx context.Context, registration run.NodeRuntimeRegistration) (run.NodeRuntimeRegistrationAck, error) {
|
|
197
|
+
payload, err := json.Marshal(registration)
|
|
198
|
+
if err != nil {
|
|
199
|
+
return run.NodeRuntimeRegistrationAck{}, err
|
|
200
|
+
}
|
|
201
|
+
var ack run.NodeRuntimeRegistrationAck
|
|
202
|
+
if err := c.call(ctx, http.MethodPost, "/runtime/session", payload, &ack); err != nil {
|
|
203
|
+
return run.NodeRuntimeRegistrationAck{}, err
|
|
204
|
+
}
|
|
205
|
+
return ack, nil
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// CancelRun cancels the active run for the given ticket with a reason.
|
|
209
|
+
func (c *Client) CancelRun(ctx context.Context, ticket, reason string) error {
|
|
210
|
+
payload, _ := json.Marshal(map[string]string{"reason": reason})
|
|
211
|
+
return c.call(ctx, http.MethodPost, "/runs/by-ticket/"+url.PathEscape(ticket)+"/cancel", payload, nil)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ListRuns returns runs matching the filter.
|
|
215
|
+
func (c *Client) ListRuns(ctx context.Context, filter run.Filter) ([]run.Run, error) {
|
|
216
|
+
q := url.Values{}
|
|
217
|
+
if filter.Repo != "" {
|
|
218
|
+
q.Set("repo", filter.Repo)
|
|
219
|
+
}
|
|
220
|
+
if filter.Workflow != "" {
|
|
221
|
+
q.Set("workflow", filter.Workflow)
|
|
222
|
+
}
|
|
223
|
+
if filter.Ticket != "" {
|
|
224
|
+
q.Set("ticket", filter.Ticket)
|
|
225
|
+
}
|
|
226
|
+
path := "/runs"
|
|
227
|
+
if s := q.Encode(); s != "" {
|
|
228
|
+
path += "?" + s
|
|
229
|
+
}
|
|
230
|
+
var out []run.Run
|
|
231
|
+
if err := c.call(ctx, http.MethodGet, path, nil, &out); err != nil {
|
|
232
|
+
return nil, err
|
|
233
|
+
}
|
|
234
|
+
return out, nil
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// GetRunByTicket returns the run for a ticket.
|
|
238
|
+
func (c *Client) GetRunByTicket(ctx context.Context, ticket string) (run.Run, error) {
|
|
239
|
+
var out run.Run
|
|
240
|
+
if err := c.call(ctx, http.MethodGet, "/runs/by-ticket/"+url.PathEscape(ticket), nil, &out); err != nil {
|
|
241
|
+
return run.Run{}, err
|
|
124
242
|
}
|
|
125
|
-
return
|
|
243
|
+
return out, nil
|
|
126
244
|
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
package server_test
|
|
2
|
+
|
|
3
|
+
// Test-local server fixture using the settled seam (d): a thin
|
|
4
|
+
// server.New(deps) http.Handler driven over in-process HTTP. Deps are the
|
|
5
|
+
// documented consumer services (workflow/repo/run/report), faked here.
|
|
6
|
+
|
|
7
|
+
import (
|
|
8
|
+
"context"
|
|
9
|
+
"net"
|
|
10
|
+
"net/http"
|
|
11
|
+
"os"
|
|
12
|
+
"path/filepath"
|
|
13
|
+
"testing"
|
|
14
|
+
|
|
15
|
+
"github.com/rajpopat27/relay-flow/internal/repo"
|
|
16
|
+
"github.com/rajpopat27/relay-flow/internal/run"
|
|
17
|
+
"github.com/rajpopat27/relay-flow/internal/runner"
|
|
18
|
+
"github.com/rajpopat27/relay-flow/internal/server"
|
|
19
|
+
"github.com/rajpopat27/relay-flow/internal/workflow"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
// fakeServices backs the handler with fakes behind the documented service
|
|
23
|
+
// seams. activeWorkflows seeds workflows with an active run (drives 409);
|
|
24
|
+
// failRepos forces a 500; slowReport simulates a long-running report call.
|
|
25
|
+
type fakeServices struct {
|
|
26
|
+
activeWorkflows map[string]bool
|
|
27
|
+
failRepos bool
|
|
28
|
+
slowReport chan struct{}
|
|
29
|
+
workflows map[string]*workflow.Workflow
|
|
30
|
+
repos map[string]repo.Info
|
|
31
|
+
runs []run.Run
|
|
32
|
+
shutdownCh chan struct{}
|
|
33
|
+
registrations []run.NodeRuntimeRegistration
|
|
34
|
+
runtimeAck run.NodeRuntimeRegistrationAck
|
|
35
|
+
processedReports map[string]bool
|
|
36
|
+
submittedReports int
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func (f *fakeServices) SubmitWorkflow(_ context.Context, yaml []byte) (*workflow.Workflow, error) {
|
|
40
|
+
wf, err := workflow.Parse("x", yaml)
|
|
41
|
+
if err != nil {
|
|
42
|
+
return nil, err
|
|
43
|
+
}
|
|
44
|
+
if f.activeWorkflows[wf.Name] {
|
|
45
|
+
return nil, errActiveRun{wf.Name}
|
|
46
|
+
}
|
|
47
|
+
f.workflows[wf.Name] = wf
|
|
48
|
+
return wf, nil
|
|
49
|
+
}
|
|
50
|
+
func (f *fakeServices) GetWorkflow(_ context.Context, name string) (*workflow.Workflow, error) {
|
|
51
|
+
if wf, ok := f.workflows[name]; ok {
|
|
52
|
+
return wf, nil
|
|
53
|
+
}
|
|
54
|
+
return nil, errNotFound{name}
|
|
55
|
+
}
|
|
56
|
+
func (f *fakeServices) ListWorkflows(context.Context) ([]*workflow.Workflow, error) {
|
|
57
|
+
out := []*workflow.Workflow{}
|
|
58
|
+
for _, w := range f.workflows {
|
|
59
|
+
out = append(out, w)
|
|
60
|
+
}
|
|
61
|
+
return out, nil
|
|
62
|
+
}
|
|
63
|
+
func (f *fakeServices) RemoveWorkflow(_ context.Context, name string) error {
|
|
64
|
+
if f.activeWorkflows[name] {
|
|
65
|
+
return errActiveRun{name}
|
|
66
|
+
}
|
|
67
|
+
if _, ok := f.workflows[name]; !ok {
|
|
68
|
+
return errNotFound{name}
|
|
69
|
+
}
|
|
70
|
+
delete(f.workflows, name)
|
|
71
|
+
return nil
|
|
72
|
+
}
|
|
73
|
+
func (f *fakeServices) ListRuns(context.Context, run.Filter) ([]run.Run, error) { return f.runs, nil }
|
|
74
|
+
func (f *fakeServices) GetRunByTicket(_ context.Context, ticket string) (run.Run, error) {
|
|
75
|
+
for _, r := range f.runs {
|
|
76
|
+
if r.Ticket.Key == ticket {
|
|
77
|
+
return r, nil
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return run.Run{}, errNotFound{ticket}
|
|
81
|
+
}
|
|
82
|
+
func (f *fakeServices) CancelRun(_ context.Context, ticket, _ string) error {
|
|
83
|
+
for i, r := range f.runs {
|
|
84
|
+
if r.Ticket.Key == ticket {
|
|
85
|
+
f.runs[i].State = run.StateCanceled
|
|
86
|
+
return nil
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return errNotFound{ticket}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
func (f *fakeServices) SubmitReport(ctx context.Context, _ run.ReportRequest) (run.ReportAck, error) {
|
|
93
|
+
f.submittedReports++
|
|
94
|
+
if f.slowReport != nil {
|
|
95
|
+
select {
|
|
96
|
+
case <-f.slowReport:
|
|
97
|
+
case <-ctx.Done():
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return run.ReportAck{Accepted: true}, nil
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
func (f *fakeServices) HasProcessedReport(_ context.Context, id run.ID, reportID string) (bool, error) {
|
|
104
|
+
return f.processedReports[string(id)+":"+reportID], nil
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
func (f *fakeServices) RegisterNodeSession(_ context.Context, registration run.NodeRuntimeRegistration) (run.NodeRuntimeRegistrationAck, error) {
|
|
108
|
+
f.registrations = append(f.registrations, registration)
|
|
109
|
+
if f.runtimeAck != (run.NodeRuntimeRegistrationAck{}) {
|
|
110
|
+
return f.runtimeAck, nil
|
|
111
|
+
}
|
|
112
|
+
return run.NodeRuntimeRegistrationAck{Accepted: true}, nil
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func (f *fakeServices) GetRepo(_ context.Context, name string) (repo.Info, error) {
|
|
116
|
+
if f.failRepos {
|
|
117
|
+
return repo.Info{}, errUnexpected{}
|
|
118
|
+
}
|
|
119
|
+
if info, ok := f.repos[name]; ok {
|
|
120
|
+
return info, nil
|
|
121
|
+
}
|
|
122
|
+
return repo.Info{}, errNotFound{name}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
func (f *fakeServices) ListRepos(context.Context) ([]repo.Info, error) {
|
|
126
|
+
out := []repo.Info{}
|
|
127
|
+
for _, r := range f.repos {
|
|
128
|
+
out = append(out, r)
|
|
129
|
+
}
|
|
130
|
+
return out, nil
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
func (f *fakeServices) DiscoverRepos(context.Context) ([]runner.RepoCandidate, error) {
|
|
134
|
+
return []runner.RepoCandidate{{Name: "payments", Path: "/srv/payments"}}, nil
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func (f *fakeServices) TaskFields(context.Context) ([]string, error) {
|
|
138
|
+
return []string{"status", "labels"}, nil
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
func (f *fakeServices) RegisterRepo(_ context.Context, input repo.RegisterInput) (repo.Info, error) {
|
|
142
|
+
if f.repos == nil {
|
|
143
|
+
f.repos = map[string]repo.Info{}
|
|
144
|
+
}
|
|
145
|
+
info := repo.Info{Name: input.Name, Path: input.Path, TaskConfig: input.TaskConfig}
|
|
146
|
+
f.repos[input.Name] = info
|
|
147
|
+
return info, nil
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
func (f *fakeServices) RemoveRepo(_ context.Context, name string) error {
|
|
151
|
+
if _, ok := f.repos[name]; !ok {
|
|
152
|
+
return errNotFound{name}
|
|
153
|
+
}
|
|
154
|
+
delete(f.repos, name)
|
|
155
|
+
return nil
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
func (f *fakeServices) Shutdown(context.Context) error {
|
|
159
|
+
if f.shutdownCh != nil {
|
|
160
|
+
select {
|
|
161
|
+
case <-f.shutdownCh:
|
|
162
|
+
default:
|
|
163
|
+
close(f.shutdownCh)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return nil
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// errActiveRun wraps server.ErrConflict so the handler maps it to 409 per
|
|
170
|
+
// the documented typed-error mapping (docs/structs-methods-interfaces.md
|
|
171
|
+
// routes + 'API responses and status codes are stable').
|
|
172
|
+
type errActiveRun struct{ name string }
|
|
173
|
+
|
|
174
|
+
func (e errActiveRun) Error() string { return "workflow has active run: " + e.name }
|
|
175
|
+
func (e errActiveRun) Unwrap() error { return server.ErrConflict }
|
|
176
|
+
|
|
177
|
+
// errNotFound wraps server.ErrNotFound so the handler maps it to 404.
|
|
178
|
+
type errNotFound struct{ name string }
|
|
179
|
+
|
|
180
|
+
func (e errNotFound) Error() string { return "not found: " + e.name }
|
|
181
|
+
func (e errNotFound) Unwrap() error { return server.ErrNotFound }
|
|
182
|
+
|
|
183
|
+
type errUnexpected struct{}
|
|
184
|
+
|
|
185
|
+
func (errUnexpected) Error() string { return "unexpected failure" }
|
|
186
|
+
|
|
187
|
+
// startHandler builds the http.Handler via server.New(deps) and serves it on
|
|
188
|
+
// an in-process listener; returns a client and a shutdown func.
|
|
189
|
+
func startHandler(t *testing.T, svc *fakeServices) (*http.Client, func()) {
|
|
190
|
+
t.Helper()
|
|
191
|
+
if svc.workflows == nil {
|
|
192
|
+
svc.workflows = map[string]*workflow.Workflow{}
|
|
193
|
+
}
|
|
194
|
+
if svc.repos == nil {
|
|
195
|
+
svc.repos = map[string]repo.Info{}
|
|
196
|
+
}
|
|
197
|
+
h := server.New(svc) // seam (d): thin http.Handler over the services
|
|
198
|
+
|
|
199
|
+
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
200
|
+
if err != nil {
|
|
201
|
+
t.Fatal(err)
|
|
202
|
+
}
|
|
203
|
+
srv := &http.Server{Handler: h}
|
|
204
|
+
go srv.Serve(ln)
|
|
205
|
+
|
|
206
|
+
client := &http.Client{Transport: &http.Transport{
|
|
207
|
+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
208
|
+
return (&net.Dialer{}).DialContext(ctx, "tcp", ln.Addr().String())
|
|
209
|
+
},
|
|
210
|
+
}}
|
|
211
|
+
cleanup := func() {
|
|
212
|
+
_ = srv.Shutdown(context.Background())
|
|
213
|
+
}
|
|
214
|
+
return client, cleanup
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// startHandlerOnSocket serves the handler on a real Unix socket inside dir so
|
|
218
|
+
// the same dir can be reused across a restart (same-state resume). The server
|
|
219
|
+
// owns socket creation/mode; cleanup shuts down and unlinks the socket.
|
|
220
|
+
func startHandlerOnSocket(t *testing.T, dir string, svc *fakeServices) (*http.Client, func()) {
|
|
221
|
+
t.Helper()
|
|
222
|
+
if svc.workflows == nil {
|
|
223
|
+
svc.workflows = map[string]*workflow.Workflow{}
|
|
224
|
+
}
|
|
225
|
+
if svc.repos == nil {
|
|
226
|
+
svc.repos = map[string]repo.Info{}
|
|
227
|
+
}
|
|
228
|
+
h := server.New(svc)
|
|
229
|
+
sock := filepath.Join(dir, "server.sock")
|
|
230
|
+
_ = os.Remove(sock) // clear any stale socket before (re)binding
|
|
231
|
+
ln, err := net.Listen("unix", sock)
|
|
232
|
+
if err != nil {
|
|
233
|
+
t.Fatal(err)
|
|
234
|
+
}
|
|
235
|
+
srv := &http.Server{Handler: h}
|
|
236
|
+
go srv.Serve(ln)
|
|
237
|
+
client := &http.Client{Transport: &http.Transport{
|
|
238
|
+
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
239
|
+
return (&net.Dialer{}).DialContext(ctx, "unix", sock)
|
|
240
|
+
},
|
|
241
|
+
}}
|
|
242
|
+
cleanup := func() {
|
|
243
|
+
_ = srv.Shutdown(context.Background())
|
|
244
|
+
_ = ln.Close()
|
|
245
|
+
_ = os.Remove(sock)
|
|
246
|
+
}
|
|
247
|
+
return client, cleanup
|
|
248
|
+
}
|