-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage.html
More file actions
688 lines (606 loc) · 23.5 KB
/
coverage.html
File metadata and controls
688 lines (606 loc) · 23.5 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>compose: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">digital.vasic.containers/pkg/compose/group.go (100.0%)</option>
<option value="file1">digital.vasic.containers/pkg/compose/options.go (100.0%)</option>
<option value="file2">digital.vasic.containers/pkg/compose/orchestrator.go (97.5%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package compose
import "sort"
// ServiceGroupEntry describes a single service's compose context,
// used as input to GroupByCompose.
type ServiceGroupEntry struct {
// ComposeFile is the path to the service's compose file.
ComposeFile string
// Profile is the optional compose profile.
Profile string
}
// ServiceGroup aggregates services that share the same compose file and
// profile so they can be managed together in a single compose command.
type ServiceGroup struct {
// Name is a derived name for the group (compose file + profile).
Name string
// ComposeFile is the shared compose file path.
ComposeFile string
// Profile is the shared compose profile.
Profile string
// Services lists the service names in this group.
Services []string
}
// GroupByCompose takes a map of service-name to ServiceGroupEntry and
// returns a slice of ServiceGroups, one per unique compose-file+profile
// combination. Groups and services within groups are sorted for
// deterministic ordering.
func GroupByCompose(
endpoints map[string]ServiceGroupEntry,
) []ServiceGroup <span class="cov7" title="15">{
type groupKey struct {
composeFile string
profile string
}
grouped := make(map[groupKey][]string)
for svcName, entry := range endpoints </span><span class="cov10" title="52">{
key := groupKey{
composeFile: entry.ComposeFile,
profile: entry.Profile,
}
grouped[key] = append(grouped[key], svcName)
}</span>
<span class="cov7" title="15">groups := make([]ServiceGroup, 0, len(grouped))
for key, services := range grouped </span><span class="cov9" title="37">{
sort.Strings(services)
name := key.composeFile
if key.profile != "" </span><span class="cov6" title="13">{
name += ":" + key.profile
}</span>
<span class="cov9" title="37">groups = append(groups, ServiceGroup{
Name: name,
ComposeFile: key.composeFile,
Profile: key.profile,
Services: services,
})</span>
}
// Sort groups by name for deterministic output.
<span class="cov7" title="15">sort.Slice(groups, func(i, j int) bool </span><span class="cov9" title="34">{
return groups[i].Name < groups[j].Name
}</span>)
<span class="cov7" title="15">return groups</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package compose
// --- Up Options ---
type upOptions struct {
Detach bool
RemoveOrphans bool
BuildFirst bool
ForceRecreate bool
NoRecreate bool
Timeout int
Wait bool
}
// UpOption configures compose up behavior.
type UpOption func(*upOptions)
func defaultUpOptions() *upOptions <span class="cov8" title="11">{
return &upOptions{
Detach: true,
RemoveOrphans: false,
BuildFirst: false,
ForceRecreate: false,
NoRecreate: false,
Timeout: 0,
Wait: false,
}
}</span>
func applyUpOptions(opts []UpOption) *upOptions <span class="cov8" title="11">{
o := defaultUpOptions()
for _, fn := range opts </span><span class="cov10" title="21">{
fn(o)
}</span>
<span class="cov8" title="11">return o</span>
}
// WithUpDetach sets whether compose up runs in detached mode.
func WithUpDetach(detach bool) UpOption <span class="cov5" title="4">{
return func(o *upOptions) </span><span class="cov5" title="4">{
o.Detach = detach
}</span>
}
// WithRemoveOrphans removes containers for services not defined in the
// compose file.
func WithRemoveOrphans(remove bool) UpOption <span class="cov4" title="3">{
return func(o *upOptions) </span><span class="cov4" title="3">{
o.RemoveOrphans = remove
}</span>
}
// WithBuildFirst forces a build before starting containers.
func WithBuildFirst(build bool) UpOption <span class="cov4" title="3">{
return func(o *upOptions) </span><span class="cov4" title="3">{
o.BuildFirst = build
}</span>
}
// WithForceRecreate forces recreation of containers even if their
// configuration has not changed.
func WithForceRecreate(force bool) UpOption <span class="cov4" title="3">{
return func(o *upOptions) </span><span class="cov4" title="3">{
o.ForceRecreate = force
}</span>
}
// WithNoRecreate prevents recreation of existing containers.
func WithNoRecreate(noRecreate bool) UpOption <span class="cov3" title="2">{
return func(o *upOptions) </span><span class="cov3" title="2">{
o.NoRecreate = noRecreate
}</span>
}
// WithUpTimeout sets the shutdown timeout in seconds.
func WithUpTimeout(seconds int) UpOption <span class="cov4" title="3">{
return func(o *upOptions) </span><span class="cov4" title="3">{
o.Timeout = seconds
}</span>
}
// WithWait waits for services to be running|healthy before returning.
func WithWait(wait bool) UpOption <span class="cov4" title="3">{
return func(o *upOptions) </span><span class="cov4" title="3">{
o.Wait = wait
}</span>
}
// --- Down Options ---
type downOptions struct {
RemoveOrphans bool
RemoveVolumes bool
RemoveImages string
Timeout int
}
// DownOption configures compose down behavior.
type DownOption func(*downOptions)
func defaultDownOptions() *downOptions <span class="cov7" title="8">{
return &downOptions{
RemoveOrphans: false,
RemoveVolumes: false,
RemoveImages: "",
Timeout: 0,
}
}</span>
func applyDownOptions(opts []DownOption) *downOptions <span class="cov7" title="8">{
o := defaultDownOptions()
for _, fn := range opts </span><span class="cov8" title="12">{
fn(o)
}</span>
<span class="cov7" title="8">return o</span>
}
// WithDownRemoveOrphans removes containers for services not defined in
// the compose file during down.
func WithDownRemoveOrphans(remove bool) DownOption <span class="cov4" title="3">{
return func(o *downOptions) </span><span class="cov4" title="3">{
o.RemoveOrphans = remove
}</span>
}
// WithDownRemoveVolumes removes named volumes declared in the compose
// file and anonymous volumes attached to containers.
func WithDownRemoveVolumes(remove bool) DownOption <span class="cov4" title="3">{
return func(o *downOptions) </span><span class="cov4" title="3">{
o.RemoveVolumes = remove
}</span>
}
// WithDownRemoveImages removes images when down completes. Valid values
// are "all" and "local".
func WithDownRemoveImages(mode string) DownOption <span class="cov4" title="3">{
return func(o *downOptions) </span><span class="cov4" title="3">{
o.RemoveImages = mode
}</span>
}
// WithDownTimeout sets the shutdown timeout in seconds.
func WithDownTimeout(seconds int) DownOption <span class="cov4" title="3">{
return func(o *downOptions) </span><span class="cov4" title="3">{
o.Timeout = seconds
}</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package compose
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
"digital.vasic.containers/pkg/logging"
)
// ComposeOrchestrator defines the interface for managing services
// through Docker Compose or compatible orchestration tools.
type ComposeOrchestrator interface {
// Up creates and starts containers for the given project.
Up(ctx context.Context, project ComposeProject, opts ...UpOption) error
// Down stops and removes containers for the given project.
Down(
ctx context.Context, project ComposeProject,
opts ...DownOption,
) error
// Status returns the current status of each service in the project.
Status(
ctx context.Context, project ComposeProject,
) ([]ServiceStatus, error)
// Logs returns a reader streaming log output for the named service.
Logs(
ctx context.Context, project ComposeProject, service string,
) (io.ReadCloser, error)
}
// DefaultOrchestrator implements ComposeOrchestrator by shelling out to
// the detected compose command.
type DefaultOrchestrator struct {
composeCmd string
composeArgs []string
workDir string
logger logging.Logger
}
// NewDefaultOrchestrator creates a DefaultOrchestrator, auto-detecting
// the available compose command. The workDir is the directory from
// which commands are executed.
func NewDefaultOrchestrator(
workDir string, logger logging.Logger,
) (*DefaultOrchestrator, error) <span class="cov4" title="4">{
cmd, args, err := detectComposeCmd()
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov4" title="4">if logger == nil </span><span class="cov3" title="3">{
logger = logging.NopLogger{}
}</span>
<span class="cov4" title="4">return &DefaultOrchestrator{
composeCmd: cmd,
composeArgs: args,
workDir: workDir,
logger: logger,
}, nil</span>
}
// NewOrchestrator creates an orchestrator with an explicit compose
// command and args (useful for testing).
func NewOrchestrator(
composeCmd string,
composeArgs []string,
workDir string,
logger logging.Logger,
) *DefaultOrchestrator <span class="cov10" title="38">{
if logger == nil </span><span class="cov9" title="36">{
logger = logging.NopLogger{}
}</span>
<span class="cov10" title="38">return &DefaultOrchestrator{
composeCmd: composeCmd,
composeArgs: composeArgs,
workDir: workDir,
logger: logger,
}</span>
}
// Up creates and starts containers.
func (o *DefaultOrchestrator) Up(
ctx context.Context,
project ComposeProject,
opts ...UpOption,
) error <span class="cov6" title="9">{
cfg := applyUpOptions(opts)
args := o.projectArgs(project)
args = append(args, "up")
if cfg.Detach </span><span class="cov6" title="8">{
args = append(args, "-d")
}</span>
<span class="cov6" title="9">if cfg.RemoveOrphans </span><span class="cov2" title="2">{
args = append(args, "--remove-orphans")
}</span>
<span class="cov6" title="9">if cfg.BuildFirst </span><span class="cov2" title="2">{
args = append(args, "--build")
}</span>
<span class="cov6" title="9">if cfg.ForceRecreate </span><span class="cov2" title="2">{
args = append(args, "--force-recreate")
}</span>
<span class="cov6" title="9">if cfg.NoRecreate </span><span class="cov1" title="1">{
args = append(args, "--no-recreate")
}</span>
<span class="cov6" title="9">if cfg.Timeout > 0 </span><span class="cov2" title="2">{
args = append(args, "--timeout",
strconv.Itoa(cfg.Timeout))
}</span>
<span class="cov6" title="9">if cfg.Wait </span><span class="cov2" title="2">{
args = append(args, "--wait")
}</span>
<span class="cov6" title="9">args = append(args, project.Services...)
o.logger.Info("compose up: %s %s", o.composeCmd,
strings.Join(args, " "))
return o.run(ctx, args)</span>
}
// Down stops and removes containers.
func (o *DefaultOrchestrator) Down(
ctx context.Context,
project ComposeProject,
opts ...DownOption,
) error <span class="cov5" title="6">{
cfg := applyDownOptions(opts)
args := o.projectArgs(project)
args = append(args, "down")
if cfg.RemoveOrphans </span><span class="cov2" title="2">{
args = append(args, "--remove-orphans")
}</span>
<span class="cov5" title="6">if cfg.RemoveVolumes </span><span class="cov2" title="2">{
args = append(args, "--volumes")
}</span>
<span class="cov5" title="6">if cfg.RemoveImages != "" </span><span class="cov2" title="2">{
args = append(args, "--rmi", cfg.RemoveImages)
}</span>
<span class="cov5" title="6">if cfg.Timeout > 0 </span><span class="cov2" title="2">{
args = append(args, "--timeout",
strconv.Itoa(cfg.Timeout))
}</span>
<span class="cov5" title="6">o.logger.Info("compose down: %s %s", o.composeCmd,
strings.Join(args, " "))
return o.run(ctx, args)</span>
}
// Status returns the status of all services in the project by parsing
// the output of `docker compose ps`.
func (o *DefaultOrchestrator) Status(
ctx context.Context,
project ComposeProject,
) ([]ServiceStatus, error) <span class="cov5" title="6">{
args := o.projectArgs(project)
args = append(args, "ps", "--format",
"{{.Name}}|{{.State}}|{{.Health}}|{{.Ports}}|{{.ExitCode}}")
out, err := o.output(ctx, args)
if err != nil </span><span class="cov2" title="2">{
return nil, fmt.Errorf("compose ps failed: %w", err)
}</span>
<span class="cov4" title="4">return parseStatusOutput(out), nil</span>
}
// Logs returns a reader for the log output of the named service.
func (o *DefaultOrchestrator) Logs(
ctx context.Context,
project ComposeProject,
service string,
) (io.ReadCloser, error) <span class="cov4" title="5">{
args := o.projectArgs(project)
args = append(args, "logs", "--no-color", service)
allArgs := append(o.composeArgs, args...)
cmd := exec.CommandContext(ctx, o.composeCmd, allArgs...)
cmd.Dir = o.workDir
stdout, err := cmd.StdoutPipe()
if err != nil </span><span class="cov0" title="0">{
return nil, fmt.Errorf(
"failed to create stdout pipe: %w", err,
)
}</span>
<span class="cov4" title="5">if err := cmd.Start(); err != nil </span><span class="cov1" title="1">{
return nil, fmt.Errorf(
"failed to start compose logs: %w", err,
)
}</span>
<span class="cov4" title="4">return &logReader{cmd: cmd, reader: stdout}, nil</span>
}
// logReader wraps a command's stdout pipe and waits for the process to
// exit on Close.
type logReader struct {
cmd *exec.Cmd
reader io.ReadCloser
}
func (r *logReader) Read(p []byte) (int, error) <span class="cov6" title="8">{
return r.reader.Read(p)
}</span>
func (r *logReader) Close() error <span class="cov5" title="6">{
_ = r.reader.Close()
return r.cmd.Wait()
}</span>
// projectArgs builds the common compose arguments for a project.
func (o *DefaultOrchestrator) projectArgs(
project ComposeProject,
) []string <span class="cov9" title="30">{
var args []string
if project.File != "" </span><span class="cov6" title="11">{
args = append(args, "-f", project.File)
}</span>
<span class="cov9" title="30">if project.Name != "" </span><span class="cov7" title="14">{
args = append(args, "--project-name", project.Name)
}</span>
<span class="cov9" title="30">if project.Profile != "" </span><span class="cov5" title="6">{
args = append(args, "--profile", project.Profile)
}</span>
<span class="cov9" title="30">return args</span>
}
// run executes the compose command and returns any error.
func (o *DefaultOrchestrator) run(
ctx context.Context, args []string,
) error <span class="cov8" title="19">{
allArgs := append(o.composeArgs, args...)
cmd := exec.CommandContext(ctx, o.composeCmd, allArgs...)
cmd.Dir = o.workDir
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil </span><span class="cov5" title="6">{
return fmt.Errorf("%s %s failed: %w\nstderr: %s",
o.composeCmd, strings.Join(allArgs, " "),
err, stderr.String())
}</span>
<span class="cov7" title="13">return nil</span>
}
// output executes the compose command and returns stdout.
func (o *DefaultOrchestrator) output(
ctx context.Context, args []string,
) (string, error) <span class="cov6" title="11">{
allArgs := append(o.composeArgs, args...)
cmd := exec.CommandContext(ctx, o.composeCmd, allArgs...)
cmd.Dir = o.workDir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil </span><span class="cov4" title="5">{
return "", fmt.Errorf("%s %s failed: %w\nstderr: %s",
o.composeCmd, strings.Join(allArgs, " "),
err, stderr.String())
}</span>
<span class="cov5" title="6">return stdout.String(), nil</span>
}
// parseStatusOutput parses the pipe-delimited output from compose ps.
func parseStatusOutput(output string) []ServiceStatus <span class="cov8" title="19">{
var statuses []ServiceStatus
scanner := bufio.NewScanner(strings.NewReader(output))
for scanner.Scan() </span><span class="cov8" title="23">{
line := strings.TrimSpace(scanner.Text())
if line == "" </span><span class="cov2" title="2">{
continue</span>
}
<span class="cov8" title="21">parts := strings.SplitN(line, "|", 5)
if len(parts) < 5 </span><span class="cov4" title="5">{
continue</span>
}
<span class="cov7" title="16">exitCode := 0
if ec, err := strconv.Atoi(
strings.TrimSpace(parts[4]),
); err == nil </span><span class="cov7" title="15">{
exitCode = ec
}</span>
<span class="cov7" title="16">ports := parsePorts(parts[3])
statuses = append(statuses, ServiceStatus{
Name: strings.TrimSpace(parts[0]),
State: strings.TrimSpace(parts[1]),
Health: strings.TrimSpace(parts[2]),
Ports: ports,
ExitCode: exitCode,
})</span>
}
<span class="cov8" title="19">return statuses</span>
}
// parsePorts splits a comma-separated list of port mappings.
func parsePorts(raw string) []string <span class="cov8" title="25">{
raw = strings.TrimSpace(raw)
if raw == "" </span><span class="cov4" title="4">{
return nil
}</span>
<span class="cov8" title="21">parts := strings.Split(raw, ",")
result := make([]string, 0, len(parts))
for _, p := range parts </span><span class="cov9" title="29">{
p = strings.TrimSpace(p)
if p != "" </span><span class="cov9" title="26">{
result = append(result, p)
}</span>
}
<span class="cov8" title="21">return result</span>
}
// detectComposeCmd tries to find a working compose command, preferring
// Docker Compose v2 plugin, then standalone docker-compose, then
// podman-compose, then podman compose.
func detectComposeCmd() (string, []string, error) <span class="cov4" title="5">{
candidates := []struct {
cmd string
args []string
}{
{"docker", []string{"compose"}},
{"docker-compose", nil},
{"podman-compose", nil},
{"podman", []string{"compose"}},
}
for _, c := range candidates </span><span class="cov7" title="15">{
checkArgs := append(c.args, "version")
cmd := exec.Command(c.cmd, checkArgs...)
if err := cmd.Run(); err == nil </span><span class="cov4" title="5">{
return c.cmd, c.args, nil
}</span>
}
<span class="cov0" title="0">return "", nil, fmt.Errorf(
"no compose command found: tried docker compose, " +
"docker-compose, podman-compose, podman compose",
)</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>