This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoker_test.go
144 lines (122 loc) · 3.36 KB
/
invoker_test.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package pink
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"
"testing"
"github.com/docker/docker/client"
"github.com/stretchr/testify/require"
)
// replace the execCommandContext by a fake one that will run the test binary
// as the executable. The Invoker will run the TestHelperProcess test and expect
// success exit code.
func setFakeExecCommandContext(t *testing.T) func() {
execCommandContext = func(ctx context.Context, command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.CommandContext(ctx, os.Args[0], cs...)
cmd.Env = []string{"GO_RUN_HELPER_PROCESS=1"}
return cmd
}
return func() {
execCommandContext = exec.CommandContext
}
}
// This test will be run by the Invoker as the targeted executable.
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_RUN_HELPER_PROCESS") != "1" {
return
}
// cleanup args, turning
// /var/folders/xxxx.../pink.test -test.Run=TestHelperProcess -- /..../pink/fake-command --some-flag some-arg
// into
// /..../pink/fake-command --some-flag some-arg
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
cmd, args := args[0], args[1:]
wd, err := os.Getwd()
require.NoError(t, err)
require.Equal(t, path.Join(wd, "fake-command"), cmd)
require.Len(t, args, 2)
require.Equal(t, "--some-flag", args[0])
require.Equal(t, "some-arg", args[1])
require.Equal(t, "C", os.Getenv("A"))
require.Equal(t, "T", os.Getenv("G"))
os.Exit(0)
}
func TestExecutableInvoker(t *testing.T) {
defer setFakeExecCommandContext(t)()
wd, err := os.Getwd()
require.NoError(t, err)
invoker := ExecutableInvoker{
PluginDir: wd,
}
err = invoker.Invoke(
context.Background(),
&Manifest{Command: []string{"fake-command"}},
&InvokerConfig{
Args: []string{"--some-flag", "some-arg"},
Env: []string{"A=C", "G=T"},
},
)
require.NoError(t, err)
}
func TestDockerInvoker(t *testing.T) {
t.Parallel()
client, err := client.NewEnvClient()
require.NoError(t, err)
defer client.Close()
t.Run("stdout", func(t *testing.T) {
var buf bytes.Buffer
invoker := NewDockerInvoker(client, &buf, os.Stderr)
err = invoker.Invoke(
context.Background(),
&Manifest{Docker: DockerConfig{ImageURL: "alpine"}},
&InvokerConfig{
Args: []string{"echo", "hello world"},
},
)
require.NoError(t, err)
require.Equal(t, "hello world", strings.TrimSpace(buf.String()))
})
t.Run("stderr", func(t *testing.T) {
var buf bytes.Buffer
invoker := NewDockerInvoker(client, os.Stdout, &buf)
err = invoker.Invoke(
context.Background(),
&Manifest{Docker: DockerConfig{ImageURL: "alpine"}},
&InvokerConfig{
Args: []string{"sh", "-c", "echo hello world 1>&2"},
},
)
require.NoError(t, err)
require.Equal(t, "hello world", strings.TrimSpace(buf.String()))
})
t.Run("with tty enabled", func(t *testing.T) {
var buf bytes.Buffer
invoker := NewDockerInvoker(client, &buf, os.Stderr)
err = invoker.Invoke(
context.Background(),
&Manifest{Docker: DockerConfig{ImageURL: "alpine", TTY: true}},
&InvokerConfig{
Args: []string{"sh", "-c", "echo stdout 1>&2; echo stderr"},
},
)
require.NoError(t, err)
require.Equal(t, "stdout\r\nstderr\r\n", buf.String())
})
}