-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathopt_helper_test.go
More file actions
56 lines (46 loc) · 1.39 KB
/
opt_helper_test.go
File metadata and controls
56 lines (46 loc) · 1.39 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
package nmap
import (
"os/exec"
"strings"
"testing"
nmaptesting "github.com/Ullaakut/nmap/v4/internal/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func nmapContainerOptions(t *testing.T) []Option {
t.Helper()
ctx := t.Context()
dockerPath, err := exec.LookPath("docker")
if err != nil {
t.Skip("docker is required to run container-based tests")
}
inspectCmd := exec.CommandContext(ctx, dockerPath, "inspect", "-f", "{{.State.Running}}", nmaptesting.ContainerName)
if output, inspectErr := inspectCmd.Output(); inspectErr == nil {
if strings.TrimSpace(string(output)) == "true" {
return []Option{
WithBinaryPath(dockerPath),
WithCustomArguments("exec", nmaptesting.ContainerName, "nmap"),
}
}
}
ctr, err := nmaptesting.StartNetworkMapper()
if err != nil {
t.Skipf("unable to start nmap test container: %v", err)
}
if ctr != nil {
t.Cleanup(func() {
_ = nmaptesting.StopContainer(ctr)
})
}
return []Option{
WithBinaryPath(dockerPath),
WithCustomArguments("exec", nmaptesting.ContainerName, "nmap"),
}
}
func assertArgsSuffix(t *testing.T, args, expected []string) {
t.Helper()
require.Len(t, args, len(expected)+3) // accounting for "exec", "<container>", "nmap"
args = args[3:] // strip "exec", "<container>", "nmap"
require.Equal(t, len(args), len(expected))
assert.Equal(t, args, expected)
}