-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_test.go
110 lines (84 loc) · 2.55 KB
/
run_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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package gont_test
import (
"bytes"
"testing"
g "cunicu.li/gont/v2/pkg"
co "cunicu.li/gont/v2/pkg/options/cmd"
"github.com/stretchr/testify/require"
"github.com/vishvananda/netns"
)
func prepare(t *testing.T) (*g.Network, *g.BaseNode) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create new network")
n1, err := n.AddNode("n1")
require.NoError(t, err, "Failed to create node")
return n, n1
}
func TestRun(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
// Run
outp := &bytes.Buffer{}
_, err := n1.Run("ip", "netns", "identify",
co.Stdout(outp))
require.NoError(t, err, "Failed to run identify")
require.Equal(t, outp.String(), n1.Namespace.Name+"\n", "Got invalid namespace")
}
func TestRunFunc(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
// Run
err := n1.RunFunc(func() error {
handle, err := netns.Get()
if err != nil {
return err
}
require.True(t, handle.Equal(n1.NsHandle), "Mismatching netns handles")
return nil
})
require.NoError(t, err, "Failed to run identify")
}
func TestRunGo(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
outp := &bytes.Buffer{}
cmd, err := n1.RunGo("../cmd/gontc/gontc.go", "identify",
co.Stdout(outp))
require.NoError(t, err, "Failed to run Go script")
require.True(t, cmd.ProcessState.Exited(), "Process did not exit")
require.True(t, cmd.ProcessState.Success(), "Process did not succeed")
require.Equal(t, outp.String(), n1.String()+"\n")
}
func TestEnter(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
exit, err := n1.Enter()
require.NoError(t, err, "Failed to enter namespace")
defer exit()
handle, err := netns.Get()
require.NoError(t, err, "Failed to get netns handle")
require.True(t, handle.Equal(n1.NsHandle), "Mismatching netns handles")
}
func TestRunSimple(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
_, err := n1.Run("true")
require.NoError(t, err)
_, err = n1.Run("false")
require.Error(t, err)
}
func TestStart(t *testing.T) {
n, n1 := prepare(t)
defer n.Close()
outp := &bytes.Buffer{}
cmd, err := n1.Start("ip", "netns", "identify",
co.Stdout(outp))
require.NoError(t, err, "Failed to run identify")
err = cmd.Wait()
require.NoError(t, err, "Failed to wait")
require.True(t, cmd.ProcessState.Exited(), "Process did not exit")
require.True(t, cmd.ProcessState.Success(), "Process did not succeed")
require.Equal(t, outp.String(), n1.Namespace.Name+"\n", "Got invalid namespace")
}