This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
adapter.go
120 lines (97 loc) · 2.4 KB
/
adapter.go
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
package adapter
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/pkg/errors"
)
type Runner struct {
command string
configFile string
dir string
args []string
env []string
}
func Server(args ...string) *Runner {
return &Runner{command: "server", args: args}
}
func (b *Runner) WithEnv(env []string) *Runner {
b.env = env
return b
}
func (b *Runner) Run(t *testing.T) error {
t.Helper()
cmd := b.exec(context.Background())
start := time.Now()
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "rdss-archivematica-channel-adapter %s", b.command)
}
fmt.Println("Start in ", time.Since(start))
return nil
}
func (b *Runner) RunBackground(t *testing.T) context.CancelFunc {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
cmd := b.exec(ctx)
start := time.Now()
if err := cmd.Start(); err != nil {
t.Fatalf("rdss-archivematica-channel-adapter %s: %v", b.command, err)
}
done := make(chan struct{})
go func() {
_ = cmd.Wait()
fmt.Println("Ran in ", time.Since(start))
done <- struct{}{}
}()
return func() {
cancel()
_ = cmd.Wait()
<-done
}
}
func (b *Runner) RunOrFail(t *testing.T) {
t.Helper()
if err := b.Run(t); err != nil {
t.Fatal(err)
}
}
func (b *Runner) exec(ctx context.Context) *exec.Cmd {
args := []string{b.command}
if b.configFile != "" {
args = append(args, "--config", b.configFile)
}
args = append(args, b.args...)
cmd := exec.CommandContext(ctx, "rdss-archivematica-channel-adapter", args...)
cmd.Env = append(removeAppEnvs(os.Environ()), b.env...)
if b.dir != "" {
cmd.Dir = b.dir
}
// If the test is killed by a timeout, go test will wait for
// os.Stderr and os.Stdout to close as a result.
//
// However, the `cmd` will still run in the background
// and hold those descriptors open.
// As a result, go test will hang forever.
//
// Avoid that by wrapping stderr and stdout, breaking the short
// circuit and forcing cmd.Run to use another pipe and goroutine
// to pass along stderr and stdout.
// See https://github.com/golang/go/issues/23019
cmd.Stdout = struct{ io.Writer }{os.Stdout}
cmd.Stderr = struct{ io.Writer }{os.Stderr}
return cmd
}
func removeAppEnvs(env []string) []string {
var clean []string
for _, value := range env {
if !strings.HasPrefix(value, "RDSS_ARCHIVEMATICA_ADAPTER_") {
clean = append(clean, value)
}
}
return clean
}